/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 | 6.93k | { |
21 | 6.93k | uint16_t x; |
22 | 6.93k | memcpy(&x, ptr, 2); |
23 | 6.93k | return le16toh_internal(x); |
24 | 6.93k | } |
25 | | |
26 | | template <ByteType B> |
27 | | inline uint32_t ReadLE32(const B* ptr) |
28 | 1.49G | { |
29 | 1.49G | uint32_t x; |
30 | 1.49G | memcpy(&x, ptr, 4); |
31 | 1.49G | return le32toh_internal(x); |
32 | 1.49G | } _Z8ReadLE32ITk8ByteTypehEjPKT_ Line | Count | Source | 28 | 1.10G | { | 29 | 1.10G | uint32_t x; | 30 | 1.10G | memcpy(&x, ptr, 4); | 31 | 1.10G | return le32toh_internal(x); | 32 | 1.10G | } |
_Z8ReadLE32ITk8ByteTypeSt4byteEjPKT_ Line | Count | Source | 28 | 388M | { | 29 | 388M | uint32_t x; | 30 | 388M | memcpy(&x, ptr, 4); | 31 | 388M | return le32toh_internal(x); | 32 | 388M | } |
|
33 | | |
34 | | template <ByteType B> |
35 | | inline uint64_t ReadLE64(const B* ptr) |
36 | 386M | { |
37 | 386M | uint64_t x; |
38 | 386M | memcpy(&x, ptr, 8); |
39 | 386M | return le64toh_internal(x); |
40 | 386M | } _Z8ReadLE64ITk8ByteTypehEmPKT_ Line | Count | Source | 36 | 327M | { | 37 | 327M | uint64_t x; | 38 | 327M | memcpy(&x, ptr, 8); | 39 | 327M | return le64toh_internal(x); | 40 | 327M | } |
_Z8ReadLE64ITk8ByteTypeSt4byteEmPKT_ Line | Count | Source | 36 | 58.8M | { | 37 | 58.8M | uint64_t x; | 38 | 58.8M | memcpy(&x, ptr, 8); | 39 | 58.8M | return le64toh_internal(x); | 40 | 58.8M | } |
|
41 | | |
42 | | template <ByteType B> |
43 | | inline void WriteLE16(B* ptr, uint16_t x) |
44 | 56 | { |
45 | 56 | uint16_t v = htole16_internal(x); |
46 | 56 | memcpy(ptr, &v, 2); |
47 | 56 | } |
48 | | |
49 | | template <ByteType B> |
50 | | inline void WriteLE32(B* ptr, uint32_t x) |
51 | 682M | { |
52 | 682M | uint32_t v = htole32_internal(x); |
53 | 682M | memcpy(ptr, &v, 4); |
54 | 682M | } _Z9WriteLE32ITk8ByteTypehEvPT_j Line | Count | Source | 51 | 27.6M | { | 52 | 27.6M | uint32_t v = htole32_internal(x); | 53 | 27.6M | memcpy(ptr, &v, 4); | 54 | 27.6M | } |
_Z9WriteLE32ITk8ByteTypeSt4byteEvPT_j Line | Count | Source | 51 | 654M | { | 52 | 654M | uint32_t v = htole32_internal(x); | 53 | 654M | memcpy(ptr, &v, 4); | 54 | 654M | } |
|
55 | | |
56 | | template <ByteType B> |
57 | | inline void WriteLE64(B* ptr, uint64_t x) |
58 | 5.47M | { |
59 | 5.47M | uint64_t v = htole64_internal(x); |
60 | 5.47M | memcpy(ptr, &v, 8); |
61 | 5.47M | } Unexecuted instantiation: _Z9WriteLE64ITk8ByteTypeSt4byteEvPT_m _Z9WriteLE64ITk8ByteTypehEvPT_m Line | Count | Source | 58 | 5.47M | { | 59 | 5.47M | uint64_t v = htole64_internal(x); | 60 | 5.47M | memcpy(ptr, &v, 8); | 61 | 5.47M | } |
|
62 | | |
63 | | template <ByteType B> |
64 | | inline uint16_t ReadBE16(const B* ptr) |
65 | 222k | { |
66 | 222k | uint16_t x; |
67 | 222k | memcpy(&x, ptr, 2); |
68 | 222k | return be16toh_internal(x); |
69 | 222k | } |
70 | | |
71 | | template <ByteType B> |
72 | | inline uint32_t ReadBE32(const B* ptr) |
73 | 16.3M | { |
74 | 16.3M | uint32_t x; |
75 | 16.3M | memcpy(&x, ptr, 4); |
76 | 16.3M | return be32toh_internal(x); |
77 | 16.3M | } _Z8ReadBE32ITk8ByteTypehEjPKT_ Line | Count | Source | 73 | 16.3M | { | 74 | 16.3M | uint32_t x; | 75 | 16.3M | memcpy(&x, ptr, 4); | 76 | 16.3M | return be32toh_internal(x); | 77 | 16.3M | } |
Unexecuted instantiation: _Z8ReadBE32ITk8ByteTypeSt4byteEjPKT_ |
78 | | |
79 | | template <ByteType B> |
80 | | inline uint64_t ReadBE64(const B* ptr) |
81 | 91.8G | { |
82 | 91.8G | uint64_t x; |
83 | 91.8G | memcpy(&x, ptr, 8); |
84 | 91.8G | return be64toh_internal(x); |
85 | 91.8G | } |
86 | | |
87 | | template <ByteType B> |
88 | | inline void WriteBE16(B* ptr, uint16_t x) |
89 | 0 | { |
90 | 0 | uint16_t v = htobe16_internal(x); |
91 | 0 | memcpy(ptr, &v, 2); |
92 | 0 | } |
93 | | |
94 | | template <ByteType B> |
95 | | inline void WriteBE32(B* ptr, uint32_t x) |
96 | 1.30G | { |
97 | 1.30G | uint32_t v = htobe32_internal(x); |
98 | 1.30G | memcpy(ptr, &v, 4); |
99 | 1.30G | } |
100 | | |
101 | | template <ByteType B> |
102 | | inline void WriteBE64(B* ptr, uint64_t x) |
103 | 51.7G | { |
104 | 51.7G | uint64_t v = htobe64_internal(x); |
105 | 51.7G | memcpy(ptr, &v, 8); |
106 | 51.7G | } |
107 | | |
108 | | #endif // BITCOIN_CRYPTO_COMMON_H |