]> git.notmuchmail.org Git - notmuch/blob - util/endian-util.h
util: detect byte order
[notmuch] / util / endian-util.h
1 /* this file mimics the macros present in recent GCC and CLANG */
2
3 #ifndef _ENDIAN_UTIL_H
4 #define _ENDIAN_UTIL_H
5
6 /* This are prefixed with UTIL to avoid collisions
7  *
8  * You can use something like the following to define UTIL_BYTE_ORDER
9  * in a configure script.
10  */
11 #if 0
12 #include <stdio.h>
13 #include <stdint.h>
14 uint32_t test = 0x34333231;
15 int main() { printf("%.4s\n", (const char*)&test); return 0; }
16 #endif
17
18 #define UTIL_ORDER_BIG_ENDIAN     4321
19 #define UTIL_ORDER_LITTLE_ENDIAN  1234
20
21
22 #if !defined(UTIL_BYTE_ORDER) || ((UTIL_BYTE_ORDER != UTIL_ORDER_BIG_ENDIAN) && \
23                                   (UTIL_BYTE_ORDER != UTIL_ORDER_LITTLE_ENDIAN))
24 #undef UTIL_BYTE_ORDER
25 #ifdef __BYTE_ORDER__
26 #  if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
27 #    define UTIL_BYTE_ORDER UTIL_ORDER_LITTLE_ENDIAN
28 #  elif (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
29 #    define UTIL_BYTE_ORDER UTIL_ORDER_BIG_ENDIAN
30 #  else
31 #    error "Unsupported __BYTE_ORDER__"
32 #  endif
33 #else
34 #  error "UTIL_BYTE_ORDER not correctly defined and __BYTE_ORDER__ not defined."
35 #endif
36 #endif
37
38 #endif