LMMP 4.3.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
lmmp_test_utils.h 文件参考
#include <stdbool.h>
#include <string.h>
#include "lmmp/lmmp.h"
#include "lmmp/lmmpn.h"
#include "lmmp/numth.h"
lmmp_test_utils.h 的引用(Include)关系图:
此图展示该文件被哪些文件直接或间接地引用了:

浏览该文件的源代码.

类型定义

typedef uint u32
typedef mp_limb_t u64
 Copyright (C) 2026 HJimmyK (Jericho Knox)

函数

static u64 add_small_inplace (mp_ptr p, size_t n, u64 v)
static u64 addmod32 (u64 a, u64 b, u64 m)
static mp_ptr alloc_limbs (size_t n)
static mp_size_t chain_mul_small (mp_ptr e, mp_size_t en, u64 v, size_t cap)
static bool divides_all (const mp_limb_t *d, mp_size_t dn, const mp_limb_t *p, mp_size_t pn)
static mp_size_t fib_pair (mp_ptr x, mp_ptr y, mp_size_t buf, mp_size_t want_n)
static bool is_prime_small (u32 n)
static u64 mod32 (const mp_limb_t *p, mp_size_t n, u32 m)
static bool mod_eq (const mp_limb_t *a, mp_size_t an, const mp_limb_t *b, mp_size_t bn)
static bool mod_is_diff (const mp_limb_t *r, mp_size_t rn, const mp_limb_t *a, mp_size_t an, const mp_limb_t *b, mp_size_t bn)
static bool mod_is_product (const mp_limb_t *r, mp_size_t rn, const mp_limb_t *a, mp_size_t an, const mp_limb_t *b, mp_size_t bn)
static bool mod_is_sum (const mp_limb_t *r, mp_size_t rn, const mp_limb_t *a, mp_size_t an, const mp_limb_t *b, mp_size_t bn)
static int mpn_cmp (const mp_limb_t *a, mp_size_t an, const mp_limb_t *b, mp_size_t bn)
static bool mpn_eq (const mp_limb_t *a, mp_size_t an, const mp_limb_t *b, mp_size_t bn)
static void mul_sorted (mp_ptr dst, const mp_limb_t *a, mp_size_t la, const mp_limb_t *b, mp_size_t lb)
static u64 mulmod32 (u64 a, u64 b, u64 m)
static mp_size_t norm_len (const mp_limb_t *p, mp_size_t n)
static void pad_add (const mp_limb_t *a, mp_size_t la, const mp_limb_t *b, mp_size_t lb, mp_size_t mx, mp_ptr sum)
static void random_limbs (mp_ptr p, size_t n, u64 *seed)
static void random_limbs_with_top (mp_ptr p, size_t n, u64 *seed, bool top_nonzero)
static u64 shr1_inplace (mp_ptr p, size_t n, u64 top_bit)
static u64 sub_small_inplace (mp_ptr p, size_t n, u64 v)
static u64 submod32 (u64 a, u64 b, u64 m)
static u64 xorshift64 (u64 *s)

变量

static const u32 MOD_P [] = {4294967291u, 4294967279u, 4294967231u, 2147483647u, 1000000007u}
static const int MOD_P_CNT = (int)(sizeof(MOD_P) / sizeof(MOD_P[0]))

类型定义说明

◆ u32

typedef uint u32

在文件 lmmp_test_utils.h 第 34 行定义.

◆ u64

typedef mp_limb_t u64

Copyright (C) 2026 HJimmyK (Jericho Knox)

This file is part of LMMP.

LMMP is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library. If not, see https://www.gnu.org/licenses/.

SPDX-License-Identifier: LGPL-3.0-or-later

在文件 lmmp_test_utils.h 第 33 行定义.

函数说明

◆ add_small_inplace()

u64 add_small_inplace ( mp_ptr p,
size_t n,
u64 v )
inlinestatic

在文件 lmmp_test_utils.h 第 63 行定义.

63 {
64 size_t i = 0;
65 u64 old = p[0];
66 p[0] += v;
67 u64 carry = (p[0] < old) ? 1 : 0;
68 while (carry && ++i < n) {
69 old = p[i];
70 ++p[i];
71 carry = (p[i] < old) ? 1 : 0;
72 }
73 return (carry && i == n) ? 1 : 0;
74}
mp_limb_t u64
Copyright (C) 2026 HJimmyK (Jericho Knox)
#define n

