Coverage Report

Created: 2023-09-25 06:34

/src/botan/build/include/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
*
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
6.28k
inline constexpr uint16_t reverse_bytes(uint16_t x) {
20
6.28k
#if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap16)
21
6.28k
   return __builtin_bswap16(x);
22
#else
23
   return static_cast<uint16_t>((x << 8) | (x >> 8));
24
#endif
25
6.28k
}
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
6.99M
inline constexpr uint32_t reverse_bytes(uint32_t x) {
34
6.99M
#if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap32)
35
6.99M
   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
6.99M
}
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
11.6M
inline constexpr uint64_t reverse_bytes(uint64_t x) {
49
11.6M
#if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap64)
50
11.6M
   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
11.6M
}
61
62
}  // namespace Botan
63
64
#endif