xref: /freebsd/contrib/llvm-project/libcxx/src/include/ryu/common.h (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
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 
48 _LIBCPP_BEGIN_NAMESPACE_STD
49 
50 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __decimalLength9(const uint32_t __v) {
51   // Function precondition: __v is not a 10-digit number.
52   // (f2s: 9 digits are sufficient for round-tripping.)
53   // (d2fixed: We print 9-digit blocks.)
54   _LIBCPP_ASSERT(__v < 1000000000, "");
55   if (__v >= 100000000) { return 9; }
56   if (__v >= 10000000) { return 8; }
57   if (__v >= 1000000) { return 7; }
58   if (__v >= 100000) { return 6; }
59   if (__v >= 10000) { return 5; }
60   if (__v >= 1000) { return 4; }
61   if (__v >= 100) { return 3; }
62   if (__v >= 10) { return 2; }
63   return 1;
64 }
65 
66 // Returns __e == 0 ? 1 : ceil(log_2(5^__e)).
67 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI  inline int32_t __pow5bits(const int32_t __e) {
68   // This approximation works up to the point that the multiplication overflows at __e = 3529.
69   // If the multiplication were done in 64 bits, it would fail at 5^4004 which is just greater
70   // than 2^9297.
71   _LIBCPP_ASSERT(__e >= 0, "");
72   _LIBCPP_ASSERT(__e <= 3528, "");
73   return static_cast<int32_t>(((static_cast<uint32_t>(__e) * 1217359) >> 19) + 1);
74 }
75 
76 // Returns floor(log_10(2^__e)).
77 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI  inline uint32_t __log10Pow2(const int32_t __e) {
78   // The first value this approximation fails for is 2^1651 which is just greater than 10^297.
79   _LIBCPP_ASSERT(__e >= 0, "");
80   _LIBCPP_ASSERT(__e <= 1650, "");
81   return (static_cast<uint32_t>(__e) * 78913) >> 18;
82 }
83 
84 // Returns floor(log_10(5^__e)).
85 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __log10Pow5(const int32_t __e) {
86   // The first value this approximation fails for is 5^2621 which is just greater than 10^1832.
87   _LIBCPP_ASSERT(__e >= 0, "");
88   _LIBCPP_ASSERT(__e <= 2620, "");
89   return (static_cast<uint32_t>(__e) * 732923) >> 20;
90 }
91 
92 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __float_to_bits(const float __f) {
93   uint32_t __bits = 0;
94   _VSTD::memcpy(&__bits, &__f, sizeof(float));
95   return __bits;
96 }
97 
98 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint64_t __double_to_bits(const double __d) {
99   uint64_t __bits = 0;
100   _VSTD::memcpy(&__bits, &__d, sizeof(double));
101   return __bits;
102 }
103 
104 _LIBCPP_END_NAMESPACE_STD
105 
106 // clang-format on
107 
108 #endif // _LIBCPP_SRC_INCLUDE_RYU_COMMON_H
109