Path: blob/main/contrib/llvm-project/libcxx/src/include/ryu/common.h
35265 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78// Copyright (c) Microsoft Corporation.9// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception1011// Copyright 2018 Ulf Adams12// Copyright (c) Microsoft Corporation. All rights reserved.1314// Boost Software License - Version 1.0 - August 17th, 20031516// Permission is hereby granted, free of charge, to any person or organization17// obtaining a copy of the software and accompanying documentation covered by18// this license (the "Software") to use, reproduce, display, distribute,19// execute, and transmit the Software, and to prepare derivative works of the20// Software, and to permit third-parties to whom the Software is furnished to21// do so, all subject to the following:2223// The copyright notices in the Software and this entire statement, including24// the above license grant, this restriction and the following disclaimer,25// must be included in all copies of the Software, in whole or in part, and26// all derivative works of the Software, unless such copies or derivative27// works are solely in the form of machine-executable object code generated by28// a source language processor.2930// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR31// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,32// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT33// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE34// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,35// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER36// DEALINGS IN THE SOFTWARE.3738#ifndef _LIBCPP_SRC_INCLUDE_RYU_COMMON_H39#define _LIBCPP_SRC_INCLUDE_RYU_COMMON_H4041// Avoid formatting to keep the changes with the original code minimal.42// clang-format off4344#include <__assert>45#include <__config>46#include <cstring>4748_LIBCPP_BEGIN_NAMESPACE_STD4950[[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_INTERNAL(__v < 1000000000, "");55if (__v >= 100000000) { return 9; }56if (__v >= 10000000) { return 8; }57if (__v >= 1000000) { return 7; }58if (__v >= 100000) { return 6; }59if (__v >= 10000) { return 5; }60if (__v >= 1000) { return 4; }61if (__v >= 100) { return 3; }62if (__v >= 10) { return 2; }63return 1;64}6566// 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 greater70// than 2^9297.71_LIBCPP_ASSERT_INTERNAL(__e >= 0, "");72_LIBCPP_ASSERT_INTERNAL(__e <= 3528, "");73return static_cast<int32_t>(((static_cast<uint32_t>(__e) * 1217359) >> 19) + 1);74}7576// 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_INTERNAL(__e >= 0, "");80_LIBCPP_ASSERT_INTERNAL(__e <= 1650, "");81return (static_cast<uint32_t>(__e) * 78913) >> 18;82}8384// 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_INTERNAL(__e >= 0, "");88_LIBCPP_ASSERT_INTERNAL(__e <= 2620, "");89return (static_cast<uint32_t>(__e) * 732923) >> 20;90}9192[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __float_to_bits(const float __f) {93uint32_t __bits = 0;94std::memcpy(&__bits, &__f, sizeof(float));95return __bits;96}9798[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint64_t __double_to_bits(const double __d) {99uint64_t __bits = 0;100std::memcpy(&__bits, &__d, sizeof(double));101return __bits;102}103104_LIBCPP_END_NAMESPACE_STD105106// clang-format on107108#endif // _LIBCPP_SRC_INCLUDE_RYU_COMMON_H109110111