Skip to content

Commit 653d8b2

Browse files
committed
Avoid overflowing shift by special casing inverse of 1
1 parent 33b7c20 commit 653d8b2

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/int_utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ class BitsInt {
194194
}
195195

196196
static constexpr inline bool IsZero(I a) { return a == 0; }
197+
static constexpr inline bool IsOne(I a) { return a == 1; }
197198
static constexpr inline I Mask(I val) { return val & MASK; }
198199
static constexpr inline I Shift(I val, int bits) { return ((val << bits) & MASK); }
199200
static constexpr inline I UnsafeShift(I val, int bits) { return (val << bits); }
@@ -252,7 +253,7 @@ template<typename I, int N, typename L, typename F> inline constexpr I GFMul(con
252253
template<typename I, typename F, int BITS, uint32_t MOD>
253254
inline I InvExtGCD(I x)
254255
{
255-
if (F::IsZero(x)) return x;
256+
if (F::IsZero(x) || F::IsOne(x)) return x;
256257
I t(0), newt(1);
257258
I r(MOD), newr = x;
258259
int rlen = BITS + 1, newrlen = F::Bits(newr, BITS);

0 commit comments

Comments
 (0)