nostrdb

an unfairly fast embedded nostr database backed by lmdb
git clone git://jb55.com/nostrdb
Log | Files | Refs | Submodules | README | LICENSE

unaligned.h (1820B)


      1 #ifndef UNALIGNED_H
      2 #define UNALIGNED_H
      3 
      4 /*
      5  * This is a simplified version of portable/punaligned.h that does not depend on
      6  * endian detection, but which assumes x86 is always little endian.
      7  * Include the portable version for better precision.
      8  */
      9 
     10 #ifndef unaligned_read_le16toh
     11 
     12 #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
     13 
     14 #define unaligned_read_le16toh(p) (*(uint16_t*)(p))
     15 #define unaligned_read_le32toh(p) (*(uint32_t*)(p))
     16 #define unaligned_read_le64toh(p) (*(uint64_t*)(p))
     17 
     18 #else
     19 
     20 #define unaligned_read_le16toh(p)  (                                        \
     21         (((uint16_t)(((uint8_t *)(p))[0])) <<  0) |                         \
     22         (((uint16_t)(((uint8_t *)(p))[1])) <<  8))
     23 
     24 #define unaligned_read_le32toh(p)  (                                        \
     25         (((uint32_t)(((uint8_t *)(p))[0])) <<  0) |                         \
     26         (((uint32_t)(((uint8_t *)(p))[1])) <<  8) |                         \
     27         (((uint32_t)(((uint8_t *)(p))[2])) << 16) |                         \
     28         (((uint32_t)(((uint8_t *)(p))[3])) << 24))
     29 
     30 #define unaligned_read_le64toh(p)  (                                        \
     31         (((uint64_t)(((uint8_t *)(p))[0])) <<  0) |                         \
     32         (((uint64_t)(((uint8_t *)(p))[1])) <<  8) |                         \
     33         (((uint64_t)(((uint8_t *)(p))[2])) << 16) |                         \
     34         (((uint64_t)(((uint8_t *)(p))[3])) << 24) |                         \
     35         (((uint64_t)(((uint8_t *)(p))[4])) << 32) |                         \
     36         (((uint64_t)(((uint8_t *)(p))[5])) << 40) |                         \
     37         (((uint64_t)(((uint8_t *)(p))[6])) << 48) |                         \
     38         (((uint64_t)(((uint8_t *)(p))[7])) << 56))
     39 #endif
     40 #endif
     41 
     42 #endif /* UNALIGNED_H */