LAMMP 4.2.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
pow_basecase.c
浏览该文件的文档.
1/**
2 * Copyright (C) 2026 HJimmyK(Jericho Knox)
3 *
4 * This file is part of LAMMP.
5 *
6 * LAMMP is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU Lesser General Public License (LGPL) as published
8 * by the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed WITHOUT ANY WARRANTY.
12 *
13 * See <https://www.gnu.org/licenses/>.
14 */
15
16#include "../../../include/lammp/impl/inlines.h"
17#include "../../../include/lammp/impl/mparam.h"
18#include "../../../include/lammp/impl/tmp_alloc.h"
19#include "../../../include/lammp/lmmpn.h"
20#include "../../../include/lammp/numth.h"
21
22
23#define mul_b(_i_) \
24 lmmp_mul_(dst, sq, rn, b##_i_, b##_i_##n); \
25 rn += b##_i_##n; \
26 rn -= (dst[rn - 1] == 0) ? 1 : 0
27
30 lmmp_param_assert(exp % 2 == 1);
32
33#define b1 base
34#define b1n n
36 rn = n;
37 lmmp_copy(dst, base, n);
38 lmmp_sqr_(sq, dst, rn);
39 rn <<= 1;
40 rn -= (sq[rn - 1] == 0) ? 1 : 0;
42 int i = 62 - lz;
43 exp <<= lz + 1;
44/*
45 For the intermediate 0, we will skip it entirely until the next 1,
46 and then perform a multiplication. This can reduce the extra copying
47 caused by sparse 1s and improve efficiency.
48 */
49 for ( ; i > 0; ) {
51 if (lz == 0) {
52 mul_b(1);
53
54 lmmp_sqr_(sq, dst, rn);
55 rn <<= 1;
56 rn -= (sq[rn - 1] == 0) ? 1 : 0;
57 --i;
58 exp <<= 1;
59 } else {
60 int j = 2;
61 if (lz & 1) {
62 lmmp_copy(dst, sq, rn);
63 lmmp_sqr_(sq, dst, rn);
64 rn <<= 1;
65 rn -= (sq[rn - 1] == 0);
66 ++j;
67 for (; j <= lz; j += 2) {
68 lmmp_sqr_(dst, sq, rn);
69 rn <<= 1;
70 rn -= (dst[rn - 1] == 0);
71 lmmp_sqr_(sq, dst, rn);
72 rn <<= 1;
73 rn -= (sq[rn - 1] == 0);
74 }
75 } else {
76 for (; j <= lz; j += 2) {
77 lmmp_sqr_(dst, sq, rn);
78 rn <<= 1;
79 rn -= (dst[rn - 1] == 0);
80 lmmp_sqr_(sq, dst, rn);
81 rn <<= 1;
82 rn -= (sq[rn - 1] == 0);
83 }
84 }
85
86 i -= lz;
87 exp <<= lz;
88 }
89 }
91
92 mul_b(1);
93
95 return rn;
96
97#undef b1
98#undef b1n
99#undef new_b
100}
#define lmmp_leading_zeros_
Definition inlines.h:160
#define lmmp_sqr_
Definition inlines.h:166
mp_limb_t * mp_ptr
Definition lmmp.h:80
#define lmmp_copy(dst, src, n)
Definition lmmp.h:367
uint64_t mp_size_t
Definition lmmp.h:77
#define lmmp_debug_assert(x)
Definition lmmp.h:390
const mp_limb_t * mp_srcptr
Definition lmmp.h:81
uint64_t mp_limb_t
Definition lmmp.h:76
#define lmmp_param_assert(x)
Definition lmmp.h:401
#define LIMB_B_2
Definition mparam.h:157
#define n
uint64_t ulong
Definition numth.h:32
mp_size_t lmmp_pow_basecase_(mp_ptr restrict dst, mp_size_t rn, mp_srcptr restrict base, mp_size_t n, ulong exp)
#define mul_b(_i_)
Copyright (C) 2026 HJimmyK(Jericho Knox)
#define TEMP_DECL
Definition tmp_alloc.h:131
#define TEMP_FREE
Definition tmp_alloc.h:150
#define TALLOC_TYPE(n, type)
Definition tmp_alloc.h:148