Skip to content

Commit 42883b7

Browse files
committed
Move clmul runtime detection to a function
1 parent a9913a4 commit 42883b7

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/minisketch.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ enum class FieldImpl {
6262
#endif
6363
};
6464

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+
6581
Sketch* Construct(int bits, int impl)
6682
{
6783
switch (FieldImpl(impl)) {
@@ -89,14 +105,7 @@ Sketch* Construct(int bits, int impl)
89105
#ifdef HAVE_CLMUL
90106
case FieldImpl::CLMUL:
91107
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()) {
100109
switch ((bits + 7) / 8) {
101110
case 1:
102111
if (FieldImpl(impl) == FieldImpl::CLMUL) return ConstructClMul1Byte(bits, impl);

0 commit comments

Comments
 (0)