引用了 n.

◆ addmod32()

u64 addmod32 ( u64 a,
u64 b,
u64 m )
inlinestatic

在文件 lmmp_test_utils.h 第 124 行定义.

124{ return (a + b) % m; } // a,b < m,和 < 2^33

被这些函数引用 mod_is_sum().

这是这个函数的调用关系图:

◆ alloc_limbs()

mp_ptr alloc_limbs ( size_t n)
inlinestatic

在文件 lmmp_test_utils.h 第 44 行定义.

44{ return (mp_ptr)lmmp_alloc(n * sizeof(mp_limb_t)); }
mp_limb_t * mp_ptr
定义 lmmp.h:96
void * lmmp_alloc(size_t size)
内存分配函数(调用lmmp_heap_alloc_fn)
uint64_t mp_limb_t
定义 lmmp.h:92

引用了 lmmp_alloc() , 以及 n.

被这些函数引用 pad_add().

函数调用图:
这是这个函数的调用关系图:

◆ chain_mul_small()

mp_size_t chain_mul_small ( mp_ptr e,
mp_size_t en,
u64 v,
size_t cap )
inlinestatic

在文件 lmmp_test_utils.h 第 210 行定义.

210 {
211 u64 hi = lmmp_mul_1_(e, e, en, v);
212 if (hi) {
213 if ((size_t)en + 1 >= cap) return -1;
214 e[en] = hi;
215 return en + 1;
216 }
217 return en;
218}
mp_limb_t lmmp_mul_1_(mp_ptr dst, mp_srcptr numa, mp_size_t na, mp_limb_t x)
乘以单limb操作 [dst,na] = [numa,na] * x
定义 mul_1.c:27

引用了 lmmp_mul_1_().

函数调用图:

◆ divides_all()

bool divides_all ( const mp_limb_t * d,
mp_size_t dn,
const mp_limb_t * p,
mp_size_t pn )
inlinestatic

在文件 lmmp_test_utils.h 第 181 行定义.

181 {
182 if (dn == 0) return false;
183 mp_ptr r = (mp_ptr)lmmp_alloc((size_t)dn * sizeof(mp_limb_t));
184 lmmp_div_(NULL, r, p, pn, d, dn);
185 bool ok = norm_len(r, dn) == 0;
186 lmmp_free(r);
187 return ok;
188}
void lmmp_free(void *ptr)
内存释放函数(调用lmmp_heap_free_fn)
static mp_size_t norm_len(const mp_limb_t *p, mp_size_t n)
void lmmp_div_(mp_ptr dstq, mp_ptr dstr, mp_srcptr numa, mp_size_t na, mp_srcptr numb, mp_size_t nb)
除法和取模操作
定义 div.c:85

引用了 lmmp_alloc(), lmmp_div_(), lmmp_free() , 以及 norm_len().

函数调用图:

◆ fib_pair()

mp_size_t fib_pair ( mp_ptr x,
mp_ptr y,
mp_size_t buf,
mp_size_t want_n )
inlinestatic

在文件 lmmp_test_utils.h 第 230 行定义.

230 {
231 ulong k = 3;
232 for (;; ++k) {
233 mp_size_t need = lmmp_fibonacci_size_(k + 1);
234 // size_ 比实际长度多 2-4 limb:need 超 buf+4 后实际长度也不可能 <= buf
235 if (need > buf + 4) return 0;
236 if (need <= buf) {
237 lmmp_zero(x, buf);
238 lmmp_zero(y, buf);
239 mp_size_t ly = lmmp_fibonacci2_(y, x, need, k); // x=F(k), y=F(k+1)
240 if (ly >= want_n) return ly;
241 }
242 }
243}
#define k
#define lmmp_zero(dst, n)
定义 lmmp.h:476
uint64_t mp_size_t
定义 lmmp.h:93
mp_size_t lmmp_fibonacci2_(mp_ptr dst, mp_ptr dst2, mp_size_t rn, ulong n)
计算 Fibonacci 数对:[dst,rn] = F[n],[dst2,rn] = F[n-1]
uint64_t ulong
定义 numth.h:39
mp_size_t lmmp_fibonacci_size_(ulong n)
计算 Fibonacci 数 F[n](及 F[n-1])所需的 limb 缓冲区长度

