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