LMMP 4.3.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
test_deterministic_extra.c 文件参考
#include "lmmp/lmmpn.h"
#include "mparam.h"
#include "lmmp/numth.h"
#include "cases.h"
#include "support/lmmp_test_utils.h"
#include <string.h>
test_deterministic_extra.c 的引用(Include)关系图:

浏览该文件的源代码.

函数

 TEST_CASE ("low/addsub", add_sub_deterministic_inplace)
 Copyright (C) 2026 HJimmyK (Jericho Knox)
 TEST_CASE ("low/addsub", addshl1_subshl1_boundary_deterministic)
 TEST_CASE ("low/div", div_1_mod_1_boundary_deterministic)
 TEST_CASE ("low/shift", shlnot_not_inplace_deterministic)
void test_deterministic_extra (void)

函数说明

◆ TEST_CASE() [1/4]

TEST_CASE ( "low/addsub" ,
add_sub_deterministic_inplace  )

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

在文件 test_deterministic_extra.c 第 37 行定义.

37 {
38 const mp_size_t n = 5;
39 const u64 av[5] = {UINT64_MAX - 1, 0, 0, 1, UINT64_MAX};
40 const u64 bv[5] = {1, 0, UINT64_MAX, UINT64_MAX - 1, 0};
41 mp_ptr a = alloc_limbs(n), b = alloc_limbs(n), d = alloc_limbs(n), r = alloc_limbs(n);
42 for (mp_size_t i = 0; i < n; ++i) { a[i] = av[i]; b[i] = bv[i]; }
43
44 mp_ptr a2 = alloc_limbs(n), b2 = alloc_limbs(n);
45 const u64 add_cy = lmmp_add_n_(r, a, b, n); // 分离缓冲参考:和
46 const u64 sub_bw = lmmp_sub_n_(d, a, b, n); // 分离缓冲参考:差(a >= b)
47 TEST_CHECK_EQ(add_cy, 0u); // 固定输入钉住参考语义:无终位进位/借位
48 TEST_CHECK_EQ(sub_bw, 0u);
49 // eqsep 原地:dst == numa / dst == numb 各两向(返回值与分离缓冲一致)
50 memcpy(a2, a, n * 8);
51 TEST_CHECK_EQ(lmmp_add_n_(a2, a2, b, n), add_cy);
52 TEST_CHECK_EQ(memcmp(a2, r, n * 8), 0);
53 memcpy(b2, b, n * 8);
54 TEST_CHECK_EQ(lmmp_add_n_(b2, a, b2, n), add_cy);
55 TEST_CHECK_EQ(memcmp(b2, r, n * 8), 0);
56 memcpy(a2, a, n * 8);
57 TEST_CHECK_EQ(lmmp_sub_n_(a2, a2, b, n), sub_bw);
58 TEST_CHECK_EQ(memcmp(a2, d, n * 8), 0);
59 memcpy(b2, b, n * 8);
60 TEST_CHECK_EQ(lmmp_sub_n_(b2, a, b2, n), sub_bw);
61 TEST_CHECK_EQ(memcmp(b2, d, n * 8), 0);
62
65}
mp_limb_t * mp_ptr
定义 lmmp.h:96
uint64_t mp_size_t
定义 lmmp.h:93
void lmmp_free(void *ptr)
内存释放函数(调用lmmp_heap_free_fn)
mp_limb_t u64
Copyright (C) 2026 HJimmyK (Jericho Knox)
mp_limb_t lmmp_sub_n_(mp_ptr dst, mp_srcptr numa, mp_srcptr numb, mp_size_t n)
无借位的n位减法 [dst,n] = [numa,n] - [numb,n]
定义 sub_n.c:84
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
#define a2
#define b2
#define n
#define TEST_CHECK_EQ(a, b)
定义 test.h:39

引用了 a2, b2, lmmp_add_n_(), lmmp_free(), lmmp_sub_n_(), n, TEST_CASE , 以及 TEST_CHECK_EQ.

