LMMP 4.3.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
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#if defined(__SIZEOF_INT128__)
27
29 /*
30 a - b - c = a + ~b + (1 - c), 借位补码化后与 add_nc_ 完全同构,
31 借用编译器对 3 项 128 位加法的并行 add/adc 分解,消除减法借位
32 提取(shr+neg)在迭代间串行链上的开销。~b 须先读入局部量:
33 表达式内直接 ~numb[i] 会被 clang 向量化(成对的 movdqu/pxor),
34 破坏进位链模式匹配,实测劣化约 2 倍。
35 */
36 mp_size_t i = 0;
37 mp_limb_t cy = 1 - c;
38
39 for (; i + 4 <= n; i += 4) {
40 mp_limb_t nb0 = ~numb[i + 0], nb1 = ~numb[i + 1], nb2 = ~numb[i + 2], nb3 = ~numb[i + 3];
41 __uint128_t s0 = (__uint128_t)numa[i + 0] + nb0 + cy;
42 __uint128_t s1 = (__uint128_t)numa[i + 1] + nb1 + (mp_limb_t)(s0 >> 64);
43 __uint128_t s2 = (__uint128_t)numa[i + 2] + nb2 + (mp_limb_t)(s1 >> 64);
44 __uint128_t s3 = (__uint128_t)numa[i + 3] + nb3 + (mp_limb_t)(s2 >> 64);
45 dst[i + 0] = (mp_limb_t)s0;
46 dst[i + 1] = (mp_limb_t)s1;
47 dst[i + 2] = (mp_limb_t)s2;
48 dst[i + 3] = (mp_limb_t)s3;
49 cy = (mp_limb_t)(s3 >> 64);
50 }
51 for (; i < n; i++) {
52 mp_limb_t nb = ~numb[i];
53 __uint128_t s = (__uint128_t)numa[i] + nb + cy;
54 dst[i] = (mp_limb_t)s;
55 cy = (mp_limb_t)(s >> 64);
56 }
57
58 return 1 - cy;
59}
60
62 return lmmp_sub_nc_(dst, numa, numb, n, 0);
63}
64
65#else /* !__SIZEOF_INT128__ 朴素参考实现 */
66
68 mp_size_t i = 0;
69 mp_limb_t cy = c;
70
71 for (; i < n; i++) {
72 mp_limb_t a, b;
73 a = numa[i];
74 b = numb[i];
75 b += cy;
76 cy = (b < cy);
77 cy += (a < b);
78 dst[i] = a - b;
79 }
80
81 return cy;
82}
83
85 mp_size_t i = 0;
86 mp_limb_t cy = 0;
87
88 for (; i < n; i++) {
89 mp_limb_t a, b;
90 a = numa[i];
91 b = numb[i];
92 b += cy;
93 cy = (b < cy);
94 cy += (a < b);
95 dst[i] = a - b;
96 }
97
98 return cy;
99}
100
101#endif /* __SIZEOF_INT128__ */
mp_limb_t * mp_ptr
定义 lmmp.h:96
uint64_t mp_size_t
定义 lmmp.h:93
const mp_limb_t * mp_srcptr
定义 lmmp.h:97
uint64_t mp_limb_t
定义 lmmp.h:92
mp_limb_t lmmp_sub_n_(mp_ptr dst, mp_srcptr numa, mp_srcptr numb, mp_size_t n)
无借位的n位减法 [dst,n] = [numa,n] - [numb,n]
定义 sub_n.c:84
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
定义 sub_n.c:67
#define numb
#define s
#define n