(no title)
el_pollo_diablo | 1 year ago
static inline uint32_t load_le32(void const *p)
{
unsigned char const *p_ = p;
_Static_assert(sizeof(uint32_t) == 4, "uint32_t is not 4 bytes long");
return
(uint32_t)p_[0] << 0 |
(uint32_t)p_[1] << 8 |
(uint32_t)p_[2] << 16 |
(uint32_t)p_[3] << 24;
}
static inline void store_le32(void *p, uint32_t v)
{
unsigned char *p_ = p;
_Static_assert(sizeof(uint32_t) == 4, "uint32_t is not 4 bytes long");
p_[0] = (unsigned char)(v >> 0);
p_[1] = (unsigned char)(v >> 8);
p_[2] = (unsigned char)(v >> 16);
p_[3] = (unsigned char)(v >> 24);
}
All that nonsense about masking with an int must stop. You want a result in uint32_t? Convert to that type before shifting. Done.Now, the C standard could still get in the way and assign a lower conversion rank to uint32_t than to int, so uint32_t operands would get promoted to int (or unsigned int) before shifting, but those left shifts would still be defined as the result would be representable in the promoted type (as promotion only makes sense if it preserves values, which implies that all values of the type before promotion are representable in the type after promotion).
No comments yet.