Coverage Report

Created: 2024-11-29 06:10

/src/botan/build/include/internal/botan/internal/simd_avx2_gfni.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* (C) 2024 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6
7
#ifndef BOTAN_SIMD_AVX2_GFNI_H_
8
#define BOTAN_SIMD_AVX2_GFNI_H_
9
10
#include <botan/internal/simd_avx2.h>
11
#include <stdexcept>
12
#include <string_view>
13
14
namespace Botan {
15
16
#define BOTAN_GFNI_ISA "gfni,avx2"
17
18
// Helper for defining GFNI constants
19
consteval uint64_t gfni_matrix(std::string_view s) {
20
   uint64_t matrix = 0;
21
   size_t bit_cnt = 0;
22
   uint8_t row = 0;
23
24
   for(char c : s) {
25
      if(c == ' ' || c == '\n') {
26
         continue;
27
      }
28
      if(c != '0' && c != '1') {
29
         throw std::runtime_error("gfni_matrix: invalid bit value");
30
      }
31
32
      if(c == '1') {
33
         row |= 0x80 >> (7 - bit_cnt % 8);
34
      }
35
      bit_cnt++;
36
37
      if(bit_cnt % 8 == 0) {
38
         matrix <<= 8;
39
         matrix |= row;
40
         row = 0;
41
      }
42
   }
43
44
   if(bit_cnt != 64) {
45
      throw std::runtime_error("gfni_matrix: invalid bit count");
46
   }
47
48
   return matrix;
49
}
50
51
template <uint64_t A, uint8_t B>
52
BOTAN_FUNC_ISA_INLINE(BOTAN_GFNI_ISA)
53
0
SIMD_8x32 gf2p8affine(const SIMD_8x32& x) {
54
0
   return SIMD_8x32(_mm256_gf2p8affine_epi64_epi8(x.raw(), _mm256_set1_epi64x(A), B));
55
0
}
56
57
template <uint64_t A, uint8_t B>
58
BOTAN_FUNC_ISA_INLINE(BOTAN_GFNI_ISA)
59
0
SIMD_8x32 gf2p8affineinv(const SIMD_8x32& x) {
60
0
   return SIMD_8x32(_mm256_gf2p8affineinv_epi64_epi8(x.raw(), _mm256_set1_epi64x(A), B));
61
0
}
62
63
0
BOTAN_FUNC_ISA_INLINE(BOTAN_GFNI_ISA) SIMD_8x32 gf2p8mul(const SIMD_8x32& a, const SIMD_8x32& b) {
64
0
   return SIMD_8x32(_mm256_gf2p8mul_epi8(a.raw(), b.raw()));
65
0
}
66
67
}  // namespace Botan
68
69
#endif