1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci#ifndef OSSL_INTERNAL_ENDIAN_H 11e1051a39Sopenharmony_ci# define OSSL_INTERNAL_ENDIAN_H 12e1051a39Sopenharmony_ci# pragma once 13e1051a39Sopenharmony_ci 14e1051a39Sopenharmony_ci/* 15e1051a39Sopenharmony_ci * IS_LITTLE_ENDIAN and IS_BIG_ENDIAN can be used to detect the endiannes 16e1051a39Sopenharmony_ci * at compile time. To use it, DECLARE_IS_ENDIAN must be used to declare 17e1051a39Sopenharmony_ci * a variable. 18e1051a39Sopenharmony_ci * 19e1051a39Sopenharmony_ci * L_ENDIAN and B_ENDIAN can be used at preprocessor time. They can be set 20e1051a39Sopenharmony_ci * in the configarion using the lib_cppflags variable. If neither is 21e1051a39Sopenharmony_ci * set, it will fall back to code works with either endianness. 22e1051a39Sopenharmony_ci */ 23e1051a39Sopenharmony_ci 24e1051a39Sopenharmony_ci# if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) 25e1051a39Sopenharmony_ci# define DECLARE_IS_ENDIAN const int ossl_is_little_endian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 26e1051a39Sopenharmony_ci# define IS_LITTLE_ENDIAN (ossl_is_little_endian) 27e1051a39Sopenharmony_ci# define IS_BIG_ENDIAN (!ossl_is_little_endian) 28e1051a39Sopenharmony_ci# if defined(L_ENDIAN) && (__BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__) 29e1051a39Sopenharmony_ci# error "L_ENDIAN defined on a big endian machine" 30e1051a39Sopenharmony_ci# endif 31e1051a39Sopenharmony_ci# if defined(B_ENDIAN) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) 32e1051a39Sopenharmony_ci# error "B_ENDIAN defined on a little endian machine" 33e1051a39Sopenharmony_ci# endif 34e1051a39Sopenharmony_ci# if !defined(L_ENDIAN) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) 35e1051a39Sopenharmony_ci# define L_ENDIAN 36e1051a39Sopenharmony_ci# endif 37e1051a39Sopenharmony_ci# if !defined(B_ENDIAN) && (__BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__) 38e1051a39Sopenharmony_ci# define B_ENDIAN 39e1051a39Sopenharmony_ci# endif 40e1051a39Sopenharmony_ci# else 41e1051a39Sopenharmony_ci# define DECLARE_IS_ENDIAN \ 42e1051a39Sopenharmony_ci const union { \ 43e1051a39Sopenharmony_ci long one; \ 44e1051a39Sopenharmony_ci char little; \ 45e1051a39Sopenharmony_ci } ossl_is_endian = { 1 } 46e1051a39Sopenharmony_ci 47e1051a39Sopenharmony_ci# define IS_LITTLE_ENDIAN (ossl_is_endian.little != 0) 48e1051a39Sopenharmony_ci# define IS_BIG_ENDIAN (ossl_is_endian.little == 0) 49e1051a39Sopenharmony_ci# endif 50e1051a39Sopenharmony_ci 51e1051a39Sopenharmony_ci#endif 52