引用了 k, lmmp_fibonacci2_(), lmmp_fibonacci_size_() , 以及 lmmp_zero.

函数调用图:

◆ is_prime_small()

bool is_prime_small ( u32 n)
inlinestatic

在文件 lmmp_test_utils.h 第 221 行定义.

221 {
222 if (n < 2) return false;
223 for (u32 d = 2; (u64)d * d <= n; ++d)
224 if (n % d == 0) return false;
225 return true;
226}

引用了 n.

◆ mod32()

u64 mod32 ( const mp_limb_t * p,
mp_size_t n,
u32 m )
inlinestatic

在文件 lmmp_test_utils.h 第 121 行定义.

121{ return lmmp_mod_1_(p, n, m); }
mp_limb_t lmmp_mod_1_(mp_srcptr numa, mp_size_t na, mp_limb_t x)
单精度数取余
定义 div.c:38

引用了 lmmp_mod_1_() , 以及 n.

被这些函数引用 mod_eq(), mod_is_diff(), mod_is_product() , 以及 mod_is_sum().

函数调用图:
这是这个函数的调用关系图:

◆ mod_eq()

bool mod_eq ( const mp_limb_t * a,
mp_size_t an,
const mp_limb_t * b,
mp_size_t bn )
inlinestatic

在文件 lmmp_test_utils.h 第 128 行定义.

128 {
129 an = norm_len(a, an);
130 bn = norm_len(b, bn);
131 for (int i = 0; i < MOD_P_CNT; ++i) {
132 u32 p = MOD_P[i];
133 if (mod32(a, an, p) != mod32(b, bn, p)) return false;
134 }
135 return true;
136}
#define an
#define bn
static const int MOD_P_CNT
static const u32 MOD_P[]
static u64 mod32(const mp_limb_t *p, mp_size_t n, u32 m)

引用了 an, bn, mod32(), mod_eq(), MOD_P, MOD_P_CNT , 以及 norm_len().

被这些函数引用 mod_eq().

函数调用图:
这是这个函数的调用关系图:

◆ mod_is_diff()

bool mod_is_diff ( const mp_limb_t * r,
mp_size_t rn,
const mp_limb_t * a,
mp_size_t an,
const mp_limb_t * b,
mp_size_t bn )
inlinestatic

在文件 lmmp_test_utils.h 第 167 行定义.

168 {
169 an = norm_len(a, an);
170 bn = norm_len(b, bn);
171 rn = norm_len(r, rn);
172 for (int i = 0; i < MOD_P_CNT; ++i) {
173 u32 p = MOD_P[i];
174 u64 want = submod32(mod32(a, an, p), mod32(b, bn, p), p);
175 if (mod32(r, rn, p) != want) return false;
176 }
177 return true;
178}
static u64 submod32(u64 a, u64 b, u64 m)

引用了 an, bn, mod32(), mod_is_diff(), MOD_P, MOD_P_CNT, norm_len() , 以及 submod32().

被这些函数引用 mod_is_diff().

函数调用图:
这是这个函数的调用关系图:

◆ mod_is_product()

bool mod_is_product ( const mp_limb_t * r,
mp_size_t rn,
const mp_limb_t * a,
mp_size_t an,
const mp_limb_t * b,
mp_size_t bn )
inlinestatic

在文件 lmmp_test_utils.h 第 139 行定义.

140 {
141 an = norm_len(a, an);
142 bn = norm_len(b, bn);
143 rn = norm_len(r, rn);
144 for (int i = 0; i < MOD_P_CNT; ++i) {
145 u32 p = MOD_P[i];
146 u64 want = mulmod32(mod32(a, an, p), mod32(b, bn, p), p);
147 if (mod32(r, rn, p) != want) return false;
148 }
149 return true;
150}
static u64 mulmod32(u64 a, u64 b, u64 m)

引用了 an, bn, mod32(), MOD_P, MOD_P_CNT, mulmod32() , 以及 norm_len().