函数调用图:

◆ TEST_CASE() [2/4]

TEST_CASE ( "low/addsub" ,
addshl1_subshl1_boundary_deterministic  )

在文件 test_deterministic_extra.c 第 120 行定义.

120 {
121 const mp_size_t n = 4;
122 const u64 av[4] = {0, 1, ~(u64)0 - 1, 3};
123 const u64 bv[4] = {(u64)1 << 63, ~(u64)0, 0, 2};
124 mp_ptr a = alloc_limbs(n), b = alloc_limbs(n), d = alloc_limbs(n + 1), t = alloc_limbs(n + 1),
125 b2 = alloc_limbs(n + 1);
126 for (mp_size_t i = 0; i < n; ++i) { a[i] = av[i]; b[i] = bv[i]; }
127
128 u64 c1 = lmmp_add_n_(b2, b, b, n);
129 u64 c2 = lmmp_add_n_(t, a, b2, n);
130 u64 cy = lmmp_addshl1_n_(d, a, b, n);
131 TEST_CHECK_EQ(memcmp(d, t, n * 8), 0);
132 TEST_CHECK_EQ(cy, c1 + c2);
133
134 u64 w1 = lmmp_sub_n_(t, a, b2, n);
135 u64 bw = lmmp_subshl1_n_(d, a, b, n);
136 TEST_CHECK_EQ(memcmp(d, t, n * 8), 0);
137 TEST_CHECK_EQ(bw, w1 + c1);
138
140}
mp_limb_t lmmp_subshl1_n_(mp_ptr dst, mp_srcptr numa, mp_srcptr numb, mp_size_t n)
减法结合左移1位操作 [dst,n] = [numa,n] - ([numb,n] << 1)
定义 shl.c:80
mp_limb_t lmmp_addshl1_n_(mp_ptr dst, mp_srcptr numa, mp_srcptr numb, mp_size_t n)
加法结合左移1位操作 [dst,n] = [numa,n] + ([numb,n] << 1)
定义 shl.c:53
#define t
#define c1

引用了 b2, c1, lmmp_add_n_(), lmmp_addshl1_n_(), lmmp_free(), lmmp_sub_n_(), lmmp_subshl1_n_(), n, t, TEST_CASE , 以及 TEST_CHECK_EQ.

函数调用图:

◆ TEST_CASE() [3/4]

TEST_CASE ( "low/div" ,
div_1_mod_1_boundary_deterministic  )

在文件 test_deterministic_extra.c 第 95 行定义.

95 {
96 const mp_size_t ns[] = {1, 2, 3};
97 const u64 fills[] = {0ull, 1ull, ~0ull};
98 const u64 divisors[] = {1ull, 2ull, 0x8000000000000000ull, ~0ull};
99 for (size_t n_index = 0; n_index < ARRAY_SIZE(ns); ++n_index) {
100 mp_size_t n = ns[n_index];
101 for (size_t fill_index = 0; fill_index < ARRAY_SIZE(fills); ++fill_index) {
102 u64 fill = fills[fill_index];
103 mp_ptr a = alloc_limbs(n), q = alloc_limbs(n + 1), t = alloc_limbs(n + 2);
104 for (mp_size_t i = 0; i < n; ++i) a[i] = (i == n - 1) ? fill : (fill >> 1);
105 for (size_t x_index = 0; x_index < ARRAY_SIZE(divisors); ++x_index) {
106 u64 x = divisors[x_index];
107 u64 rem = lmmp_div_1_(q, a, n, x);
108 TEST_CHECK(rem < x);
109 TEST_CHECK_EQ(lmmp_mod_1_(a, n, x), rem);
110 u64 hi = lmmp_mul_1_(t, q, n, x);
111 t[n] = hi;
112 add_small_inplace(t, n + 1, rem);
113 TEST_CHECK(mpn_eq(t, n + 1, a, n)); // q*x + r == a
114 }
116 }
117 }
118}
mp_limb_t lmmp_div_1_(mp_ptr dstq, mp_srcptr numa, mp_size_t na, mp_limb_t x)
单精度数除法
定义 div.c:137
mp_limb_t lmmp_mod_1_(mp_srcptr numa, mp_size_t na, mp_limb_t x)
单精度数取余
定义 div.c:38
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
#define ARRAY_SIZE(a)
Copyright (C) 2026 HJimmyK (Jericho Knox)
定义 perf.h:40
#define TEST_CHECK(cond)
定义 test.h:37

