Skip to content

Commit 1b146e9

Browse files
committed
py/mpconfig: Introduce reusable MP_HTOBE32(), etc. macros.
Macros to convert big-endian values to host byte order and vice-versa. These were defined in adhoc way for some ports (e.g. esp8266), allow reuse, provide default implementations, while allow ports to override.
1 parent 5b1b80a commit 1b146e9

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

Diff for: ports/esp8266/posix_helpers.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,13 @@ void *realloc(void *ptr, size_t size) {
5555
return p;
5656
}
5757

58-
#define PLATFORM_HTONL(_n) ((uint32_t)( (((_n) & 0xff) << 24) | (((_n) & 0xff00) << 8) | (((_n) >> 8) & 0xff00) | (((_n) >> 24) & 0xff) ))
5958
#undef htonl
6059
#undef ntohl
6160
uint32_t ntohl(uint32_t netlong) {
62-
return PLATFORM_HTONL(netlong);
61+
return MP_BE32TOH(netlong);
6362
}
6463
uint32_t htonl(uint32_t netlong) {
65-
return PLATFORM_HTONL(netlong);
64+
return MP_HTOBE32(netlong);
6665
}
6766

6867
time_t time(time_t *t) {

Diff for: py/mpconfig.h

+20
Original file line numberDiff line numberDiff line change
@@ -1293,4 +1293,24 @@ typedef double mp_float_t;
12931293
#define MP_UNLIKELY(x) __builtin_expect((x), 0)
12941294
#endif
12951295

1296+
#ifndef MP_HTOBE16
1297+
#if MP_ENDIANNESS_LITTLE
1298+
# define MP_HTOBE16(x) ((uint16_t)( (((x) & 0xff) << 8) | (((x) >> 8) & 0xff) ))
1299+
# define MP_BE16TOH(x) MP_HTOBE16(x)
1300+
#else
1301+
# define MP_HTOBE16(x) (x)
1302+
# define MP_BE16TOH(x) (x)
1303+
#endif
1304+
#endif
1305+
1306+
#ifndef MP_HTOBE32
1307+
#if MP_ENDIANNESS_LITTLE
1308+
# define MP_HTOBE32(x) ((uint32_t)( (((x) & 0xff) << 24) | (((x) & 0xff00) << 8) | (((x) >> 8) & 0xff00) | (((x) >> 24) & 0xff) ))
1309+
# define MP_BE32TOH(x) MP_HTOBE32(x)
1310+
#else
1311+
# define MP_HTOBE32(x) (x)
1312+
# define MP_BE32TOH(x) (x)
1313+
#endif
1314+
#endif
1315+
12961316
#endif // MICROPY_INCLUDED_PY_MPCONFIG_H

0 commit comments

Comments
 (0)