-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
normalize.hpp
46 lines (39 loc) 路 1.14 KB
/
normalize.hpp
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
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
// https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star!
#pragma once
#include "../Configuration.hpp"
#include "../TypeTraits/FloatTraits.hpp"
namespace ArduinoJson {
namespace Polyfills {
template <typename T>
int16_t normalize(T& value) {
using namespace TypeTraits;
int16_t powersOf10 = 0;
int8_t index = sizeof(T) == 8 ? 8 : 5;
int bit = 1 << index;
if (value >= ARDUINOJSON_POSITIVE_EXPONENTIATION_THRESHOLD) {
for (; index >= 0; index--) {
if (value >= FloatTraits<T>::positiveBinaryPowerOfTen(index)) {
value *= FloatTraits<T>::negativeBinaryPowerOfTen(index);
powersOf10 = int16_t(powersOf10 + bit);
}
bit >>= 1;
}
}
if (value > 0 && value <= ARDUINOJSON_NEGATIVE_EXPONENTIATION_THRESHOLD) {
for (; index >= 0; index--) {
if (value / 10 < FloatTraits<T>::negativeBinaryPowerOfTen(index)) {
value *= FloatTraits<T>::positiveBinaryPowerOfTen(index);
powersOf10 = int16_t(powersOf10 - bit);
}
bit >>= 1;
}
}
return powersOf10;
}
}
}