LMMP 4.3.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
addsub.c
浏览该文件的文档.
1/**
2 * Copyright (C) 2026 HJimmyK (Jericho Knox)
3 *
4 * This file is part of LMMP.
5 *
6 * LMMP is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 3 of
9 * the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see
18 * <https://www.gnu.org/licenses/>.
19 *
20 * SPDX-License-Identifier: LGPL-3.0-or-later
21 */
22
23#include "limb_sizes.h"
24
25static double bench_add_n(mp_size_t n, const bench_options *o) {
26 mp_ptr a = alloc_limbs(n), b = alloc_limbs(n), d = alloc_limbs(n);
27 fill_random(a, n, 0x123456789ull + n);
28 fill_random(b, n, 0x987654321ull + n);
29 double us;
30 BENCH_MEASURE(us, o, bench_sink += lmmp_add_n_(d, a, b, n););
31 lmmp_free(a);
32 lmmp_free(b);
33 lmmp_free(d);
34 return us;
35}
36
37static double bench_add_nc(mp_size_t n, const bench_options *o) {
38 mp_ptr a = alloc_limbs(n), b = alloc_limbs(n), d = alloc_limbs(n);
39 fill_random(a, n, 0x12345678aull + n);
40 fill_random(b, n, 0x987654322ull + n);
41 double us;
42 BENCH_MEASURE(us, o, bench_sink += lmmp_add_nc_(d, a, b, n, 1););
43 lmmp_free(a);
44 lmmp_free(b);
45 lmmp_free(d);
46 return us;
47}
48
49static double bench_sub_n(mp_size_t n, const bench_options *o) {
50 mp_ptr a = alloc_limbs(n), b = alloc_limbs(n), d = alloc_limbs(n);
51 fill_random(a, n, 0x12345678bull + n);
52 fill_random(b, n, 0x876543210ull + n);
53 double us;
54 BENCH_MEASURE(us, o, bench_sink += lmmp_sub_n_(d, a, b, n););
55 lmmp_free(a);
56 lmmp_free(b);
57 lmmp_free(d);
58 return us;
59}
60
61static double bench_sub_nc(mp_size_t n, const bench_options *o) {
62 mp_ptr a = alloc_limbs(n), b = alloc_limbs(n), d = alloc_limbs(n);
63 fill_random(a, n, 0x12345678cull + n);
64 fill_random(b, n, 0x765432101ull + n);
65 double us;
66 BENCH_MEASURE(us, o, bench_sink += lmmp_sub_nc_(d, a, b, n, 1););
67 lmmp_free(a);
68 lmmp_free(b);
69 lmmp_free(d);
70 return us;
71}
72
73int main(void) {
74 setvbuf(stdout, NULL, _IONBF, 0);
76 puts("[lmmpn/addsub]");
77 run_limb_sizes("add_n", bench_add_n);
78 run_limb_sizes("add_nc", bench_add_nc);
79 run_limb_sizes("sub_n", bench_sub_n);
80 run_limb_sizes("sub_nc", bench_sub_nc);
82 return 0;
83}
static double bench_add_n(mp_size_t n, const bench_options *o)
Copyright (C) 2026 HJimmyK (Jericho Knox)
static double bench_add_nc(mp_size_t n, const bench_options *o)
static double bench_sub_n(mp_size_t n, const bench_options *o)
static double bench_sub_nc(mp_size_t n, const bench_options *o)
int main(void)
Copyright (C) 2026 HJimmyK (Jericho Knox)
定义 main.c:26
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)
void lmmp_global_init(void)
全局初始化函数(线程局部的)
void lmmp_global_deinit(void)
全局释放函数(线程局部的)
mp_limb_t lmmp_add_nc_(mp_ptr dst, mp_srcptr numa, mp_srcptr numb, mp_size_t n, mp_limb_t c)
带进位的n位加法 [dst,n] = [numa,n] + [numb,n] + c
定义 add_n.c:58
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_sub_nc_(mp_ptr dst, mp_srcptr numa, mp_srcptr numb, mp_size_t n, mp_limb_t c)
带借位的n位减法 [dst,n] = [numa,n] - [numb,n] - c
定义 sub_n.c:67
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 n
#define BENCH_MEASURE(result, options,...)
定义 perf.h:93