blob: 291c6b58c8ed9ed2ff2cc970bbfc0b486cc338ec [file] [log] [blame]
Daniel Boulby318e7a52022-10-21 20:20:52 +01001//===-- 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//===----------------------------------------------------------------------===//
dp-arm75b60572017-05-04 12:12:06 +010013
14#ifndef INT_ENDIANNESS_H
15#define INT_ENDIANNESS_H
16
Daniel Boulby318e7a52022-10-21 20:20:52 +010017#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
dp-arm75b60572017-05-04 12:12:06 +010018 defined(__ORDER_LITTLE_ENDIAN__)
19
Daniel Boulby318e7a52022-10-21 20:20:52 +010020// Clang and GCC provide built-in endianness definitions.
dp-arm75b60572017-05-04 12:12:06 +010021#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
22#define _YUGA_LITTLE_ENDIAN 0
Daniel Boulby318e7a52022-10-21 20:20:52 +010023#define _YUGA_BIG_ENDIAN 1
dp-arm75b60572017-05-04 12:12:06 +010024#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
25#define _YUGA_LITTLE_ENDIAN 1
Daniel Boulby318e7a52022-10-21 20:20:52 +010026#define _YUGA_BIG_ENDIAN 0
27#endif // __BYTE_ORDER__
dp-arm75b60572017-05-04 12:12:06 +010028
Daniel Boulby318e7a52022-10-21 20:20:52 +010029#else // Compilers other than Clang or GCC.
dp-arm75b60572017-05-04 12:12:06 +010030
31#if defined(__SVR4) && defined(__sun)
32#include <sys/byteorder.h>
33
34#if defined(_BIG_ENDIAN)
35#define _YUGA_LITTLE_ENDIAN 0
Daniel Boulby318e7a52022-10-21 20:20:52 +010036#define _YUGA_BIG_ENDIAN 1
dp-arm75b60572017-05-04 12:12:06 +010037#elif defined(_LITTLE_ENDIAN)
38#define _YUGA_LITTLE_ENDIAN 1
Daniel Boulby318e7a52022-10-21 20:20:52 +010039#define _YUGA_BIG_ENDIAN 0
40#else // !_LITTLE_ENDIAN
dp-arm75b60572017-05-04 12:12:06 +010041#error "unknown endianness"
Daniel Boulby318e7a52022-10-21 20:20:52 +010042#endif // !_LITTLE_ENDIAN
dp-arm75b60572017-05-04 12:12:06 +010043
Daniel Boulby318e7a52022-10-21 20:20:52 +010044#endif // Solaris
dp-arm75b60572017-05-04 12:12:06 +010045
Daniel Boulby318e7a52022-10-21 20:20:52 +010046// ..
dp-arm75b60572017-05-04 12:12:06 +010047
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
Daniel Boulby318e7a52022-10-21 20:20:52 +010054#define _YUGA_BIG_ENDIAN 1
dp-arm75b60572017-05-04 12:12:06 +010055#elif _BYTE_ORDER == _LITTLE_ENDIAN
56#define _YUGA_LITTLE_ENDIAN 1
Daniel Boulby318e7a52022-10-21 20:20:52 +010057#define _YUGA_BIG_ENDIAN 0
58#endif // _BYTE_ORDER
dp-arm75b60572017-05-04 12:12:06 +010059
Daniel Boulby318e7a52022-10-21 20:20:52 +010060#endif // *BSD
dp-arm75b60572017-05-04 12:12:06 +010061
Daniel Boulby318e7a52022-10-21 20:20:52 +010062#if defined(__OpenBSD__)
dp-arm75b60572017-05-04 12:12:06 +010063#include <machine/endian.h>
64
65#if _BYTE_ORDER == _BIG_ENDIAN
66#define _YUGA_LITTLE_ENDIAN 0
Daniel Boulby318e7a52022-10-21 20:20:52 +010067#define _YUGA_BIG_ENDIAN 1
dp-arm75b60572017-05-04 12:12:06 +010068#elif _BYTE_ORDER == _LITTLE_ENDIAN
69#define _YUGA_LITTLE_ENDIAN 1
Daniel Boulby318e7a52022-10-21 20:20:52 +010070#define _YUGA_BIG_ENDIAN 0
71#endif // _BYTE_ORDER
dp-arm75b60572017-05-04 12:12:06 +010072
Daniel Boulby318e7a52022-10-21 20:20:52 +010073#endif // OpenBSD
dp-arm75b60572017-05-04 12:12:06 +010074
Daniel Boulby318e7a52022-10-21 20:20:52 +010075// ..
dp-arm75b60572017-05-04 12:12:06 +010076
Daniel Boulby318e7a52022-10-21 20:20:52 +010077// Mac OSX has __BIG_ENDIAN__ or __LITTLE_ENDIAN__ automatically set by the
78// compiler (at least with GCC)
79#if defined(__APPLE__) || defined(__ellcc__)
dp-arm75b60572017-05-04 12:12:06 +010080
81#ifdef __BIG_ENDIAN__
82#if __BIG_ENDIAN__
83#define _YUGA_LITTLE_ENDIAN 0
Daniel Boulby318e7a52022-10-21 20:20:52 +010084#define _YUGA_BIG_ENDIAN 1
dp-arm75b60572017-05-04 12:12:06 +010085#endif
Daniel Boulby318e7a52022-10-21 20:20:52 +010086#endif // __BIG_ENDIAN__
dp-arm75b60572017-05-04 12:12:06 +010087
88#ifdef __LITTLE_ENDIAN__
89#if __LITTLE_ENDIAN__
90#define _YUGA_LITTLE_ENDIAN 1
Daniel Boulby318e7a52022-10-21 20:20:52 +010091#define _YUGA_BIG_ENDIAN 0
dp-arm75b60572017-05-04 12:12:06 +010092#endif
Daniel Boulby318e7a52022-10-21 20:20:52 +010093#endif // __LITTLE_ENDIAN__
dp-arm75b60572017-05-04 12:12:06 +010094
Daniel Boulby318e7a52022-10-21 20:20:52 +010095#endif // Mac OSX
dp-arm75b60572017-05-04 12:12:06 +010096
Daniel Boulby318e7a52022-10-21 20:20:52 +010097// ..
dp-arm75b60572017-05-04 12:12:06 +010098
99#if defined(_WIN32)
100
101#define _YUGA_LITTLE_ENDIAN 1
Daniel Boulby318e7a52022-10-21 20:20:52 +0100102#define _YUGA_BIG_ENDIAN 0
dp-arm75b60572017-05-04 12:12:06 +0100103
Daniel Boulby318e7a52022-10-21 20:20:52 +0100104#endif // Windows
dp-arm75b60572017-05-04 12:12:06 +0100105
Daniel Boulby318e7a52022-10-21 20:20:52 +0100106#endif // Clang or GCC.
dp-arm75b60572017-05-04 12:12:06 +0100107
Daniel Boulby318e7a52022-10-21 20:20:52 +0100108// .
dp-arm75b60572017-05-04 12:12:06 +0100109
110#if !defined(_YUGA_LITTLE_ENDIAN) || !defined(_YUGA_BIG_ENDIAN)
111#error Unable to determine endian
Daniel Boulby318e7a52022-10-21 20:20:52 +0100112#endif // Check we found an endianness correctly.
dp-arm75b60572017-05-04 12:12:06 +0100113
Daniel Boulby318e7a52022-10-21 20:20:52 +0100114#endif // INT_ENDIANNESS_H