/src/botan/build/include/botan/internal/bswap.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Byte Swapping Operations |
3 | | * (C) 1999-2011,2018 Jack Lloyd |
4 | | * (C) 2007 Yves Jerschow |
5 | | * |
6 | | * Botan is released under the Simplified BSD License (see license.txt) |
7 | | */ |
8 | | |
9 | | #ifndef BOTAN_BYTE_SWAP_H_ |
10 | | #define BOTAN_BYTE_SWAP_H_ |
11 | | |
12 | | #include <botan/types.h> |
13 | | |
14 | | namespace Botan { |
15 | | |
16 | | /** |
17 | | * Swap a 16 bit integer |
18 | | */ |
19 | 0 | inline constexpr uint16_t reverse_bytes(uint16_t x) { |
20 | 0 | #if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap16) |
21 | 0 | return __builtin_bswap16(x); |
22 | | #else |
23 | | return static_cast<uint16_t>((x << 8) | (x >> 8)); |
24 | | #endif |
25 | 0 | } |
26 | | |
27 | | /** |
28 | | * Swap a 32 bit integer |
29 | | * |
30 | | * We cannot use MSVC's _byteswap_ulong because it does not consider |
31 | | * the builtin to be constexpr. |
32 | | */ |
33 | 192 | inline constexpr uint32_t reverse_bytes(uint32_t x) { |
34 | 192 | #if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap32) |
35 | 192 | return __builtin_bswap32(x); |
36 | | #else |
37 | | // MSVC at least recognizes this as a bswap |
38 | | return ((x & 0x000000FF) << 24) | ((x & 0x0000FF00) << 8) | ((x & 0x00FF0000) >> 8) | ((x & 0xFF000000) >> 24); |
39 | | #endif |
40 | 192 | } |
41 | | |
42 | | /** |
43 | | * Swap a 64 bit integer |
44 | | * |
45 | | * We cannot use MSVC's _byteswap_uint64 because it does not consider |
46 | | * the builtin to be constexpr. |
47 | | */ |
48 | 531k | inline constexpr uint64_t reverse_bytes(uint64_t x) { |
49 | 531k | #if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap64) |
50 | 531k | return __builtin_bswap64(x); |
51 | | #else |
52 | | uint32_t hi = static_cast<uint32_t>(x >> 32); |
53 | | uint32_t lo = static_cast<uint32_t>(x); |
54 | | |
55 | | hi = reverse_bytes(hi); |
56 | | lo = reverse_bytes(lo); |
57 | | |
58 | | return (static_cast<uint64_t>(lo) << 32) | hi; |
59 | | #endif |
60 | 531k | } |
61 | | |
62 | | /** |
63 | | * Swap 4 Ts in an array |
64 | | */ |
65 | | template <typename T> |
66 | 0 | inline constexpr void bswap_4(T x[4]) { |
67 | 0 | x[0] = reverse_bytes(x[0]); |
68 | 0 | x[1] = reverse_bytes(x[1]); |
69 | 0 | x[2] = reverse_bytes(x[2]); |
70 | 0 | x[3] = reverse_bytes(x[3]); |
71 | 0 | } Unexecuted instantiation: void Botan::bswap_4<unsigned int>(unsigned int*) Unexecuted instantiation: void Botan::bswap_4<unsigned long>(unsigned long*) |
72 | | |
73 | | } // namespace Botan |
74 | | |
75 | | #endif |