10eae32dcSDimitry Andric // -*- C++ -*- 20eae32dcSDimitry Andric //===----------------------------------------------------------------------===// 30eae32dcSDimitry Andric // 40eae32dcSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 50eae32dcSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 60eae32dcSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 70eae32dcSDimitry Andric // 80eae32dcSDimitry Andric //===----------------------------------------------------------------------===// 90eae32dcSDimitry Andric 100eae32dcSDimitry Andric // Copyright (c) Microsoft Corporation. 110eae32dcSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 120eae32dcSDimitry Andric 130eae32dcSDimitry Andric 140eae32dcSDimitry Andric // Copyright 2018 Ulf Adams 150eae32dcSDimitry Andric // Copyright (c) Microsoft Corporation. All rights reserved. 160eae32dcSDimitry Andric 170eae32dcSDimitry Andric // Boost Software License - Version 1.0 - August 17th, 2003 180eae32dcSDimitry Andric 190eae32dcSDimitry Andric // Permission is hereby granted, free of charge, to any person or organization 200eae32dcSDimitry Andric // obtaining a copy of the software and accompanying documentation covered by 210eae32dcSDimitry Andric // this license (the "Software") to use, reproduce, display, distribute, 220eae32dcSDimitry Andric // execute, and transmit the Software, and to prepare derivative works of the 230eae32dcSDimitry Andric // Software, and to permit third-parties to whom the Software is furnished to 240eae32dcSDimitry Andric // do so, all subject to the following: 250eae32dcSDimitry Andric 260eae32dcSDimitry Andric // The copyright notices in the Software and this entire statement, including 270eae32dcSDimitry Andric // the above license grant, this restriction and the following disclaimer, 280eae32dcSDimitry Andric // must be included in all copies of the Software, in whole or in part, and 290eae32dcSDimitry Andric // all derivative works of the Software, unless such copies or derivative 300eae32dcSDimitry Andric // works are solely in the form of machine-executable object code generated by 310eae32dcSDimitry Andric // a source language processor. 320eae32dcSDimitry Andric 330eae32dcSDimitry Andric // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 340eae32dcSDimitry Andric // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 350eae32dcSDimitry Andric // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 360eae32dcSDimitry Andric // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 370eae32dcSDimitry Andric // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 380eae32dcSDimitry Andric // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 390eae32dcSDimitry Andric // DEALINGS IN THE SOFTWARE. 400eae32dcSDimitry Andric 410eae32dcSDimitry Andric #ifndef _LIBCPP_SRC_INCLUDE_RYU_RYU_H 420eae32dcSDimitry Andric #define _LIBCPP_SRC_INCLUDE_RYU_RYU_H 430eae32dcSDimitry Andric 440eae32dcSDimitry Andric // Avoid formatting to keep the changes with the original code minimal. 450eae32dcSDimitry Andric // clang-format off 460eae32dcSDimitry Andric 47*81ad6265SDimitry Andric #include <__charconv/chars_format.h> 48*81ad6265SDimitry Andric #include <__charconv/to_chars_result.h> 49*81ad6265SDimitry Andric #include <__config> 50*81ad6265SDimitry Andric #include <__debug> 51*81ad6265SDimitry Andric #include <__errc> 52*81ad6265SDimitry Andric #include <cstdint> 53*81ad6265SDimitry Andric #include <cstring> 54*81ad6265SDimitry Andric #include <type_traits> 55*81ad6265SDimitry Andric 560eae32dcSDimitry Andric #include "include/ryu/f2s.h" 570eae32dcSDimitry Andric #include "include/ryu/d2s.h" 580eae32dcSDimitry Andric #include "include/ryu/d2fixed.h" 590eae32dcSDimitry Andric 60*81ad6265SDimitry Andric #if defined(_MSC_VER) 61*81ad6265SDimitry Andric #include <intrin.h> // for _umul128(), __shiftright128(), _BitScanForward{,64} 62*81ad6265SDimitry Andric #endif // defined(_MSC_VER) 630eae32dcSDimitry Andric 640eae32dcSDimitry Andric #if defined(_WIN64) || defined(_M_AMD64) || defined(__x86_64__) || defined(__aarch64__) 650eae32dcSDimitry Andric #define _LIBCPP_64_BIT 660eae32dcSDimitry Andric #endif 670eae32dcSDimitry Andric 680eae32dcSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 690eae32dcSDimitry Andric 700eae32dcSDimitry Andric // https://github.com/ulfjack/ryu/tree/59661c3/ryu 710eae32dcSDimitry Andric 72*81ad6265SDimitry Andric #if !defined(_MSC_VER) 730eae32dcSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline unsigned char _BitScanForward64(unsigned long* __index, unsigned long long __mask) { 740eae32dcSDimitry Andric if (__mask == 0) { 750eae32dcSDimitry Andric return false; 760eae32dcSDimitry Andric } 770eae32dcSDimitry Andric *__index = __builtin_ctzll(__mask); 780eae32dcSDimitry Andric return true; 790eae32dcSDimitry Andric } 800eae32dcSDimitry Andric 810eae32dcSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline unsigned char _BitScanForward(unsigned long* __index, unsigned int __mask) { 820eae32dcSDimitry Andric if (__mask == 0) { 830eae32dcSDimitry Andric return false; 840eae32dcSDimitry Andric } 850eae32dcSDimitry Andric *__index = __builtin_ctz(__mask); 860eae32dcSDimitry Andric return true; 870eae32dcSDimitry Andric } 88*81ad6265SDimitry Andric #endif // !_MSC_VER 890eae32dcSDimitry Andric 900eae32dcSDimitry Andric template <class _Floating> 910eae32dcSDimitry Andric [[nodiscard]] to_chars_result _Floating_to_chars_ryu( 920eae32dcSDimitry Andric char* const _First, char* const _Last, const _Floating _Value, const chars_format _Fmt) noexcept { 930eae32dcSDimitry Andric if constexpr (_IsSame<_Floating, float>::value) { 940eae32dcSDimitry Andric return __f2s_buffered_n(_First, _Last, _Value, _Fmt); 950eae32dcSDimitry Andric } else { 960eae32dcSDimitry Andric return __d2s_buffered_n(_First, _Last, _Value, _Fmt); 970eae32dcSDimitry Andric } 980eae32dcSDimitry Andric } 990eae32dcSDimitry Andric 1000eae32dcSDimitry Andric template <class _Floating> 1010eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI to_chars_result _Floating_to_chars_scientific_precision( 1020eae32dcSDimitry Andric char* const _First, char* const _Last, const _Floating _Value, int _Precision) noexcept { 1030eae32dcSDimitry Andric 1040eae32dcSDimitry Andric // C11 7.21.6.1 "The fprintf function"/5: 1050eae32dcSDimitry Andric // "A negative precision argument is taken as if the precision were omitted." 1060eae32dcSDimitry Andric // /8: "e,E [...] if the precision is missing, it is taken as 6" 1070eae32dcSDimitry Andric 1080eae32dcSDimitry Andric if (_Precision < 0) { 1090eae32dcSDimitry Andric _Precision = 6; 1100eae32dcSDimitry Andric } else if (_Precision < 1'000'000'000) { // Match ' to fix compilation with GCC in C++11 mode 1110eae32dcSDimitry Andric // _Precision is ok. 1120eae32dcSDimitry Andric } else { 1130eae32dcSDimitry Andric // Avoid integer overflow. 1140eae32dcSDimitry Andric // (This defensive check is slightly nonconformant; it can be carefully improved in the future.) 1150eae32dcSDimitry Andric return {_Last, errc::value_too_large}; 1160eae32dcSDimitry Andric } 1170eae32dcSDimitry Andric 1180eae32dcSDimitry Andric return __d2exp_buffered_n(_First, _Last, _Value, static_cast<uint32_t>(_Precision)); 1190eae32dcSDimitry Andric } 1200eae32dcSDimitry Andric 1210eae32dcSDimitry Andric template <class _Floating> 1220eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI to_chars_result _Floating_to_chars_fixed_precision( 1230eae32dcSDimitry Andric char* const _First, char* const _Last, const _Floating _Value, int _Precision) noexcept { 1240eae32dcSDimitry Andric 1250eae32dcSDimitry Andric // C11 7.21.6.1 "The fprintf function"/5: 1260eae32dcSDimitry Andric // "A negative precision argument is taken as if the precision were omitted." 1270eae32dcSDimitry Andric // /8: "f,F [...] If the precision is missing, it is taken as 6" 1280eae32dcSDimitry Andric 1290eae32dcSDimitry Andric if (_Precision < 0) { 1300eae32dcSDimitry Andric _Precision = 6; 1310eae32dcSDimitry Andric } else if (_Precision < 1'000'000'000) { // Match ' to fix compilation with GCC in C++11 mode 1320eae32dcSDimitry Andric // _Precision is ok. 1330eae32dcSDimitry Andric } else { 1340eae32dcSDimitry Andric // Avoid integer overflow. 1350eae32dcSDimitry Andric // (This defensive check is slightly nonconformant; it can be carefully improved in the future.) 1360eae32dcSDimitry Andric return {_Last, errc::value_too_large}; 1370eae32dcSDimitry Andric } 1380eae32dcSDimitry Andric 1390eae32dcSDimitry Andric return __d2fixed_buffered_n(_First, _Last, _Value, static_cast<uint32_t>(_Precision)); 1400eae32dcSDimitry Andric } 1410eae32dcSDimitry Andric 1420eae32dcSDimitry Andric #undef _LIBCPP_64_BIT 1430eae32dcSDimitry Andric #undef _LIBCPP_INTRINSIC128 1440eae32dcSDimitry Andric 1450eae32dcSDimitry Andric _LIBCPP_END_NAMESPACE_STD 1460eae32dcSDimitry Andric 1470eae32dcSDimitry Andric // clang-format on 1480eae32dcSDimitry Andric 1490eae32dcSDimitry Andric #endif // _LIBCPP_SRC_INCLUDE_RYU_RYU_H 150