xref: /freebsd/crypto/openssl/include/internal/endian.h (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 /*
2  * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9 
10 #ifndef OSSL_INTERNAL_ENDIAN_H
11 # define OSSL_INTERNAL_ENDIAN_H
12 # pragma once
13 
14 /*
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
17  * a variable.
18  *
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.
22  */
23 
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"
30 #  endif
31 #  if defined(B_ENDIAN) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
32 #   error "B_ENDIAN defined on a little endian machine"
33 #  endif
34 #  if !defined(L_ENDIAN) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
35 #   define L_ENDIAN
36 #  endif
37 #  if !defined(B_ENDIAN) && (__BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__)
38 #   define B_ENDIAN
39 #  endif
40 # else
41 #  define DECLARE_IS_ENDIAN \
42     const union { \
43         long one; \
44         char little; \
45     } ossl_is_endian = { 1 }
46 
47 #  define IS_LITTLE_ENDIAN (ossl_is_endian.little != 0)
48 #  define IS_BIG_ENDIAN    (ossl_is_endian.little == 0)
49 # endif
50 
51 #endif
52