xref: /isa-l_crypto/include/endian_helper.h (revision 1de5344d2e55af6b245565bde0dec2868130cf14)
1 /**********************************************************************
2   Copyright(c) 2011-2016 Intel Corporation All rights reserved.
3 
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions
6   are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in
11       the documentation and/or other materials provided with the
12       distribution.
13     * Neither the name of Intel Corporation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29 
30 #ifndef _ENDIAN_HELPER_H_
31 #define _ENDIAN_HELPER_H_
32 
33 /**
34  *  @file  endian_helper.h
35  *  @brief Byte order helper routines
36  *
37  */
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 #if defined(__ICC)
44 #define byteswap32(x) _bswap(x)
45 #define byteswap64(x) _bswap64(x)
46 #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
47 #define byteswap32(x) __builtin_bswap32(x)
48 #define byteswap64(x) __builtin_bswap64(x)
49 #else
50 #define byteswap32(x) (((x) << 24) | (((x) & 0xff00) << 8) | (((x) & 0xff0000) >> 8) | ((x) >> 24))
51 #define byteswap64(x)                                                                              \
52         ((((x) & (0xffull << 0)) << 56) | (((x) & (0xffull << 8)) << 40) |                         \
53          (((x) & (0xffull << 16)) << 24) | (((x) & (0xffull << 24)) << 8) |                        \
54          (((x) & (0xffull << 32)) >> 8) | (((x) & (0xffull << 40)) >> 24) |                        \
55          (((x) & (0xffull << 48)) >> 40) | (((x) & (0xffull << 56)) >> 56))
56 #endif
57 
58 // This check works when using GCC (or LLVM).  Assume little-endian
59 // if any other compiler is being used.
60 #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) &&                                    \
61         __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
62 #define to_le32(x) byteswap32(x)
63 #define to_le64(x) byteswap64(x)
64 #define to_be32(x) (x)
65 #define to_be64(x) (x)
66 #else
67 #define to_le32(x) (x)
68 #define to_le64(x) (x)
69 #define to_be32(x) byteswap32(x)
70 #define to_be64(x) byteswap64(x)
71 #endif
72 
73 #ifdef __cplusplus
74 }
75 #endif
76 
77 #endif // _ISA_HELPER_H_
78