aboutsummaryrefslogtreecommitdiffstats
path: root/include/osmocom/core/endian.h
blob: 02c6406dfba92bd18d3b9f7d5da55406b99b033c (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
#pragma once

/**
 * GNU and FreeBSD have various ways to express the
 * endianess but none of them is similiar enough. This
 * will create two defines that allows to decide on the
 * endian. The following will be defined to either 0 or
 * 1 at the end of the file.
 *
 *      OSMO_IS_LITTLE_ENDIAN
 *      OSMO_IS_BIG_ENDIAN
 *
 */

#if defined(__FreeBSD__)
#include <sys/endian.h>
        #if BYTE_ORDER == LITTLE_ENDIAN
                #define OSMO_IS_LITTLE_ENDIAN           1
                #define OSMO_IS_BIG_ENDIAN              0
        #elif BYTE_ORDER == BIG_ENDIAN
                #define OSMO_IS_LITTLE_ENDIAN           0
                #define OSMO_IS_BIG_ENDIAN              1
        #else
                #error "Unknown endian"
        #endif
#elif defined(__APPLE__)
#include <machine/endian.h>
	#if defined(__DARWIN_LITTLE_ENDIAN)
		#define OSMO_IS_LITTLE_ENDIAN		1
		#define OSMO_IS_BIG_ENDIAN		0
	#elif define(__DARWIN_BIG_ENDIAN)
		#define OSMO_IS_LITTLE_ENDIAN		0
		#define OSMO_IS_BIG_ENDIAN		1
	#else
		#error "Unknown endian"
	#endif
#else
#include <endian.h>
        #if __BYTE_ORDER == __LITTLE_ENDIAN
                #define OSMO_IS_LITTLE_ENDIAN           1
                #define OSMO_IS_BIG_ENDIAN              0
        #elif __BYTE_ORDER == __BIG_ENDIAN
                #define OSMO_IS_LITTLE_ENDIAN           0
                #define OSMO_IS_BIG_ENDIAN              1
        #else
                #error "Unknown endian"
        #endif
#endif