Bitcoin ABC  0.22.12
P2P Digital Currency
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 
17 static inline uint16_t ReadLE16(const uint8_t *ptr) {
18  uint16_t x;
19  memcpy((char *)&x, ptr, 2);
20  return le16toh(x);
21 }
22 
23 static inline uint32_t ReadLE32(const uint8_t *ptr) {
24  uint32_t x;
25  memcpy((char *)&x, ptr, 4);
26  return le32toh(x);
27 }
28 
29 static inline uint64_t ReadLE64(const uint8_t *ptr) {
30  uint64_t x;
31  memcpy((char *)&x, ptr, 8);
32  return le64toh(x);
33 }
34 
35 static inline void WriteLE16(uint8_t *ptr, uint16_t x) {
36  uint16_t v = htole16(x);
37  memcpy(ptr, (char *)&v, 2);
38 }
39 
40 static inline void WriteLE32(uint8_t *ptr, uint32_t x) {
41  uint32_t v = htole32(x);
42  memcpy(ptr, (char *)&v, 4);
43 }
44 
45 static inline void WriteLE64(uint8_t *ptr, uint64_t x) {
46  uint64_t v = htole64(x);
47  memcpy(ptr, (char *)&v, 8);
48 }
49 
50 static inline uint32_t ReadBE32(const uint8_t *ptr) {
51  uint32_t x;
52  memcpy((char *)&x, ptr, 4);
53  return be32toh(x);
54 }
55 
56 static inline uint64_t ReadBE64(const uint8_t *ptr) {
57  uint64_t x;
58  memcpy((char *)&x, ptr, 8);
59  return be64toh(x);
60 }
61 
62 static inline void WriteBE32(uint8_t *ptr, uint32_t x) {
63  uint32_t v = htobe32(x);
64  memcpy(ptr, (char *)&v, 4);
65 }
66 
67 static inline void WriteBE64(uint8_t *ptr, uint64_t x) {
68  uint64_t v = htobe64(x);
69  memcpy(ptr, (char *)&v, 8);
70 }
71 
76 uint64_t static inline CountBits(uint64_t x) {
77 #if HAVE_DECL___BUILTIN_CLZL
78  if (sizeof(unsigned long) >= sizeof(uint64_t)) {
79  return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0;
80  }
81 #endif
82 #if HAVE_DECL___BUILTIN_CLZLL
83  if (sizeof(unsigned long long) >= sizeof(uint64_t)) {
84  return x ? 8 * sizeof(unsigned long long) - __builtin_clzll(x) : 0;
85  }
86 #endif
87  int ret = 0;
88  while (x) {
89  x >>= 1;
90  ++ret;
91  }
92  return ret;
93 }
94 
95 #endif // BITCOIN_CRYPTO_COMMON_H
static uint16_t ReadLE16(const uint8_t *ptr)
Definition: common.h:17
static void WriteLE32(uint8_t *ptr, uint32_t x)
Definition: common.h:40
uint64_t htobe64(uint64_t host_64bits)
Definition: endian.h:192
static uint64_t ReadBE64(const uint8_t *ptr)
Definition: common.h:56
static void WriteLE16(uint8_t *ptr, uint16_t x)
Definition: common.h:35
uint32_t be32toh(uint32_t big_endian_32bits)
Definition: endian.h:180
uint32_t htole32(uint32_t host_32bits)
Definition: endian.h:174
uint32_t htobe32(uint32_t host_32bits)
Definition: endian.h:168
uint64_t be64toh(uint64_t big_endian_64bits)
Definition: endian.h:204
static void WriteBE32(uint8_t *ptr, uint32_t x)
Definition: common.h:62
uint16_t le16toh(uint16_t little_endian_16bits)
Definition: endian.h:162
static uint32_t ReadLE32(const uint8_t *ptr)
Definition: common.h:23
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:76
static uint32_t ReadBE32(const uint8_t *ptr)
Definition: common.h:50
uint16_t htole16(uint16_t host_16bits)
Definition: endian.h:150
uint64_t le64toh(uint64_t little_endian_64bits)
Definition: endian.h:210
static void WriteLE64(uint8_t *ptr, uint64_t x)
Definition: common.h:45
uint64_t htole64(uint64_t host_64bits)
Definition: endian.h:198
static void WriteBE64(uint8_t *ptr, uint64_t x)
Definition: common.h:67
uint32_t le32toh(uint32_t little_endian_32bits)
Definition: endian.h:186
static uint64_t ReadLE64(const uint8_t *ptr)
Definition: common.h:29