xref: /freebsd-src/contrib/llvm-project/libcxx/src/include/ryu/common.h (revision 0eae32dcef82f6f06de6419a0d623d7def0cc8f6)
1*0eae32dcSDimitry Andric //===----------------------------------------------------------------------===//
2*0eae32dcSDimitry Andric //
3*0eae32dcSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0eae32dcSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0eae32dcSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0eae32dcSDimitry Andric //
7*0eae32dcSDimitry Andric //===----------------------------------------------------------------------===//
8*0eae32dcSDimitry Andric 
9*0eae32dcSDimitry Andric // Copyright (c) Microsoft Corporation.
10*0eae32dcSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11*0eae32dcSDimitry Andric 
12*0eae32dcSDimitry Andric // Copyright 2018 Ulf Adams
13*0eae32dcSDimitry Andric // Copyright (c) Microsoft Corporation. All rights reserved.
14*0eae32dcSDimitry Andric 
15*0eae32dcSDimitry Andric // Boost Software License - Version 1.0 - August 17th, 2003
16*0eae32dcSDimitry Andric 
17*0eae32dcSDimitry Andric // Permission is hereby granted, free of charge, to any person or organization
18*0eae32dcSDimitry Andric // obtaining a copy of the software and accompanying documentation covered by
19*0eae32dcSDimitry Andric // this license (the "Software") to use, reproduce, display, distribute,
20*0eae32dcSDimitry Andric // execute, and transmit the Software, and to prepare derivative works of the
21*0eae32dcSDimitry Andric // Software, and to permit third-parties to whom the Software is furnished to
22*0eae32dcSDimitry Andric // do so, all subject to the following:
23*0eae32dcSDimitry Andric 
24*0eae32dcSDimitry Andric // The copyright notices in the Software and this entire statement, including
25*0eae32dcSDimitry Andric // the above license grant, this restriction and the following disclaimer,
26*0eae32dcSDimitry Andric // must be included in all copies of the Software, in whole or in part, and
27*0eae32dcSDimitry Andric // all derivative works of the Software, unless such copies or derivative
28*0eae32dcSDimitry Andric // works are solely in the form of machine-executable object code generated by
29*0eae32dcSDimitry Andric // a source language processor.
30*0eae32dcSDimitry Andric 
31*0eae32dcSDimitry Andric // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32*0eae32dcSDimitry Andric // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33*0eae32dcSDimitry Andric // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
34*0eae32dcSDimitry Andric // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
35*0eae32dcSDimitry Andric // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
36*0eae32dcSDimitry Andric // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
37*0eae32dcSDimitry Andric // DEALINGS IN THE SOFTWARE.
38*0eae32dcSDimitry Andric 
39*0eae32dcSDimitry Andric #ifndef _LIBCPP_SRC_INCLUDE_RYU_COMMON_H
40*0eae32dcSDimitry Andric #define _LIBCPP_SRC_INCLUDE_RYU_COMMON_H
41*0eae32dcSDimitry Andric 
42*0eae32dcSDimitry Andric // Avoid formatting to keep the changes with the original code minimal.
43*0eae32dcSDimitry Andric // clang-format off
44*0eae32dcSDimitry Andric 
45*0eae32dcSDimitry Andric #include "__config"
46*0eae32dcSDimitry Andric 
47*0eae32dcSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
48*0eae32dcSDimitry Andric 
49*0eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __decimalLength9(const uint32_t __v) {
50*0eae32dcSDimitry Andric   // Function precondition: __v is not a 10-digit number.
51*0eae32dcSDimitry Andric   // (f2s: 9 digits are sufficient for round-tripping.)
52*0eae32dcSDimitry Andric   // (d2fixed: We print 9-digit blocks.)
53*0eae32dcSDimitry Andric   _LIBCPP_ASSERT(__v < 1000000000, "");
54*0eae32dcSDimitry Andric   if (__v >= 100000000) { return 9; }
55*0eae32dcSDimitry Andric   if (__v >= 10000000) { return 8; }
56*0eae32dcSDimitry Andric   if (__v >= 1000000) { return 7; }
57*0eae32dcSDimitry Andric   if (__v >= 100000) { return 6; }
58*0eae32dcSDimitry Andric   if (__v >= 10000) { return 5; }
59*0eae32dcSDimitry Andric   if (__v >= 1000) { return 4; }
60*0eae32dcSDimitry Andric   if (__v >= 100) { return 3; }
61*0eae32dcSDimitry Andric   if (__v >= 10) { return 2; }
62*0eae32dcSDimitry Andric   return 1;
63*0eae32dcSDimitry Andric }
64*0eae32dcSDimitry Andric 
65*0eae32dcSDimitry Andric // Returns __e == 0 ? 1 : ceil(log_2(5^__e)).
66*0eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI  inline int32_t __pow5bits(const int32_t __e) {
67*0eae32dcSDimitry Andric   // This approximation works up to the point that the multiplication overflows at __e = 3529.
68*0eae32dcSDimitry Andric   // If the multiplication were done in 64 bits, it would fail at 5^4004 which is just greater
69*0eae32dcSDimitry Andric   // than 2^9297.
70*0eae32dcSDimitry Andric   _LIBCPP_ASSERT(__e >= 0, "");
71*0eae32dcSDimitry Andric   _LIBCPP_ASSERT(__e <= 3528, "");
72*0eae32dcSDimitry Andric   return static_cast<int32_t>(((static_cast<uint32_t>(__e) * 1217359) >> 19) + 1);
73*0eae32dcSDimitry Andric }
74*0eae32dcSDimitry Andric 
75*0eae32dcSDimitry Andric // Returns floor(log_10(2^__e)).
76*0eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI  inline uint32_t __log10Pow2(const int32_t __e) {
77*0eae32dcSDimitry Andric   // The first value this approximation fails for is 2^1651 which is just greater than 10^297.
78*0eae32dcSDimitry Andric   _LIBCPP_ASSERT(__e >= 0, "");
79*0eae32dcSDimitry Andric   _LIBCPP_ASSERT(__e <= 1650, "");
80*0eae32dcSDimitry Andric   return (static_cast<uint32_t>(__e) * 78913) >> 18;
81*0eae32dcSDimitry Andric }
82*0eae32dcSDimitry Andric 
83*0eae32dcSDimitry Andric // Returns floor(log_10(5^__e)).
84*0eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __log10Pow5(const int32_t __e) {
85*0eae32dcSDimitry Andric   // The first value this approximation fails for is 5^2621 which is just greater than 10^1832.
86*0eae32dcSDimitry Andric   _LIBCPP_ASSERT(__e >= 0, "");
87*0eae32dcSDimitry Andric   _LIBCPP_ASSERT(__e <= 2620, "");
88*0eae32dcSDimitry Andric   return (static_cast<uint32_t>(__e) * 732923) >> 20;
89*0eae32dcSDimitry Andric }
90*0eae32dcSDimitry Andric 
91*0eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __float_to_bits(const float __f) {
92*0eae32dcSDimitry Andric   uint32_t __bits = 0;
93*0eae32dcSDimitry Andric   _VSTD::memcpy(&__bits, &__f, sizeof(float));
94*0eae32dcSDimitry Andric   return __bits;
95*0eae32dcSDimitry Andric }
96*0eae32dcSDimitry Andric 
97*0eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint64_t __double_to_bits(const double __d) {
98*0eae32dcSDimitry Andric   uint64_t __bits = 0;
99*0eae32dcSDimitry Andric   _VSTD::memcpy(&__bits, &__d, sizeof(double));
100*0eae32dcSDimitry Andric   return __bits;
101*0eae32dcSDimitry Andric }
102*0eae32dcSDimitry Andric 
103*0eae32dcSDimitry Andric _LIBCPP_END_NAMESPACE_STD
104*0eae32dcSDimitry Andric 
105*0eae32dcSDimitry Andric // clang-format on
106*0eae32dcSDimitry Andric 
107*0eae32dcSDimitry Andric #endif // _LIBCPP_SRC_INCLUDE_RYU_COMMON_H
108