1*0eae32dcSDimitry Andric // -*- C++ -*- 2*0eae32dcSDimitry Andric //===----------------------------------------------------------------------===// 3*0eae32dcSDimitry Andric // 4*0eae32dcSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5*0eae32dcSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 6*0eae32dcSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7*0eae32dcSDimitry Andric // 8*0eae32dcSDimitry Andric //===----------------------------------------------------------------------===// 9*0eae32dcSDimitry Andric 10*0eae32dcSDimitry Andric // Copyright (c) Microsoft Corporation. 11*0eae32dcSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 12*0eae32dcSDimitry Andric 13*0eae32dcSDimitry Andric 14*0eae32dcSDimitry Andric // Copyright 2018 Ulf Adams 15*0eae32dcSDimitry Andric // Copyright (c) Microsoft Corporation. All rights reserved. 16*0eae32dcSDimitry Andric 17*0eae32dcSDimitry Andric // Boost Software License - Version 1.0 - August 17th, 2003 18*0eae32dcSDimitry Andric 19*0eae32dcSDimitry Andric // Permission is hereby granted, free of charge, to any person or organization 20*0eae32dcSDimitry Andric // obtaining a copy of the software and accompanying documentation covered by 21*0eae32dcSDimitry Andric // this license (the "Software") to use, reproduce, display, distribute, 22*0eae32dcSDimitry Andric // execute, and transmit the Software, and to prepare derivative works of the 23*0eae32dcSDimitry Andric // Software, and to permit third-parties to whom the Software is furnished to 24*0eae32dcSDimitry Andric // do so, all subject to the following: 25*0eae32dcSDimitry Andric 26*0eae32dcSDimitry Andric // The copyright notices in the Software and this entire statement, including 27*0eae32dcSDimitry Andric // the above license grant, this restriction and the following disclaimer, 28*0eae32dcSDimitry Andric // must be included in all copies of the Software, in whole or in part, and 29*0eae32dcSDimitry Andric // all derivative works of the Software, unless such copies or derivative 30*0eae32dcSDimitry Andric // works are solely in the form of machine-executable object code generated by 31*0eae32dcSDimitry Andric // a source language processor. 32*0eae32dcSDimitry Andric 33*0eae32dcSDimitry Andric // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 34*0eae32dcSDimitry Andric // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 35*0eae32dcSDimitry Andric // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 36*0eae32dcSDimitry Andric // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 37*0eae32dcSDimitry Andric // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 38*0eae32dcSDimitry Andric // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 39*0eae32dcSDimitry Andric // DEALINGS IN THE SOFTWARE. 40*0eae32dcSDimitry Andric 41*0eae32dcSDimitry Andric #ifndef _LIBCPP_SRC_INCLUDE_RYU_RYU_H 42*0eae32dcSDimitry Andric #define _LIBCPP_SRC_INCLUDE_RYU_RYU_H 43*0eae32dcSDimitry Andric 44*0eae32dcSDimitry Andric // Avoid formatting to keep the changes with the original code minimal. 45*0eae32dcSDimitry Andric // clang-format off 46*0eae32dcSDimitry Andric 47*0eae32dcSDimitry Andric #include "__charconv/chars_format.h" 48*0eae32dcSDimitry Andric #include "__charconv/to_chars_result.h" 49*0eae32dcSDimitry Andric #include "__config" 50*0eae32dcSDimitry Andric #include "__debug" 51*0eae32dcSDimitry Andric #include "__errc" 52*0eae32dcSDimitry Andric #include "cstdint" 53*0eae32dcSDimitry Andric #include "cstring" 54*0eae32dcSDimitry Andric #include "type_traits" 55*0eae32dcSDimitry Andric #include "include/ryu/f2s.h" 56*0eae32dcSDimitry Andric #include "include/ryu/d2s.h" 57*0eae32dcSDimitry Andric #include "include/ryu/d2fixed.h" 58*0eae32dcSDimitry Andric 59*0eae32dcSDimitry Andric #if defined(_M_X64) && defined(_LIBCPP_COMPILER_MSVC) 60*0eae32dcSDimitry Andric #include <intrin0.h> // for _umul128() and __shiftright128() 61*0eae32dcSDimitry Andric #endif // defined(_M_X64) && defined(_LIBCPP_COMPILER_MSVC) 62*0eae32dcSDimitry Andric 63*0eae32dcSDimitry Andric #if defined(_WIN64) || defined(_M_AMD64) || defined(__x86_64__) || defined(__aarch64__) 64*0eae32dcSDimitry Andric #define _LIBCPP_64_BIT 65*0eae32dcSDimitry Andric #endif 66*0eae32dcSDimitry Andric 67*0eae32dcSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 68*0eae32dcSDimitry Andric 69*0eae32dcSDimitry Andric // https://github.com/ulfjack/ryu/tree/59661c3/ryu 70*0eae32dcSDimitry Andric 71*0eae32dcSDimitry Andric #if !defined(_LIBCPP_COMPILER_MSVC) 72*0eae32dcSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline unsigned char _BitScanForward64(unsigned long* __index, unsigned long long __mask) { 73*0eae32dcSDimitry Andric if (__mask == 0) { 74*0eae32dcSDimitry Andric return false; 75*0eae32dcSDimitry Andric } 76*0eae32dcSDimitry Andric *__index = __builtin_ctzll(__mask); 77*0eae32dcSDimitry Andric return true; 78*0eae32dcSDimitry Andric } 79*0eae32dcSDimitry Andric 80*0eae32dcSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline unsigned char _BitScanForward(unsigned long* __index, unsigned int __mask) { 81*0eae32dcSDimitry Andric if (__mask == 0) { 82*0eae32dcSDimitry Andric return false; 83*0eae32dcSDimitry Andric } 84*0eae32dcSDimitry Andric *__index = __builtin_ctz(__mask); 85*0eae32dcSDimitry Andric return true; 86*0eae32dcSDimitry Andric } 87*0eae32dcSDimitry Andric #endif // _LIBCPP_COMPILER_MSVC 88*0eae32dcSDimitry Andric 89*0eae32dcSDimitry Andric template <class _Floating> 90*0eae32dcSDimitry Andric [[nodiscard]] to_chars_result _Floating_to_chars_ryu( 91*0eae32dcSDimitry Andric char* const _First, char* const _Last, const _Floating _Value, const chars_format _Fmt) noexcept { 92*0eae32dcSDimitry Andric if constexpr (_IsSame<_Floating, float>::value) { 93*0eae32dcSDimitry Andric return __f2s_buffered_n(_First, _Last, _Value, _Fmt); 94*0eae32dcSDimitry Andric } else { 95*0eae32dcSDimitry Andric return __d2s_buffered_n(_First, _Last, _Value, _Fmt); 96*0eae32dcSDimitry Andric } 97*0eae32dcSDimitry Andric } 98*0eae32dcSDimitry Andric 99*0eae32dcSDimitry Andric template <class _Floating> 100*0eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI to_chars_result _Floating_to_chars_scientific_precision( 101*0eae32dcSDimitry Andric char* const _First, char* const _Last, const _Floating _Value, int _Precision) noexcept { 102*0eae32dcSDimitry Andric 103*0eae32dcSDimitry Andric // C11 7.21.6.1 "The fprintf function"/5: 104*0eae32dcSDimitry Andric // "A negative precision argument is taken as if the precision were omitted." 105*0eae32dcSDimitry Andric // /8: "e,E [...] if the precision is missing, it is taken as 6" 106*0eae32dcSDimitry Andric 107*0eae32dcSDimitry Andric if (_Precision < 0) { 108*0eae32dcSDimitry Andric _Precision = 6; 109*0eae32dcSDimitry Andric } else if (_Precision < 1'000'000'000) { // Match ' to fix compilation with GCC in C++11 mode 110*0eae32dcSDimitry Andric // _Precision is ok. 111*0eae32dcSDimitry Andric } else { 112*0eae32dcSDimitry Andric // Avoid integer overflow. 113*0eae32dcSDimitry Andric // (This defensive check is slightly nonconformant; it can be carefully improved in the future.) 114*0eae32dcSDimitry Andric return {_Last, errc::value_too_large}; 115*0eae32dcSDimitry Andric } 116*0eae32dcSDimitry Andric 117*0eae32dcSDimitry Andric return __d2exp_buffered_n(_First, _Last, _Value, static_cast<uint32_t>(_Precision)); 118*0eae32dcSDimitry Andric } 119*0eae32dcSDimitry Andric 120*0eae32dcSDimitry Andric template <class _Floating> 121*0eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI to_chars_result _Floating_to_chars_fixed_precision( 122*0eae32dcSDimitry Andric char* const _First, char* const _Last, const _Floating _Value, int _Precision) noexcept { 123*0eae32dcSDimitry Andric 124*0eae32dcSDimitry Andric // C11 7.21.6.1 "The fprintf function"/5: 125*0eae32dcSDimitry Andric // "A negative precision argument is taken as if the precision were omitted." 126*0eae32dcSDimitry Andric // /8: "f,F [...] If the precision is missing, it is taken as 6" 127*0eae32dcSDimitry Andric 128*0eae32dcSDimitry Andric if (_Precision < 0) { 129*0eae32dcSDimitry Andric _Precision = 6; 130*0eae32dcSDimitry Andric } else if (_Precision < 1'000'000'000) { // Match ' to fix compilation with GCC in C++11 mode 131*0eae32dcSDimitry Andric // _Precision is ok. 132*0eae32dcSDimitry Andric } else { 133*0eae32dcSDimitry Andric // Avoid integer overflow. 134*0eae32dcSDimitry Andric // (This defensive check is slightly nonconformant; it can be carefully improved in the future.) 135*0eae32dcSDimitry Andric return {_Last, errc::value_too_large}; 136*0eae32dcSDimitry Andric } 137*0eae32dcSDimitry Andric 138*0eae32dcSDimitry Andric return __d2fixed_buffered_n(_First, _Last, _Value, static_cast<uint32_t>(_Precision)); 139*0eae32dcSDimitry Andric } 140*0eae32dcSDimitry Andric 141*0eae32dcSDimitry Andric #undef _LIBCPP_64_BIT 142*0eae32dcSDimitry Andric #undef _LIBCPP_INTRINSIC128 143*0eae32dcSDimitry Andric 144*0eae32dcSDimitry Andric _LIBCPP_END_NAMESPACE_STD 145*0eae32dcSDimitry Andric 146*0eae32dcSDimitry Andric // clang-format on 147*0eae32dcSDimitry Andric 148*0eae32dcSDimitry Andric #endif // _LIBCPP_SRC_INCLUDE_RYU_RYU_H 149