Skip to content

Commit 288efd3

Browse files
committed
Minor cleaning.
1 parent 2e1d8ba commit 288efd3

File tree

2 files changed

+72
-97
lines changed

2 files changed

+72
-97
lines changed

include/fast_float/decimal_to_binary.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414

1515
namespace fast_float {
1616

17-
18-
19-
2017
// This will compute or rather approximate w * 5**q and return a pair of 64-bit words approximating
2118
// the result, with the "high" part corresponding to the most significant bits and the
2219
// low part corresponding to the least significant bits.
@@ -91,7 +88,7 @@ adjusted_mantissa compute_float(int64_t q, uint64_t w) noexcept {
9188
// 2. We need an extra bit for rounding purposes
9289
// 3. We might lose a bit due to the "upperbit" routine (result too small, requiring a shift)
9390
value128 product = compute_product_approximation<binary::mantissa_explicit_bits() + 3>(q, w);
94-
if(product.low == 0xFFFFFFFFFFFFFFFF) { // could guard it further
91+
if(product.low == 0xFFFFFFFFFFFFFFFF) { // could guard it further
9592
// In some very rare cases, this could happen, in which case we might need a more accurate
9693
// computation that what we can provide cheaply. This is very, very unlikely.
9794
answer.power2 = -1; // This (a negative value) indicates an error condition.

include/fast_float/float_common.h

Lines changed: 71 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33

44
#include <cfloat>
55
#include <cstdint>
6-
#ifndef _WIN32
7-
// strcasecmp, strncasecmp
8-
#include <strings.h>
9-
#endif
106

117
#if defined(_MSC_VER) && !defined(__clang__)
128
#define FASTFLOAT_VISUAL_STUDIO 1
@@ -16,14 +12,15 @@
1612
#define fastfloat_really_inline __forceinline
1713
#else
1814
#define fastfloat_really_inline inline __attribute__((always_inline))
19-
#endif
15+
#endif
2016

2117
namespace fast_float {
2218

2319
// 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) {
2522
char running_diff{0};
26-
for(size_t i = 0; i < length; i++) {
23+
for (size_t i = 0; i < length; i++) {
2724
running_diff |= (input1[i] ^ input2[i]);
2825
}
2926
return (running_diff == 0) || (running_diff == 32);
@@ -33,14 +30,20 @@ inline bool fastfloat_strncasecmp(const char * input1, const char * input2, size
3330
#error "FLT_EVAL_METHOD should be defined, please include cfloat."
3431
#endif
3532

36-
37-
38-
39-
40-
4133
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, 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, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
43-
return table[c];
34+
static const bool table[] = {
35+
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
36+
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
37+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
38+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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];
4447
}
4548

4649
namespace {
@@ -49,18 +52,15 @@ constexpr uint32_t max_digit_without_overflow = 19;
4952
constexpr int32_t decimal_point_range = 2047;
5053
} // namespace
5154

52-
5355
struct value128 {
5456
uint64_t low;
5557
uint64_t high;
5658
value128(uint64_t _low, uint64_t _high) : low(_low), high(_high) {}
5759
value128() : low(0), high(0) {}
5860
};
5961

60-
6162
/* 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) {
6464
#ifdef FASTFLOAT_VISUAL_STUDIO
6565
unsigned long leading_zero = 0;
6666
// Search the mask data from most significant bit (MSB)
@@ -74,18 +74,18 @@ int leading_zeroes(uint64_t input_num) {
7474
#endif
7575
}
7676

77-
7877
#if defined(_WIN32) && !defined(__clang__)
7978
// Note MinGW falls here too
8079
#include <intrin.h>
8180

82-
#if !defined(_M_X64) && !defined(_M_ARM64)// _umul128 for x86, arm
81+
#if !defined(_M_X64) && !defined(_M_ARM64) // _umul128 for x86, arm
8382
// this is a slow emulation routine for 32-bit Windows
8483
//
8584
fastfloat_really_inline uint64_t __emulu(uint32_t x, uint32_t y) {
8685
return x * (uint64_t)y;
8786
}
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) {
8989
uint64_t ad = __emulu((uint32_t)(ab >> 32), (uint32_t)cd);
9090
uint64_t bd = __emulu((uint32_t)ab, (uint32_t)cd);
9191
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
9797
}
9898
#endif
9999

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) {
101102
value128 answer;
102103
#ifdef _M_ARM64
103104
// ARM64 has native support for 64-bit multiplications, no need to emultate
104105
answer.high = __umulh(value1, value2);
105106
answer.low = value1 * value2;
106107
#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
108110
#endif // _M_ARM64
109111
return answer;
110112
}
111113

112114
#else
113115

114116
// 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) {
117119
value128 answer;
118120
__uint128_t r = ((__uint128_t)value1) * value2;
119121
answer.low = uint64_t(r);
@@ -125,9 +127,9 @@ value128 full_multiplication(uint64_t value1, uint64_t value2) {
125127

126128
struct adjusted_mantissa {
127129
uint64_t mantissa;
128-
int power2;// a negative value indicate an invalid result
130+
int power2; // a negative value indicate an invalid result
129131
adjusted_mantissa() = default;
130-
//bool operator==(const adjusted_mantissa &o) const = default;
132+
// bool operator==(const adjusted_mantissa &o) const = default;
131133
bool operator==(const adjusted_mantissa &o) const {
132134
return mantissa == o.mantissa && power2 == o.power2;
133135
}
@@ -143,46 +145,47 @@ struct decimal {
143145
// Copies are not allowed since this is a fat object.
144146
decimal(const decimal &) = delete;
145147
// 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:
148150
decimal(decimal &&) = default;
149-
decimal& operator=(decimal&& other) = default;
151+
decimal &operator=(decimal &&other) = default;
150152
// Generates a mantissa by truncating to 19 digits; this function assumes
151153
// that num_digits >= 19 (the caller is responsible for the check).
152154
// This function should be reasonably fast.
153155
inline uint64_t to_truncated_mantissa() {
154-
uint64_t val;
156+
uint64_t val;
155157
// 8 first digits
156158
::memcpy(&val, digits, sizeof(uint64_t));
157159
val = val * 2561 >> 8;
158160
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);
160163
// 8 more digits for a total of 16
161164
::memcpy(&val, digits + sizeof(uint64_t), sizeof(uint64_t));
162165
val = val * 2561 >> 8;
163166
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);
165169
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++) {
167172
mantissa = mantissa * 10 + digits[i]; // can be accelerated
168173
}
169174
return mantissa;
170175
}
171-
// Generate san exponent matching to_truncated_mantissa()
176+
// Generate san exponent matching to_truncated_mantissa()
172177
inline int32_t to_truncated_exponent() {
173178
return decimal_point - max_digit_without_overflow;
174-
}
175-
179+
}
176180
};
177181

178182
constexpr static double powers_of_ten_double[] = {
179183
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11,
180184
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};
183187

184-
template <typename T>
185-
struct binary_format {
188+
template <typename T> struct binary_format {
186189
static constexpr int mantissa_explicit_bits();
187190
static constexpr int minimum_exponent();
188191
static constexpr int infinite_power();
@@ -195,102 +198,77 @@ struct binary_format {
195198
static constexpr T exact_power_of_ten(int64_t power);
196199
};
197200

198-
template <>
199-
constexpr int binary_format<double>::mantissa_explicit_bits() {
201+
template <> constexpr int binary_format<double>::mantissa_explicit_bits() {
200202
return 52;
201203
}
202-
template <>
203-
constexpr int binary_format<float>::mantissa_explicit_bits() {
204+
template <> constexpr int binary_format<float>::mantissa_explicit_bits() {
204205
return 23;
205206
}
206207

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() {
209209
return 23;
210210
}
211211

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() {
214213
return 10;
215214
}
216215

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() {
220217
return -4;
221218
}
222219

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() {
225221
return -17;
226222
}
227223

228-
template <>
229-
constexpr int binary_format<double>::minimum_exponent() {
224+
template <> constexpr int binary_format<double>::minimum_exponent() {
230225
return -1023;
231226
}
232-
template <>
233-
constexpr int binary_format<float>::minimum_exponent() {
227+
template <> constexpr int binary_format<float>::minimum_exponent() {
234228
return -127;
235229
}
236230

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;
240233
}
241-
template <>
242-
constexpr int binary_format<float>::infinite_power() {
234+
template <> constexpr int binary_format<float>::infinite_power() {
243235
return 0xFF;
244236
}
245237

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; }
254240

255-
template <>
256-
constexpr int binary_format<double>::min_exponent_fast_path() {
241+
template <> constexpr int binary_format<double>::min_exponent_fast_path() {
257242
#if (FLT_EVAL_METHOD != 1) && (FLT_EVAL_METHOD != 0)
258243
return 0;
259244
#else
260245
return -22;
261246
#endif
262247
}
263-
template <>
264-
constexpr int binary_format<float>::min_exponent_fast_path() {
248+
template <> constexpr int binary_format<float>::min_exponent_fast_path() {
265249
#if (FLT_EVAL_METHOD != 1) && (FLT_EVAL_METHOD != 0)
266250
return 0;
267251
#else
268252
return -10;
269253
#endif
270254
}
271255

272-
273-
template <>
274-
constexpr int binary_format<double>::max_exponent_fast_path() {
256+
template <> constexpr int binary_format<double>::max_exponent_fast_path() {
275257
return 22;
276258
}
277-
template <>
278-
constexpr int binary_format<float>::max_exponent_fast_path() {
259+
template <> constexpr int binary_format<float>::max_exponent_fast_path() {
279260
return 10;
280261
}
281262

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() {
285264
return uint64_t(2) << mantissa_explicit_bits();
286265
}
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() {
289267
return uint64_t(2) << mantissa_explicit_bits();
290268
}
291269

292270
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) {
294272
return powers_of_ten_double[power];
295273
}
296274
template <>
@@ -299,17 +277,17 @@ constexpr float binary_format<float>::exact_power_of_ten(int64_t power) {
299277
return powers_of_ten_float[power];
300278
}
301279

302-
303-
304280
} // namespace fast_float
305281

306282
// for convenience:
307283
#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;
313291
}
314292

315293
#endif

0 commit comments

Comments
 (0)