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