LMMP
4.3.0
Lamina High-Precision Arithmetic Library
Toggle main menu visibility
载入中...
搜索中...
未找到
factorial.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 "
support/perf.h
"
24
#include "
lmmp/lmmpn.h
"
25
#include "
lmmp/numth.h
"
26
27
static
double
bench_factorial
(
uint
n
,
const
bench_options
*o) {
28
mp_bitcnt_t
bits = 0;
29
mp_size_t
need =
lmmp_factorial_size_
(
n
, &bits);
30
mp_ptr
d = alloc_limbs(need + 2);
31
double
us;
32
BENCH_MEASURE
(us, o,
lmmp_factorial_
(d, bits, need,
n
); bench_sink += d[0];);
33
lmmp_free
(d);
34
return
us;
35
}
36
37
static
void
run_factorial
(
void
) {
38
const
uint
SZ[] = {100, 1000, 10000};
39
for
(
size_t
i = 0; i <
ARRAY_SIZE
(SZ); ++i) {
40
uint
n
= SZ[i];
41
char
buf[64];
42
snprintf(buf,
sizeof
(buf),
"factorial(%u)"
,
n
);
43
report(buf,
bench_factorial
(
n
, &bench_default), 0);
44
}
45
}
46
47
int
main
(
void
) {
48
setvbuf(stdout, NULL, _IONBF, 0);
49
lmmp_global_init
();
50
puts(
"[numth/factorial/factorial]"
);
51
run_factorial
();
52
lmmp_global_deinit
();
53
return
0;
54
}
main
int main(void)
Copyright (C) 2026 HJimmyK (Jericho Knox)
定义
main.c:26
mp_ptr
mp_limb_t * mp_ptr
定义
lmmp.h:96
mp_bitcnt_t
size_t mp_bitcnt_t
定义
lmmp.h:98
mp_size_t
uint64_t mp_size_t
定义
lmmp.h:93
lmmp_free
void lmmp_free(void *ptr)
内存释放函数(调用lmmp_heap_free_fn)
定义
memory.c:437
lmmp_global_init
void lmmp_global_init(void)
全局初始化函数(线程局部的)
定义
global.c:58
lmmp_global_deinit
void lmmp_global_deinit(void)
全局释放函数(线程局部的)
定义
global.c:65
lmmpn.h
n
#define n
numth.h
uint
uint32_t uint
定义
numth.h:38
lmmp_factorial_size_
mp_size_t lmmp_factorial_size_(uint n, mp_bitcnt_t *bits)
计算 n!
lmmp_factorial_
mp_size_t lmmp_factorial_(mp_ptr dst, mp_bitcnt_t bits, mp_size_t rn, uint n)
计算 n!
run_factorial
static void run_factorial(void)
定义
factorial.c:37
bench_factorial
static double bench_factorial(uint n, const bench_options *o)
Copyright (C) 2026 HJimmyK (Jericho Knox)
定义
factorial.c:27
perf.h
ARRAY_SIZE
#define ARRAY_SIZE(a)
Copyright (C) 2026 HJimmyK (Jericho Knox)
定义
perf.h:40
BENCH_MEASURE
#define BENCH_MEASURE(result, options,...)
定义
perf.h:93
bench_options
定义
perf.h:44
src
lmmp
numth
perf
factorial.c
生成于
, 为 LMMP使用
1.18.0