core/io/bytes_le.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file bytes_le.h | ||
| 3 | * @brief Little-endian byte readers shared by the wire-decoding stores. | ||
| 4 | * | ||
| 5 | * Each byte is read on its own and shifted into place rather than casting the pointer to a wider | ||
| 6 | * type, so the value comes out the same whatever the compiler believes about alignment or byte | ||
| 7 | * order. The casts are what carry the sign: the phone packs a temperature and a percent as signed | ||
| 8 | * values, and reading them unsigned turns a small negative into a large positive. | ||
| 9 | * | ||
| 10 | * @ingroup lib_core | ||
| 11 | */ | ||
| 12 | #pragma once | ||
| 13 | #include <stdint.h> | ||
| 14 | |||
| 15 | /** | ||
| 16 | * @addtogroup lib_core | ||
| 17 | * @{ | ||
| 18 | */ | ||
| 19 | |||
| 20 | /** | ||
| 21 | * @brief Reads a little-endian signed 16-bit value out of a byte pair. | ||
| 22 | * | ||
| 23 | * @param p The first of the two bytes. | ||
| 24 | * @return The value the pair encodes. | ||
| 25 | */ | ||
| 26 | 59 | static inline int16_t read_i16_le(const uint8_t *p) | |
| 27 | { | ||
| 28 | 59 | return (int16_t)(p[0] | (p[1] << 8)); | |
| 29 | } | ||
| 30 | |||
| 31 | /** | ||
| 32 | * @brief Reads a little-endian signed 32-bit value out of four bytes. | ||
| 33 | * | ||
| 34 | * @param p The first of the four bytes. | ||
| 35 | * @return The value the four bytes encode. | ||
| 36 | */ | ||
| 37 | 45 | static inline int32_t read_i32_le(const uint8_t *p) | |
| 38 | { | ||
| 39 | 45 | return (int32_t)((uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24)); | |
| 40 | } | ||
| 41 | |||
| 42 | /** @} */ | ||
| 43 |