Coverage Report

Created: 2021-06-10 10:30

/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 constexpr T expand_top_bit(T a)
24
9.96G
   {
25
9.96G
   return static_cast<T>(0) - (a >> (sizeof(T)*8-1));
26
9.96G
   }
unsigned long Botan::expand_top_bit<unsigned long>(unsigned long)
Line
Count
Source
24
9.88G
   {
25
9.88G
   return static_cast<T>(0) - (a >> (sizeof(T)*8-1));
26
9.88G
   }
unsigned char Botan::expand_top_bit<unsigned char>(unsigned char)
Line
Count
Source
24
74.0M
   {
25
74.0M
   return static_cast<T>(0) - (a >> (sizeof(T)*8-1));
26
74.0M
   }
unsigned int Botan::expand_top_bit<unsigned int>(unsigned int)
Line
Count
Source
24
971k
   {
25
971k
   return static_cast<T>(0) - (a >> (sizeof(T)*8-1));
26
971k
   }
int Botan::expand_top_bit<int>(int)
Line
Count
Source
24
147
   {
25
147
   return static_cast<T>(0) - (a >> (sizeof(T)*8-1));
26
147
   }
unsigned short Botan::expand_top_bit<unsigned short>(unsigned short)
Line
Count
Source
24
69.7k
   {
25
69.7k
   return static_cast<T>(0) - (a >> (sizeof(T)*8-1));
26
69.7k
   }
27
28
/**
29
* If arg is zero, return ~0. Otherwise return 0
30
*/
31
template<typename T>
32
inline constexpr T ct_is_zero(T x)
33
8.55G
   {
34
8.55G
   return expand_top_bit<T>(~x & (x - 1));
35
8.55G
   }
unsigned long Botan::ct_is_zero<unsigned long>(unsigned long)
Line
Count
Source
33
8.51G
   {
34
8.51G
   return expand_top_bit<T>(~x & (x - 1));
35
8.51G
   }
unsigned char Botan::ct_is_zero<unsigned char>(unsigned char)
Line
Count
Source
33
37.8M
   {
34
37.8M
   return expand_top_bit<T>(~x & (x - 1));
35
37.8M
   }
unsigned int Botan::ct_is_zero<unsigned int>(unsigned int)
Line
Count
Source
33
248k
   {
34
248k
   return expand_top_bit<T>(~x & (x - 1));
35
248k
   }
int Botan::ct_is_zero<int>(int)
Line
Count
Source
33
147
   {
34
147
   return expand_top_bit<T>(~x & (x - 1));
35
147
   }
unsigned short Botan::ct_is_zero<unsigned short>(unsigned short)
Line
Count
Source
33
34.7k
   {
34
34.7k
   return expand_top_bit<T>(~x & (x - 1));
35
34.7k
   }
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
7.06M
   {
45
7.06M
   return (arg != 0) && (arg != 1) && ((arg & static_cast<T>(arg-1)) == 0);
46
7.06M
   }
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 constexpr size_t high_bit(T n)
56
7.75M
   {
57
7.75M
   size_t hb = 0;
58
59
54.2M
   for(size_t s = 8*sizeof(T) / 2; s > 0; s /= 2)
60
46.4M
      {
61
46.4M
      const size_t z = s * ((~ct_is_zero(n >> s)) & 1);
62
46.4M
      hb += z;
63
46.4M
      n >>= z;
64
46.4M
      }
65
66
7.75M
   hb += n;
67
68
7.75M
   return hb;
69
7.75M
   }
unsigned long Botan::high_bit<unsigned long>(unsigned long)
Line
Count
Source
56
7.70M
   {
57
7.70M
   size_t hb = 0;
58
59
53.9M
   for(size_t s = 8*sizeof(T) / 2; s > 0; s /= 2)
60
46.2M
      {
61
46.2M
      const size_t z = s * ((~ct_is_zero(n >> s)) & 1);
62
46.2M
      hb += z;
63
46.2M
      n >>= z;
64
46.2M
      }
65
66
7.70M
   hb += n;
67
68
7.70M
   return hb;
69
7.70M
   }
