-
Notifications
You must be signed in to change notification settings - Fork 13
/
from_chars.cpp
363 lines (307 loc) · 12.5 KB
/
from_chars.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright 2022 Peter Dimov
// Copyright 2023 Matt Borland
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
// https://stackoverflow.com/questions/38060411/visual-studio-2015-wont-suppress-error-c4996
#ifndef _SCL_SECURE_NO_WARNINGS
# define _SCL_SECURE_NO_WARNINGS
#endif
#ifndef NO_WARN_MBCS_MFC_DEPRECATION
# define NO_WARN_MBCS_MFC_DEPRECATION
#endif
#include <boost/charconv/detail/fast_float/fast_float.hpp>
#include <boost/charconv/detail/from_chars_float_impl.hpp>
#include <boost/charconv/from_chars.hpp>
#include <boost/charconv/detail/bit_layouts.hpp>
#include <boost/charconv/detail/generate_nan.hpp>
#include <system_error>
#include <string>
#include <cstdlib>
#include <cerrno>
#include <cstring>
#include <limits>
#if BOOST_CHARCONV_LDBL_BITS > 64
# include <boost/charconv/detail/compute_float80.hpp>
# include <boost/charconv/detail/emulated128.hpp>
#endif
#if defined(__GNUC__) && __GNUC__ < 5
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#endif
boost::charconv::from_chars_result boost::charconv::from_chars(const char* first, const char* last, float& value, boost::charconv::chars_format fmt) noexcept
{
if (fmt != boost::charconv::chars_format::hex)
{
return boost::charconv::detail::fast_float::from_chars(first, last, value, fmt);
}
return boost::charconv::detail::from_chars_float_impl(first, last, value, fmt);
}
boost::charconv::from_chars_result boost::charconv::from_chars(const char* first, const char* last, double& value, boost::charconv::chars_format fmt) noexcept
{
if (fmt != boost::charconv::chars_format::hex)
{
return boost::charconv::detail::fast_float::from_chars(first, last, value, fmt);
}
return boost::charconv::detail::from_chars_float_impl(first, last, value, fmt);
}
#ifdef BOOST_CHARCONV_HAS_FLOAT128
boost::charconv::from_chars_result boost::charconv::from_chars(const char* first, const char* last, __float128& value, boost::charconv::chars_format fmt) noexcept
{
bool sign {};
std::int64_t exponent {};
#if defined(BOOST_CHARCONV_HAS_INT128) && ((defined(__clang_major__) && __clang_major__ > 12 ) || \
(defined(BOOST_GCC) && BOOST_GCC > 100000))
boost::uint128_type significand {};
#else
boost::charconv::detail::uint128 significand {};
#endif
auto r = boost::charconv::detail::parser(first, last, sign, significand, exponent, fmt);
if (r.ec == std::errc::value_too_large)
{
r.ec = std::errc();
#if BOOST_CHARCONV_HAS_BUILTIN(__builtin_inf)
value = sign ? -static_cast<__float128>(__builtin_inf()) : static_cast<__float128>(__builtin_inf());
#else // Conversion from HUGE_VALL should work
value = sign ? -static_cast<__float128>(HUGE_VALL) : static_cast<__float128>(HUGE_VALL);
#endif
return r;
}
else if (r.ec == std::errc::not_supported)
{
r.ec = std::errc();
if (significand == 0)
{
#if BOOST_CHARCONV_HAS_BUILTIN(__builtin_nanq)
value = sign ? -static_cast<__float128>(__builtin_nanq("")) : static_cast<__float128>(__builtin_nanq(""));
#elif BOOST_CHARCONV_HAS_BUILTIN(__nanq)
value = sign ? -static_cast<__float128>(__nanq("")) : static_cast<__float128>(__nanq(""));
#else
value = boost::charconv::detail::nanq();
value = sign ? -value : value;
#endif
}
else
{
#if BOOST_CHARCONV_HAS_BUILTIN(__builtin_nansq)
value = sign ? -static_cast<__float128>(__builtin_nansq("")) : static_cast<__float128>(__builtin_nansq(""));
#elif BOOST_CHARCONV_HAS_BUILTIN(__nansq)
value = sign ? -static_cast<__float128>(__nansq("")) : static_cast<__float128>(__nansq(""));
#else
value = boost::charconv::detail::nans();
value = sign ? -value : value;
#endif
}
return r;
}
else if (r.ec != std::errc())
{
return r;
}
else if (significand == 0)
{
value = sign ? -0.0Q : 0.0Q;
return r;
}
std::errc success {};
auto return_val = boost::charconv::detail::compute_float128(exponent, significand, sign, success);
r.ec = static_cast<std::errc>(success);
if (r.ec == std::errc() || r.ec == std::errc::result_out_of_range)
{
value = return_val;
}
else if (r.ec == std::errc::not_supported)
{
// Fallback routine
r = boost::charconv::detail::from_chars_strtod(first, last, value);
}
return r;
}
#endif
#ifdef BOOST_CHARCONV_HAS_FLOAT16
boost::charconv::from_chars_result boost::charconv::from_chars(const char* first, const char* last, std::float16_t& value, boost::charconv::chars_format fmt) noexcept
{
float f;
const auto r = boost::charconv::from_chars(first, last, f, fmt);
if (r.ec == std::errc())
{
value = static_cast<std::float16_t>(f);
}
return r;
}
#endif
#ifdef BOOST_CHARCONV_HAS_FLOAT32
boost::charconv::from_chars_result boost::charconv::from_chars(const char* first, const char* last, std::float32_t& value, boost::charconv::chars_format fmt) noexcept
{
static_assert(std::numeric_limits<std::float32_t>::digits == FLT_MANT_DIG &&
std::numeric_limits<std::float32_t>::min_exponent == FLT_MIN_EXP,
"float and std::float32_t are not the same layout like they should be");
float f;
std::memcpy(&f, &value, sizeof(float));
const auto r = boost::charconv::from_chars(first, last, f, fmt);
std::memcpy(&value, &f, sizeof(std::float32_t));
return r;
}
#endif
#ifdef BOOST_CHARCONV_HAS_FLOAT64
boost::charconv::from_chars_result boost::charconv::from_chars(const char* first, const char* last, std::float64_t& value, boost::charconv::chars_format fmt) noexcept
{
static_assert(std::numeric_limits<std::float64_t>::digits == DBL_MANT_DIG &&
std::numeric_limits<std::float64_t>::min_exponent == DBL_MIN_EXP,
"double and std::float64_t are not the same layout like they should be");
double d;
std::memcpy(&d, &value, sizeof(double));
const auto r = boost::charconv::from_chars(first, last, d, fmt);
std::memcpy(&value, &d, sizeof(std::float64_t));
return r;
}
#endif
#ifdef BOOST_CHARCONV_HAS_BRAINFLOAT16
boost::charconv::from_chars_result boost::charconv::from_chars(const char* first, const char* last, std::bfloat16_t& value, boost::charconv::chars_format fmt) noexcept
{
float f;
const auto r = boost::charconv::from_chars(first, last, f, fmt);
if (r.ec == std::errc())
{
value = static_cast<std::bfloat16_t>(f);
}
return r;
}
#endif
#if BOOST_CHARCONV_LDBL_BITS == 64 || defined(BOOST_MSVC)
// Since long double is just a double we use the double implementation and cast into value
boost::charconv::from_chars_result boost::charconv::from_chars(const char* first, const char* last, long double& value, boost::charconv::chars_format fmt) noexcept
{
static_assert(sizeof(double) == sizeof(long double), "64 bit long double detected, but the size is incorrect");
double d;
std::memcpy(&d, &value, sizeof(double));
const auto r = boost::charconv::from_chars(first, last, d, fmt);
std::memcpy(&value, &d, sizeof(long double));
return r;
}
#else
boost::charconv::from_chars_result boost::charconv::from_chars(const char* first, const char* last, long double& value, boost::charconv::chars_format fmt) noexcept
{
static_assert(std::numeric_limits<long double>::is_iec559, "Long double must be IEEE 754 compliant");
bool sign {};
std::int64_t exponent {};
#if defined(BOOST_CHARCONV_HAS_INT128) && ((defined(__clang_major__) && __clang_major__ > 12 ) || \
(defined(BOOST_GCC) && BOOST_GCC > 100000))
boost::uint128_type significand {};
#else
boost::charconv::detail::uint128 significand {};
#endif
auto r = boost::charconv::detail::parser(first, last, sign, significand, exponent, fmt);
if (r.ec == std::errc::value_too_large)
{
r.ec = std::errc();
value = sign ? -std::numeric_limits<long double>::infinity() : std::numeric_limits<long double>::infinity();
return r;
}
else if (r.ec == std::errc::not_supported)
{
r.ec = std::errc();
if (significand == 0)
{
value = sign ? -std::numeric_limits<long double>::quiet_NaN() : std::numeric_limits<long double>::quiet_NaN();
}
else
{
value = sign ? -std::numeric_limits<long double>::signaling_NaN() : std::numeric_limits<long double>::signaling_NaN();
}
return r;
}
else if (r.ec != std::errc())
{
return r;
}
else if (significand == 0)
{
value = sign ? -0.0L : 0.0L;
return r;
}
std::errc success {};
auto return_val = boost::charconv::detail::compute_float80<long double>(exponent, significand, sign, success);
r.ec = success;
if (r.ec == std::errc() || r.ec == std::errc::result_out_of_range)
{
value = return_val;
}
else if (r.ec == std::errc::not_supported)
{
// Fallback routine
r = boost::charconv::detail::from_chars_strtod(first, last, value);
}
return r;
}
#if defined(BOOST_CHARCONV_HAS_STDFLOAT128) && defined(BOOST_CHARCONV_HAS_FLOAT128)
boost::charconv::from_chars_result boost::charconv::from_chars(const char* first, const char* last, std::float128_t& value, boost::charconv::chars_format fmt) noexcept
{
static_assert(sizeof(__float128) == sizeof(std::float128_t));
__float128 q;
std::memcpy(&q, &value, sizeof(__float128));
const auto r = boost::charconv::from_chars(first, last, q, fmt);
std::memcpy(&value, &q, sizeof(std::float128_t));
return r;
}
#endif
#endif // long double implementations
namespace {
template <typename T>
boost::charconv::from_chars_result from_chars_strict_impl(const char *first, const char *last, T &value, boost::charconv::chars_format fmt) noexcept
{
T temp_value;
const auto r = boost::charconv::from_chars(first, last, temp_value, fmt);
if (r)
{
value = temp_value;
}
return r;
}
}
boost::charconv::from_chars_result boost::charconv::from_chars_strict(const char* first, const char* last, float& value, boost::charconv::chars_format fmt) noexcept
{
return from_chars_strict_impl(first, last, value, fmt);
}
boost::charconv::from_chars_result boost::charconv::from_chars_strict(const char* first, const char* last, double& value, boost::charconv::chars_format fmt) noexcept
{
return from_chars_strict_impl(first, last, value, fmt);
}
boost::charconv::from_chars_result boost::charconv::from_chars_strict(const char* first, const char* last, long double& value, boost::charconv::chars_format fmt) noexcept
{
return from_chars_strict_impl(first, last, value, fmt);
}
#ifdef BOOST_CHARCONV_HAS_FLOAT128
boost::charconv::from_chars_result boost::charconv::from_chars_strict(const char* first, const char* last, __float128& value, boost::charconv::chars_format fmt) noexcept
{
return from_chars_strict_impl(first, last, value, fmt);
}
#endif
#ifdef BOOST_CHARCONV_HAS_FLOAT16
boost::charconv::from_chars_result boost::charconv::from_chars_strict(const char* first, const char* last, std::float16_t& value, boost::charconv::chars_format fmt) noexcept
{
return from_chars_strict_impl(first, last, value, fmt);
}
#endif
#ifdef BOOST_CHARCONV_HAS_FLOAT32
boost::charconv::from_chars_result boost::charconv::from_chars_strict(const char* first, const char* last, std::float32_t& value, boost::charconv::chars_format fmt) noexcept
{
return from_chars_strict_impl(first, last, value, fmt);
}
#endif
#ifdef BOOST_CHARCONV_HAS_FLOAT64
boost::charconv::from_chars_result boost::charconv::from_chars_strict(const char* first, const char* last, std::float64_t& value, boost::charconv::chars_format fmt) noexcept
{
return from_chars_strict_impl(first, last, value, fmt);
}
#endif
#if defined(BOOST_CHARCONV_HAS_STDFLOAT128) && defined(BOOST_CHARCONV_HAS_FLOAT128)
boost::charconv::from_chars_result boost::charconv::from_chars_strict(const char* first, const char* last, std::float128_t& value, boost::charconv::chars_format fmt) noexcept
{
return from_chars_strict_impl(first, last, value, fmt);
}
#endif
#ifdef BOOST_CHARCONV_HAS_BRAINFLOAT16
boost::charconv::from_chars_result boost::charconv::from_chars_strict(const char* first, const char* last, std::bfloat16_t& value, boost::charconv::chars_format fmt) noexcept
{
return from_chars_strict_impl(first, last, value, fmt);
}
#endif