LMMP 4.3.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
add_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 mp_size_t i = 0;
30 mp_limb_t cy = c;
31
32 for (; i + 4 <= n; i += 4) {
33 __uint128_t s0 = (__uint128_t)numa[i + 0] + numb[i + 0] + cy;
34 __uint128_t s1 = (__uint128_t)numa[i + 1] + numb[i + 1] + (mp_limb_t)(s0 >> 64);
35 __uint128_t s2 = (__uint128_t)numa[i + 2] + numb[i + 2] + (mp_limb_t)(s1 >> 64);
36 __uint128_t s3 = (__uint128_t)numa[i + 3] + numb[i + 3] + (mp_limb_t)(s2 >> 64);
37 dst[i + 0] = (mp_limb_t)s0;
38 dst[i + 1] = (mp_limb_t)s1;
39 dst[i + 2] = (mp_limb_t)s2;
40 dst[i + 3] = (mp_limb_t)s3;
41 cy = (mp_limb_t)(s3 >> 64);
42 }
43 for (; i < n; i++) {
44 __uint128_t s = (__uint128_t)numa[i] + numb[i] + cy;
45 dst[i] = (mp_limb_t)s;
46 cy = (mp_limb_t)(s >> 64);
47 }
48
49 return cy;
50}
51
53 return lmmp_add_nc_(dst, numa, numb, n, 0);
54}
55
56#else /* !__SIZEOF_INT128__ 朴素参考实现 */
57
59 mp_size_t i = 0;
60 mp_limb_t cy = c;
61
62 for (; i < n; i++) {
63 mp_limb_t a, b, r;
64 a = numa[i];
65 b = numb[i];
66 r = a + cy;
67 cy = (r < cy);
68 r += b;
69 cy += (r < b);
70 dst[i] = r;
71 }
72
73 return cy;
74}
75
77 mp_size_t i = 0;
78 mp_limb_t cy = 0;
79
80 for (; i < n; i++) {
81 mp_limb_t a, b, r;
82 a = numa[i];
83 b = numb[i];
84 r = a + cy;
85 cy = (r < cy);
86 r += b;
87 cy += (r < b);
88 dst[i] = r;
89 }
90
91 return cy;
92}
93
94#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_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
定义 add_n.c:58
mp_limb_t lmmp_add_n_(mp_ptr dst, mp_srcptr numa, mp_srcptr numb, mp_size_t n)
无进位的n位加法 [dst,n] = [numa,n] + [numb,n]
定义 add_n.c:76
#define numb
#define s
#define n