3
3
4
4
#include < cfloat>
5
5
#include < cstdint>
6
- #ifndef _WIN32
7
- // strcasecmp, strncasecmp
8
- #include < strings.h>
9
- #endif
10
6
11
7
#if defined(_MSC_VER) && !defined(__clang__)
12
8
#define FASTFLOAT_VISUAL_STUDIO 1
16
12
#define fastfloat_really_inline __forceinline
17
13
#else
18
14
#define fastfloat_really_inline inline __attribute__ ((always_inline))
19
- #endif
15
+ #endif
20
16
21
17
namespace fast_float {
22
18
23
19
// Compares two ASCII strings in a case insensitive manner.
24
- inline bool fastfloat_strncasecmp (const char * input1, const char * input2, size_t length) {
20
+ inline bool fastfloat_strncasecmp (const char *input1, const char *input2,
21
+ size_t length) {
25
22
char running_diff{0 };
26
- for (size_t i = 0 ; i < length; i++) {
23
+ for (size_t i = 0 ; i < length; i++) {
27
24
running_diff |= (input1[i] ^ input2[i]);
28
25
}
29
26
return (running_diff == 0 ) || (running_diff == 32 );
@@ -33,14 +30,20 @@ inline bool fastfloat_strncasecmp(const char * input1, const char * input2, size
33
30
#error "FLT_EVAL_METHOD should be defined, please include cfloat."
34
31
#endif
35
32
36
-
37
-
38
-
39
-
40
-
41
33
bool is_space (uint8_t c) {
42
- static const bool table[] = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0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 0 , 0 , 0 , 0 , 0 ,
39
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
40
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
41
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
42
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
43
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
44
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
45
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
46
+ return table[c];
44
47
}
45
48
46
49
namespace {
@@ -49,18 +52,15 @@ constexpr uint32_t max_digit_without_overflow = 19;
49
52
constexpr int32_t decimal_point_range = 2047 ;
50
53
} // namespace
51
54
52
-
53
55
struct value128 {
54
56
uint64_t low;
55
57
uint64_t high;
56
58
value128 (uint64_t _low, uint64_t _high) : low(_low), high(_high) {}
57
59
value128 () : low(0 ), high(0 ) {}
58
60
};
59
61
60
-
61
62
/* result might be undefined when input_num is zero */
62
- fastfloat_really_inline
63
- int leading_zeroes (uint64_t input_num) {
63
+ fastfloat_really_inline int leading_zeroes (uint64_t input_num) {
64
64
#ifdef FASTFLOAT_VISUAL_STUDIO
65
65
unsigned long leading_zero = 0 ;
66
66
// Search the mask data from most significant bit (MSB)
@@ -74,18 +74,18 @@ int leading_zeroes(uint64_t input_num) {
74
74
#endif
75
75
}
76
76
77
-
78
77
#if defined(_WIN32) && !defined(__clang__)
79
78
// Note MinGW falls here too
80
79
#include < intrin.h>
81
80
82
- #if !defined(_M_X64) && !defined(_M_ARM64)// _umul128 for x86, arm
81
+ #if !defined(_M_X64) && !defined(_M_ARM64) // _umul128 for x86, arm
83
82
// this is a slow emulation routine for 32-bit Windows
84
83
//
85
84
fastfloat_really_inline uint64_t __emulu (uint32_t x, uint32_t y) {
86
85
return x * (uint64_t )y;
87
86
}
88
- fastfloat_really_inline uint64_t _umul128 (uint64_t ab, uint64_t cd, uint64_t *hi) {
87
+ fastfloat_really_inline uint64_t _umul128 (uint64_t ab, uint64_t cd,
88
+ uint64_t *hi) {
89
89
uint64_t ad = __emulu ((uint32_t )(ab >> 32 ), (uint32_t )cd);
90
90
uint64_t bd = __emulu ((uint32_t )ab, (uint32_t )cd);
91
91
uint64_t adbc = ad + __emulu ((uint32_t )ab, (uint32_t )(cd >> 32 ));
@@ -97,23 +97,25 @@ fastfloat_really_inline uint64_t _umul128(uint64_t ab, uint64_t cd, uint64_t *hi
97
97
}
98
98
#endif
99
99
100
- fastfloat_really_inline value128 full_multiplication (uint64_t value1, uint64_t value2) {
100
+ fastfloat_really_inline value128 full_multiplication (uint64_t value1,
101
+ uint64_t value2) {
101
102
value128 answer;
102
103
#ifdef _M_ARM64
103
104
// ARM64 has native support for 64-bit multiplications, no need to emultate
104
105
answer.high = __umulh (value1, value2);
105
106
answer.low = value1 * value2;
106
107
#else
107
- answer.low = _umul128 (value1, value2, &answer.high ); // _umul128 not available on ARM64
108
+ answer.low =
109
+ _umul128 (value1, value2, &answer.high ); // _umul128 not available on ARM64
108
110
#endif // _M_ARM64
109
111
return answer;
110
112
}
111
113
112
114
#else
113
115
114
116
// compute value1 * value2
115
- fastfloat_really_inline
116
- value128 full_multiplication ( uint64_t value1, uint64_t value2) {
117
+ fastfloat_really_inline value128 full_multiplication ( uint64_t value1,
118
+ uint64_t value2) {
117
119
value128 answer;
118
120
__uint128_t r = ((__uint128_t )value1) * value2;
119
121
answer.low = uint64_t (r);
@@ -125,9 +127,9 @@ value128 full_multiplication(uint64_t value1, uint64_t value2) {
125
127
126
128
struct adjusted_mantissa {
127
129
uint64_t mantissa;
128
- int power2;// a negative value indicate an invalid result
130
+ int power2; // a negative value indicate an invalid result
129
131
adjusted_mantissa () = default ;
130
- // bool operator==(const adjusted_mantissa &o) const = default;
132
+ // bool operator==(const adjusted_mantissa &o) const = default;
131
133
bool operator ==(const adjusted_mantissa &o) const {
132
134
return mantissa == o.mantissa && power2 == o.power2 ;
133
135
}
@@ -143,46 +145,47 @@ struct decimal {
143
145
// Copies are not allowed since this is a fat object.
144
146
decimal (const decimal &) = delete ;
145
147
// Copies are not allowed since this is a fat object.
146
- decimal & operator =(const decimal &) = delete ;
147
- // Moves are allowed:
148
+ decimal &operator =(const decimal &) = delete ;
149
+ // Moves are allowed:
148
150
decimal (decimal &&) = default ;
149
- decimal& operator =(decimal&& other) = default ;
151
+ decimal & operator =(decimal && other) = default ;
150
152
// Generates a mantissa by truncating to 19 digits; this function assumes
151
153
// that num_digits >= 19 (the caller is responsible for the check).
152
154
// This function should be reasonably fast.
153
155
inline uint64_t to_truncated_mantissa () {
154
- uint64_t val;
156
+ uint64_t val;
155
157
// 8 first digits
156
158
::memcpy (&val, digits, sizeof (uint64_t ));
157
159
val = val * 2561 >> 8 ;
158
160
val = (val & 0x00FF00FF00FF00FF ) * 6553601 >> 16 ;
159
- uint64_t mantissa = uint32_t ((val & 0x0000FFFF0000FFFF ) * 42949672960001 >> 32 );
161
+ uint64_t mantissa =
162
+ uint32_t ((val & 0x0000FFFF0000FFFF ) * 42949672960001 >> 32 );
160
163
// 8 more digits for a total of 16
161
164
::memcpy (&val, digits + sizeof (uint64_t ), sizeof(uint64_t ));
162
165
val = val * 2561 >> 8 ;
163
166
val = (val & 0x00FF00FF00FF00FF ) * 6553601 >> 16 ;
164
- uint32_t eight_digits_value = uint32_t ((val & 0x0000FFFF0000FFFF ) * 42949672960001 >> 32 );
167
+ uint32_t eight_digits_value =
168
+ uint32_t ((val & 0x0000FFFF0000FFFF ) * 42949672960001 >> 32 );
165
169
mantissa = 100000000 * mantissa + eight_digits_value;
166
- for (uint32_t i = 2 *sizeof (uint64_t ); i < max_digit_without_overflow; i++) {
170
+ for (uint32_t i = 2 * sizeof (uint64_t ); i < max_digit_without_overflow;
171
+ i++) {
167
172
mantissa = mantissa * 10 + digits[i]; // can be accelerated
168
173
}
169
174
return mantissa;
170
175
}
171
- // Generate san exponent matching to_truncated_mantissa()
176
+ // Generate san exponent matching to_truncated_mantissa()
172
177
inline int32_t to_truncated_exponent () {
173
178
return decimal_point - max_digit_without_overflow;
174
- }
175
-
179
+ }
176
180
};
177
181
178
182
constexpr static double powers_of_ten_double[] = {
179
183
1e0 , 1e1 , 1e2 , 1e3 , 1e4 , 1e5 , 1e6 , 1e7 , 1e8 , 1e9 , 1e10 , 1e11 ,
180
184
1e12 , 1e13 , 1e14 , 1e15 , 1e16 , 1e17 , 1e18 , 1e19 , 1e20 , 1e21 , 1e22 };
181
- constexpr static float powers_of_ten_float[] = {
182
- 1e0 , 1e1 , 1e2 , 1e3 , 1e4 , 1e5 , 1e6 , 1e7 , 1e8 , 1e9 , 1e10 };
185
+ constexpr static float powers_of_ten_float[] = {1e0 , 1e1 , 1e2 , 1e3 , 1e4 , 1e5 ,
186
+ 1e6 , 1e7 , 1e8 , 1e9 , 1e10 };
183
187
184
- template <typename T>
185
- struct binary_format {
188
+ template <typename T> struct binary_format {
186
189
static constexpr int mantissa_explicit_bits ();
187
190
static constexpr int minimum_exponent ();
188
191
static constexpr int infinite_power ();
@@ -195,102 +198,77 @@ struct binary_format {
195
198
static constexpr T exact_power_of_ten (int64_t power);
196
199
};
197
200
198
- template <>
199
- constexpr int binary_format<double >::mantissa_explicit_bits() {
201
+ template <> constexpr int binary_format<double >::mantissa_explicit_bits() {
200
202
return 52 ;
201
203
}
202
- template <>
203
- constexpr int binary_format<float >::mantissa_explicit_bits() {
204
+ template <> constexpr int binary_format<float >::mantissa_explicit_bits() {
204
205
return 23 ;
205
206
}
206
207
207
- template <>
208
- constexpr int binary_format<double >::max_exponent_round_to_even() {
208
+ template <> constexpr int binary_format<double >::max_exponent_round_to_even() {
209
209
return 23 ;
210
210
}
211
211
212
- template <>
213
- constexpr int binary_format<float >::max_exponent_round_to_even() {
212
+ template <> constexpr int binary_format<float >::max_exponent_round_to_even() {
214
213
return 10 ;
215
214
}
216
215
217
-
218
- template <>
219
- constexpr int binary_format<double >::min_exponent_round_to_even() {
216
+ template <> constexpr int binary_format<double >::min_exponent_round_to_even() {
220
217
return -4 ;
221
218
}
222
219
223
- template <>
224
- constexpr int binary_format<float >::min_exponent_round_to_even() {
220
+ template <> constexpr int binary_format<float >::min_exponent_round_to_even() {
225
221
return -17 ;
226
222
}
227
223
228
- template <>
229
- constexpr int binary_format<double >::minimum_exponent() {
224
+ template <> constexpr int binary_format<double >::minimum_exponent() {
230
225
return -1023 ;
231
226
}
232
- template <>
233
- constexpr int binary_format<float >::minimum_exponent() {
227
+ template <> constexpr int binary_format<float >::minimum_exponent() {
234
228
return -127 ;
235
229
}
236
230
237
- template <>
238
- constexpr int binary_format<double >::infinite_power() {
239
- return 0x7FF ;
231
+ template <> constexpr int binary_format<double >::infinite_power() {
232
+ return 0x7FF ;
240
233
}
241
- template <>
242
- constexpr int binary_format<float >::infinite_power() {
234
+ template <> constexpr int binary_format<float >::infinite_power() {
243
235
return 0xFF ;
244
236
}
245
237
246
- template <>
247
- constexpr int binary_format<double >::sign_index() {
248
- return 63 ;
249
- }
250
- template <>
251
- constexpr int binary_format<float >::sign_index() {
252
- return 31 ;
253
- }
238
+ template <> constexpr int binary_format<double >::sign_index() { return 63 ; }
239
+ template <> constexpr int binary_format<float >::sign_index() { return 31 ; }
254
240
255
- template <>
256
- constexpr int binary_format<double >::min_exponent_fast_path() {
241
+ template <> constexpr int binary_format<double >::min_exponent_fast_path() {
257
242
#if (FLT_EVAL_METHOD != 1) && (FLT_EVAL_METHOD != 0)
258
243
return 0 ;
259
244
#else
260
245
return -22 ;
261
246
#endif
262
247
}
263
- template <>
264
- constexpr int binary_format<float >::min_exponent_fast_path() {
248
+ template <> constexpr int binary_format<float >::min_exponent_fast_path() {
265
249
#if (FLT_EVAL_METHOD != 1) && (FLT_EVAL_METHOD != 0)
266
250
return 0 ;
267
251
#else
268
252
return -10 ;
269
253
#endif
270
254
}
271
255
272
-
273
- template <>
274
- constexpr int binary_format<double >::max_exponent_fast_path() {
256
+ template <> constexpr int binary_format<double >::max_exponent_fast_path() {
275
257
return 22 ;
276
258
}
277
- template <>
278
- constexpr int binary_format<float >::max_exponent_fast_path() {
259
+ template <> constexpr int binary_format<float >::max_exponent_fast_path() {
279
260
return 10 ;
280
261
}
281
262
282
-
283
- template <>
284
- constexpr uint64_t binary_format<double >::max_mantissa_fast_path() {
263
+ template <> constexpr uint64_t binary_format<double >::max_mantissa_fast_path() {
285
264
return uint64_t (2 ) << mantissa_explicit_bits ();
286
265
}
287
- template <>
288
- constexpr uint64_t binary_format<float >::max_mantissa_fast_path() {
266
+ template <> constexpr uint64_t binary_format<float >::max_mantissa_fast_path() {
289
267
return uint64_t (2 ) << mantissa_explicit_bits ();
290
268
}
291
269
292
270
template <>
293
- constexpr double binary_format<double >::exact_power_of_ten(int64_t power) {
271
+ constexpr double binary_format<double >::exact_power_of_ten(int64_t power) {
294
272
return powers_of_ten_double[power];
295
273
}
296
274
template <>
@@ -299,17 +277,17 @@ constexpr float binary_format<float>::exact_power_of_ten(int64_t power) {
299
277
return powers_of_ten_float[power];
300
278
}
301
279
302
-
303
-
304
280
} // namespace fast_float
305
281
306
282
// for convenience:
307
283
#include < ostream>
308
- std::ostream& operator <<(std::ostream& out, const fast_float::decimal& d) {
309
- out << " 0." ;
310
- for (size_t i = 0 ; i < d.num_digits ; i++) { out << int32_t (d.digits [i]); }
311
- out << " * 10 ** " << d.decimal_point ;
312
- return out;
284
+ std::ostream &operator <<(std::ostream &out, const fast_float::decimal &d) {
285
+ out << " 0." ;
286
+ for (size_t i = 0 ; i < d.num_digits ; i++) {
287
+ out << int32_t (d.digits [i]);
288
+ }
289
+ out << " * 10 ** " << d.decimal_point ;
290
+ return out;
313
291
}
314
292
315
293
#endif
0 commit comments