Path: blob/main/contrib/llvm-project/libcxx/src/include/ryu/ryu.h
35265 views
// -*- C++ -*-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-exception6//7//===----------------------------------------------------------------------===//89// Copyright (c) Microsoft Corporation.10// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception1112// Copyright 2018 Ulf Adams13// Copyright (c) Microsoft Corporation. All rights reserved.1415// Boost Software License - Version 1.0 - August 17th, 20031617// Permission is hereby granted, free of charge, to any person or organization18// obtaining a copy of the software and accompanying documentation covered by19// this license (the "Software") to use, reproduce, display, distribute,20// execute, and transmit the Software, and to prepare derivative works of the21// Software, and to permit third-parties to whom the Software is furnished to22// do so, all subject to the following:2324// The copyright notices in the Software and this entire statement, including25// 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, and27// all derivative works of the Software, unless such copies or derivative28// works are solely in the form of machine-executable object code generated by29// a source language processor.3031// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR32// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,33// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT34// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE35// 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 OTHER37// DEALINGS IN THE SOFTWARE.3839#ifndef _LIBCPP_SRC_INCLUDE_RYU_RYU_H40#define _LIBCPP_SRC_INCLUDE_RYU_RYU_H4142// Avoid formatting to keep the changes with the original code minimal.43// clang-format off4445#include <__charconv/chars_format.h>46#include <__charconv/to_chars_result.h>47#include <__config>48#include <__system_error/errc.h>49#include <cstdint>50#include <cstring>51#include <type_traits>5253#include "include/ryu/f2s.h"54#include "include/ryu/d2s.h"55#include "include/ryu/d2fixed.h"5657#if defined(_MSC_VER)58#include <intrin.h> // for _umul128(), __shiftright128(), _BitScanForward{,64}59#endif // defined(_MSC_VER)6061#if defined(_WIN64) || defined(_M_AMD64) || defined(__x86_64__) || defined(__aarch64__)62#define _LIBCPP_64_BIT63#endif6465_LIBCPP_BEGIN_NAMESPACE_STD6667// https://github.com/ulfjack/ryu/tree/59661c3/ryu6869#if !defined(_MSC_VER)70_LIBCPP_HIDE_FROM_ABI inline unsigned char _BitScanForward64(unsigned long* __index, unsigned long long __mask) {71if (__mask == 0) {72return false;73}74*__index = __builtin_ctzll(__mask);75return true;76}7778_LIBCPP_HIDE_FROM_ABI inline unsigned char _BitScanForward(unsigned long* __index, unsigned int __mask) {79if (__mask == 0) {80return false;81}82*__index = __builtin_ctz(__mask);83return true;84}85#endif // !_MSC_VER8687template <class _Floating>88[[nodiscard]] to_chars_result _Floating_to_chars_ryu(89char* const _First, char* const _Last, const _Floating _Value, const chars_format _Fmt) noexcept {90if constexpr (_IsSame<_Floating, float>::value) {91return __f2s_buffered_n(_First, _Last, _Value, _Fmt);92} else {93return __d2s_buffered_n(_First, _Last, _Value, _Fmt);94}95}9697template <class _Floating>98[[nodiscard]] _LIBCPP_HIDE_FROM_ABI to_chars_result _Floating_to_chars_scientific_precision(99char* const _First, char* const _Last, const _Floating _Value, int _Precision) noexcept {100101// C11 7.21.6.1 "The fprintf function"/5:102// "A negative precision argument is taken as if the precision were omitted."103// /8: "e,E [...] if the precision is missing, it is taken as 6"104105if (_Precision < 0) {106_Precision = 6;107} else if (_Precision < 1'000'000'000) { // Match ' to fix compilation with GCC in C++11 mode108// _Precision is ok.109} else {110// Avoid integer overflow.111// (This defensive check is slightly nonconformant; it can be carefully improved in the future.)112return {_Last, errc::value_too_large};113}114115return __d2exp_buffered_n(_First, _Last, _Value, static_cast<uint32_t>(_Precision));116}117118template <class _Floating>119[[nodiscard]] _LIBCPP_HIDE_FROM_ABI to_chars_result _Floating_to_chars_fixed_precision(120char* const _First, char* const _Last, const _Floating _Value, int _Precision) noexcept {121122// C11 7.21.6.1 "The fprintf function"/5:123// "A negative precision argument is taken as if the precision were omitted."124// /8: "f,F [...] If the precision is missing, it is taken as 6"125126if (_Precision < 0) {127_Precision = 6;128} else if (_Precision < 1'000'000'000) { // Match ' to fix compilation with GCC in C++11 mode129// _Precision is ok.130} else {131// Avoid integer overflow.132// (This defensive check is slightly nonconformant; it can be carefully improved in the future.)133return {_Last, errc::value_too_large};134}135136return __d2fixed_buffered_n(_First, _Last, _Value, static_cast<uint32_t>(_Precision));137}138139#undef _LIBCPP_64_BIT140#undef _LIBCPP_INTRINSIC128141142_LIBCPP_END_NAMESPACE_STD143144// clang-format on145146#endif // _LIBCPP_SRC_INCLUDE_RYU_RYU_H147148149