Skip to content

Commit 52f5d5d

Browse files
committed
Avoid overflowing shift by special casing inverse of 1
1 parent 67b87ac commit 52f5d5d

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
@@ -176,6 +176,7 @@ class BitsInt {
176176
}
177177

178178
static constexpr inline bool IsZero(I a) { return a == 0; }
179+
static constexpr inline bool IsOne(I a) { return a == 1; }
179180
static constexpr inline I Mask(I val) { return val & MASK; }
180181
static constexpr inline I Shift(I val, int bits) { return ((val << bits) & MASK); }
181182
static constexpr inline I UnsafeShift(I val, int bits) { return (val << bits); }
@@ -234,7 +235,7 @@ template<typename I, int N, typename L, typename F> inline constexpr I GFMul(con
234235
template<typename I, typename F, int BITS, uint32_t MOD>
235236
inline I InvExtGCD(I x)
236237
{
237-
if (F::IsZero(x)) return x;
238+
if (F::IsZero(x) || F::IsOne(x)) return x;
238239
I t(0), newt(1);
239240
I r(MOD), newr = x;
240241
int rlen = BITS + 1, newrlen = F::Bits(newr, BITS);

0 commit comments

Comments
 (0)