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