/src/botan/build/include/botan/internal/bit_ops.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Bit/Word Operations |
3 | | * (C) 1999-2008 Jack Lloyd |
4 | | * (C) Copyright Projet SECRET, INRIA, Rocquencourt |
5 | | * (C) Bhaskar Biswas and Nicolas Sendrier |
6 | | * (C) 2014 cryptosource GmbH |
7 | | * (C) 2014 Falko Strenzke fstrenzke@cryptosource.de |
8 | | * |
9 | | * Botan is released under the Simplified BSD License (see license.txt) |
10 | | */ |
11 | | |
12 | | #ifndef BOTAN_BIT_OPS_H_ |
13 | | #define BOTAN_BIT_OPS_H_ |
14 | | |
15 | | #include <botan/types.h> |
16 | | |
17 | | namespace Botan { |
18 | | |
19 | | /** |
20 | | * If top bit of arg is set, return ~0. Otherwise return 0. |
21 | | */ |
22 | | template<typename T> |
23 | | inline T expand_top_bit(T a) |
24 | 9.40G | { |
25 | 9.40G | return static_cast<T>(0) - (a >> (sizeof(T)*8-1)); |
26 | 9.40G | } unsigned long Botan::expand_top_bit<unsigned long>(unsigned long) Line | Count | Source | 24 | 9.38G | { | 25 | 9.38G | return static_cast<T>(0) - (a >> (sizeof(T)*8-1)); | 26 | 9.38G | } |
unsigned char Botan::expand_top_bit<unsigned char>(unsigned char) Line | Count | Source | 24 | 21.2M | { | 25 | 21.2M | return static_cast<T>(0) - (a >> (sizeof(T)*8-1)); | 26 | 21.2M | } |
unsigned int Botan::expand_top_bit<unsigned int>(unsigned int) Line | Count | Source | 24 | 992k | { | 25 | 992k | return static_cast<T>(0) - (a >> (sizeof(T)*8-1)); | 26 | 992k | } |
int Botan::expand_top_bit<int>(int) Line | Count | Source | 24 | 129 | { | 25 | 129 | return static_cast<T>(0) - (a >> (sizeof(T)*8-1)); | 26 | 129 | } |
unsigned short Botan::expand_top_bit<unsigned short>(unsigned short) Line | Count | Source | 24 | 160k | { | 25 | 160k | return static_cast<T>(0) - (a >> (sizeof(T)*8-1)); | 26 | 160k | } |
|
27 | | |
28 | | /** |
29 | | * If arg is zero, return ~0. Otherwise return 0 |
30 | | */ |
31 | | template<typename T> |
32 | | inline T ct_is_zero(T x) |
33 | 8.19G | { |
34 | 8.19G | return expand_top_bit<T>(~x & (x - 1)); |
35 | 8.19G | } unsigned long Botan::ct_is_zero<unsigned long>(unsigned long) Line | Count | Source | 33 | 8.17G | { | 34 | 8.17G | return expand_top_bit<T>(~x & (x - 1)); | 35 | 8.17G | } |
unsigned char Botan::ct_is_zero<unsigned char>(unsigned char) Line | Count | Source | 33 | 21.2M | { | 34 | 21.2M | return expand_top_bit<T>(~x & (x - 1)); | 35 | 21.2M | } |
unsigned int Botan::ct_is_zero<unsigned int>(unsigned int) Line | Count | Source | 33 | 296k | { | 34 | 296k | return expand_top_bit<T>(~x & (x - 1)); | 35 | 296k | } |
int Botan::ct_is_zero<int>(int) Line | Count | Source | 33 | 129 | { | 34 | 129 | return expand_top_bit<T>(~x & (x - 1)); | 35 | 129 | } |
unsigned short Botan::ct_is_zero<unsigned short>(unsigned short) Line | Count | Source | 33 | 80.3k | { | 34 | 80.3k | return expand_top_bit<T>(~x & (x - 1)); | 35 | 80.3k | } |
|
36 | | |
37 | | /** |
38 | | * Power of 2 test. T should be an unsigned integer type |
39 | | * @param arg an integer value |
40 | | * @return true iff arg is 2^n for some n > 0 |
41 | | */ |
42 | | template<typename T> |
43 | | inline constexpr bool is_power_of_2(T arg) |
44 | 8.76M | { |
45 | 8.76M | return (arg != 0) && (arg != 1) && ((arg & static_cast<T>(arg-1)) == 0); |
46 | 8.76M | } |
47 | | |
48 | | /** |
49 | | * Return the index of the highest set bit |
50 | | * T is an unsigned integer type |
51 | | * @param n an integer value |
52 | | * @return index of the highest set bit in n |
53 | | */ |
54 | | template<typename T> |
55 | | inline size_t high_bit(T n) |
56 | 11.0M | { |
57 | 11.0M | size_t hb = 0; |
58 | 11.0M | |
59 | 77.1M | for(size_t s = 8*sizeof(T) / 2; s > 0; s /= 2) |
60 | 66.1M | { |
61 | 66.1M | const size_t z = s * ((~ct_is_zero(n >> s)) & 1); |
62 | 66.1M | hb += z; |
63 | 66.1M | n >>= z; |
64 | 66.1M | } |
65 | 11.0M | |
66 | 11.0M | hb += n; |
67 | 11.0M | |
68 | 11.0M | return hb; |
69 | 11.0M | } unsigned long Botan::high_bit<unsigned long>(unsigned long) Line | Count | Source | 56 | 10.9M | { | 57 | 10.9M | size_t hb = 0; | 58 | 10.9M | | 59 | 76.8M | for(size_t s = 8*sizeof(T) / 2; s > 0; s /= 2) | 60 | 65.8M | { | 61 | 65.8M | const size_t z = s * ((~ct_is_zero(n >> s)) & 1); | 62 | 65.8M | hb += z; | 63 | 65.8M | n >>= z; | 64 | 65.8M | } | 65 | 10.9M | | 66 | 10.9M | hb += n; | 67 | 10.9M | | 68 | 10.9M | return hb; | 69 | 10.9M | } |
unsigned long Botan::high_bit<unsigned int>(unsigned int) Line | Count | Source | 56 | 59.2k | { | 57 | 59.2k | size_t hb = 0; | 58 | 59.2k | | 59 | 355k | for(size_t s = 8*sizeof(T) / 2; s > 0; s /= 2) | 60 | 296k | { | 61 | 296k | const size_t z = s * ((~ct_is_zero(n >> s)) & 1); | 62 | 296k | hb += z; | 63 | 296k | n >>= z; | 64 | 296k | } | 65 | 59.2k | | 66 | 59.2k | hb += n; | 67 | 59.2k | | 68 | 59.2k | return hb; | 69 | 59.2k | } |
unsigned long Botan::high_bit<unsigned char>(unsigned char) Line | Count | Source | 56 | 43 | { | 57 | 43 | size_t hb = 0; | 58 | 43 | | 59 | 172 | for(size_t s = 8*sizeof(T) / 2; s > 0; s /= 2) | 60 | 129 | { | 61 | 129 | const size_t z = s * ((~ct_is_zero(n >> s)) & 1); | 62 | 129 | hb += z; | 63 | 129 | n >>= z; | 64 | 129 | } | 65 | 43 | | 66 | 43 | hb += n; | 67 | 43 | | 68 | 43 | return hb; | 69 | 43 | } |
Unexecuted instantiation: unsigned long Botan::high_bit<unsigned short>(unsigned short) |
70 | | |
71 | | /** |
72 | | * Return the number of significant bytes in n |
73 | | * @param n an integer value |
74 | | * @return number of significant bytes in n |
75 | | */ |
76 | | template<typename T> |
77 | | inline size_t significant_bytes(T n) |
78 | 49.6k | { |
79 | 49.6k | size_t b = 0; |
80 | 49.6k | |
81 | 198k | for(size_t s = 8*sizeof(n) / 2; s >= 8; s /= 2) |
82 | 148k | { |
83 | 148k | const size_t z = s * (~ct_is_zero(n >> s) & 1); |
84 | 148k | b += z/8; |
85 | 148k | n >>= z; |
86 | 148k | } |
87 | 49.6k | |
88 | 49.6k | b += (n != 0); |
89 | 49.6k | |
90 | 49.6k | return b; |
91 | 49.6k | } |
92 | | |
93 | | /** |
94 | | * Count the trailing zero bits in n |
95 | | * @param n an integer value |
96 | | * @return maximum x st 2^x divides n |
97 | | */ |
98 | | template<typename T> |
99 | | inline size_t ctz(T n) |
100 | 3.27M | { |
101 | 3.27M | /* |
102 | 3.27M | * If n == 0 then this function will compute 8*sizeof(T)-1, so |
103 | 3.27M | * initialize lb to 1 if n == 0 to produce the expected result. |
104 | 3.27M | */ |
105 | 3.27M | size_t lb = ct_is_zero(n) & 1; |
106 | 3.27M | |
107 | 22.9M | for(size_t s = 8*sizeof(T) / 2; s > 0; s /= 2) |
108 | 19.6M | { |
109 | 19.6M | const T mask = (static_cast<T>(1) << s) - 1; |
110 | 19.6M | const size_t z = s * (ct_is_zero(n & mask) & 1); |
111 | 19.6M | lb += z; |
112 | 19.6M | n >>= z; |
113 | 19.6M | } |
114 | 3.27M | |
115 | 3.27M | return lb; |
116 | 3.27M | } unsigned long Botan::ctz<unsigned long>(unsigned long) Line | Count | Source | 100 | 3.27M | { | 101 | 3.27M | /* | 102 | 3.27M | * If n == 0 then this function will compute 8*sizeof(T)-1, so | 103 | 3.27M | * initialize lb to 1 if n == 0 to produce the expected result. | 104 | 3.27M | */ | 105 | 3.27M | size_t lb = ct_is_zero(n) & 1; | 106 | 3.27M | | 107 | 22.9M | for(size_t s = 8*sizeof(T) / 2; s > 0; s /= 2) | 108 | 19.6M | { | 109 | 19.6M | const T mask = (static_cast<T>(1) << s) - 1; | 110 | 19.6M | const size_t z = s * (ct_is_zero(n & mask) & 1); | 111 | 19.6M | lb += z; | 112 | 19.6M | n >>= z; | 113 | 19.6M | } | 114 | 3.27M | | 115 | 3.27M | return lb; | 116 | 3.27M | } |
Unexecuted instantiation: unsigned long Botan::ctz<unsigned int>(unsigned int) |
117 | | |
118 | | template<typename T> |
119 | | uint8_t ceil_log2(T x) |
120 | 134k | { |
121 | 134k | static_assert(sizeof(T) < 32, "Abnormally large scalar"); |
122 | 134k | |
123 | 134k | if(x >> (sizeof(T)*8-1)) |
124 | 0 | return sizeof(T)*8; |
125 | 134k | |
126 | 134k | uint8_t result = 0; |
127 | 134k | T compare = 1; |
128 | 134k | |
129 | 953k | while(compare < x) |
130 | 818k | { |
131 | 818k | compare <<= 1; |
132 | 818k | result++; |
133 | 818k | } |
134 | 134k | |
135 | 134k | return result; |
136 | 134k | } |
137 | | |
138 | | // Potentially variable time ctz used for OCB |
139 | | inline size_t var_ctz32(uint32_t n) |
140 | 4.84k | { |
141 | 4.84k | #if defined(BOTAN_BUILD_COMPILER_IS_GCC) || defined(BOTAN_BUILD_COMPILER_IS_CLANG) |
142 | 4.84k | if(n == 0) |
143 | 0 | return 32; |
144 | 4.84k | return __builtin_ctz(n); |
145 | | #else |
146 | | return ctz<uint32_t>(n); |
147 | | #endif |
148 | | } |
149 | | |
150 | | } |
151 | | |
152 | | #endif |