函数调用图:

◆ mod_is_sum()

bool mod_is_sum ( const mp_limb_t * r,
mp_size_t rn,
const mp_limb_t * a,
mp_size_t an,
const mp_limb_t * b,
mp_size_t bn )
inlinestatic

在文件 lmmp_test_utils.h 第 153 行定义.

154 {
155 an = norm_len(a, an);
156 bn = norm_len(b, bn);
157 rn = norm_len(r, rn);
158 for (int i = 0; i < MOD_P_CNT; ++i) {
159 u32 p = MOD_P[i];
160 u64 want = addmod32(mod32(a, an, p), mod32(b, bn, p), p);
161 if (mod32(r, rn, p) != want) return false;
162 }
163 return true;
164}
static u64 addmod32(u64 a, u64 b, u64 m)

引用了 addmod32(), an, bn, mod32(), mod_is_sum(), MOD_P, MOD_P_CNT , 以及 norm_len().

被这些函数引用 mod_is_sum().

函数调用图:
这是这个函数的调用关系图:

◆ mpn_cmp()

int mpn_cmp ( const mp_limb_t * a,
mp_size_t an,
const mp_limb_t * b,
mp_size_t bn )
inlinestatic

在文件 lmmp_test_utils.h 第 99 行定义.

99 {
100 an = norm_len(a, an);
101 bn = norm_len(b, bn);
102 if (an != bn) return an < bn ? -1 : 1;
103 for (mp_size_t i = an; i-- > 0;)
104 if (a[i] != b[i]) return a[i] < b[i] ? -1 : 1;
105 return 0;
106}

引用了 an, bn , 以及 norm_len().

被这些函数引用 mpn_eq().

函数调用图:
这是这个函数的调用关系图:

◆ mpn_eq()

bool mpn_eq ( const mp_limb_t * a,
mp_size_t an,
const mp_limb_t * b,
mp_size_t bn )
inlinestatic

在文件 lmmp_test_utils.h 第 108 行定义.

108 {
109 return mpn_cmp(a, an, b, bn) == 0;
110}
static int mpn_cmp(const mp_limb_t *a, mp_size_t an, const mp_limb_t *b, mp_size_t bn)

引用了 an, bn , 以及 mpn_cmp().

函数调用图:

◆ mul_sorted()

void mul_sorted ( mp_ptr dst,
const mp_limb_t * a,
mp_size_t la,
const mp_limb_t * b,
mp_size_t lb )
inlinestatic

在文件 lmmp_test_utils.h 第 203 行定义.

204 {
205 if (la >= lb) lmmp_mul_(dst, a, la, b, lb);
206 else lmmp_mul_(dst, b, lb, a, la);
207}
void lmmp_mul_(mp_ptr dst, mp_srcptr numa, mp_size_t na, mp_srcptr numb, mp_size_t nb)
不等长乘法操作 [dst,na+nb] = [numa,na] * [numb,nb]

引用了 lmmp_mul_().

函数调用图:

◆ mulmod32()

u64 mulmod32 ( u64 a,
u64 b,
u64 m )
inlinestatic

在文件 lmmp_test_utils.h 第 123 行定义.

123{ return (a * b) % m; } // a,b < m < 2^32

被这些函数引用 mod_is_product().

这是这个函数的调用关系图:

◆ norm_len()

mp_size_t norm_len ( const mp_limb_t * p,
mp_size_t n )
inlinestatic

在文件 lmmp_test_utils.h 第 57 行定义.

57 {
58 while (n > 0 && p[n - 1] == 0) --n;
59 return n;
60}

引用了 n.

被这些函数引用 divides_all(), mod_eq(), mod_is_diff(), mod_is_product(), mod_is_sum() , 以及 mpn_cmp().

这是这个函数的调用关系图:

◆ pad_add()

void pad_add ( const mp_limb_t * a,
mp_size_t la,
const mp_limb_t * b,
mp_size_t lb,
mp_size_t mx,
mp_ptr sum )
inlinestatic

在文件 lmmp_test_utils.h 第 191 行定义.

