1 /*
2  * Copyright (c) 2023, The TrustedFirmware-M Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef CC3XX_ENDIAN_HELPERS_H
9 #define CC3XX_ENDIAN_HELPERS_H
10 
11 #include <stdint.h>
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 /* Conveniently, the compiler is smart enough to make this a single-instruction
18  * endianness reversal where the architecture has such an instruction
19  */
bswap_32(uint32_t in)20 static inline uint32_t bswap_32(uint32_t in) {
21     uint32_t out = 0;
22 
23     out |= (in & 0xFF000000) >> 24;
24     out |= (in & 0x00FF0000) >> 8;
25     out |= (in & 0x0000FF00) << 8;
26     out |= (in & 0x000000FF) << 24;
27 
28     return out;
29 }
30 
bswap_64(uint64_t in)31 static inline uint64_t bswap_64(uint64_t in) {
32     uint64_t out = 0;
33 
34     out |= (in & 0xFF00000000000000) >> 56;
35     out |= (in & 0x00FF000000000000) >> 40;
36     out |= (in & 0x0000FF0000000000) >> 24;
37     out |= (in & 0x000000FF00000000) >> 8;
38     out |= (in & 0x00000000FF000000) << 8;
39     out |= (in & 0x0000000000FF0000) << 24;
40     out |= (in & 0x000000000000FF00) << 40;
41     out |= (in & 0x00000000000000FF) << 56;
42 
43     return out;
44 }
45 
46 #ifdef __cplusplus
47 }
48 #endif
49 
50 #endif /* CC3XX_ENDIAN_HELPERS_H */
51