@@ -62,6 +62,22 @@ enum class FieldImpl {
62
62
#endif
63
63
};
64
64
65
+ static inline bool EnableClmul ()
66
+ {
67
+ #ifdef HAVE_CLMUL
68
+ #ifdef _MSC_VER
69
+ int regs[4 ];
70
+ __cpuid (regs, 1 );
71
+ return (regs[2 ] & 0x2 );
72
+ #else
73
+ uint32_t eax, ebx, ecx, edx;
74
+ return (__get_cpuid (1 , &eax, &ebx, &ecx, &edx) && (ecx & 0x2 ));
75
+ #endif
76
+ #else
77
+ return false ;
78
+ #endif
79
+ }
80
+
65
81
Sketch* Construct (int bits, int impl)
66
82
{
67
83
switch (FieldImpl (impl)) {
@@ -86,47 +102,56 @@ Sketch* Construct(int bits, int impl)
86
102
default :
87
103
return nullptr ;
88
104
}
105
+ break ;
89
106
#ifdef HAVE_CLMUL
90
107
case FieldImpl::CLMUL:
91
- case FieldImpl::CLMUL_TRI: {
92
- #ifdef _MSC_VER
93
- int regs[4 ];
94
- __cpuid (regs, 1 );
95
- if (regs[2 ] & 0x2 ) {
96
- #else
97
- uint32_t eax, ebx, ecx, edx;
98
- if (__get_cpuid (1 , &eax, &ebx, &ecx, &edx) && (ecx & 0x2 )) {
99
- #endif
108
+ if (EnableClmul ()) {
100
109
switch ((bits + 7 ) / 8 ) {
101
110
case 1 :
102
- if (FieldImpl (impl) == FieldImpl::CLMUL) return ConstructClMul1Byte (bits, impl);
103
- if (FieldImpl (impl) == FieldImpl::CLMUL_TRI) return ConstructClMulTri1Byte (bits, impl);
111
+ return ConstructClMul1Byte (bits, impl);
104
112
case 2 :
105
- if (FieldImpl (impl) == FieldImpl::CLMUL) return ConstructClMul2Bytes (bits, impl);
106
- if (FieldImpl (impl) == FieldImpl::CLMUL_TRI) return ConstructClMulTri2Bytes (bits, impl);
113
+ return ConstructClMul2Bytes (bits, impl);
107
114
case 3 :
108
- if (FieldImpl (impl) == FieldImpl::CLMUL) return ConstructClMul3Bytes (bits, impl);
109
- if (FieldImpl (impl) == FieldImpl::CLMUL_TRI) return ConstructClMulTri3Bytes (bits, impl);
115
+ return ConstructClMul3Bytes (bits, impl);
110
116
case 4 :
111
- if (FieldImpl (impl) == FieldImpl::CLMUL) return ConstructClMul4Bytes (bits, impl);
112
- if (FieldImpl (impl) == FieldImpl::CLMUL_TRI) return ConstructClMulTri4Bytes (bits, impl);
117
+ return ConstructClMul4Bytes (bits, impl);
113
118
case 5 :
114
- if (FieldImpl (impl) == FieldImpl::CLMUL) return ConstructClMul5Bytes (bits, impl);
115
- if (FieldImpl (impl) == FieldImpl::CLMUL_TRI) return ConstructClMulTri5Bytes (bits, impl);
119
+ return ConstructClMul5Bytes (bits, impl);
116
120
case 6 :
117
- if (FieldImpl (impl) == FieldImpl::CLMUL) return ConstructClMul6Bytes (bits, impl);
118
- if (FieldImpl (impl) == FieldImpl::CLMUL_TRI) return ConstructClMulTri6Bytes (bits, impl);
121
+ return ConstructClMul6Bytes (bits, impl);
119
122
case 7 :
120
- if (FieldImpl (impl) == FieldImpl::CLMUL) return ConstructClMul7Bytes (bits, impl);
121
- if (FieldImpl (impl) == FieldImpl::CLMUL_TRI) return ConstructClMulTri7Bytes (bits, impl);
123
+ return ConstructClMul7Bytes (bits, impl);
122
124
case 8 :
123
- if (FieldImpl (impl) == FieldImpl::CLMUL) return ConstructClMul8Bytes (bits, impl);
124
- if (FieldImpl (impl) == FieldImpl::CLMUL_TRI) return ConstructClMulTri8Bytes (bits, impl);
125
+ return ConstructClMul8Bytes (bits, impl);
125
126
default :
126
127
return nullptr ;
127
128
}
128
129
}
129
- }
130
+ break ;
131
+ case FieldImpl::CLMUL_TRI:
132
+ if (EnableClmul ()) {
133
+ switch ((bits + 7 ) / 8 ) {
134
+ case 1 :
135
+ return ConstructClMulTri1Byte (bits, impl);
136
+ case 2 :
137
+ return ConstructClMulTri2Bytes (bits, impl);
138
+ case 3 :
139
+ return ConstructClMulTri3Bytes (bits, impl);
140
+ case 4 :
141
+ return ConstructClMulTri4Bytes (bits, impl);
142
+ case 5 :
143
+ return ConstructClMulTri5Bytes (bits, impl);
144
+ case 6 :
145
+ return ConstructClMulTri6Bytes (bits, impl);
146
+ case 7 :
147
+ return ConstructClMulTri7Bytes (bits, impl);
148
+ case 8 :
149
+ return ConstructClMulTri8Bytes (bits, impl);
150
+ default :
151
+ return nullptr ;
152
+ }
153
+ }
154
+ break ;
130
155
#endif
131
156
}
132
157
return nullptr ;
0 commit comments