Fabcoin Core  0.16.2
P2P Digital Currency
common.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2017 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 FABCOIN_CRYPTO_COMMON_H
6 #define FABCOIN_CRYPTO_COMMON_H
7 
8 #if defined(HAVE_CONFIG_H)
10 #endif
11 
12 #include <stdint.h>
13 #include <string.h>
14 
15 #include <compat/endian.h>
16 
17 uint16_t static inline ReadLE16(const unsigned char* ptr)
18 {
19  uint16_t x;
20  memcpy((char*)&x, ptr, 2);
21  return le16toh(x);
22 }
23 
24 uint32_t static inline ReadLE32(const unsigned char* ptr)
25 {
26  uint32_t x;
27  memcpy((char*)&x, ptr, 4);
28  return le32toh(x);
29 }
30 
31 uint64_t static inline ReadLE64(const unsigned char* ptr)
32 {
33  uint64_t x;
34  memcpy((char*)&x, ptr, 8);
35  return le64toh(x);
36 }
37 
38 void static inline WriteLE16(unsigned char* ptr, uint16_t x)
39 {
40  uint16_t v = htole16(x);
41  memcpy(ptr, (char*)&v, 2);
42 }
43 
44 void static inline WriteLE32(unsigned char* ptr, uint32_t x)
45 {
46  uint32_t v = htole32(x);
47  memcpy(ptr, (char*)&v, 4);
48 }
49 
50 void static inline WriteLE64(unsigned char* ptr, uint64_t x)
51 {
52  uint64_t v = htole64(x);
53  memcpy(ptr, (char*)&v, 8);
54 }
55 
56 uint32_t static inline ReadBE32(const unsigned char* ptr)
57 {
58  uint32_t x;
59  memcpy((char*)&x, ptr, 4);
60  return be32toh(x);
61 }
62 
63 uint64_t static inline ReadBE64(const unsigned char* ptr)
64 {
65  uint64_t x;
66  memcpy((char*)&x, ptr, 8);
67  return be64toh(x);
68 }
69 
70 void static inline WriteBE32(unsigned char* ptr, uint32_t x)
71 {
72  uint32_t v = htobe32(x);
73  memcpy(ptr, (char*)&v, 4);
74 }
75 
76 void static inline WriteBE64(unsigned char* ptr, uint64_t x)
77 {
78  uint64_t v = htobe64(x);
79  memcpy(ptr, (char*)&v, 8);
80 }
81 
83 uint64_t static inline CountBits(uint64_t x)
84 {
85 #ifdef HAVE_DECL___BUILTIN_CLZL
86  if (sizeof(unsigned long) >= sizeof(uint64_t)) {
87  return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0;
88  }
89 #endif
90 #ifdef HAVE_DECL___BUILTIN_CLZLL
91  if (sizeof(unsigned long long) >= sizeof(uint64_t)) {
92  return x ? 8 * sizeof(unsigned long long) - __builtin_clzll(x) : 0;
93  }
94 #endif
95  int ret = 0;
96  while (x) {
97  x >>= 1;
98  ++ret;
99  }
100  return ret;
101 }
102 
103 #endif // FABCOIN_CRYPTO_COMMON_H
uint32_t htobe32(uint32_t host_32bits)
Definition: endian.h:139
uint64_t htole64(uint64_t host_64bits)
Definition: endian.h:174
uint32_t le32toh(uint32_t little_endian_32bits)
Definition: endian.h:160
#define x(i)
uint64_t le64toh(uint64_t little_endian_64bits)
Definition: endian.h:188
uint16_t le16toh(uint16_t little_endian_16bits)
Definition: endian.h:132
void * memcpy(void *a, const void *b, size_t c)
uint16_t htole16(uint16_t host_16bits)
Definition: endian.h:118
uint32_t htole32(uint32_t host_32bits)
Definition: endian.h:146
uint64_t htobe64(uint64_t host_64bits)
Definition: endian.h:167
uint64_t be64toh(uint64_t big_endian_64bits)
Definition: endian.h:181
uint32_t be32toh(uint32_t big_endian_32bits)
Definition: endian.h:153