29#define getbit(p, bi) ((p[(bi - 1) / LIMB_BITS] >> (bi - 1) % LIMB_BITS) & 1)
44 if (nbits_in_r < nbits)
45 r += p[i + 1] << nbits_in_r;
56static inline void mul_128(u128 a, u128 b, u128* hi, u128*
lo) {
57#if defined(__clang__) && !defined(LMMP_ASM_ARM64)
58 ulong a0 = _u128low(a),
a1 = _u128high(a);
59 ulong b0 = _u128low(b),
b1 = _u128high(b);
60 u128 p00 = (u128)
a0 *
b0;
61 u128 p01 = (u128)
a0 *
b1;
62 u128 p10 = (u128)
a1 *
b0;
63 u128 p11 = (u128)
a1 *
b1;
66 u128 l = (mid << 64) + p00;
69 *hi = p11 + (mid >> 64) + cl + (cm << 64);
72 _umul128to256_(_u128high(a), _u128low(a), _u128high(b), _u128low(b), u);
73 *
lo = ((u128)u[1] << 64) | u[0];
74 *hi = ((u128)u[3] << 64) | u[2];
79static inline void sqr_128(u128 a, u128* hi, u128*
lo) {
80#if defined(__clang__) && !defined(LMMP_ASM_ARM64)
81 ulong a0 = _u128low(a),
a1 = _u128high(a);
82 u128 p0 = (u128)
a0 *
a0;
83 u128 m = (u128)
a0 *
a1;
84 u128 p1 = (u128)
a1 *
a1;
87 u128 l = (ml << 64) + p0;
90 *hi = p1 + (ml >> 64) + cl + (mh << 64);
94 *
lo = ((u128)u[1] << 64) | u[0];
95 *hi = ((u128)u[3] << 64) | u[2];
102static inline u128
mont2_redc(u128 thi, u128 tlo, u128
n, u128 ninv) {
103#if defined(LMMP_ASM_ARM64)
107 ulong t0 = _u128low(tlo), t1 = _u128high(tlo);
108 ulong p0 = _u128low(ql), p1 = _u128high(ql);
109 ulong k0 = (t0 + p0) < t0;
114 ulong h0 = _u128low(thi) + _u128low(qh);
115 ulong j0 = h0 < _u128low(qh);
118 ulong h1 = _u128high(thi) + _u128high(qh);
119 ulong j1 = h1 < _u128high(qh);
122 ulong n0 = _u128low(
n), n1 = _u128high(
n);
123 if (j1 || (h1 > n1) || (h1 == n1 && h0 >= n0)) {
128 return ((u128)h1 << 64) | h0;
131 u128 qh, ql,
lo, hi, c,
c1, c2;
171 u128
n = _u128load(mod);
172 if (_u128load(bp) == 0) {
183 unsigned windowsize = lmmp_powmod_win_size_(ebi);
188 u128 ninv = 0 - _u128load(inv);
200 unsigned this_windowsize;
206 if (windowsize > 1) {
211 expbits =
getbits(ep, ebi, windowsize);
217 u = pp[expbits >> 1];
223 while (
getbit(ep, ebi) == 0) {
232 expbits =
getbits(ep, ebi, windowsize);
233 this_windowsize =
LMMP_MIN(windowsize, ebi);
236 this_windowsize -= cnt;
237 ebi -= this_windowsize;
239 while (this_windowsize > 1) {
242 this_windowsize -= 2;
245 if (this_windowsize != 0)
static void mul_128(u128 a, u128 b, u128 *hi, u128 *lo)
static void sqr_128(u128 a, u128 *hi, u128 *lo)
static u128 mont2_mul_2(u128 a, u128 b, u128 n, u128 ninv)
static u128 mont2_redc(u128 thi, u128 tlo, u128 n, u128 ninv)
static u128 mont2_sqr(u128 a, u128 n, u128 ninv)
#define lmmp_copy(dst, src, n)
#define lmmp_zero(dst, n)
#define lmmp_debug_assert(x)
const mp_limb_t * mp_srcptr
#define LMMP_MIN(l, o)
返回两个数中的较小值
#define lmmp_param_assert(x)
void lmmp_div_(mp_ptr dstq, mp_ptr dstr, mp_srcptr numa, mp_size_t na, mp_srcptr numb, mp_size_t nb)
除法和取模操作
static void _umul128to256_(uint64_t a_high, uint64_t a_low, uint64_t b_high, uint64_t b_low, uint64_t rr[4])
#define ctz_shr_u64(r, x, cnt)
Copyright (C) 2026 HJimmyK (Jericho Knox)
static void _usqr128to256_(uint64_t a_high, uint64_t a_low, uint64_t rr[4])
void lmmp_binvert_2_(mp_ptr dst, mp_srcptr numa)
计算 [numa,2] 在 B^2 下的逆元
void lmmp_powmod_2_(mp_ptr dst, mp_srcptr bp, mp_srcptr ep, mp_size_t en, mp_srcptr mod)
计算奇模数模幂 [dst,2] = [bp,2]^[ep,en] % [mod,2]
static mp_bitcnt_t count_bits(mp_srcptr p, mp_size_t n)
static mp_limb_t getbits(const mp_limb_t *p, mp_bitcnt_t bi, mp_bitcnt_t nbits)
#define restrict
Copyright (C) 2026 HJimmyK (Jericho Knox)
#define TALLOC_TYPE(n, type)