libsigrok-internal.h: add 48bit endianess aware byte stream helpers

This commit is contained in:
Gerhard Sittig 2021-02-27 14:19:18 +01:00
parent 40a0b2f466
commit 797c8d9034
1 changed files with 29 additions and 0 deletions

View File

@ -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)) #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. * Write a 64 bits unsigned integer to memory stored as big endian.
* @param p a pointer to the output memory * @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); *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. * Write unsigned 64bit little endian integer to raw memory, increment write position.
* @param[in, out] p Pointer into byte stream. * @param[in, out] p Pointer into byte stream.