File indexing completed on 2025-01-18 10:13:16
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef UPB_BASE_INTERNAL_ENDIAN_H_
0009 #define UPB_BASE_INTERNAL_ENDIAN_H_
0010
0011 #include <stdint.h>
0012
0013
0014 #include "upb/port/def.inc"
0015
0016 #ifdef __cplusplus
0017 extern "C" {
0018 #endif
0019
0020 UPB_INLINE bool upb_IsLittleEndian(void) {
0021 const int x = 1;
0022 return *(char*)&x == 1;
0023 }
0024
0025 UPB_INLINE uint32_t upb_BigEndian32(uint32_t val) {
0026 if (upb_IsLittleEndian()) return val;
0027
0028 return ((val & 0xff) << 24) | ((val & 0xff00) << 8) |
0029 ((val & 0xff0000) >> 8) | ((val & 0xff000000) >> 24);
0030 }
0031
0032 UPB_INLINE uint64_t upb_BigEndian64(uint64_t val) {
0033 if (upb_IsLittleEndian()) return val;
0034
0035 const uint64_t hi = ((uint64_t)upb_BigEndian32((uint32_t)val)) << 32;
0036 const uint64_t lo = upb_BigEndian32((uint32_t)(val >> 32));
0037 return hi | lo;
0038 }
0039
0040 #ifdef __cplusplus
0041 }
0042 #endif
0043
0044 #include "upb/port/undef.inc"
0045
0046 #endif