22 #ifndef LELY_UTIL_ENDIAN_H_
23 #define LELY_UTIL_ENDIAN_H_
30 #ifndef LELY_UTIL_ENDIAN_INLINE
31 #define LELY_UTIL_ENDIAN_INLINE static inline
34 #ifndef LELY_BIG_ENDIAN
35 #if defined(__BIG_ENDIAN__) || defined(__big_endian__) \
36 || (__GNUC__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) \
37 || defined(__ARMEB__) || defined(__AARCH64EB__) \
38 || defined(__THUMBEB__)
40 #define LELY_BIG_ENDIAN 1
45 #undef LELY_LITTLE_ENDIAN
48 #ifndef LELY_LITTLE_ENDIAN
49 #if defined(__LITTLE_ENDIAN__) || defined(__little_endian__) \
50 || (__GNUC__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) \
51 || defined(__i386__) || defined(_M_IX86) \
52 || defined(__x86_64__) || defined(_M_AMD64) \
53 || defined(__ARMEL__) || defined(__AARCH64EL__) \
54 || defined(__THUMBEL__)
56 #define LELY_LITTLE_ENDIAN 1
60 #if LELY_LITTLE_ENDIAN
61 #undef LELY_BIG_ENDIAN
64 #if !LELY_BIG_ENDIAN && !LELY_LITTLE_ENDIAN
65 #error Unable to determine byte order or byte order is not supported.
74 LELY_UTIL_ENDIAN_INLINE uint_least16_t
htobe16(uint_least16_t x);
79 LELY_UTIL_ENDIAN_INLINE uint_least16_t
betoh16(uint_least16_t x);
84 LELY_UTIL_ENDIAN_INLINE uint_least16_t
htole16(uint_least16_t x);
89 LELY_UTIL_ENDIAN_INLINE uint_least16_t
letoh16(uint_least16_t x);
94 LELY_UTIL_ENDIAN_INLINE uint_least32_t
htobe32(uint_least32_t x);
99 LELY_UTIL_ENDIAN_INLINE uint_least32_t
betoh32(uint_least32_t x);
104 LELY_UTIL_ENDIAN_INLINE uint_least32_t
htole32(uint_least32_t x);
109 LELY_UTIL_ENDIAN_INLINE uint_least32_t
letoh32(uint_least32_t x);
114 LELY_UTIL_ENDIAN_INLINE uint_least64_t
htobe64(uint_least64_t x);
119 LELY_UTIL_ENDIAN_INLINE uint_least64_t
betoh64(uint_least64_t x);
124 LELY_UTIL_ENDIAN_INLINE uint_least64_t
htole64(uint_least64_t x);
129 LELY_UTIL_ENDIAN_INLINE uint_least64_t
letoh64(uint_least64_t x);
133 LELY_UTIL_ENDIAN_INLINE
void stbe_i16(uint_least8_t dst[2], int_least16_t x);
136 LELY_UTIL_ENDIAN_INLINE int_least16_t
ldbe_i16(
const uint_least8_t src[2]);
139 LELY_UTIL_ENDIAN_INLINE
void stbe_u16(uint_least8_t dst[2], uint_least16_t x);
142 LELY_UTIL_ENDIAN_INLINE uint_least16_t
ldbe_u16(
const uint_least8_t src[2]);
145 LELY_UTIL_ENDIAN_INLINE
void stle_i16(uint_least8_t dst[2], int_least16_t x);
148 LELY_UTIL_ENDIAN_INLINE int_least16_t
ldle_i16(
const uint_least8_t src[2]);
151 LELY_UTIL_ENDIAN_INLINE
void stle_u16(uint_least8_t dst[2], uint_least16_t x);
154 LELY_UTIL_ENDIAN_INLINE uint_least16_t
ldle_u16(
const uint_least8_t src[2]);
157 LELY_UTIL_ENDIAN_INLINE
void stbe_i32(uint_least8_t dst[4], int_least32_t x);
160 LELY_UTIL_ENDIAN_INLINE int_least32_t
ldbe_i32(
const uint_least8_t src[4]);
163 LELY_UTIL_ENDIAN_INLINE
void stbe_u32(uint_least8_t dst[4], uint_least32_t x);
166 LELY_UTIL_ENDIAN_INLINE uint_least32_t
ldbe_u32(
const uint_least8_t src[4]);
169 LELY_UTIL_ENDIAN_INLINE
void stle_i32(uint_least8_t dst[4], int_least32_t x);
172 LELY_UTIL_ENDIAN_INLINE int_least32_t
ldle_i32(
const uint_least8_t src[4]);
175 LELY_UTIL_ENDIAN_INLINE
void stle_u32(uint_least8_t dst[4], uint_least32_t x);
178 LELY_UTIL_ENDIAN_INLINE uint_least32_t
ldle_u32(
const uint_least8_t src[4]);
181 LELY_UTIL_ENDIAN_INLINE
void stbe_i64(uint_least8_t dst[8], int_least64_t x);
184 LELY_UTIL_ENDIAN_INLINE int_least64_t
ldbe_i64(
const uint_least8_t src[8]);
187 LELY_UTIL_ENDIAN_INLINE
void stbe_u64(uint_least8_t dst[8], uint_least64_t x);
190 LELY_UTIL_ENDIAN_INLINE uint_least64_t
ldbe_u64(
const uint_least8_t src[8]);
193 LELY_UTIL_ENDIAN_INLINE
void stle_i64(uint_least8_t dst[8], int_least64_t x);
196 LELY_UTIL_ENDIAN_INLINE int_least64_t
ldle_i64(
const uint_least8_t src[8]);
199 LELY_UTIL_ENDIAN_INLINE
void stle_u64(uint_least8_t dst[8], uint_least64_t x);
202 LELY_UTIL_ENDIAN_INLINE uint_least64_t
ldle_u64(
const uint_least8_t src[8]);
204 #ifdef LELY_FLT16_TYPE
210 LELY_UTIL_ENDIAN_INLINE
void stbe_flt16(uint_least8_t dst[2], flt16_t x);
216 LELY_UTIL_ENDIAN_INLINE flt16_t ldbe_flt16(
const uint_least8_t src[2]);
222 LELY_UTIL_ENDIAN_INLINE
void stle_flt16(uint_least8_t dst[2], flt16_t x);
228 LELY_UTIL_ENDIAN_INLINE flt16_t ldle_flt16(
const uint_least8_t src[2]);
232 #ifdef LELY_FLT32_TYPE
238 LELY_UTIL_ENDIAN_INLINE
void stbe_flt32(uint_least8_t dst[4], flt32_t x);
244 LELY_UTIL_ENDIAN_INLINE flt32_t ldbe_flt32(
const uint_least8_t src[4]);
250 LELY_UTIL_ENDIAN_INLINE
void stle_flt32(uint_least8_t dst[4], flt32_t x);
256 LELY_UTIL_ENDIAN_INLINE flt32_t ldle_flt32(
const uint_least8_t src[4]);
260 #ifdef LELY_FLT64_TYPE
266 LELY_UTIL_ENDIAN_INLINE
void stbe_flt64(uint_least8_t dst[8], flt64_t x);
272 LELY_UTIL_ENDIAN_INLINE flt64_t ldbe_flt64(
const uint_least8_t src[8]);
278 LELY_UTIL_ENDIAN_INLINE
void stle_flt64(uint_least8_t dst[8], flt64_t x);
284 LELY_UTIL_ENDIAN_INLINE flt64_t ldle_flt64(
const uint_least8_t src[8]);
300 void bcpybe(uint_least8_t *dst,
int dstbit,
const uint_least8_t *src,
301 int srcbit,
size_t n);
315 void bcpyle(uint_least8_t *dst,
int dstbit,
const uint_least8_t *src,
316 int srcbit,
size_t n);
319 inline uint_least16_t
322 x &= UINT16_C(0xffff);
325 #elif LELY_LITTLE_ENDIAN
332 inline uint_least16_t
340 inline uint_least16_t
343 x &= UINT16_C(0xffff);
346 #elif LELY_LITTLE_ENDIAN
353 inline uint_least16_t
361 inline uint_least32_t
364 x &= UINT32_C(0xffffffff);
367 #elif LELY_LITTLE_ENDIAN
374 inline uint_least32_t
382 inline uint_least32_t
385 x &= UINT32_C(0xffffffff);
388 #elif LELY_LITTLE_ENDIAN
395 inline uint_least32_t
403 inline uint_least64_t
406 x &= UINT64_C(0xffffffffffffffff);
409 #elif LELY_LITTLE_ENDIAN
416 inline uint_least64_t
424 inline uint_least64_t
427 x &= UINT64_C(0xffffffffffffffff);
430 #elif LELY_LITTLE_ENDIAN
437 inline uint_least64_t
461 memcpy(dst, &x,
sizeof(x));
463 dst[0] = (x >> 8) & 0xff;
468 inline uint_least16_t
472 uint_least16_t x = 0;
473 memcpy(&x, src,
sizeof(x));
476 return ((uint_least16_t)(src[0] & 0xff) << 8)
477 | ((uint_least16_t)(src[1] & 0xff));
498 memcpy(dst, &x,
sizeof(x));
501 dst[1] = (x >> 8) & 0xff;
505 inline uint_least16_t
509 uint_least16_t x = 0;
510 memcpy(&x, src,
sizeof(x));
513 return (uint_least16_t)(src[0] & 0xff)
514 | ((uint_least16_t)(src[1] & 0xff) << 8);
535 memcpy(dst, &x,
sizeof(x));
537 dst[0] = (x >> 24) & 0xff;
538 dst[1] = (x >> 16) & 0xff;
539 dst[2] = (x >> 8) & 0xff;
544 inline uint_least32_t
548 uint_least32_t x = 0;
549 memcpy(&x, src,
sizeof(x));
552 return ((uint_least32_t)(src[0] & 0xff) << 24)
553 | ((uint_least32_t)(src[1] & 0xff) << 16)
554 | ((uint_least32_t)(src[2] & 0xff) << 8)
555 | ((uint_least32_t)(src[3] & 0xff));
576 memcpy(dst, &x,
sizeof(x));
579 dst[1] = (x >> 8) & 0xff;
580 dst[2] = (x >> 16) & 0xff;
581 dst[3] = (x >> 24) & 0xff;
585 inline uint_least32_t
589 uint_least32_t x = 0;
590 memcpy(&x, src,
sizeof(x));
593 return (uint_least32_t)(src[0] & 0xff)
594 | ((uint_least32_t)(src[1] & 0xff) << 8)
595 | ((uint_least32_t)(src[2] & 0xff) << 16)
596 | ((uint_least32_t)(src[3] & 0xff) << 24);
617 memcpy(dst, &x,
sizeof(x));
619 dst[0] = (x >> 56) & 0xff;
620 dst[1] = (x >> 48) & 0xff;
621 dst[2] = (x >> 40) & 0xff;
622 dst[3] = (x >> 32) & 0xff;
623 dst[4] = (x >> 24) & 0xff;
624 dst[5] = (x >> 16) & 0xff;
625 dst[6] = (x >> 8) & 0xff;
630 inline uint_least64_t
634 uint_least64_t x = 0;
635 memcpy(&x, src,
sizeof(x));
638 return ((uint_least64_t)(src[0] & 0xff) << 56)
639 | ((uint_least64_t)(src[1] & 0xff) << 48)
640 | ((uint_least64_t)(src[2] & 0xff) << 40)
641 | ((uint_least64_t)(src[3] & 0xff) << 32)
642 | ((uint_least64_t)(src[4] & 0xff) << 24)
643 | ((uint_least64_t)(src[5] & 0xff) << 16)
644 | ((uint_least64_t)(src[6] & 0xff) << 8)
645 | ((uint_least64_t)(src[7] & 0xff));
666 memcpy(dst, &x,
sizeof(x));
669 dst[1] = (x >> 8) & 0xff;
670 dst[2] = (x >> 16) & 0xff;
671 dst[3] = (x >> 24) & 0xff;
672 dst[4] = (x >> 32) & 0xff;
673 dst[5] = (x >> 40) & 0xff;
674 dst[6] = (x >> 48) & 0xff;
675 dst[7] = (x >> 56) & 0xff;
679 inline uint_least64_t
683 uint_least64_t x = 0;
684 memcpy(&x, src,
sizeof(x));
687 return (uint_least64_t)(src[0] & 0xff)
688 | ((uint_least64_t)(src[1] & 0xff) << 8)
689 | ((uint_least64_t)(src[2] & 0xff) << 16)
690 | ((uint_least64_t)(src[3] & 0xff) << 24)
691 | ((uint_least64_t)(src[4] & 0xff) << 32)
692 | ((uint_least64_t)(src[5] & 0xff) << 40)
693 | ((uint_least64_t)(src[6] & 0xff) << 48)
694 | ((uint_least64_t)(src[7] & 0xff) << 56);
698 #ifdef LELY_FLT16_TYPE
701 stbe_flt16(uint_least8_t dst[2], flt16_t x)
703 uint_least16_t tmp = 0;
704 memcpy(&tmp, &x,
sizeof(x));
709 ldbe_flt16(
const uint_least8_t src[2])
713 memcpy(&x, &tmp,
sizeof(x));
718 stle_flt16(uint_least8_t dst[2], flt16_t x)
720 uint_least16_t tmp = 0;
721 memcpy(&tmp, &x,
sizeof(x));
726 ldle_flt16(
const uint_least8_t src[2])
730 memcpy(&x, &tmp,
sizeof(x));
736 #ifdef LELY_FLT32_TYPE
739 stbe_flt32(uint_least8_t dst[4], flt32_t x)
741 uint_least32_t tmp = 0;
742 memcpy(&tmp, &x,
sizeof(x));
747 ldbe_flt32(
const uint_least8_t src[4])
751 memcpy(&x, &tmp,
sizeof(x));
756 stle_flt32(uint_least8_t dst[4], flt32_t x)
758 uint_least32_t tmp = 0;
759 memcpy(&tmp, &x,
sizeof(x));
764 ldle_flt32(
const uint_least8_t src[4])
768 memcpy(&x, &tmp,
sizeof(x));
774 #ifdef LELY_FLT64_TYPE
777 stbe_flt64(uint_least8_t dst[8], flt64_t x)
779 uint_least64_t tmp = 0;
780 memcpy(&tmp, &x,
sizeof(x));
785 ldbe_flt64(
const uint_least8_t src[8])
789 memcpy(&x, &tmp,
sizeof(x));
794 stle_flt64(uint_least8_t dst[8], flt64_t x)
796 uint_least64_t tmp = 0;
797 memcpy(&tmp, &x,
sizeof(x));
802 ldle_flt64(
const uint_least8_t src[8])
806 memcpy(&x, &tmp,
sizeof(x));
This header file is part of the utilities library; it contains the bit function definitions.
uint_least64_t bswap64(uint_least64_t x)
Reverses the byte order of the 64-bit unsigned integer x.
uint_least16_t bswap16(uint_least16_t x)
Reverses the byte order of the 16-bit unsigned integer x.
uint_least32_t bswap32(uint_least32_t x)
Reverses the byte order of the 32-bit unsigned integer x.
uint_least16_t betoh16(uint_least16_t x)
Converts a 16-bit unsigned integer from big-endian to host byte order.
void stbe_i64(uint_least8_t dst[8], int_least64_t x)
Stores a 64-bit signed integer in big-endian byte order.
void stbe_i16(uint_least8_t dst[2], int_least16_t x)
Stores a 16-bit signed integer in big-endian byte order.
int_least32_t ldbe_i32(const uint_least8_t src[4])
Loads a 32-bit signed integer in big-endian byte order.
void stle_u64(uint_least8_t dst[8], uint_least64_t x)
Stores a 64-bit unsigned integer in little-endian byte order.
uint_least16_t ldbe_u16(const uint_least8_t src[2])
Loads a 16-bit unsigned integer in big-endian byte order.
uint_least32_t betoh32(uint_least32_t x)
Converts a 32-bit unsigned integer from big-endian to host byte order.
uint_least32_t ldbe_u32(const uint_least8_t src[4])
Loads a 32-bit unsigned integer in big-endian byte order.
uint_least16_t htole16(uint_least16_t x)
Converts a 16-bit unsigned integer from host to little-endian byte order.
uint_least64_t betoh64(uint_least64_t x)
Converts a 64-bit unsigned integer from big-endian to host byte order.
void stbe_i32(uint_least8_t dst[4], int_least32_t x)
Stores a 32-bit signed integer in big-endian byte order.
uint_least32_t ldle_u32(const uint_least8_t src[4])
Loads a 32-bit unsigned integer in little-endian byte order.
uint_least64_t ldbe_u64(const uint_least8_t src[8])
Loads a 64-bit unsigned integer in big-endian byte order.
uint_least32_t htole32(uint_least32_t x)
Converts a 32-bit unsigned integer from host to little-endian byte order.
void bcpyle(uint_least8_t *dst, int dstbit, const uint_least8_t *src, int srcbit, size_t n)
Copies n bits from a source to a destination buffer.
void stle_i64(uint_least8_t dst[8], int_least64_t x)
Stores a 64-bit signed integer in little-endian byte order.
int_least64_t ldbe_i64(const uint_least8_t src[8])
Loads a 64-bit signed integer in big-endian byte order.
void stbe_u64(uint_least8_t dst[8], uint_least64_t x)
Stores a 64-bit unsigned integer in big-endian byte order.
uint_least32_t letoh32(uint_least32_t x)
Converts a 32-bit unsigned integer from little-endian to host byte order.
void bcpybe(uint_least8_t *dst, int dstbit, const uint_least8_t *src, int srcbit, size_t n)
Copies n bits from a source to a destination buffer.
uint_least16_t htobe16(uint_least16_t x)
Converts a 16-bit unsigned integer from host to big-endian byte order.
uint_least64_t letoh64(uint_least64_t x)
Converts a 64-bit unsigned integer from little-endian to host byte order.
int_least32_t ldle_i32(const uint_least8_t src[4])
Loads a 32-bit signed integer in little-endian byte order.
int_least16_t ldle_i16(const uint_least8_t src[2])
Loads a 16-bit signed integer in little-endian byte order.
int_least64_t ldle_i64(const uint_least8_t src[8])
Loads a 64-bit signed integer in little-endian byte order.
uint_least32_t htobe32(uint_least32_t x)
Converts a 32-bit unsigned integer from host to big-endian byte order.
void stbe_u16(uint_least8_t dst[2], uint_least16_t x)
Stores a 16-bit unsigned integer in big-endian byte order.
uint_least16_t ldle_u16(const uint_least8_t src[2])
Loads a 16-bit unsigned integer in little-endian byte order.
void stle_i16(uint_least8_t dst[2], int_least16_t x)
Stores a 16-bit signed integer in little-endian byte order.
uint_least64_t htobe64(uint_least64_t x)
Converts a 64-bit unsigned integer from host to big-endian byte order.
uint_least64_t ldle_u64(const uint_least8_t src[8])
Loads a 64-bit unsigned integer in little-endian byte order.
uint_least64_t htole64(uint_least64_t x)
Converts a 64-bit unsigned integer from host to little-endian byte order.
int_least16_t ldbe_i16(const uint_least8_t src[2])
Loads a 16-bit signed integer in big-endian byte order.
void stle_i32(uint_least8_t dst[4], int_least32_t x)
Stores a 32-bit signed integer in little-endian byte order.
uint_least16_t letoh16(uint_least16_t x)
Converts a 16-bit unsigned integer from little-endian to host byte order.
void stle_u16(uint_least8_t dst[2], uint_least16_t x)
Stores a 16-bit unsigned integer in little-endian byte order.
void stbe_u32(uint_least8_t dst[4], uint_least32_t x)
Stores a 32-bit unsigned integer in big-endian byte order.
void stle_u32(uint_least8_t dst[4], uint_least32_t x)
Stores a 32-bit unsigned integer in little-endian byte order.
This header file is part of the utilities library; it contains the IEEE 754 floating-point format typ...
This header file is part of the C11 and POSIX compatibility library; it includes <string....