Use the variable name _ for unused return values
[bitcoinplatinum.git] / src / crypto / common.h
blobbcca3d30ea7d686b4244049a3d63f28f50ca8d7c
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.
5 #ifndef BITCOIN_CRYPTO_COMMON_H
6 #define BITCOIN_CRYPTO_COMMON_H
8 #if defined(HAVE_CONFIG_H)
9 #include "bitcoin-config.h"
10 #endif
12 #include <stdint.h>
13 #include <string.h>
15 #include "compat/endian.h"
17 uint16_t static inline ReadLE16(const unsigned char* ptr)
19 uint16_t x;
20 memcpy((char*)&x, ptr, 2);
21 return le16toh(x);
24 uint32_t static inline ReadLE32(const unsigned char* ptr)
26 uint32_t x;
27 memcpy((char*)&x, ptr, 4);
28 return le32toh(x);
31 uint64_t static inline ReadLE64(const unsigned char* ptr)
33 uint64_t x;
34 memcpy((char*)&x, ptr, 8);
35 return le64toh(x);
38 void static inline WriteLE16(unsigned char* ptr, uint16_t x)
40 uint16_t v = htole16(x);
41 memcpy(ptr, (char*)&v, 2);
44 void static inline WriteLE32(unsigned char* ptr, uint32_t x)
46 uint32_t v = htole32(x);
47 memcpy(ptr, (char*)&v, 4);
50 void static inline WriteLE64(unsigned char* ptr, uint64_t x)
52 uint64_t v = htole64(x);
53 memcpy(ptr, (char*)&v, 8);
56 uint32_t static inline ReadBE32(const unsigned char* ptr)
58 uint32_t x;
59 memcpy((char*)&x, ptr, 4);
60 return be32toh(x);
63 uint64_t static inline ReadBE64(const unsigned char* ptr)
65 uint64_t x;
66 memcpy((char*)&x, ptr, 8);
67 return be64toh(x);
70 void static inline WriteBE32(unsigned char* ptr, uint32_t x)
72 uint32_t v = htobe32(x);
73 memcpy(ptr, (char*)&v, 4);
76 void static inline WriteBE64(unsigned char* ptr, uint64_t x)
78 uint64_t v = htobe64(x);
79 memcpy(ptr, (char*)&v, 8);
82 /** Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set. */
83 uint64_t static inline CountBits(uint64_t x)
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;
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;
94 #endif
95 int ret = 0;
96 while (x) {
97 x >>= 1;
98 ++ret;
100 return ret;
103 #endif // BITCOIN_CRYPTO_COMMON_H