LMMP
4.3.0
Lamina High-Precision Arithmetic Library
Toggle main menu visibility
载入中...
搜索中...
未找到
add_n_sub_n.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 "
lmmp/lmmpn.h
"
24
25
/*
26
汇编不可用时的通用 C 实现(eqsep 重叠安全:每组加载先于同组存储)。
27
汇编构建使用 asm/{x64,arm64}/add_n_sub_n.S。
28
差链与 sub_n_ 同样补码化: d = a + ~b + k, k0 = 1, 借位 = 1 - k,
29
借用编译器对 3 项 128 位加法的并行 add/adc 分解;~b 须先读入局部量,
30
避免被 clang 向量化破坏进位链模式匹配。
31
*/
32
33
mp_limb_t
lmmp_add_n_sub_n_
(
mp_ptr
dsta,
mp_ptr
dstb,
mp_srcptr
numa,
mp_srcptr
numb
,
mp_size_t
n
) {
34
mp_size_t
i = 0;
35
mp_limb_t
cy = 0;
36
mp_limb_t
k
= 1;
37
38
for
(; i + 4 <=
n
; i += 4) {
39
mp_limb_t
a0
= numa[i + 0],
a1
= numa[i + 1],
a2
= numa[i + 2],
a3
= numa[i + 3];
40
mp_limb_t
b0
=
numb
[i + 0],
b1
=
numb
[i + 1],
b2
=
numb
[i + 2],
b3
=
numb
[i + 3];
41
mp_limb_t
nb0 =
~b0
, nb1 =
~b1
, nb2 =
~b2
, nb3 =
~b3
;
42
43
__uint128_t s0 = (__uint128_t)
a0
+
b0
+ cy;
44
__uint128_t s1 = (__uint128_t)
a1
+
b1
+ (
mp_limb_t
)(s0 >> 64);
45
__uint128_t s2 = (__uint128_t)
a2
+
b2
+ (
mp_limb_t
)(s1 >> 64);
46
__uint128_t s3 = (__uint128_t)
a3
+
b3
+ (
mp_limb_t
)(s2 >> 64);
47
cy = (
mp_limb_t
)(s3 >> 64);
48
49
__uint128_t t0 = (__uint128_t)
a0
+ nb0 +
k
;
50
__uint128_t t1 = (__uint128_t)
a1
+ nb1 + (
mp_limb_t
)(t0 >> 64);
51
__uint128_t t2 = (__uint128_t)
a2
+ nb2 + (
mp_limb_t
)(t1 >> 64);
52
__uint128_t t3 = (__uint128_t)
a3
+ nb3 + (
mp_limb_t
)(t2 >> 64);
53
k
= (
mp_limb_t
)(t3 >> 64);
54
55
dsta[i + 0] = (
mp_limb_t
)s0;
56
dsta[i + 1] = (
mp_limb_t
)s1;
57
dsta[i + 2] = (
mp_limb_t
)s2;
58
dsta[i + 3] = (
mp_limb_t
)s3;
59
dstb[i + 0] = (
mp_limb_t
)t0;
60
dstb[i + 1] = (
mp_limb_t
)t1;
61
dstb[i + 2] = (
mp_limb_t
)t2;
62
dstb[i + 3] = (
mp_limb_t
)t3;
63
}
64
for
(; i <
n
; i++) {
65
mp_limb_t
a = numa[i], b =
numb
[i], nb = ~b;
66
__uint128_t
s
= (__uint128_t)a + b + cy;
67
__uint128_t
t
= (__uint128_t)a + nb +
k
;
68
dsta[i] = (
mp_limb_t
)
s
;
69
cy = (
mp_limb_t
)(
s
>> 64);
70
dstb[i] = (
mp_limb_t
)
t
;
71
k
= (
mp_limb_t
)(
t
>> 64);
72
}
73
74
return
2 * cy + (1 -
k
);
75
}
k
#define k
mp_ptr
mp_limb_t * mp_ptr
定义
lmmp.h:96
mp_size_t
uint64_t mp_size_t
定义
lmmp.h:93
mp_srcptr
const mp_limb_t * mp_srcptr
定义
lmmp.h:97
mp_limb_t
uint64_t mp_limb_t
定义
lmmp.h:92
lmmpn.h
lmmp_add_n_sub_n_
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])
定义
add_n_sub_n.c:33
b0
#define b0
b1
#define b1
a0
#define a0
a1
#define a1
a2
#define a2
b2
#define b2
t
#define t
a3
#define a3
numb
#define numb
s
#define s
n
#define n
b3
#define b3
src
lmmp
lmmpn
arch
generic
add_n_sub_n.c
生成于
, 为 LMMP使用
1.18.0