Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
common.h
Go to the documentation of this file.
1// Copyright (c) 2014 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_CRYPTO_COMMON_H
6#define BITCOIN_CRYPTO_COMMON_H
7
8#if defined(HAVE_CONFIG_H)
9#include <config/bitcoin-config.h>
10#endif
11
12#include <cstdint>
13#include <cstring>
14
15#include <compat/endian.h>
16
17static inline uint16_t ReadLE16(const uint8_t *ptr) {
18 uint16_t x;
19 memcpy((char *)&x, ptr, 2);
20 return le16toh(x);
21}
22
23static inline uint32_t ReadLE32(const uint8_t *ptr) {
24 uint32_t x;
25 memcpy((char *)&x, ptr, 4);
26 return le32toh(x);
27}
28
29static inline uint64_t ReadLE64(const uint8_t *ptr) {
30 uint64_t x;
31 memcpy((char *)&x, ptr, 8);
32 return le64toh(x);
33}
34
35static inline void WriteLE16(uint8_t *ptr, uint16_t x) {
36 uint16_t v = htole16(x);
37 memcpy(ptr, (char *)&v, 2);
38}
39
40static inline void WriteLE32(uint8_t *ptr, uint32_t x) {
41 uint32_t v = htole32(x);
42 memcpy(ptr, (char *)&v, 4);
43}
44
45static inline void WriteLE64(uint8_t *ptr, uint64_t x) {
46 uint64_t v = htole64(x);
47 memcpy(ptr, (char *)&v, 8);
48}
49
50uint16_t static inline ReadBE16(const uint8_t *ptr) {
51 uint16_t x;
52 memcpy((char *)&x, ptr, 2);
53 return be16toh(x);
54}
55
56static inline uint32_t ReadBE32(const uint8_t *ptr) {
57 uint32_t x;
58 memcpy((char *)&x, ptr, 4);
59 return be32toh(x);
60}
61
62static inline uint64_t ReadBE64(const uint8_t *ptr) {
63 uint64_t x;
64 memcpy((char *)&x, ptr, 8);
65 return be64toh(x);
66}
67
68static inline void WriteBE32(uint8_t *ptr, uint32_t x) {
69 uint32_t v = htobe32(x);
70 memcpy(ptr, (char *)&v, 4);
71}
72
73static inline void WriteBE64(uint8_t *ptr, uint64_t x) {
74 uint64_t v = htobe64(x);
75 memcpy(ptr, (char *)&v, 8);
76}
77
82uint64_t static inline CountBits(uint64_t x) {
83#if HAVE_DECL___BUILTIN_CLZL
84 if (sizeof(unsigned long) >= sizeof(uint64_t)) {
85 return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0;
86 }
87#endif
88#if HAVE_DECL___BUILTIN_CLZLL
89 if (sizeof(unsigned long long) >= sizeof(uint64_t)) {
90 return x ? 8 * sizeof(unsigned long long) - __builtin_clzll(x) : 0;
91 }
92#endif
93 int ret = 0;
94 while (x) {
95 x >>= 1;
96 ++ret;
97 }
98 return ret;
99}
100
101#endif // BITCOIN_CRYPTO_COMMON_H
static void WriteLE16(uint8_t *ptr, uint16_t x)
Definition common.h:35
static void WriteLE32(uint8_t *ptr, uint32_t x)
Definition common.h:40
static void WriteBE64(uint8_t *ptr, uint64_t x)
Definition common.h:73
static void WriteBE32(uint8_t *ptr, uint32_t x)
Definition common.h:68
static uint64_t ReadLE64(const uint8_t *ptr)
Definition common.h:29
static uint16_t ReadBE16(const uint8_t *ptr)
Definition common.h:50
static uint32_t ReadBE32(const uint8_t *ptr)
Definition common.h:56
static uint16_t ReadLE16(const uint8_t *ptr)
Definition common.h:17
static uint64_t ReadBE64(const uint8_t *ptr)
Definition common.h:62
static uint64_t CountBits(uint64_t x)
Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set.
Definition common.h:82
static uint32_t ReadLE32(const uint8_t *ptr)
Definition common.h:23
static void WriteLE64(uint8_t *ptr, uint64_t x)
Definition common.h:45
uint16_t be16toh(uint16_t big_endian_16bits)
Definition endian.h:156
uint32_t le32toh(uint32_t little_endian_32bits)
Definition endian.h:186
uint32_t htobe32(uint32_t host_32bits)
Definition endian.h:168
uint16_t le16toh(uint16_t little_endian_16bits)
Definition endian.h:162
uint64_t htobe64(uint64_t host_64bits)
Definition endian.h:192
uint64_t be64toh(uint64_t big_endian_64bits)
Definition endian.h:204
uint32_t be32toh(uint32_t big_endian_32bits)
Definition endian.h:180
uint64_t htole64(uint64_t host_64bits)
Definition endian.h:198
uint32_t htole32(uint32_t host_32bits)
Definition endian.h:174
uint16_t htole16(uint16_t host_16bits)
Definition endian.h:150
uint64_t le64toh(uint64_t little_endian_64bits)
Definition endian.h:210
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85