LMMP
4.3.0
Lamina High-Precision Arithmetic Library
Toggle main menu visibility
载入中...
搜索中...
未找到
mul_hard.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 "
impl/mul_hard.h
"
24
25
/* 常量行程循环的完全展开提示: clang 用 #pragma unroll (行程数已知时
26
无视代价模型强制全展开), gcc 用 #pragma GCC unroll 上界 (覆盖最大
27
行程 37); 其余编译器退化为普通循环, 语义不变仅性能差异。 */
28
#if defined(__clang__)
29
#define LMMP_UNROLL _Pragma("unroll")
30
#elif defined(__GNUC__)
31
#define LMMP_UNROLL _Pragma("GCC unroll 64")
32
#else
33
#define LMMP_UNROLL
34
#endif
35
36
#define DEF_MUL_HARD(n_) \
37
void lmmp_mul_hard_##n_##_(mp_ptr restrict dst, mp_srcptr restrict numa, mp_srcptr restrict numb) { \
38
mp_limb_t cl = 0; \
39
mp_size_t i, j; \
40
LMMP_UNROLL \
41
for (i = 0; i < n_; i++) { \
42
__uint128_t t = (__uint128_t)numa[i] * numb[0] + cl; \
43
dst[i] = (mp_limb_t)t; \
44
cl = (mp_limb_t)(t >> 64); \
45
} \
46
dst[n_] = cl; \
47
for (j = 1; j < n_; j++) { \
48
cl = 0; \
49
LMMP_UNROLL \
50
for (i = 0; i < n_; i++) { \
51
__uint128_t t = (__uint128_t)numa[i] * numb[j] + dst[i + j] + cl; \
52
dst[i + j] = (mp_limb_t)t; \
53
cl = (mp_limb_t)(t >> 64); \
54
} \
55
dst[n_ + j] = cl; \
56
} \
57
}
58
59
#define DEF_SQR_HARD(n_) \
60
void lmmp_sqr_hard_##n_##_(mp_ptr restrict dst, mp_srcptr restrict numa) { \
61
mp_limb_t cl, x; \
62
mp_size_t i, j; \
63
dst[0] = 0; \
64
x = numa[0]; \
65
cl = 0; \
66
LMMP_UNROLL \
67
for (i = 1; i < n_; i++) { \
68
__uint128_t t = (__uint128_t)numa[i] * x + cl; \
69
dst[i] = (mp_limb_t)t; \
70
cl = (mp_limb_t)(t >> 64); \
71
} \
72
dst[n_] = cl; \
73
/* i: dst[i+1..i+n] += a[i+1..n)*a_i */
\
74
LMMP_UNROLL \
75
for (j = 1; j + 1 < n_; j++) { \
76
x = numa[j]; \
77
cl = 0; \
78
LMMP_UNROLL \
79
for (i = j + 1; i < n_; i++) { \
80
__uint128_t t = (__uint128_t)numa[i] * x + dst[i + j] + cl; \
81
dst[i + j] = (mp_limb_t)t; \
82
cl = (mp_limb_t)(t >> 64); \
83
} \
84
dst[n_ + j] = cl; \
85
} \
86
/* dst[1..2n-2] = 2*dst, 2n-1 = carry(0) */
\
87
cl = 0; \
88
LMMP_UNROLL \
89
for (i = 1; i + 1 < 2 * n_; i++) { \
90
__uint128_t t = (__uint128_t)dst[i] * 2 + cl; \
91
dst[i] = (mp_limb_t)t; \
92
cl = (mp_limb_t)(t >> 64); \
93
} \
94
dst[2 * n_ - 1] = cl; \
95
cl = 0; \
96
LMMP_UNROLL \
97
for (i = 0; i < n_; i++) { \
98
__uint128_t t = (__uint128_t)numa[i] * numa[i] + dst[2 * i] + cl; \
99
dst[2 * i] = (mp_limb_t)t; \
100
cl = (mp_limb_t)(t >> 64); \
101
t = (__uint128_t)dst[2 * i + 1] + cl; \
102
dst[2 * i + 1] = (mp_limb_t)t; \
103
cl = (mp_limb_t)(t >> 64); \
104
} \
105
}
106
107
DEF_MUL_HARD
(1)
108
DEF_MUL_HARD
(2)
109
DEF_MUL_HARD
(3)
110
DEF_MUL_HARD
(4)
111
DEF_MUL_HARD
(5)
112
DEF_MUL_HARD
(6)
113
DEF_MUL_HARD
(7)
114
DEF_MUL_HARD
(8)
115
DEF_MUL_HARD
(9)
116
DEF_MUL_HARD
(10)
117
DEF_MUL_HARD
(11)
118
DEF_MUL_HARD
(12)
119
DEF_MUL_HARD
(13)
120
DEF_MUL_HARD
(14)
121
DEF_MUL_HARD
(15)
122
DEF_MUL_HARD
(16)
123
DEF_MUL_HARD
(17)
124
DEF_MUL_HARD
(18)
125
DEF_MUL_HARD
(19)
126
127
DEF_SQR_HARD
(1)
128
DEF_SQR_HARD
(2)
129
DEF_SQR_HARD
(3)
130
DEF_SQR_HARD
(4)
131
DEF_SQR_HARD
(5)
132
DEF_SQR_HARD
(6)
133
DEF_SQR_HARD
(7)
134
DEF_SQR_HARD
(8)
135
DEF_SQR_HARD
(9)
136
DEF_SQR_HARD
(10)
137
DEF_SQR_HARD
(11)
138
DEF_SQR_HARD
(12)
139
DEF_SQR_HARD
(13)
140
DEF_SQR_HARD
(14)
141
DEF_SQR_HARD
(15)
142
DEF_SQR_HARD
(16)
143
DEF_SQR_HARD
(17)
144
DEF_SQR_HARD
(18)
145
DEF_SQR_HARD
(19)
DEF_MUL_HARD
#define DEF_MUL_HARD(n_)
定义
mul_hard.c:36
DEF_SQR_HARD
#define DEF_SQR_HARD(n_)
定义
mul_hard.c:59
mul_hard.h
src
lmmp
lmmpn
arch
generic
mul_hard.c
生成于
, 为 LMMP使用
1.18.0