unsigned long Botan::high_bit<unsigned int>(unsigned int)
Line
Count
Source
56
49.7k
   {
57
49.7k
   size_t hb = 0;
58
59
298k
   for(size_t s = 8*sizeof(T) / 2; s > 0; s /= 2)
60
248k
      {
61
248k
      const size_t z = s * ((~ct_is_zero(n >> s)) & 1);
62
248k
      hb += z;
63
248k
      n >>= z;
64
248k
      }
65
66
49.7k
   hb += n;
67
68
49.7k
   return hb;
69
49.7k
   }
unsigned long Botan::high_bit<unsigned char>(unsigned char)
Line
Count
Source
56
49
   {
57
49
   size_t hb = 0;
58
59
196
   for(size_t s = 8*sizeof(T) / 2; s > 0; s /= 2)
60
147
      {
61
147
      const size_t z = s * ((~ct_is_zero(n >> s)) & 1);
62
147
      hb += z;
63
147
      n >>= z;
64
147
      }
65
66
49
   hb += n;
67
68
49
   return hb;
69
49
   }
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 constexpr size_t significant_bytes(T n)
78
47.7k
   {
79
47.7k
   size_t b = 0;
80
81
190k
   for(size_t s = 8*sizeof(n) / 2; s >= 8; s /= 2)
82
143k
      {
83
143k
      const size_t z = s * (~ct_is_zero(n >> s) & 1);
84
143k
      b += z/8;
85
143k
      n >>= z;
86
143k
      }
87
88
47.7k
   b += (n != 0);
89
90
47.7k
   return b;
91
47.7k
   }
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 constexpr size_t ctz(T n)
100
61.9M
   {
101
   /*
102
   * If n == 0 then this function will compute 8*sizeof(T)-1, so
103
   * initialize lb to 1 if n == 0 to produce the expected result.
104
   */
105
61.9M
   size_t lb = ct_is_zero(n) & 1;
106
107
433M
   for(size_t s = 8*sizeof(T) / 2; s > 0; s /= 2)
108
371M
      {
109
371M
      const T mask = (static_cast<T>(1) << s) - 1;
110
371M
      const size_t z = s * (ct_is_zero(n & mask) & 1);
111
371M
      lb += z;
112
371M
      n >>= z;
113
371M
      }
114
115
61.9M
   return lb;
116
61.9M
   }
unsigned long Botan::ctz<unsigned long>(unsigned long)
Line
Count
Source
100
61.9M
   {
101
   /*
102
   * If n == 0 then this function will compute 8*sizeof(T)-1, so
103
   * initialize lb to 1 if n == 0 to produce the expected result.
104
   */
105
61.9M
   size_t lb = ct_is_zero(n) & 1;
106
107
433M
   for(size_t s = 8*sizeof(T) / 2; s > 0; s /= 2)
108
371M
      {
109
371M
      const T mask = (static_cast<T>(1) << s) - 1;
110
371M
      const size_t z = s * (ct_is_zero(n & mask) & 1);
111
371M
      lb += z;
112
371M
      n >>= z;
113
371M
      }
114
115
61.9M
   return lb;
116
61.9M
   }
Unexecuted instantiation: unsigned long Botan::ctz<unsigned int>(unsigned int)
117
118
template<typename T>
119
constexpr uint8_t ceil_log2(T x)
120
136k
   {
121
136k
   static_assert(sizeof(T) < 32, "Abnormally large scalar");
122
123
136k
   if(x >> (sizeof(T)*8-1))
124
0
      return sizeof(T)*8;
125
126
136k
   uint8_t result = 0;
127
136k
   T compare = 1;
128
129
957k
   while(compare < x)
130
820k
      {
131
820k
      compare <<= 1;
132
820k
      result++;
133
820k
      }
134
135
136k
   return result;
136
136k
   }
