/src/botan/build/include/internal/botan/internal/bswap.h
Line | Count | Source |
1 | | /* |
2 | | * Byte Swapping Operations |
3 | | * (C) 1999-2011,2018 Jack Lloyd |
4 | | * (C) 2007 Yves Jerschow |
5 | | * (C) 2024 René Meusel - Rohde & Schwarz Cybersecurity |
6 | | * |
7 | | * TODO: C++23: replace this entire implementation with std::byteswap |
8 | | * |
9 | | * Botan is released under the Simplified BSD License (see license.txt) |
10 | | */ |
11 | | |
12 | | #ifndef BOTAN_BYTE_SWAP_H_ |
13 | | #define BOTAN_BYTE_SWAP_H_ |
14 | | |
15 | | #include <botan/types.h> |
16 | | |
17 | | #include <botan/compiler.h> |
18 | | #include <concepts> |
19 | | |
20 | | namespace Botan { |
21 | | |
22 | | /** |
23 | | * Swap the byte order of an unsigned integer |
24 | | */ |
25 | | template <std::unsigned_integral T> |
26 | | requires(sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8) |
27 | 5.22M | inline constexpr T reverse_bytes(T x) { |
28 | | if constexpr(sizeof(T) == 1) { |
29 | | return x; |
30 | 238k | } else if constexpr(sizeof(T) == 2) { |
31 | 238k | #if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap16) |
32 | 238k | return static_cast<T>(__builtin_bswap16(x)); |
33 | | #else |
34 | | return static_cast<T>((x << 8) | (x >> 8)); |
35 | | #endif |
36 | 2.01M | } else if constexpr(sizeof(T) == 4) { |
37 | 2.01M | #if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap32) |
38 | 2.01M | return static_cast<T>(__builtin_bswap32(x)); |
39 | | #else |
40 | | // MSVC at least recognizes this as a bswap |
41 | | return static_cast<T>(((x & 0x000000FF) << 24) | ((x & 0x0000FF00) << 8) | ((x & 0x00FF0000) >> 8) | |
42 | | ((x & 0xFF000000) >> 24)); |
43 | | #endif |
44 | 2.97M | } else if constexpr(sizeof(T) == 8) { |
45 | 2.97M | #if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap64) |
46 | 2.97M | return static_cast<T>(__builtin_bswap64(x)); |
47 | | #else |
48 | | uint32_t hi = static_cast<uint32_t>(x >> 32); |
49 | | uint32_t lo = static_cast<uint32_t>(x); |
50 | | |
51 | | hi = reverse_bytes(hi); |
52 | | lo = reverse_bytes(lo); |
53 | | |
54 | | return (static_cast<T>(lo) << 32) | hi; |
55 | | #endif |
56 | 2.97M | } |
57 | 5.22M | } _ZN5Botan13reverse_bytesITkNSt3__117unsigned_integralEmQooooooeqstT_Li1EeqstS2_Li2EeqstS2_Li4EeqstS2_Li8EEES2_S2_ Line | Count | Source | 27 | 2.97M | inline constexpr T reverse_bytes(T x) { | 28 | | if constexpr(sizeof(T) == 1) { | 29 | | return x; | 30 | | } else if constexpr(sizeof(T) == 2) { | 31 | | #if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap16) | 32 | | return static_cast<T>(__builtin_bswap16(x)); | 33 | | #else | 34 | | return static_cast<T>((x << 8) | (x >> 8)); | 35 | | #endif | 36 | | } else if constexpr(sizeof(T) == 4) { | 37 | | #if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap32) | 38 | | return static_cast<T>(__builtin_bswap32(x)); | 39 | | #else | 40 | | // MSVC at least recognizes this as a bswap | 41 | | return static_cast<T>(((x & 0x000000FF) << 24) | ((x & 0x0000FF00) << 8) | ((x & 0x00FF0000) >> 8) | | 42 | | ((x & 0xFF000000) >> 24)); | 43 | | #endif | 44 | 2.97M | } else if constexpr(sizeof(T) == 8) { | 45 | 2.97M | #if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap64) | 46 | 2.97M | return static_cast<T>(__builtin_bswap64(x)); | 47 | | #else | 48 | | uint32_t hi = static_cast<uint32_t>(x >> 32); | 49 | | uint32_t lo = static_cast<uint32_t>(x); | 50 | | | 51 | | hi = reverse_bytes(hi); | 52 | | lo = reverse_bytes(lo); | 53 | | | 54 | | return (static_cast<T>(lo) << 32) | hi; | 55 | | #endif | 56 | 2.97M | } | 57 | 2.97M | } |
_ZN5Botan13reverse_bytesITkNSt3__117unsigned_integralEjQooooooeqstT_Li1EeqstS2_Li2EeqstS2_Li4EeqstS2_Li8EEES2_S2_ Line | Count | Source | 27 | 2.01M | inline constexpr T reverse_bytes(T x) { | 28 | | if constexpr(sizeof(T) == 1) { | 29 | | return x; | 30 | | } else if constexpr(sizeof(T) == 2) { | 31 | | #if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap16) | 32 | | return static_cast<T>(__builtin_bswap16(x)); | 33 | | #else | 34 | | return static_cast<T>((x << 8) | (x >> 8)); | 35 | | #endif | 36 | 2.01M | } else if constexpr(sizeof(T) == 4) { | 37 | 2.01M | #if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap32) | 38 | 2.01M | return static_cast<T>(__builtin_bswap32(x)); | 39 | | #else | 40 | | // MSVC at least recognizes this as a bswap | 41 | | return static_cast<T>(((x & 0x000000FF) << 24) | ((x & 0x0000FF00) << 8) | ((x & 0x00FF0000) >> 8) | | 42 | | ((x & 0xFF000000) >> 24)); | 43 | | #endif | 44 | | } else if constexpr(sizeof(T) == 8) { | 45 | | #if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap64) | 46 | | return static_cast<T>(__builtin_bswap64(x)); | 47 | | #else | 48 | | uint32_t hi = static_cast<uint32_t>(x >> 32); | 49 | | uint32_t lo = static_cast<uint32_t>(x); | 50 | | | 51 | | hi = reverse_bytes(hi); | 52 | | lo = reverse_bytes(lo); | 53 | | | 54 | | return (static_cast<T>(lo) << 32) | hi; | 55 | | #endif | 56 | | } | 57 | 2.01M | } |
_ZN5Botan13reverse_bytesITkNSt3__117unsigned_integralEtQooooooeqstT_Li1EeqstS2_Li2EeqstS2_Li4EeqstS2_Li8EEES2_S2_ Line | Count | Source | 27 | 238k | inline constexpr T reverse_bytes(T x) { | 28 | | if constexpr(sizeof(T) == 1) { | 29 | | return x; | 30 | 238k | } else if constexpr(sizeof(T) == 2) { | 31 | 238k | #if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap16) | 32 | 238k | return static_cast<T>(__builtin_bswap16(x)); | 33 | | #else | 34 | | return static_cast<T>((x << 8) | (x >> 8)); | 35 | | #endif | 36 | | } else if constexpr(sizeof(T) == 4) { | 37 | | #if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap32) | 38 | | return static_cast<T>(__builtin_bswap32(x)); | 39 | | #else | 40 | | // MSVC at least recognizes this as a bswap | 41 | | return static_cast<T>(((x & 0x000000FF) << 24) | ((x & 0x0000FF00) << 8) | ((x & 0x00FF0000) >> 8) | | 42 | | ((x & 0xFF000000) >> 24)); | 43 | | #endif | 44 | | } else if constexpr(sizeof(T) == 8) { | 45 | | #if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap64) | 46 | | return static_cast<T>(__builtin_bswap64(x)); | 47 | | #else | 48 | | uint32_t hi = static_cast<uint32_t>(x >> 32); | 49 | | uint32_t lo = static_cast<uint32_t>(x); | 50 | | | 51 | | hi = reverse_bytes(hi); | 52 | | lo = reverse_bytes(lo); | 53 | | | 54 | | return (static_cast<T>(lo) << 32) | hi; | 55 | | #endif | 56 | | } | 57 | 238k | } |
|
58 | | |
59 | | } // namespace Botan |
60 | | |
61 | | #endif |