LAMMP 4.1.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
add_n_sub_n.c
浏览该文件的文档.
1/*
2 * LAMMP - Copyright (C) 2025-2026 HJimmyK(Jericho Knox)
3 * This file is part of lammp, under the GNU LGPL v2 license.
4 * See LICENSE in the project root for the full license text.
5 */
6
7#include "../../include/lammp/impl/mparam.h"
8#include "../../include/lammp/lmmpn.h"
9
11 /*
12 这段代码看起来有点奇怪的原因是,对于使用x64汇编时,我们会使用带进位的加法和减法,而x64中
13 只能使用同一个进位寄存器,所以我们需要将两条指令分开执行。
14 而不使用汇编时,编译器通常不会使用进位寄存器。因此我们可以同时读取两路内存,以减少读写次数。
15 */
16#ifdef USE_ASM
17 mp_limb_t acyo = 0, scyo = 0;
18 mp_size_t off, this_n;
19
20 if (dsta != numa && dsta != numb) {
21 for (off = 0; off < n; off += PART_SIZE) {
22 this_n = LMMP_MIN(n - off, PART_SIZE);
23 acyo = lmmp_add_nc_(dsta + off, numa + off, numb + off, this_n, acyo);
24 scyo = lmmp_sub_nc_(dstb + off, numa + off, numb + off, this_n, scyo);
25 }
26 } else if (dstb != numa && dstb != numb) {
27 for (off = 0; off < n; off += PART_SIZE) {
28 this_n = LMMP_MIN(n - off, PART_SIZE);
29 scyo = lmmp_sub_nc_(dstb + off, numa + off, numb + off, this_n, scyo);
30 acyo = lmmp_add_nc_(dsta + off, numa + off, numb + off, this_n, acyo);
31 }
32 } else {
34 for (off = 0; off < n; off += PART_SIZE) {
35 this_n = LMMP_MIN(n - off, PART_SIZE);
36 acyo = lmmp_add_nc_(tp, numa + off, numb + off, this_n, acyo);
37 scyo = lmmp_sub_nc_(dstb + off, numa + off, numb + off, this_n, scyo);
38 lmmp_copy(dsta + off, tp, this_n);
39 }
40 }
41 return 2 * acyo + scyo;
42#else
43 mp_size_t i;
44 mp_limb_t acyo, scyo;
45
46 for (i = 0, acyo = 0, scyo = 0; i < n; i++) {
47 mp_limb_t a, b, r;
48 a = numa[i];
49 b = numb[i];
50 r = a + acyo;
51 acyo = (r < acyo);
52 r += b;
53 acyo += (r < b);
54 dsta[i] = r;
55
56 b += scyo;
57 scyo = (b < scyo);
58 scyo += (a < b);
59 dstb[i] = a - b;
60 }
61 return 2 * acyo + scyo;
62#endif
63}
mp_limb_t lmmp_add_n_sub_n_(mp_ptr dsta, mp_ptr dstb, mp_srcptr numa, mp_srcptr numb, mp_size_t n)
同时执行n位加法和减法 ([dsta,n],[dstb,n]) = ([numa,n]+[numb,n],[numa,n]-[numb,n])
Definition add_n_sub_n.c:10
mp_limb_t * mp_ptr
Definition lmmp.h:215
#define lmmp_copy(dst, src, n)
Definition lmmp.h:364
uint64_t mp_size_t
Definition lmmp.h:212
const mp_limb_t * mp_srcptr
Definition lmmp.h:216
uint64_t mp_limb_t
Definition lmmp.h:211
#define LMMP_MIN(l, o)
Definition lmmp.h:348
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
Definition add_n.c:9
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
Definition sub_n.c:9
#define PART_SIZE
Definition mparam.h:89
#define tp