X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=blobdiff_plain;f=src%2Flibsigrok-internal.h;h=63780af57b42b65d4ff7a86a88aeb0e14fd7ab14;hp=378c86d41d4283a50d55aa0ab1fbbaaefdb5a57f;hb=e4bcc63de6cb4c9e64649f0f69088f04ef4ae524;hpb=6205515cc8bed6bc17c2facf9612a072facdfd3e diff --git a/src/libsigrok-internal.h b/src/libsigrok-internal.h index 378c86d4..63780af5 100644 --- a/src/libsigrok-internal.h +++ b/src/libsigrok-internal.h @@ -74,6 +74,16 @@ static inline uint8_t read_u8(const uint8_t *p) } #define R8(x) read_u8((const uint8_t *)(x)) +/** + * Read an 8 bits signed integer out of memory. + * @param x a pointer to the input memory + * @return the corresponding signed integer + */ +static inline int8_t read_i8(const uint8_t *p) +{ + return (int8_t)p[0]; +} + /** * Read a 16 bits big endian unsigned integer out of memory. * @param x a pointer to the input memory @@ -357,6 +367,28 @@ static inline float read_fltle(const uint8_t *p) } #define RLFL(x) read_fltle((const uint8_t *)(x)) +/** + * Read a 64 bits big endian float out of memory (double precision). + * @param x a pointer to the input memory + * @return the corresponding floating point value + */ +static inline double read_dblbe(const uint8_t *p) +{ + /* + * Implementor's note: Strictly speaking the "union" trick + * is not portable. But this phrase was found to work on the + * project's supported platforms, and serve well until a more + * appropriate phrase is found. + */ + union { uint64_t u64; double flt; } u; + double f; + + u.u64 = read_u64be(p); + f = u.flt; + + return f; +} + /** * Read a 64 bits little endian float out of memory (double precision). * @param x a pointer to the input memory @@ -519,6 +551,23 @@ static inline uint8_t read_u8_inc(const uint8_t **p) return v; } +/** + * Read signed 8bit integer from raw memory, increment read position. + * @param[in, out] p Pointer into byte stream. + * @return Retrieved integer value, signed. + */ +static inline int8_t read_i8_inc(const uint8_t **p) +{ + int8_t v; + + if (!p || !*p) + return 0; + v = read_i8(*p); + *p += sizeof(v); + + return v; +} + /** * Read unsigned 16bit integer from raw memory (big endian format), increment read position. * @param[in, out] p Pointer into byte stream. @@ -554,17 +603,34 @@ static inline uint16_t read_u16le_inc(const uint8_t **p) } /** - * Read unsigned 32bit integer from raw memory (big endian format), increment read position. + * Read signed 16bit integer from raw memory (big endian format), increment read position. * @param[in, out] p Pointer into byte stream. - * @return Retrieved integer value, unsigned. + * @return Retrieved integer value, signed. */ -static inline uint32_t read_u32be_inc(const uint8_t **p) +static inline int16_t read_i16be_inc(const uint8_t **p) { - uint32_t v; + int16_t v; if (!p || !*p) return 0; - v = read_u32be(*p); + v = read_i16be(*p); + *p += sizeof(v); + + return v; +} + +/** + * Read signed 16bit integer from raw memory (little endian format), increment read position. + * @param[in, out] p Pointer into byte stream. + * @return Retrieved integer value, signed. + */ +static inline int16_t read_i16le_inc(const uint8_t **p) +{ + int16_t v; + + if (!p || !*p) + return 0; + v = read_i16le(*p); *p += sizeof(v); return v; @@ -587,6 +653,23 @@ static inline uint32_t read_u24le_inc(const uint8_t **p) return v; } +/** + * Read unsigned 32bit integer from raw memory (big endian format), increment read position. + * @param[in, out] p Pointer into byte stream. + * @return Retrieved integer value, unsigned. + */ +static inline uint32_t read_u32be_inc(const uint8_t **p) +{ + uint32_t v; + + if (!p || !*p) + return 0; + v = read_u32be(*p); + *p += sizeof(v); + + return v; +} + /** * Read unsigned 32bit integer from raw memory (little endian format), increment read position. * @param[in, out] p Pointer into byte stream. @@ -604,6 +687,40 @@ static inline uint32_t read_u32le_inc(const uint8_t **p) return v; } +/** + * Read signed 32bit integer from raw memory (big endian format), increment read position. + * @param[in, out] p Pointer into byte stream. + * @return Retrieved integer value, signed. + */ +static inline int32_t read_i32be_inc(const uint8_t **p) +{ + int32_t v; + + if (!p || !*p) + return 0; + v = read_i32be(*p); + *p += sizeof(v); + + return v; +} + +/** + * Read signed 32bit integer from raw memory (little endian format), increment read position. + * @param[in, out] p Pointer into byte stream. + * @return Retrieved integer value, signed. + */ +static inline int32_t read_i32le_inc(const uint8_t **p) +{ + int32_t v; + + if (!p || !*p) + return 0; + v = read_i32le(*p); + *p += sizeof(v); + + return v; +} + /** * Read unsigned 64bit integer from raw memory (big endian format), increment read position. * @param[in, out] p Pointer into byte stream. @@ -638,6 +755,23 @@ static inline uint64_t read_u64le_inc(const uint8_t **p) return v; } +/** + * Read 32bit float from raw memory (big endian format), increment read position. + * @param[in, out] p Pointer into byte stream. + * @return Retrieved float value. + */ +static inline float read_fltbe_inc(const uint8_t **p) +{ + float v; + + if (!p || !*p) + return 0; + v = read_fltbe(*p); + *p += sizeof(v); + + return v; +} + /** * Read 32bit float from raw memory (little endian format), increment read position. * @param[in, out] p Pointer into byte stream. @@ -655,6 +789,23 @@ static inline float read_fltle_inc(const uint8_t **p) return v; } +/** + * Read 64bit float from raw memory (big endian format), increment read position. + * @param[in, out] p Pointer into byte stream. + * @return Retrieved float value. + */ +static inline double read_dblbe_inc(const uint8_t **p) +{ + double v; + + if (!p || !*p) + return 0; + v = read_dblbe(*p); + *p += sizeof(v); + + return v; +} + /** * Read 64bit float from raw memory (little endian format), increment read position. * @param[in, out] p Pointer into byte stream.