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