LAMMP 4.2.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
add_n_sub_n.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/mparam.h"
17#include "../../../include/lammp/lmmpn.h"
18
19
21 /*
22 这段代码看起来有点奇怪的原因是,对于使用x64汇编时,我们会使用带进位的加法和减法,而x64中
23 只能使用同一个进位寄存器,所以我们需要将两条指令分开执行。
24 而不使用汇编时,编译器通常不会使用进位寄存器。因此我们可以同时读取两路内存,以减少读写次数。
25 */
26#ifdef USE_ASM
27 mp_limb_t acyo = 0, scyo = 0;
29
30 if (dsta != numa && dsta != numb) {
31 for (off = 0; off < n; off += PART_SIZE) {
35 }
36 } else if (dstb != numa && dstb != numb) {
37 for (off = 0; off < n; off += PART_SIZE) {
41 }
42 } else {
44 for (off = 0; off < n; off += PART_SIZE) {
49 }
50 }
51 return 2 * acyo + scyo;
52#else
55
56 for (i = 0, acyo = 0, scyo = 0; i < n; i++) {
57 mp_limb_t a, b, r;
58 a = numa[i];
59 b = numb[i];
60 r = a + acyo;
61 acyo = (r < acyo);
62 r += b;
63 acyo += (r < b);
64 dsta[i] = r;
65
66 b += scyo;
67 scyo = (b < scyo);
68 scyo += (a < b);
69 dstb[i] = a - b;
70 }
71 return 2 * acyo + scyo;
72#endif
73}
mp_limb_t lmmp_add_n_sub_n_(mp_ptr dsta, mp_ptr dstb, mp_srcptr numa, mp_srcptr numb, mp_size_t n)
Copyright (C) 2026 HJimmyK(Jericho Knox)
Definition add_n_sub_n.c:20
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
const mp_limb_t * mp_srcptr
Definition lmmp.h:81
uint64_t mp_limb_t
Definition lmmp.h:76
#define LMMP_MIN(l, o)
Definition lmmp.h:351
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:19
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:19
#define PART_SIZE
Definition mparam.h:89
#define numb
#define tp
#define n