Coverage Report

Created: 2022-05-14 06:06

/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
13.0G
   {
25
13.0G
   return static_cast<T>(0) - (a >> (sizeof(T)*8-1));
26
13.0G
   }
unsigned long Botan::expand_top_bit<unsigned long>(unsigned long)
Line
Count
Source
24
12.9G
   {
25
12.9G
   return static_cast<T>(0) - (a >> (sizeof(T)*8-1));
26
12.9G
   }
unsigned char Botan::expand_top_bit<unsigned char>(unsigned char)
Line
Count
Source
24
75.6M
   {
25
75.6M
   return static_cast<T>(0) - (a >> (sizeof(T)*8-1));
26
75.6M
   }
unsigned int Botan::expand_top_bit<unsigned int>(unsigned int)
Line
Count
Source
24
278k
   {
25
278k
   return static_cast<T>(0) - (a >> (sizeof(T)*8-1));
26
278k
   }
unsigned short Botan::expand_top_bit<unsigned short>(unsigned short)
Line
Count
Source
24
37.9k
   {
25
37.9k
   return static_cast<T>(0) - (a >> (sizeof(T)*8-1));
26
37.9k
   }
int Botan::expand_top_bit<int>(int)
Line
Count
Source
24
150
   {
25
150
   return static_cast<T>(0) - (a >> (sizeof(T)*8-1));
26
150
   }
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
10.6G
   {
34
10.6G
   return expand_top_bit<T>(~x & (x - 1));
35
10.6G
   }
unsigned long Botan::ct_is_zero<unsigned long>(unsigned long)
Line
Count
Source
33
10.6G
   {
34
10.6G
   return expand_top_bit<T>(~x & (x - 1));
35
10.6G
   }
unsigned char Botan::ct_is_zero<unsigned char>(unsigned char)
Line
Count
Source
33
41.9M
   {
34
41.9M
   return expand_top_bit<T>(~x & (x - 1));
35
41.9M
   }
unsigned int Botan::ct_is_zero<unsigned int>(unsigned int)
Line
Count
Source
33
278k
   {
34
278k
   return expand_top_bit<T>(~x & (x - 1));
35
278k
   }
unsigned short Botan::ct_is_zero<unsigned short>(unsigned short)
Line
Count
Source
33
19.0k
   {
34
19.0k
   return expand_top_bit<T>(~x & (x - 1));
35
19.0k
   }
int Botan::ct_is_zero<int>(int)
Line
Count
Source
33
150
   {
34
150
   return expand_top_bit<T>(~x & (x - 1));
35
150
   }
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.89M
   {
45
8.89M
   return (arg != 0) && (arg != 1) && ((arg & static_cast<T>(arg-1)) == 0);
46
8.89M
   }
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
13.0M
   {
57
13.0M
   size_t hb = 0;
58
59
91.0M
   for(size_t s = 8*sizeof(T) / 2; s > 0; s /= 2)
60
78.0M
      {
61
78.0M
      const size_t z = s * ((~ct_is_zero(n >> s)) & 1);
62
78.0M
      hb += z;
63
78.0M
      n >>= z;
64
78.0M
      }
65
66
13.0M
   hb += n;
67
68
13.0M
   return hb;
69
13.0M
   }
unsigned long Botan::high_bit<unsigned long>(unsigned long)
Line
Count
Source
56
12.9M
   {
57
12.9M
   size_t hb = 0;
58
59
90.7M
   for(size_t s = 8*sizeof(T) / 2; s > 0; s /= 2)
60
77.7M
      {
61
77.7M
      const size_t z = s * ((~ct_is_zero(n >> s)) & 1);
62
77.7M
      hb += z;
63
77.7M
      n >>= z;
64
77.7M
      }
65
66
12.9M
   hb += n;
67
68
12.9M
   return hb;
69
12.9M
   }
unsigned long Botan::high_bit<unsigned int>(unsigned int)
Line
Count
Source
56
55.7k
   {
57
55.7k
   size_t hb = 0;
58
59
334k
   for(size_t s = 8*sizeof(T) / 2; s > 0; s /= 2)
60
278k
      {
61
278k
      const size_t z = s * ((~ct_is_zero(n >> s)) & 1);
62
278k
      hb += z;
63
278k
      n >>= z;
64
278k
      }
65
66
55.7k
   hb += n;
67
68
55.7k
   return hb;
69
55.7k
   }
