Skip to content

Commit d67bcec

Browse files
authored
Make addmod/mulmod constexpr (#314)
1 parent 9717258 commit d67bcec

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

include/intx/intx.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ inline constexpr uint128& operator-=(uint128& x, uint128 y) noexcept
486486
return x = x - y;
487487
}
488488

489-
inline uint128& operator*=(uint128& x, uint128 y) noexcept
489+
inline constexpr uint128& operator*=(uint128& x, uint128 y) noexcept
490490
{
491491
return x = x * y;
492492
}
@@ -1806,7 +1806,7 @@ inline constexpr uint<N>& operator>>=(uint<N>& x, const T& y) noexcept
18061806
}
18071807

18081808

1809-
inline uint256 addmod(const uint256& x, const uint256& y, const uint256& mod) noexcept
1809+
inline constexpr uint256 addmod(const uint256& x, const uint256& y, const uint256& mod) noexcept
18101810
{
18111811
// Fast path for mod >= 2^192, with x and y at most slightly bigger than mod.
18121812
// This is always the case when x and y are already reduced modulo mod.
@@ -1840,7 +1840,7 @@ inline uint256 addmod(const uint256& x, const uint256& y, const uint256& mod) no
18401840
return udivrem(n, mod).rem;
18411841
}
18421842

1843-
inline uint256 mulmod(const uint256& x, const uint256& y, const uint256& mod) noexcept
1843+
inline constexpr uint256 mulmod(const uint256& x, const uint256& y, const uint256& mod) noexcept
18441844
{
18451845
return udivrem(umul(x, y), mod).rem;
18461846
}

test/unittests/test_uint256.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ constexpr decltype(&addmod) addmod_impls[] = {
176176

177177
TEST(uint256, addmod)
178178
{
179+
static_assert(
180+
addmod(0xdce049946eccbbf77ed1e8e2a3c89e15a8e897df2194150700f5096dea864cdb_u256,
181+
0x397dd0df188eaffbf5216c6be56fe49002fbdc23b95a58a60f69e56f6f87f424_u256,
182+
0xf0f9d0006f7b450e8f73f621a6ca3b56_u128) == 0x7533da49e8c499530049fbf08733976b_u128);
183+
179184
for (auto&& impl : addmod_impls)
180185
{
181186
const auto x = 0xab0f4afc4c78548d4c30e1ab3449e3_u128;
@@ -208,6 +213,11 @@ TEST(uint256, addmod_ec2)
208213

209214
TEST(uint256, mulmod)
210215
{
216+
static_assert(
217+
mulmod(0x4028c97ce32bf74a3a3137956b07a5a699ca8422bdf672f547_u256,
218+
0x8c9f09b6227ba6542a97343c679e1d11d8bfa29228c18615c2_u256,
219+
0xf0f9d0006f7b450e8f73f621a6ca3b56_u128) == 0xca283039a2ad0dbd3d60fbadb29e9c7a_u128);
220+
211221
const auto x = 0xab0f4afc4c78548d4c30e1ab3449e3_u128;
212222
const auto y = 0xf0a4485af15508e448cdddb0d1301664_u128;
213223
const auto mod = 0xf0f9d0006f7b450e8f73f621a6ca3b56_u128;

0 commit comments

Comments
 (0)