Skip to content

Commit 886254c

Browse files
committed
Reduce size of make_float()
1 parent 4e7099d commit 886254c

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

src/ArduinoJson/Numbers/FloatTraits.hpp

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,15 @@ struct FloatTraits<T, 8 /*64bits*/> {
3131

3232
template <typename TExponent>
3333
static T make_float(T m, TExponent e) {
34-
if (e > 0) {
35-
for (uint8_t index = 0; e != 0; index++) {
36-
if (e & 1)
37-
m *= positiveBinaryPowersOfTen()[index];
38-
e >>= 1;
39-
}
40-
} else {
34+
auto powersOfTen =
35+
e > 0 ? positiveBinaryPowersOfTen() : negativeBinaryPowersOfTen();
36+
if (e <= 0)
4137
e = TExponent(-e);
42-
for (uint8_t index = 0; e != 0; index++) {
43-
if (e & 1)
44-
m *= negativeBinaryPowersOfTen()[index];
45-
e >>= 1;
46-
}
38+
39+
for (uint8_t index = 0; e != 0; index++) {
40+
if (e & 1)
41+
m *= powersOfTen[index];
42+
e >>= 1;
4743
}
4844
return m;
4945
}
@@ -151,19 +147,15 @@ struct FloatTraits<T, 4 /*32bits*/> {
151147

152148
template <typename TExponent>
153149
static T make_float(T m, TExponent e) {
154-
if (e > 0) {
155-
for (uint8_t index = 0; e != 0; index++) {
156-
if (e & 1)
157-
m *= positiveBinaryPowersOfTen()[index];
158-
e >>= 1;
159-
}
160-
} else {
161-
e = -e;
162-
for (uint8_t index = 0; e != 0; index++) {
163-
if (e & 1)
164-
m *= negativeBinaryPowersOfTen()[index];
165-
e >>= 1;
166-
}
150+
auto powersOfTen =
151+
e > 0 ? positiveBinaryPowersOfTen() : negativeBinaryPowersOfTen();
152+
if (e <= 0)
153+
e = TExponent(-e);
154+
155+
for (uint8_t index = 0; e != 0; index++) {
156+
if (e & 1)
157+
m *= powersOfTen[index];
158+
e >>= 1;
167159
}
168160
return m;
169161
}

0 commit comments

Comments
 (0)