8000 Improved float serialization when `-fsingle-precision-constant` is used · java64/ArduinoJson@7657522 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7657522

Browse files
committed
Improved float serialization when -fsingle-precision-constant is used
1 parent 037f90a commit 7657522

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ HEAD
55
----
66

77
* Disabled lazy number deserialization (issue #772)
8+
* Improved float serialization when `-fsingle-precision-constant` is used
89

910
> ### BREAKING CHANGES
1011
>

src/ArduinoJson/Numbers/FloatTraits.hpp

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,46 @@ struct FloatTraits<T, 8 /*64bits*/> {
4444

4545
static T positiveBinaryPowerOfTen(int index) {
4646
static T factors[] = {
47-
1e1, 1e2, 1e4, 1e8, 1e16, 1e32,
48-
// workaround to support platforms with single precision literals
49-
forge(0x4D384F03, 0xE93FF9F5), forge(0x5A827748, 0xF9301D32),
50-
forge(0x75154FDD, 0x7F73BF3C)};
47+
1e1,
48+
1e2,
49+
1e4,
50+
1e8,
51+
1e16,
52+
forge(0x4693B8B5, 0xB5056E17), // 1e32
53+
forge(0x4D384F03, 0xE93FF9F5), // 1e64
54+
forge(0x5A827748, 0xF9301D32), // 1e128
55+
forge(0x75154FDD, 0x7F73BF3C) // 1e256
56+
};
5157
return factors[index];
5258
}
5359

5460
static T negativeBinaryPowerOfTen(int index) {
5561
static T factors[] = {
56-
1e-1, 1e-2, 1e-4, 1e-8, 1e-16, 1e-32,
57-
// workaround to support platforms with single precision literals
58-
forge(0x32A50FFD, 0x44F4A73D), forge(0x255BBA08, 0xCF8C979D),
59-
forge(0x0AC80628, 0x64AC6F43)};
62+
forge(0x3FB99999, 0x9999999A), // 1e-1
63+
forge(0x3F847AE1, 0x47AE147B), // 1e-2
64+
forge(0x3F1A36E2, 0xEB1C432D), // 1e-4
65+
forge(0x3E45798E, 0xE2308C3A), // 1e-8
66+
forge(0x3C9CD2B2, 0x97D889BC), // 1e-16
67+
forge(0x3949F623, 0xD5A8A733), // 1e-32
68+
forge(0x32A50FFD, 0x44F4A73D), // 1e-64
69+
forge(0x255BBA08, 0xCF8C979D), // 1e-128
70+
forge(0x0AC80628, 0x64AC6F43) // 1e-256
71+
};
6072
return factors[index];
6173
}
6274

6375
static T negativeBinaryPowerOfTenPlusOne(int index) {
6476
static T factors[] = {
65-
1e0, 1e-1, 1e-3, 1e-7, 1e-15, 1e-31,
66-
// workaround to support platforms with single precision literals
67-
forge(0x32DA53FC, 0x9631D10D), forge(0x25915445, 0x81B7DEC2),
68-
forge(0x0AFE07B2, 0x7DD78B14)};
77+
1e0,
78+
forge(0x3FB99999, 0x9999999A), // 1e-1
79+
forge(0x3F50624D, 0xD2F1A9FC), // 1e-3
80+
forge(0x3E7AD7F2, 0x9ABCAF48), // 1e-7
81+
forge(0x3CD203AF, 0x9EE75616), // 1e-15
82+
forge(0x398039D6, 0x65896880), // 1e-31
83+
forge(0x32DA53FC, 0x9631D10D), // 1e-63
84+
forge(0x25915445, 0x81B7DEC2), // 1e-127
85+
forge(0x0AFE07B2, 0x7DD78B14) // 1e-255
86+
};
6987
return factors[index];
7088
}
7189

@@ -77,6 +95,9 @@ struct FloatTraits<T, 8 /*64bits*/> {
7795
return forge(0x7ff00000, 0x00000000);
7896
}
7997

98+
// constructs a double floating point values from its binary representation
99+
// we use this function to workaround platforms with single precision literals
100+
// (for example, when -fsingle-precision-constant is passed to GCC)
80101
static T forge(uint32_t msb, uint32_t lsb) {
81102
union {
82103
uint64_t integerBits;

0 commit comments

Comments
 (0)
0