unsigned long Botan::high_bit<unsigned char>(unsigned char)
Line
Count
Source
56
50
   {
57
50
   size_t hb = 0;
58
59
200
   for(size_t s = 8*sizeof(T) / 2; s > 0; s /= 2)
60
150
      {
61
150
      const size_t z = s * ((~ct_is_zero(n >> s)) & 1);
62
150
      hb += z;
63
150
      n >>= z;
64
150
      }
65
66
50
   hb += n;
67
68
50
   return hb;
69
50
   }
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
49.7k
   {
79
49.7k
   size_t b = 0;
80
81
199k
   for(size_t s = 8*sizeof(n) / 2; s >= 8; s /= 2)
82
149k
      {
83
149k
      const size_t z = s * (~ct_is_zero(n >> s) & 1);
84
149k
      b += z/8;
85
149k
      n >>= z;
86
149k
      }
87
88
49.7k
   b += (n != 0);
89
90
49.7k
   return b;
91
49.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
73.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
73.9M
   size_t lb = ct_is_zero(n) & 1;
106
107
517M
   for(size_t s = 8*sizeof(T) / 2; s > 0; s /= 2)
108
443M
      {
109
443M
      const T mask = (static_cast<T>(1) << s) - 1;
110
443M
      const size_t z = s * (ct_is_zero(n & mask) & 1);
111
443M
      lb += z;
112
443M
      n >>= z;
113
443M
      }
114
115
73.9M
   return lb;
116
73.9M
   }
unsigned long Botan::ctz<unsigned long>(unsigned long)
Line
Count
Source
100
73.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
73.9M
   size_t lb = ct_is_zero(n) & 1;
106
107
517M
   for(size_t s = 8*sizeof(T) / 2; s > 0; s /= 2)
108
443M
      {
109
443M
      const T mask = (static_cast<T>(1) << s) - 1;
110
443M
      const size_t z = s * (ct_is_zero(n & mask) & 1);
111
443M
      lb += z;
112
443M
      n >>= z;
113
443M
      }
114
115
73.9M
   return lb;
116
73.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
114k
   {
121
114k
   static_assert(sizeof(T) < 32, "Abnormally large scalar");
122
123
114k
   if(x >> (sizeof(T)*8-1))
124
0
      return sizeof(T)*8;
125
126
114k
   uint8_t result = 0;
127
114k
   T compare = 1;
128
129
805k
   while(compare < x)
130
691k
      {
131
691k
      compare <<= 1;
132
691k
      result++;
133
691k
      }
134
135
114k
   return result;
136
114k
   }
137
138
// Potentially variable time ctz used for OCB
139
inline constexpr size_t var_ctz32(uint32_t n)
140
1.17k
   {
141
1.17k
#if defined(BOTAN_BUILD_COMPILER_IS_GCC) || defined(BOTAN_BUILD_COMPILER_IS_CLANG)
142
1.17k
   if(n == 0)
143
0
      return 32;
144
1.17k
   return __builtin_ctz(n);
145
#else
146
   return ctz<uint32_t>(n);
147
#endif
148
1.17k
   }
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
14.8G
   {
172
   //return (mask & a) | (~mask & b);
173
14.8G
   return (b ^ (mask & (a ^ b)));
174
14.8G
   }
unsigned long Botan::choose<unsigned long>(unsigned long, unsigned long, unsigned long)
Line
Count
Source
171
14.6G
   {
172
   //return (mask & a) | (~mask & b);
173
14.6G
   return (b ^ (mask & (a ^ b)));
174
14.6G
   }
unsigned char Botan::choose<unsigned char>(unsigned char, unsigned char, unsigned char)
Line
Count
Source
171
74.4M
   {
172
   //return (mask & a) | (~mask & b);
173
74.4M
   return (b ^ (mask & (a ^ b)));
174
74.4M
   }
Unexecuted instantiation: unsigned short Botan::choose<unsigned short>(unsigned short, unsigned short, unsigned short)
unsigned int Botan::choose<unsigned int>(unsigned int, unsigned int, unsigned int)
Line
Count
Source
171
131M
   {
172
   //return (mask & a) | (~mask & b);
173
131M
   return (b ^ (mask & (a ^ b)));
174
131M
   }
175
176
template<typename T>
177
inline constexpr T majority(T a, T b, T c)
178
78.7M
   {
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
78.7M
   return choose(a ^ b, c, b);
189
78.7M
   }
unsigned int Botan::majority<unsigned int>(unsigned int, unsigned int, unsigned int)
Line
Count
Source
178
65.5M
   {
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
65.5M
   return choose(a ^ b, c, b);
189
65.5M
   }
unsigned long Botan::majority<unsigned long>(unsigned long, unsigned long, unsigned long)
Line
Count
Source
178
13.1M
   {
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
13.1M
   return choose(a ^ b, c, b);
189
13.1M
   }
190
191
}
192
193
#endif