diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2017-11-08 19:45:18 +0200 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2017-11-08 19:47:37 +0200 |
commit | 1b146e9de97708b70c3ad96022886e2ce44d246d (patch) | |
tree | 1f3c8505318a0686c122a2d5611e5ae00833c0f9 | |
parent | 5b1b80a8dbceb9be67dfd894cdf2a49a7426fd68 (diff) |
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.
-rw-r--r-- | ports/esp8266/posix_helpers.c | 5 | ||||
-rw-r--r-- | py/mpconfig.h | 20 |
2 files changed, 22 insertions, 3 deletions
diff --git a/ports/esp8266/posix_helpers.c b/ports/esp8266/posix_helpers.c index 1fc677c5c..b72c4ff9d 100644 --- a/ports/esp8266/posix_helpers.c +++ b/ports/esp8266/posix_helpers.c @@ -55,14 +55,13 @@ void *realloc(void *ptr, size_t size) { return p; } -#define PLATFORM_HTONL(_n) ((uint32_t)( (((_n) & 0xff) << 24) | (((_n) & 0xff00) << 8) | (((_n) >> 8) & 0xff00) | (((_n) >> 24) & 0xff) )) #undef htonl #undef ntohl uint32_t ntohl(uint32_t netlong) { - return PLATFORM_HTONL(netlong); + return MP_BE32TOH(netlong); } uint32_t htonl(uint32_t netlong) { - return PLATFORM_HTONL(netlong); + return MP_HTOBE32(netlong); } time_t time(time_t *t) { diff --git a/py/mpconfig.h b/py/mpconfig.h index 6a32ea2a6..fa1094bfc 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -1293,4 +1293,24 @@ typedef double mp_float_t; #define MP_UNLIKELY(x) __builtin_expect((x), 0) #endif +#ifndef MP_HTOBE16 +#if MP_ENDIANNESS_LITTLE +# define MP_HTOBE16(x) ((uint16_t)( (((x) & 0xff) << 8) | (((x) >> 8) & 0xff) )) +# define MP_BE16TOH(x) MP_HTOBE16(x) +#else +# define MP_HTOBE16(x) (x) +# define MP_BE16TOH(x) (x) +#endif +#endif + +#ifndef MP_HTOBE32 +#if MP_ENDIANNESS_LITTLE +# define MP_HTOBE32(x) ((uint32_t)( (((x) & 0xff) << 24) | (((x) & 0xff00) << 8) | (((x) >> 8) & 0xff00) | (((x) >> 24) & 0xff) )) +# define MP_BE32TOH(x) MP_HTOBE32(x) +#else +# define MP_HTOBE32(x) (x) +# define MP_BE32TOH(x) (x) +#endif +#endif + #endif // MICROPY_INCLUDED_PY_MPCONFIG_H |