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