137
138
// Potentially variable time ctz used for OCB
139
inline constexpr size_t var_ctz32(uint32_t n)
140
925
   {
141
925
#if defined(BOTAN_BUILD_COMPILER_IS_GCC) || defined(BOTAN_BUILD_COMPILER_IS_CLANG)
142
925
   if(n == 0)
143
0
      return 32;
144
925
   return __builtin_ctz(n);
145
#else
146
   return ctz<uint32_t>(n);
147
#endif
148
925
   }
149
150
template<typename T>
151
inline constexpr T bit_permute_step(T x, T mask, size_t shift)
152
0
   {
153
   /*
154
   See https://reflectionsonsecurity.wordpress.com/2014/05/11/efficient-bit-permutation-using-delta-swaps/
155
   and http://programming.sirrida.de/bit_perm.html
156
   */
157
0
   const T swap = ((x >> shift) ^ x) & mask;
158
0
   return (x ^ swap) ^ (swap << shift);
159
0
   }
160
161
template<typename T>
162
inline constexpr void swap_bits(T& x, T& y, T mask, size_t shift)
163
0
   {
164
0
   const T swap = ((x >> shift) ^ y) & mask;
165
0
   x ^= swap << shift;
166
0
   y ^= swap;
167
0
   }
168
169
template<typename T>
170
inline constexpr T choose(T mask, T a, T b)
171
11.2G
   {
172
   //return (mask & a) | (~mask & b);
173
11.2G
   return (b ^ (mask & (a ^ b)));
174
11.2G
   }
unsigned long Botan::choose<unsigned long>(unsigned long, unsigned long, unsigned long)
Line
Count
Source
171
11.0G
   {
172
   //return (mask & a) | (~mask & b);
173
11.0G
   return (b ^ (mask & (a ^ b)));
174
11.0G
   }
unsigned char Botan::choose<unsigned char>(unsigned char, unsigned char, unsigned char)
Line
Count
Source
171
72.6M
   {
172
   //return (mask & a) | (~mask & b);
173
72.6M
   return (b ^ (mask & (a ^ b)));
174
72.6M
   }
unsigned int Botan::choose<unsigned int>(unsigned int, unsigned int, unsigned int)
Line
Count
Source
171
128M
   {
172
   //return (mask & a) | (~mask & b);
173
128M
   return (b ^ (mask & (a ^ b)));
174
128M
   }
Unexecuted instantiation: unsigned short Botan::choose<unsigned short>(unsigned short, unsigned short, unsigned short)
175
176
template<typename T>
177
inline constexpr T majority(T a, T b, T c)
178
70.9M
   {
179
   /*
180
   Considering each bit of a, b, c individually
181
182
   If a xor b is set, then c is the deciding vote.
183
184
   If a xor b is not set then either a and b are both set or both unset.
185
   In either case the value of c doesn't matter, and examining b (or a)
186
   allows us to determine which case we are in.
187
   */
188
70.9M
   return choose(a ^ b, c, b);
189
70.9M
   }
unsigned int Botan::majority<unsigned int>(unsigned int, unsigned int, unsigned int)
Line
Count
Source
178
63.8M
   {
179
   /*
180
   Considering each bit of a, b, c individually
181
182
   If a xor b is set, then c is the deciding vote.
183
184
   If a xor b is not set then either a and b are both set or both unset.
185
   In either case the value of c doesn't matter, and examining b (or a)
186
   allows us to determine which case we are in.
187
   */
188
63.8M
   return choose(a ^ b, c, b);
189
63.8M
   }
unsigned long Botan::majority<unsigned long>(unsigned long, unsigned long, unsigned long)
Line
Count
Source
178
7.09M
   {
179
   /*
180
   Considering each bit of a, b, c individually
181
182
   If a xor b is set, then c is the deciding vote.
183
184
   If a xor b is not set then either a and b are both set or both unset.
185
   In either case the value of c doesn't matter, and examining b (or a)
186
   allows us to determine which case we are in.
187
   */
188
7.09M
   return choose(a ^ b, c, b);
189
7.09M
   }
190
191
}
192
193
#endif