192 {
193 lmmp_zero(sum, mx);
194 memcpy(sum, a, (size_t)la * 8);
195 mp_ptr pad = alloc_limbs(mx);
196 lmmp_zero(pad, mx);
197 memcpy(pad, b, (size_t)lb * 8);
198 lmmp_add_n_(sum, sum, pad, mx);
199 lmmp_free(pad);
200}
static mp_ptr alloc_limbs(size_t n)
mp_limb_t lmmp_add_n_(mp_ptr dst, mp_srcptr numa, mp_srcptr numb, mp_size_t n)
无进位的n位加法 [dst,n] = [numa,n] + [numb,n]
定义 add_n.c:76

引用了 alloc_limbs(), lmmp_add_n_(), lmmp_free() , 以及 lmmp_zero.

函数调用图:

◆ random_limbs()

void random_limbs ( mp_ptr p,
size_t n,
u64 * seed )
inlinestatic

在文件 lmmp_test_utils.h 第 52 行定义.

52 {
53 random_limbs_with_top(p, n, seed, true);
54}
static void random_limbs_with_top(mp_ptr p, size_t n, u64 *seed, bool top_nonzero)

引用了 n , 以及 random_limbs_with_top().

函数调用图:

◆ random_limbs_with_top()

void random_limbs_with_top ( mp_ptr p,
size_t n,
u64 * seed,
bool top_nonzero )
inlinestatic

在文件 lmmp_test_utils.h 第 47 行定义.

47 {
48 for (size_t i = 0; i < n; ++i) p[i] = xorshift64(seed);
49 if (n > 0 && top_nonzero) p[n - 1] |= (u64)1 << 63;
50}
static u64 xorshift64(u64 *s)

引用了 n , 以及 xorshift64().

被这些函数引用 random_limbs().

函数调用图:
这是这个函数的调用关系图:

◆ shr1_inplace()

u64 shr1_inplace ( mp_ptr p,
size_t n,
u64 top_bit )
inlinestatic

在文件 lmmp_test_utils.h 第 91 行定义.

91 {
92 u64 low = p[0] & 1;
93 for (size_t i = 0; i + 1 < n; ++i) p[i] = (p[i] >> 1) | (p[i + 1] << 63);
94 if (n > 0) p[n - 1] = (p[n - 1] >> 1) | (top_bit << 63);
95 return low;
96}

引用了 n.

◆ sub_small_inplace()

u64 sub_small_inplace ( mp_ptr p,
size_t n,
u64 v )
inlinestatic

在文件 lmmp_test_utils.h 第 77 行定义.

77 {
78 size_t i = 0;
79 u64 old = p[0];
80 p[0] -= v;
81 u64 borrow = (p[0] > old) ? 1 : 0;
82 while (borrow && ++i < n) {
83 old = p[i];
84 --p[i];
85 borrow = (p[i] > old) ? 1 : 0;
86 }
87 return (borrow && i == n) ? 1 : 0;
88}

引用了 n.

◆ submod32()

u64 submod32 ( u64 a,
u64 b,
u64 m )
inlinestatic

在文件 lmmp_test_utils.h 第 125 行定义.

125{ return (a + m - b) % m; }

被这些函数引用 mod_is_diff().

这是这个函数的调用关系图:

◆ xorshift64()

u64 xorshift64 ( u64 * s)
inlinestatic

在文件 lmmp_test_utils.h 第 37 行定义.

37 {
38 *s ^= *s << 13;
39 *s ^= *s >> 7;
40 *s ^= *s << 17;
41 return *s;
42}
#define s

引用了 s.

被这些函数引用 random_limbs_with_top().

这是这个函数的调用关系图:

变量说明

◆ MOD_P

const u32 MOD_P[] = {4294967291u, 4294967279u, 4294967231u, 2147483647u, 1000000007u}
static

在文件 lmmp_test_utils.h 第 117 行定义.

被这些函数引用 mod_eq(), mod_is_diff(), mod_is_product() , 以及 mod_is_sum().

◆ MOD_P_CNT

const int MOD_P_CNT = (int)(sizeof(MOD_P) / sizeof(MOD_P[0]))
static

在文件 lmmp_test_utils.h 第 118 行定义.

被这些函数引用 mod_eq(), mod_is_diff(), mod_is_product() , 以及 mod_is_sum().