/bitcoin/src/crypto/common.h
Line | Count | Source |
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_CRYPTO_COMMON_H |
6 | | #define BITCOIN_CRYPTO_COMMON_H |
7 | | |
8 | | #include <compat/endian.h> |
9 | | |
10 | | #include <concepts> |
11 | | #include <cstddef> |
12 | | #include <cstdint> |
13 | | #include <cstring> |
14 | | |
15 | | template <typename B> |
16 | | concept ByteType = std::same_as<B, unsigned char> || std::same_as<B, std::byte>; |
17 | | |
18 | | template <ByteType B> |
19 | | inline uint16_t ReadLE16(const B* ptr) |
20 | 51.3k | { |
21 | 51.3k | uint16_t x; |
22 | 51.3k | memcpy(&x, ptr, 2); |
23 | 51.3k | return le16toh_internal(x); |
24 | 51.3k | } |
25 | | |
26 | | template <ByteType B> |
27 | | inline uint32_t ReadLE32(const B* ptr) |
28 | 1.84G | { |
29 | 1.84G | uint32_t x; |
30 | 1.84G | memcpy(&x, ptr, 4); |
31 | 1.84G | return le32toh_internal(x); |
32 | 1.84G | } _Z8ReadLE32ITk8ByteTypehEjPKT_ Line | Count | Source | 28 | 1.25G | { | 29 | 1.25G | uint32_t x; | 30 | 1.25G | memcpy(&x, ptr, 4); | 31 | 1.25G | return le32toh_internal(x); | 32 | 1.25G | } |
_Z8ReadLE32ITk8ByteTypeSt4byteEjPKT_ Line | Count | Source | 28 | 584M | { | 29 | 584M | uint32_t x; | 30 | 584M | memcpy(&x, ptr, 4); | 31 | 584M | return le32toh_internal(x); | 32 | 584M | } |
|
33 | | |
34 | | template <ByteType B> |
35 | | inline uint64_t ReadLE64(const B* ptr) |
36 | 1.04G | { |
37 | 1.04G | uint64_t x; |
38 | 1.04G | memcpy(&x, ptr, 8); |
39 | 1.04G | return le64toh_internal(x); |
40 | 1.04G | } _Z8ReadLE64ITk8ByteTypehEmPKT_ Line | Count | Source | 36 | 963M | { | 37 | 963M | uint64_t x; | 38 | 963M | memcpy(&x, ptr, 8); | 39 | 963M | return le64toh_internal(x); | 40 | 963M | } |
_Z8ReadLE64ITk8ByteTypeSt4byteEmPKT_ Line | Count | Source | 36 | 77.9M | { | 37 | 77.9M | uint64_t x; | 38 | 77.9M | memcpy(&x, ptr, 8); | 39 | 77.9M | return le64toh_internal(x); | 40 | 77.9M | } |
|
41 | | |
42 | | template <ByteType B> |
43 | | inline void WriteLE16(B* ptr, uint16_t x) |
44 | 42 | { |
45 | 42 | uint16_t v = htole16_internal(x); |
46 | 42 | memcpy(ptr, &v, 2); |
47 | 42 | } |
48 | | |
49 | | template <ByteType B> |
50 | | inline void WriteLE32(B* ptr, uint32_t x) |
51 | 582M | { |
52 | 582M | uint32_t v = htole32_internal(x); |
53 | 582M | memcpy(ptr, &v, 4); |
54 | 582M | } _Z9WriteLE32ITk8ByteTypehEvPT_j Line | Count | Source | 51 | 57.6k | { | 52 | 57.6k | uint32_t v = htole32_internal(x); | 53 | 57.6k | memcpy(ptr, &v, 4); | 54 | 57.6k | } |
_Z9WriteLE32ITk8ByteTypeSt4byteEvPT_j Line | Count | Source | 51 | 582M | { | 52 | 582M | uint32_t v = htole32_internal(x); | 53 | 582M | memcpy(ptr, &v, 4); | 54 | 582M | } |
|
55 | | |
56 | | template <ByteType B> |
57 | | inline void WriteLE64(B* ptr, uint64_t x) |
58 | 21.0k | { |
59 | 21.0k | uint64_t v = htole64_internal(x); |
60 | 21.0k | memcpy(ptr, &v, 8); |
61 | 21.0k | } _Z9WriteLE64ITk8ByteTypeSt4byteEvPT_m Line | Count | Source | 58 | 15.9k | { | 59 | 15.9k | uint64_t v = htole64_internal(x); | 60 | 15.9k | memcpy(ptr, &v, 8); | 61 | 15.9k | } |
_Z9WriteLE64ITk8ByteTypehEvPT_m Line | Count | Source | 58 | 5.09k | { | 59 | 5.09k | uint64_t v = htole64_internal(x); | 60 | 5.09k | memcpy(ptr, &v, 8); | 61 | 5.09k | } |
|
62 | | |
63 | | template <ByteType B> |
64 | | inline uint16_t ReadBE16(const B* ptr) |
65 | 1.01M | { |
66 | 1.01M | uint16_t x; |
67 | 1.01M | memcpy(&x, ptr, 2); |
68 | 1.01M | return be16toh_internal(x); |
69 | 1.01M | } |
70 | | |
71 | | template <ByteType B> |
72 | | inline uint32_t ReadBE32(const B* ptr) |
73 | 3.81M | { |
74 | 3.81M | uint32_t x; |
75 | 3.81M | memcpy(&x, ptr, 4); |
76 | 3.81M | return be32toh_internal(x); |
77 | 3.81M | } _Z8ReadBE32ITk8ByteTypehEjPKT_ Line | Count | Source | 73 | 3.81M | { | 74 | 3.81M | uint32_t x; | 75 | 3.81M | memcpy(&x, ptr, 4); | 76 | 3.81M | return be32toh_internal(x); | 77 | 3.81M | } |
Unexecuted instantiation: _Z8ReadBE32ITk8ByteTypeSt4byteEjPKT_ |
78 | | |
79 | | template <ByteType B> |
80 | | inline uint64_t ReadBE64(const B* ptr) |
81 | 26.8G | { |
82 | 26.8G | uint64_t x; |
83 | 26.8G | memcpy(&x, ptr, 8); |
84 | 26.8G | return be64toh_internal(x); |
85 | 26.8G | } |
86 | | |
87 | | template <ByteType B> |
88 | | inline void WriteBE16(B* ptr, uint16_t x) |
89 | 54 | { |
90 | 54 | uint16_t v = htobe16_internal(x); |
91 | 54 | memcpy(ptr, &v, 2); |
92 | 54 | } |
93 | | |
94 | | template <ByteType B> |
95 | | inline void WriteBE32(B* ptr, uint32_t x) |
96 | 610M | { |
97 | 610M | uint32_t v = htobe32_internal(x); |
98 | 610M | memcpy(ptr, &v, 4); |
99 | 610M | } |
100 | | |
101 | | template <ByteType B> |
102 | | inline void WriteBE64(B* ptr, uint64_t x) |
103 | 15.0G | { |
104 | 15.0G | uint64_t v = htobe64_internal(x); |
105 | 15.0G | memcpy(ptr, &v, 8); |
106 | 15.0G | } |
107 | | |
108 | | #endif // BITCOIN_CRYPTO_COMMON_H |