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