引用了 ARRAY_SIZE, lmmp_div_1_(), lmmp_free(), lmmp_mod_1_(), lmmp_mul_1_(), n, t, TEST_CASE, TEST_CHECK , 以及 TEST_CHECK_EQ.

函数调用图:

◆ TEST_CASE() [4/4]

TEST_CASE ( "low/shift" ,
shlnot_not_inplace_deterministic  )

在文件 test_deterministic_extra.c 第 67 行定义.

67 {
68 const mp_size_t n = 6;
69 const u64 vals[6] = {
70 0x0000000000000001ull, 0x8000000000000000ull,
71 0xffffffffffffffffull, 0x123456789abcdef0ull,
72 0x0fedcba987654321ull, 0xaaaaaaaa55555555ull
73 };
74 mp_ptr a = alloc_limbs(n), orig = alloc_limbs(n), r = alloc_limbs(n);
75 for (mp_size_t i = 0; i < n; ++i) a[i] = vals[i];
76 memcpy(orig, a, n * 8);
77
78 lmmp_not_(a, a, n);
79 for (mp_size_t i = 0; i < n; ++i) TEST_CHECK_EQ(a[i], ~orig[i]);
80 lmmp_not_(a, a, n);
81 for (mp_size_t i = 0; i < n; ++i) TEST_CHECK_EQ(a[i], orig[i]);
82
83 const mp_size_t s_values[] = {0, 1, 7, 31, 63};
84 for (size_t s_index = 0; s_index < ARRAY_SIZE(s_values); ++s_index) {
85 mp_size_t s = s_values[s_index];
86 mp_limb_t r1 = lmmp_shlnot_(r, orig, n, s);
87 memcpy(a, orig, n * 8);
88 mp_limb_t r2 = lmmp_shlnot_(a, a, n, s);
90 TEST_CHECK_EQ(memcmp(a, r, n * 8), 0);
91 }
92 lmmp_free(a); lmmp_free(orig); lmmp_free(r);
93}
uint64_t mp_limb_t
定义 lmmp.h:92
mp_limb_t lmmp_shlnot_(mp_ptr dst, mp_srcptr numa, mp_size_t na, mp_size_t shl)
左移后按位取反操作 [dst,na] = ~([numa,na] << shl),dst的低shl位填充1
void lmmp_not_(mp_ptr dst, mp_srcptr numa, mp_size_t na)
按位取反操作 [dst,na] = ~[numa,na] (对每个limb执行按位非操作)
定义 not.c:26
#define r2
#define r1
#define s

引用了 ARRAY_SIZE, lmmp_free(), lmmp_not_(), lmmp_shlnot_(), n, r1, r2, s, TEST_CASE , 以及 TEST_CHECK_EQ.

函数调用图:

◆ test_deterministic_extra()

void test_deterministic_extra ( void )

在文件 test_deterministic_extra.c 第 142 行定义.

142 {
143 TEST_RUN("low/addsub", add_sub_deterministic_inplace);
144 TEST_RUN("low/shift", shlnot_not_inplace_deterministic);
145 TEST_RUN("low/div", div_1_mod_1_boundary_deterministic);
146 TEST_RUN("low/addsub", addshl1_subshl1_boundary_deterministic);
147}
#define TEST_RUN(category, name)
定义 test.h:36

引用了 test_deterministic_extra() , 以及 TEST_RUN.

被这些函数引用 main() , 以及 test_deterministic_extra().

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