LMMP 4.3.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
shr.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 (shr == 0) {
28 lmmp_copy(dst, numa, na);
29 return 0;
30 }
31 const mp_size_t rshr = LIMB_BITS - shr;
32 mp_limb_t retval = numa[0] << rshr;
33 mp_size_t i = 0;
34 for (; i + 1 < na; i++) dst[i] = (numa[i] >> shr) | (numa[i + 1] << rshr);
35 dst[i] = numa[i] >> shr;
36 return retval;
37}
38
40 if (shr == 0) {
41 lmmp_copy(dst, numa, na);
42 return 0;
43 }
44 const mp_size_t rshr = LIMB_BITS - shr;
45 mp_limb_t retval = numa[0] << rshr;
46 mp_size_t i = 0;
47 for (; i + 1 < na; i++) dst[i] = (numa[i] >> shr) | (numa[i + 1] << rshr);
48 c &= ~(((mp_limb_t)1 << rshr) - 1);
49 dst[i] = (numa[i] >> shr) | c;
50 return retval;
51}
52
54 __uint128_t pr = (__uint128_t)numa[0] + numb[0] + c;
55 mp_limb_t l = (mp_limb_t)pr & 1;
56 for (mp_size_t i = 1; i < n; i++) {
57 __uint128_t cr = (__uint128_t)numa[i] + numb[i] + (mp_limb_t)(pr >> 64);
58 dst[i - 1] = ((mp_limb_t)pr >> 1) | ((mp_limb_t)cr << 63);
59 pr = cr;
60 }
61 dst[n - 1] = ((mp_limb_t)pr >> 1) | ((mp_limb_t)(pr >> 64) << 63);
62 return l;
63}
64
68
70 mp_limb_t o = numa[0];
71 mp_limb_t u = o - numb[0];
72 mp_limb_t pr = u - c;
73 mp_limb_t d = (u > o) + (pr > u);
74 mp_limb_t l = pr & 1;
75 for (mp_size_t i = 1; i < n; i++) {
76 o = numa[i];
77 u = o - numb[i];
78 mp_limb_t r = u - d;
79 d = (u > o) + (r > u);
80 dst[i - 1] = (pr >> 1) | (r << 63);
81 pr = r;
82 }
83 dst[n - 1] = (pr >> 1) | (d << 63);
84 return l;
85}
86
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_shr1add_nc_(mp_ptr dst, mp_srcptr numa, mp_srcptr numb, mp_size_t n, mp_limb_t c)
带进位加法后右移1位 [dst,n] = ([numa,n] + [numb,n] + c) >> 1
定义 shr.c:53
mp_limb_t lmmp_shr_c_(mp_ptr dst, mp_srcptr numa, mp_size_t na, mp_size_t shr, mp_limb_t c)
带进位的右移操作 [dst,na] = [numa,na]>>shr,dst的高shr位填充c的高shr位
定义 shr.c:39
mp_limb_t lmmp_shr1add_n_(mp_ptr dst, mp_srcptr numa, mp_srcptr numb, mp_size_t n)
加法后右移1位 [dst,n] = ([numa,n] + [numb,n]) >> 1
定义 shr.c:65
mp_limb_t lmmp_shr_(mp_ptr dst, mp_srcptr numa, mp_size_t na, mp_size_t shr)
右移操作 [dst,na] = [numa,na]>>shr,dst的高shr位填充0
定义 shr.c:26
mp_limb_t lmmp_shr1sub_nc_(mp_ptr dst, mp_srcptr numa, mp_srcptr numb, mp_size_t n, mp_limb_t c)
带借位减法后右移1位 [dst,n] = ([numa,n] - [numb,n] - c) >> 1
定义 shr.c:69
mp_limb_t lmmp_shr1sub_n_(mp_ptr dst, mp_srcptr numa, mp_srcptr numb, mp_size_t n)
减法后右移1位 [dst,n] = ([numa,n] - [numb,n]) >> 1
定义 shr.c:87
#define numb
#define n