aboutsummaryrefslogtreecommitdiff
path: root/src/bytorder.h
blob: 25519504524e7198786063e021d05afdd72ef901 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#if defined(ALF_LSB_FIRST) && defined(ALF_MSB_FIRST)
#  error Cannot define both ALF_LSB_FIRST and ALF_MSB_FIRST
#endif

/* user defined one or the other in COPT, use it. */
#if defined(ALF_LSB_FIRST) || defined(ALF_MSB_FIRST)
#  define ALF_ENDIAN_OK
#endif

/* try to get the byte order on this platform.
   if we succeed, either ALF_LSB_FIRST or ALF_MSB_FIRST will
   be defined, and so will ALF_ENDIAN_OK. */

#ifndef ALF_ENDIAN_OK
#  if defined(__BYTE_ORDER__)
#    if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#      define ALF_LSB_FIRST
#      define ALF_ENDIAN_OK
#    else
#      if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#        define ALF_MSB_FIRST
#        define ALF_ENDIAN_OK
#      endif
#    endif
#  endif
#endif

/* if the __BYTE_ORDER__ macro wasn't defined... */
#ifndef ALF_ENDIAN_OK
#  if defined(__LITTLE_ENDIAN__ || defined __LITTLE_ENDIAN || defined LITTLE_ENDIAN)
#    define ALF_LSB_FIRST
#    define ALF_ENDIAN_OK
#  else
#    if defined(__BIG_ENDIAN__ || defined __BIG_ENDIAN || defined BIG_ENDIAN)
#      define ALF_MSB_FIRST
#      define ALF_ENDIAN_OK
#    endif
#  endif
#endif

#ifdef ALF_ENDIAN_OK
#  ifdef ALF_LSB_FIRST
#    define HIBYTE 2
#    define MIDBYTE 1
#    define LOBYTE 0
#  else
#    define HIBYTE 1
#    define MIDBYTE 2
#    define LOBYTE 3
#  endif
#else
#  warning Cannot determine endianness, falling back to safe but slow append_bit().
#  warning Recompile with -DALF_LSB_FIRST or -DALF_MSB_FIRST.
#endif