2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #ifndef OSSL_INTERNAL_ENDIAN_H
11 # define OSSL_INTERNAL_ENDIAN_H
15 * IS_LITTLE_ENDIAN and IS_BIG_ENDIAN can be used to detect the endiannes
16 * at compile time. To use it, DECLARE_IS_ENDIAN must be used to declare
19 * L_ENDIAN and B_ENDIAN can be used at preprocessor time. They can be set
20 * in the configarion using the lib_cppflags variable. If neither is
21 * set, it will fall back to code works with either endianness.
24 # if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__)
25 # define DECLARE_IS_ENDIAN const int ossl_is_little_endian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
26 # define IS_LITTLE_ENDIAN (ossl_is_little_endian)
27 # define IS_BIG_ENDIAN (!ossl_is_little_endian)
28 # if defined(L_ENDIAN) && (__BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__)
29 # error "L_ENDIAN defined on a big endian machine"
31 # if defined(B_ENDIAN) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
32 # error "B_ENDIAN defined on a little endian machine"
34 # if !defined(L_ENDIAN) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
37 # if !defined(B_ENDIAN) && (__BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__)
41 # define DECLARE_IS_ENDIAN \
45 } ossl_is_endian = { 1 }
47 # define IS_LITTLE_ENDIAN (ossl_is_endian.little != 0)
48 # define IS_BIG_ENDIAN (ossl_is_endian.little == 0)