From 797c8d9034213296dacf1b3fe2044138f623f4d6 Mon Sep 17 00:00:00 2001 From: Gerhard Sittig Date: Sat, 27 Feb 2021 14:19:18 +0100 Subject: [PATCH] libsigrok-internal.h: add 48bit endianess aware byte stream helpers --- src/libsigrok-internal.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/libsigrok-internal.h b/src/libsigrok-internal.h index 9111bc37..1c37ee27 100644 --- a/src/libsigrok-internal.h +++ b/src/libsigrok-internal.h @@ -475,6 +475,22 @@ static inline void write_u32le(uint8_t *p, uint32_t x) } #define WL32(p, x) write_u32le((uint8_t *)(p), (uint32_t)(x)) +/** + * Write a 48 bits unsigned integer to memory stored as little endian. + * @param p a pointer to the output memory + * @param x the input unsigned integer + */ +static inline void write_u48le(uint8_t *p, uint64_t x) +{ + p[0] = x & 0xff; x >>= 8; + p[1] = x & 0xff; x >>= 8; + p[2] = x & 0xff; x >>= 8; + p[3] = x & 0xff; x >>= 8; + p[4] = x & 0xff; x >>= 8; + p[5] = x & 0xff; x >>= 8; +} +#define WL48(p, x) write_u48le((uint8_t *)(p), (uint64_t)(x)) + /** * Write a 64 bits unsigned integer to memory stored as big endian. * @param p a pointer to the output memory @@ -905,6 +921,19 @@ static inline void write_u32le_inc(uint8_t **p, uint32_t x) *p += sizeof(x); } +/** + * Write unsigned 48bit little endian integer to raw memory, increment write position. + * @param[in, out] p Pointer into byte stream. + * @param[in] x Value to write. + */ +static inline void write_u48le_inc(uint8_t **p, uint64_t x) +{ + if (!p || !*p) + return; + write_u48le(*p, x); + *p += 48 / 8 * sizeof(uint8_t); +} + /** * Write unsigned 64bit little endian integer to raw memory, increment write position. * @param[in, out] p Pointer into byte stream. -- 2.30.2