Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
byteswap.h
Go to the documentation of this file.
1// Copyright (c) 2014-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_COMPAT_BYTESWAP_H
6#define BITCOIN_COMPAT_BYTESWAP_H
7
8#include <cstdint>
9
10// All internal_bswap_* functions can be replaced with std::byteswap once we
11// require c++23. Both libstdc++ and libc++ implement std::byteswap via these
12// builtins.
13
14#ifndef DISABLE_BUILTIN_BSWAPS
15# if defined __has_builtin
16# if __has_builtin(__builtin_bswap16)
17# define bitcoin_builtin_bswap16(x) __builtin_bswap16(x)
18# endif
19# if __has_builtin(__builtin_bswap32)
20# define bitcoin_builtin_bswap32(x) __builtin_bswap32(x)
21# endif
22# if __has_builtin(__builtin_bswap64)
23# define bitcoin_builtin_bswap64(x) __builtin_bswap64(x)
24# endif
25# endif
26#endif
27
29{
30#ifdef bitcoin_builtin_bswap16
32#else
33 return (x >> 8) | (x << 8);
34#endif
35}
36
38{
39#ifdef bitcoin_builtin_bswap32
41#else
42 return (((x & 0xff000000U) >> 24) | ((x & 0x00ff0000U) >> 8) |
43 ((x & 0x0000ff00U) << 8) | ((x & 0x000000ffU) << 24));
44#endif
45}
46
48{
49#ifdef bitcoin_builtin_bswap64
51#else
52 return (((x & 0xff00000000000000ull) >> 56)
53 | ((x & 0x00ff000000000000ull) >> 40)
54 | ((x & 0x0000ff0000000000ull) >> 24)
55 | ((x & 0x000000ff00000000ull) >> 8)
56 | ((x & 0x00000000ff000000ull) << 8)
57 | ((x & 0x0000000000ff0000ull) << 24)
58 | ((x & 0x000000000000ff00ull) << 40)
59 | ((x & 0x00000000000000ffull) << 56));
60#endif
61}
62
63#endif // BITCOIN_COMPAT_BYTESWAP_H
constexpr uint16_t internal_bswap_16(uint16_t x)
Definition byteswap.h:28
constexpr uint64_t internal_bswap_64(uint64_t x)
Definition byteswap.h:47
constexpr uint32_t internal_bswap_32(uint32_t x)
Definition byteswap.h:37
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82