LMMP 4.3.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
shl.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
27 if (shl == 0) {
28 lmmp_copy(dst, numa, na);
29 return 0;
30 }
31 const mp_size_t rshr = LIMB_BITS - shl;
32 mp_limb_t retval = numa[na - 1] >> rshr;
33 // 降序:dst==numa 原地时每个 numa[i-1] 在被覆盖前读取
34 for (mp_size_t i = na - 1; i > 0; i--) dst[i] = (numa[i] << shl) | (numa[i - 1] >> rshr);
35 dst[0] = numa[0] << shl;
36 return retval;
37}
38
40 if (shl == 0) {
41 lmmp_copy(dst, numa, na);
42 return 0;
43 }
44 const mp_size_t rshr = LIMB_BITS - shl;
45 mp_limb_t retval = numa[na - 1] >> rshr;
46 c &= ((mp_limb_t)1 << shl) - 1;
47 // 同 lmmp_shl_:降序写入保证原地正确
48 for (mp_size_t i = na - 1; i > 0; i--) dst[i] = (numa[i] << shl) | (numa[i - 1] >> rshr);
49 dst[0] = (numa[0] << shl) | c;
50 return retval;
51}
52
54 mp_limb_t c = 0, mb = 0;
55 mp_size_t i = 0;
56
57#define LMMP_ADDSHL1_STEP(k) \
58 { \
59 mp_limb_t v = numb[i + (k)]; \
60 mp_limb_t lo = (v << 1) | mb; \
61 mb = v >> (LIMB_BITS - 1); \
62 __uint128_t s = (__uint128_t)lo + numa[i + (k)] + c; \
63 dst[i + (k)] = (mp_limb_t)s; \
64 c = (mp_limb_t)(s >> 64); \
65 }
66
67 for (; i + 4 <= n; i += 4) {
72 }
73 for (; i < n; i++) {
75 }
76#undef LMMP_ADDSHL1_STEP
77 return c + mb;
78}
79
81 mp_limb_t d = 0, mb = 0;
82 mp_size_t i = 0;
83
84#define LMMP_SUBSHL1_STEP(k) \
85 { \
86 mp_limb_t v = numb[i + (k)]; \
87 mp_limb_t lo = (v << 1) | mb; \
88 mb = v >> (LIMB_BITS - 1); \
89 mp_limb_t o = numa[i + (k)]; \
90 mp_limb_t u = o - lo; \
91 mp_limb_t r = u - d; \
92 d = (u > o) + (r > u); \
93 dst[i + (k)] = r; \
94 }
95
96 for (; i + 4 <= n; i += 4) {
101 }
102 for (; i < n; i++) {
104 }
105#undef LMMP_SUBSHL1_STEP
106 return d + mb;
107}
mp_limb_t * mp_ptr
定义 lmmp.h:96
#define lmmp_copy(dst, src, n)
定义 lmmp.h:474
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
#define LIMB_BITS
定义 lmmp.h:102
mp_limb_t lmmp_subshl1_n_(mp_ptr dst, mp_srcptr numa, mp_srcptr numb, mp_size_t n)
减法结合左移1位操作 [dst,n] = [numa,n] - ([numb,n] << 1)
定义 shl.c:80
mp_limb_t lmmp_shl_c_(mp_ptr dst, mp_srcptr numa, mp_size_t na, mp_size_t shl, mp_limb_t c)
带进位的左移操作 [dst,na] = [numa,na]<<shl,dst的低shl位填充c的低shl位
定义 shl.c:39
mp_limb_t lmmp_addshl1_n_(mp_ptr dst, mp_srcptr numa, mp_srcptr numb, mp_size_t n)
加法结合左移1位操作 [dst,n] = [numa,n] + ([numb,n] << 1)
定义 shl.c:53
mp_limb_t lmmp_shl_(mp_ptr dst, mp_srcptr numa, mp_size_t na, mp_size_t shl)
左移操作 [dst,na] = [numa,na]<<shl,dst的低shl位填充0
定义 shl.c:26
#define numb
#define n
#define LMMP_SUBSHL1_STEP(k)
#define LMMP_ADDSHL1_STEP(k)