Botan
3.2.0
Crypto and TLS for C&
src
lib
utils
bswap.h
Go to the documentation of this file.
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
inline
constexpr
uint16_t
reverse_bytes
(uint16_t x) {
20
#if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap16)
21
return
__builtin_bswap16(x);
22
#else
23
return
static_cast<
uint16_t
>
((x << 8) | (x >> 8));
24
#endif
25
}
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
inline
constexpr
uint32_t
reverse_bytes
(uint32_t x) {
34
#if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap32)
35
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
}
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
inline
constexpr
uint64_t
reverse_bytes
(uint64_t x) {
49
#if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap64)
50
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
}
61
62
}
// namespace Botan
63
64
#endif
Botan
Definition
alg_id.cpp:13
Botan::reverse_bytes
constexpr uint16_t reverse_bytes(uint16_t x)
Definition
bswap.h:19
Generated by
1.9.8