LAMMP 4.2.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
powlo.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/tmp_alloc.h"
17#include "../../../include/lammp/impl/longlong.h"
18#include "../../../include/lammp/impl/inlines.h"
19#include "../../../include/lammp/impl/mparam.h"
20#include "../../../include/lammp/lmmpn.h"
21#include "../../../include/lammp/numth.h"
22
23
24static inline void lmmp_sqrlo_n_(
29) {
30 if (n < MULLO_DC_THRESHOLD) {
32 } else {
34 }
35}
36
37static inline void lmmp_mullo_n_(
43) {
44 if (n < MULLO_DC_THRESHOLD) {
46 } else {
48 }
49}
50
53 static mp_bitcnt_t x[] = {7, 25, 81, 241, 673, 1793, 4609, 11521, 28161, ~(mp_bitcnt_t)0};
54 for (k = 0; eb > x[k++];);
55 return k;
56}
57
58#define getbit(p, bi) ((p[(bi - 1) / LIMB_BITS] >> (bi - 1) % LIMB_BITS) & 1)
59
62 mp_limb_t r;
64
65 if (bi <= nbits) {
66 return p[0] & (((mp_limb_t)1 << bi) - 1);
67 } else {
68 bi -= nbits; /* bit index of low bit to extract */
69 i = bi / LIMB_BITS; /* word index of low bit to extract */
70 bi %= LIMB_BITS; /* bit index in low word */
71 r = p[i] >> bi; /* extract (low) bits */
72 nbits_in_r = LIMB_BITS - bi; /* number of bits now in r */
73 if (nbits_in_r < nbits) /* did we get enough bits? */
74 r += p[i + 1] << nbits_in_r; /* prepend bits from higher word */
75 return r & (((mp_limb_t)1 << nbits) - 1);
76 }
77}
78
80 return (n - 1) * LIMB_BITS + lmmp_limb_bits_(p[n - 1]);
81}
82
84 lmmp_param_assert(ep[en - 1] > 0);
85 lmmp_param_assert(n > 0 && en > 0);
86 lmmp_param_assert(dst != NULL && bp != NULL && ep != NULL);
90 mp_limb_t* pp;
91 long i;
92 int flipflop;
95 mp_ptr restrict scratch = tp + 3 * n;
96
97 ebi = count_bits(ep, en);
98
100 if (windowsize > 1) {
103
104 pp = TALLOC_TYPE((n << (windowsize - 1)), mp_limb_t);
105
106 this_pp = pp;
107
109
110 /* Store b^2 in tp. */
112
113 /* Precompute odd powers of b and put them in the temporary area at pp. */
114 i = (1 << (windowsize - 1)) - 1;
115 do {
117 this_pp += n;
119 } while (--i != 0);
120
122 ebi -= windowsize;
123
125 ebi += cnt;
126
127 lmmp_copy(dst, pp + n * (expbits >> 1), n);
128 } else {
129 pp = tp + n;
130 lmmp_copy(pp, bp, n);
131 lmmp_copy(dst, bp, n);
132 --ebi;
133 }
134
135 flipflop = 0;
136
137 do {
138 while (getbit(ep, ebi) == 0) {
142 if (--ebi == 0)
143 goto done;
144 }
145
146 /* The next bit of the exponent is 1. Now extract the largest block of
147 bits <= windowsize, and such that the least significant bit is 1. */
148
151
155
156 while (this_windowsize > 1) {
159 this_windowsize -= 2;
160 }
161
162 if (this_windowsize != 0)
164 else {
167 }
168 lmmp_mullo_n_(dst, tp, pp + n * (expbits >> 1), n, scratch);
169 } while (ebi != 0);
170
171done:
172 if (flipflop)
173 lmmp_copy(tp, dst, n);
174 TEMP_FREE;
175}
#define k
#define lmmp_limb_bits_
Definition inlines.h:162
mp_limb_t * mp_ptr
Definition lmmp.h:117
#define LMMP_SWAP(x, y, type)
交换两个变量的值
Definition lmmp.h:445
#define lmmp_copy(dst, src, n)
Definition lmmp.h:461
size_t mp_bitcnt_t
Definition lmmp.h:119
uint64_t mp_size_t
Definition lmmp.h:114
#define lmmp_debug_assert(x)
Definition lmmp.h:485
const mp_limb_t * mp_srcptr
Definition lmmp.h:118
uint64_t mp_limb_t
Definition lmmp.h:113
#define LMMP_MIN(l, o)
返回两个数中的较小值
Definition lmmp.h:437
#define LIMB_BITS
Definition lmmp.h:123
#define lmmp_param_assert(x)
Definition lmmp.h:496
void lmmp_mullo_dc_(mp_ptr dst, mp_srcptr numa, mp_srcptr numb, mp_ptr tp, mp_size_t n)
低位乘法 [dst,n] = [numa,n] * [numb,n] mod B^n
void lmmp_sqrlo_dc_(mp_ptr dst, mp_srcptr numa, mp_ptr tp, mp_size_t n)
低位平方 [dst,n] = [numa,n]^2 mod B^n
void lmmp_mullo_fft_(mp_ptr dst, mp_srcptr numa, mp_srcptr numb, mp_size_t n, mp_ptr scratch)
低位FFT乘法 [dst,n] = [numa,n] * [numb,n] mod B^n
Definition mullo.c:22
#define ctz_shr_u64(r, x, cnt)
Copyright (C) 2026 HJimmyK(Jericho Knox)
Definition longlong.h:74
#define MULLO_DC_THRESHOLD
Definition mparam.h:59
#define numb
#define tp
#define n
#define scratch
static mp_size_t win_size(mp_size_t eb)
Definition powlo.c:51
#define getbit(p, bi)
Definition powlo.c:58
static mp_bitcnt_t count_bits(mp_srcptr p, mp_size_t n)
Definition powlo.c:79
void lmmp_powlo_(mp_ptr restrict dst, mp_srcptr restrict bp, mp_size_t n, mp_srcptr restrict ep, mp_size_t en)
Definition powlo.c:83
static mp_limb_t getbits(const mp_limb_t *p, mp_bitcnt_t bi, mp_bitcnt_t nbits)
Definition powlo.c:60
static void lmmp_sqrlo_n_(mp_ptr restrict dst, mp_srcptr restrict numa, mp_size_t n, mp_ptr restrict tp)
Copyright (C) 2026 HJimmyK(Jericho Knox)
Definition powlo.c:24
static void lmmp_mullo_n_(mp_ptr restrict dst, mp_srcptr restrict numa, mp_srcptr restrict numb, mp_size_t n, mp_ptr restrict tp)
Definition powlo.c:37
#define TEMP_DECL
Definition tmp_alloc.h:131
#define TEMP_FREE
Definition tmp_alloc.h:158
#define TALLOC_TYPE(n, type)
Definition tmp_alloc.h:156