Skip to content

Commit 9d027d3

Browse files
committed
Merge #47: Simplify field impl selection
c6cff39 Simplify switch/case and add breaks as necessary (Cory Fields) 42883b7 Move clmul runtime detection to a function (Cory Fields) Pull request description: For context see: #46 In addition to adding the missing breaks, this disentangles the cases. ACKs for top commit: sipa: ACK c6cff39 Tree-SHA512: 54bd57dde1c4960f2405f6d32676e8cda2fb91884ec93e9b566fb64ddb15821ea06410e592bc928d0443c617fb0f1427024f5256187b9e9d53f09148ad7e851e
2 parents a9913a4 + c6cff39 commit 9d027d3

File tree

1 file changed

+51
-26
lines changed

1 file changed

+51
-26
lines changed

src/minisketch.cpp

Lines changed: 51 additions & 26 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)) {
@@ -86,47 +102,56 @@ Sketch* Construct(int bits, int impl)
86102
default:
87103
return nullptr;
88104
}
105+
break;
89106
#ifdef HAVE_CLMUL
90107
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()) {
100109
switch ((bits + 7) / 8) {
101110
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);
104112
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);
107114
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);
110116
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);
113118
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);
116120
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);
119122
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);
122124
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);
125126
default:
126127
return nullptr;
127128
}
128129
}
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;
130155
#endif
131156
}
132157
return nullptr;

0 commit comments

Comments
 (0)