1//===-- int_endianness.h - configuration header for compiler-rt -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is a configuration header for compiler-rt.
10// This file is not part of the interface of this library.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef INT_ENDIANNESS_H
15#define INT_ENDIANNESS_H
16
17#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
18 defined(__ORDER_LITTLE_ENDIAN__)
19
20// Clang and GCC provide built-in endianness definitions.
21#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
22#define _YUGA_LITTLE_ENDIAN 0
23#define _YUGA_BIG_ENDIAN 1
24#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
25#define _YUGA_LITTLE_ENDIAN 1
26#define _YUGA_BIG_ENDIAN 0
27#endif // __BYTE_ORDER__
28
29#else // Compilers other than Clang or GCC.
30
31#if defined(__SVR4) && defined(__sun)
32#include <sys/byteorder.h>
33
34#if defined(_BIG_ENDIAN)
35#define _YUGA_LITTLE_ENDIAN 0
36#define _YUGA_BIG_ENDIAN 1
37#elif defined(_LITTLE_ENDIAN)
38#define _YUGA_LITTLE_ENDIAN 1
39#define _YUGA_BIG_ENDIAN 0
40#else // !_LITTLE_ENDIAN
41#error "unknown endianness"
42#endif // !_LITTLE_ENDIAN
43
44#endif // Solaris
45
46// ..
47
48#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \
49 defined(__minix)
50#include <sys/endian.h>
51
52#if _BYTE_ORDER == _BIG_ENDIAN
53#define _YUGA_LITTLE_ENDIAN 0
54#define _YUGA_BIG_ENDIAN 1
55#elif _BYTE_ORDER == _LITTLE_ENDIAN
56#define _YUGA_LITTLE_ENDIAN 1
57#define _YUGA_BIG_ENDIAN 0
58#endif // _BYTE_ORDER
59
60#endif // *BSD
61
62#if defined(__OpenBSD__)
63#include <machine/endian.h>
64
65#if _BYTE_ORDER == _BIG_ENDIAN
66#define _YUGA_LITTLE_ENDIAN 0
67#define _YUGA_BIG_ENDIAN 1
68#elif _BYTE_ORDER == _LITTLE_ENDIAN
69#define _YUGA_LITTLE_ENDIAN 1
70#define _YUGA_BIG_ENDIAN 0
71#endif // _BYTE_ORDER
72
73#endif // OpenBSD
74
75// ..
76
77// Mac OSX has __BIG_ENDIAN__ or __LITTLE_ENDIAN__ automatically set by the
78// compiler (at least with GCC)
79#if defined(__APPLE__) || defined(__ellcc__)
80
81#ifdef __BIG_ENDIAN__
82#if __BIG_ENDIAN__
83#define _YUGA_LITTLE_ENDIAN 0
84#define _YUGA_BIG_ENDIAN 1
85#endif
86#endif // __BIG_ENDIAN__
87
88#ifdef __LITTLE_ENDIAN__
89#if __LITTLE_ENDIAN__
90#define _YUGA_LITTLE_ENDIAN 1
91#define _YUGA_BIG_ENDIAN 0
92#endif
93#endif // __LITTLE_ENDIAN__
94
95#endif // Mac OSX
96
97// ..
98
99#if defined(_WIN32)
100
101#define _YUGA_LITTLE_ENDIAN 1
102#define _YUGA_BIG_ENDIAN 0
103
104#endif // Windows
105
106#endif // Clang or GCC.
107
108// .
109
110#if !defined(_YUGA_LITTLE_ENDIAN) || !defined(_YUGA_BIG_ENDIAN)
111#error Unable to determine endian
112#endif // Check we found an endianness correctly.
113
114#endif // INT_ENDIANNESS_H
115

source code of compiler-rt/lib/builtins/int_endianness.h