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 4781ad6265SDimitry Andric #include <__charconv/chars_format.h> 4881ad6265SDimitry Andric #include <__charconv/to_chars_result.h> 4981ad6265SDimitry Andric #include <__config> 50*06c3fb27SDimitry Andric #include <__system_error/errc.h> 5181ad6265SDimitry Andric #include <cstdint> 5281ad6265SDimitry Andric #include <cstring> 5381ad6265SDimitry Andric #include <type_traits> 5481ad6265SDimitry Andric 550eae32dcSDimitry Andric #include "include/ryu/f2s.h" 560eae32dcSDimitry Andric #include "include/ryu/d2s.h" 570eae32dcSDimitry Andric #include "include/ryu/d2fixed.h" 580eae32dcSDimitry Andric 5981ad6265SDimitry Andric #if defined(_MSC_VER) 6081ad6265SDimitry Andric #include <intrin.h> // for _umul128(), __shiftright128(), _BitScanForward{,64} 6181ad6265SDimitry Andric #endif // defined(_MSC_VER) 620eae32dcSDimitry Andric 630eae32dcSDimitry Andric #if defined(_WIN64) || defined(_M_AMD64) || defined(__x86_64__) || defined(__aarch64__) 640eae32dcSDimitry Andric #define _LIBCPP_64_BIT 650eae32dcSDimitry Andric #endif 660eae32dcSDimitry Andric 670eae32dcSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 680eae32dcSDimitry Andric 690eae32dcSDimitry Andric // https://github.com/ulfjack/ryu/tree/59661c3/ryu 700eae32dcSDimitry Andric 7181ad6265SDimitry Andric #if !defined(_MSC_VER) 720eae32dcSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline unsigned char _BitScanForward64(unsigned long* __index, unsigned long long __mask) { 730eae32dcSDimitry Andric if (__mask == 0) { 740eae32dcSDimitry Andric return false; 750eae32dcSDimitry Andric } 760eae32dcSDimitry Andric *__index = __builtin_ctzll(__mask); 770eae32dcSDimitry Andric return true; 780eae32dcSDimitry Andric } 790eae32dcSDimitry Andric 800eae32dcSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline unsigned char _BitScanForward(unsigned long* __index, unsigned int __mask) { 810eae32dcSDimitry Andric if (__mask == 0) { 820eae32dcSDimitry Andric return false; 830eae32dcSDimitry Andric } 840eae32dcSDimitry Andric *__index = __builtin_ctz(__mask); 850eae32dcSDimitry Andric return true; 860eae32dcSDimitry Andric } 8781ad6265SDimitry Andric #endif // !_MSC_VER 880eae32dcSDimitry Andric 890eae32dcSDimitry Andric template <class _Floating> 900eae32dcSDimitry Andric [[nodiscard]] to_chars_result _Floating_to_chars_ryu( 910eae32dcSDimitry Andric char* const _First, char* const _Last, const _Floating _Value, const chars_format _Fmt) noexcept { 920eae32dcSDimitry Andric if constexpr (_IsSame<_Floating, float>::value) { 930eae32dcSDimitry Andric return __f2s_buffered_n(_First, _Last, _Value, _Fmt); 940eae32dcSDimitry Andric } else { 950eae32dcSDimitry Andric return __d2s_buffered_n(_First, _Last, _Value, _Fmt); 960eae32dcSDimitry Andric } 970eae32dcSDimitry Andric } 980eae32dcSDimitry Andric 990eae32dcSDimitry Andric template <class _Floating> 1000eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI to_chars_result _Floating_to_chars_scientific_precision( 1010eae32dcSDimitry Andric char* const _First, char* const _Last, const _Floating _Value, int _Precision) noexcept { 1020eae32dcSDimitry Andric 1030eae32dcSDimitry Andric // C11 7.21.6.1 "The fprintf function"/5: 1040eae32dcSDimitry Andric // "A negative precision argument is taken as if the precision were omitted." 1050eae32dcSDimitry Andric // /8: "e,E [...] if the precision is missing, it is taken as 6" 1060eae32dcSDimitry Andric 1070eae32dcSDimitry Andric if (_Precision < 0) { 1080eae32dcSDimitry Andric _Precision = 6; 1090eae32dcSDimitry Andric } else if (_Precision < 1'000'000'000) { // Match ' to fix compilation with GCC in C++11 mode 1100eae32dcSDimitry Andric // _Precision is ok. 1110eae32dcSDimitry Andric } else { 1120eae32dcSDimitry Andric // Avoid integer overflow. 1130eae32dcSDimitry Andric // (This defensive check is slightly nonconformant; it can be carefully improved in the future.) 1140eae32dcSDimitry Andric return {_Last, errc::value_too_large}; 1150eae32dcSDimitry Andric } 1160eae32dcSDimitry Andric 1170eae32dcSDimitry Andric return __d2exp_buffered_n(_First, _Last, _Value, static_cast<uint32_t>(_Precision)); 1180eae32dcSDimitry Andric } 1190eae32dcSDimitry Andric 1200eae32dcSDimitry Andric template <class _Floating> 1210eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI to_chars_result _Floating_to_chars_fixed_precision( 1220eae32dcSDimitry Andric char* const _First, char* const _Last, const _Floating _Value, int _Precision) noexcept { 1230eae32dcSDimitry Andric 1240eae32dcSDimitry Andric // C11 7.21.6.1 "The fprintf function"/5: 1250eae32dcSDimitry Andric // "A negative precision argument is taken as if the precision were omitted." 1260eae32dcSDimitry Andric // /8: "f,F [...] If the precision is missing, it is taken as 6" 1270eae32dcSDimitry Andric 1280eae32dcSDimitry Andric if (_Precision < 0) { 1290eae32dcSDimitry Andric _Precision = 6; 1300eae32dcSDimitry Andric } else if (_Precision < 1'000'000'000) { // Match ' to fix compilation with GCC in C++11 mode 1310eae32dcSDimitry Andric // _Precision is ok. 1320eae32dcSDimitry Andric } else { 1330eae32dcSDimitry Andric // Avoid integer overflow. 1340eae32dcSDimitry Andric // (This defensive check is slightly nonconformant; it can be carefully improved in the future.) 1350eae32dcSDimitry Andric return {_Last, errc::value_too_large}; 1360eae32dcSDimitry Andric } 1370eae32dcSDimitry Andric 1380eae32dcSDimitry Andric return __d2fixed_buffered_n(_First, _Last, _Value, static_cast<uint32_t>(_Precision)); 1390eae32dcSDimitry Andric } 1400eae32dcSDimitry Andric 1410eae32dcSDimitry Andric #undef _LIBCPP_64_BIT 1420eae32dcSDimitry Andric #undef _LIBCPP_INTRINSIC128 1430eae32dcSDimitry Andric 1440eae32dcSDimitry Andric _LIBCPP_END_NAMESPACE_STD 1450eae32dcSDimitry Andric 1460eae32dcSDimitry Andric // clang-format on 1470eae32dcSDimitry Andric 1480eae32dcSDimitry Andric #endif // _LIBCPP_SRC_INCLUDE_RYU_RYU_H 149