8000 Add `pgm_ptr<T>` · smartcoder00/ArduinoJson@4e7099d · GitHub
[go: up one dir, main page]

Skip to content

Commit 4e7099d

Browse files
committed
Add pgm_ptr<T>
1 parent 26948cb commit 4e7099d

File tree

4 files changed

+62
-25
lines changed

4 files changed

+62
-25
lines changed

src/ArduinoJson/Numbers/FloatParts.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ struct FloatParts {
6363

6464
if (value >= ARDUINOJSON_POSITIVE_EXPONENTIATION_THRESHOLD) {
6565< 10000 div class="diff-text-inner"> for (; index >= 0; index--) {
66-
if (value >= traits::positiveBinaryPowerOfTen(index)) {
67-
value *= traits::negativeBinaryPowerOfTen(index);
66+
if (value >= traits::positiveBinaryPowersOfTen()[index]) {
67+
value *= traits::negativeBinaryPowersOfTen()[index];
6868
powersOf10 = int16_t(powersOf10 + bit);
6969
}
7070
bit >>= 1;
@@ -73,8 +73,8 @@ struct FloatParts {
7373

7474
if (value > 0 && value <= ARDUINOJSON_NEGATIVE_EXPONENTIATION_THRESHOLD) {
7575
for (; index >= 0; index--) {
76-
if (value < traits::negativeBinaryPowerOfTenPlusOne(index)) {
77-
value *= traits::positiveBinaryPowerOfTen(index);
76+
if (value < traits::negativeBinaryPowersOfTenPlusOne()[index]) {
77+
value *= traits::positiveBinaryPowersOfTen()[index];
7878
powersOf10 = int16_t(powersOf10 - bit);
7979
}
8080
bit >>= 1;

src/ArduinoJson/Numbers/FloatTraits.hpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@ struct FloatTraits<T, 8 /*64bits*/> {
3434
if (e > 0) {
3535
for (uint8_t index = 0; e != 0; index++) {
3636
if (e & 1)
37-
m *= positiveBinaryPowerOfTen(index);
37+
m *= positiveBinaryPowersOfTen()[index];
3838
e >>= 1;
3939
}
4040
} else {
4141
e = TExponent(-e);
4242
for (uint8_t index = 0; e != 0; index++) {
4343
if (e & 1)
44-
m *= negativeBinaryPowerOfTen(index);
44+
m *= negativeBinaryPowersOfTen()[index];
4545
e >>= 1;
4646
}
4747
}
4848
return m;
4949
}
5050

51-
static T positiveBinaryPowerOfTen(int index) {
51+
static pgm_ptr<T> positiveBinaryPowersOfTen() {
5252
ARDUINOJSON_DEFINE_PROGMEM_ARRAY( //
5353
uint64_t, factors,
5454
{
@@ -62,10 +62,10 @@ struct FloatTraits<T, 8 /*64bits*/> {
6262
0x5A827748F9301D32, // 1e128
6363
0x75154FDD7F73BF3C, // 1e256
6464
});
65-
return forge(pgm_read(factors + index));
65+
return pgm_ptr<T>(reinterpret_cast<const T*>(factors));
6666
}
6767

68-
static T negativeBinaryPowerOfTen(int index) {
68+
static pgm_ptr<T> negativeBinaryPowersOfTen() {
6969
ARDUINOJSON_DEFINE_PROGMEM_ARRAY( //
7070
uint64_t, factors,
7171
{
@@ -79,10 +79,10 @@ struct FloatTraits<T, 8 /*64bits*/> {
7979
0x255BBA08CF8C979D, // 1e-128
8080
0x0AC8062864AC6F43 // 1e-256
8181
});
82-
return forge(pgm_read(factors + index));
82+
return pgm_ptr<T>(reinterpret_cast<const T*>(factors));
8383
}
8484

85-
static T negativeBinaryPowerOfTenPlusOne(int index) {
85+
static pgm_ptr<T> negativeBinaryPowersOfTenPlusOne() {
8686
ARDUINOJSON_DEFINE_PROGMEM_ARRAY( //
8787
uint64_t, factors,
8888
{
@@ -96,7 +96,7 @@ struct FloatTraits<T, 8 /*64bits*/> {
9696
0x2591544581B7DEC2, // 1e-127
9797
0x0AFE07B27DD78B14 // 1e-255
9898
});
99-
return forge(pgm_read(factors + index));
99+
return pgm_ptr<T>(reinterpret_cast<const T*>(factors));
100100
}
101101

102102
static T nan() {
@@ -154,21 +154,21 @@ struct FloatTraits<T, 4 /*32bits*/> {
154154
if (e > 0) {
155155
for (uint8_t index = 0; e != 0; index++) {
156156
if (e & 1)
157-
m *= positiveBinaryPowerOfTen(index);
157+
m *= positiveBinaryPowersOfTen()[index];
158158
e >>= 1;
159159
}
160160
} else {
161161
e = -e;
162162
for (uint8_t index = 0; e != 0; index++) {
163163
if (e & 1)
164-
m *= negativeBinaryPowerOfTen(index);
164+
m *= negativeBinaryPowersOfTen()[index];
165165
e >>= 1;
166166
}
167167
}
168168
return m;
169169
}
170170

171-
static T positiveBinaryPowerOfTen(int index) {
171+
static pgm_ptr<T> positiveBinaryPowersOfTen() {
172172
ARDUINOJSON_DEFINE_PROGMEM_ARRAY(uint32_t, factors,
173173
{
174174
0x41200000, // 1e1f
@@ -178,10 +178,10 @@ struct FloatTraits<T, 4 /*32bits*/> {
178178
0x5a0e1bca, // 1e16f
179179
0x749dc5ae // 1e32f
180180
});
181-
return forge(pgm_read(factors + index));
181+
return pgm_ptr<T>(reinterpret_cast<const T*>(factors));
182182
}
183183

184-
static T negativeBinaryPowerOfTen(int index) {
184+
static pgm_ptr<T> negativeBinaryPowersOfTen() {
185185
ARDUINOJSON_DEFINE_PROGMEM_ARRAY(uint32_t, factors,
186186
{
187187
0x3dcccccd, // 1e-1f
@@ -191,10 +191,10 @@ struct FloatTraits<T, 4 /*32bits*/> {
191191
0x24e69595, // 1e-16f
192192
0x0a4fb11f // 1e-32f
193193
});
194-
return forge(pgm_read(factors + index));
194+
return pgm_ptr<T>(reinterpret_cast<const T*>(factors));
195195
}
196196

197-
static T negativeBinaryPowerOfTenPlusOne(int index) {
197+
static pgm_ptr<T> negativeBinaryPowersOfTenPlusOne() {
198198
ARDUINOJSON_DEFINE_PROGMEM_ARRAY(uint32_t, factors,
199199
{
200200
0x3f800000, // 1e0f
@@ -204,7 +204,7 @@ struct FloatTraits<T, 4 /*32bits*/> {
204204
0x26901d7d, // 1e-15f
205205
0x0c01ceb3 // 1e-31f
206206
});
207-
return forge(pgm_read(factors + index));
207+
return pgm_ptr<T>(reinterpret_cast<const T*>(factors));
208208
}
209209

210210
static T forge(uint32_t bits) {

src/ArduinoJson/Polyfills/pgmspace.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,29 @@ inline uint32_t pgm_read_dword(ArduinoJson::detail::pgm_p p) {
106106
}
107107
#endif
108108

109+
#ifndef pgm_read_float
110+
inline float pgm_read_float(ArduinoJson::detail::pgm_p p) {
111+
float result;
112+
memcpy_P(&result, p.address, sizeof(float));
113+
return result;
114+
}
115+
#endif
116+
117+
#ifndef pgm_read_double
118+
# if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_FLOAT__) && \
119+
__SIZEOF_DOUBLE__ == __SIZEOF_FLOAT__
120+
inline double pgm_read_double(ArduinoJson::detail::pgm_p p) {
121+
return pgm_read_float(p.address);
122+
}
123+
# else
124+
inline double pgm_read_double(ArduinoJson::detail::pgm_p p) {
125+
double result;
126+
memcpy_P(&result, p.address, sizeof(double));
127+
return result;
128+
}
129+
# endif
130+
#endif
131+
109132
#ifndef pgm_read_ptr
110133
inline void* pgm_read_ptr(ArduinoJson::detail::pgm_p p) {
111134
void* result;

src/ArduinoJson/Polyfills/pgmspace_generic.hpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ inline uint32_t pgm_read(const uint32_t* p) {
2929
return pgm_read_dword(p);
3030
}
3131

32-
template <typename T>
33-
inline T pgm_read(const T* p) {
34-
T result;
35-
memcpy_P(&result, p, sizeof(T));
36-
return result;
32+
inline double pgm_read(const double* p) {
33+
return pgm_read_double(p);
34+
}
35+
36+
inline float pgm_read(const float* p) {
37+
return pgm_read_float(p);
3738
}
3839

3940
#else
@@ -50,4 +51,17 @@ inline T pgm_read(const T* p) {
5051

5152
#endif
5253

54+
template <typename T>
55+
class pgm_ptr {
56+
public:
57+
explicit pgm_ptr(const T* ptr) : _ptr(ptr) {}
58+
59+
T operator[](intptr_t index) const {
60+
return pgm_read(_ptr + index);
61+
}
62+
63+
private:
64+
const T* _ptr;
65+
};
66+
5367
ARDUINOJSON_END_PRIVATE_NAMESPACE

0 commit comments

Comments
 (0)
0