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