LMMP 4.3.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
cmp.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
28 lmmp_param_assert(numa != NULL);
29 lmmp_param_assert(numb != NULL);
30 /* 顶部剥皮 r = 4,1,2,3 个 limb(依 n mod 4,0 取 4):短行程早退窗口,
31 余段长度恰为 4 的倍数,下方 4-limb 步进无需标量尾与运行时版本化 */
32 mp_size_t r = n & 3;
33 if (r == 0) r = 4;
34 for (mp_size_t k = 0; k < r; k++) {
35 mp_limb_t x = numa[n - 1 - k];
36 mp_limb_t y = numb[n - 1 - k];
37 if (x != y)
38 return (x > y ? 1 : -1);
39 }
40 /* 余段自顶向下按 4 limb 一组:异或折叠判差,有差组内自顶定位 */
41 for (mp_size_t i = n - r; i != 0;) {
42 i -= 4;
43 mp_limb_t d = (numa[i + 0] ^ numb[i + 0]) | (numa[i + 1] ^ numb[i + 1]) |
44 (numa[i + 2] ^ numb[i + 2]) | (numa[i + 3] ^ numb[i + 3]);
45 if (d) {
46 for (mp_size_t j = i + 3; j >= i; j--) {
47 if (numa[j] != numb[j])
48 return (numa[j] > numb[j] ? 1 : -1);
49 }
50 }
51 }
52 return 0;
53}
54
57 lmmp_param_assert(p != NULL);
58 /* 同 lmmp_cmp_ 的剥皮结构 */
59 mp_size_t r = n & 3;
60 if (r == 0) r = 4;
61 for (mp_size_t k = 0; k < r; k++) {
62 if (p[n - 1 - k] != 0)
63 return 0;
64 }
65 /* 余段按 4 limb 一组,或折叠非零检测 */
66 for (mp_size_t i = n - r; i != 0;) {
67 i -= 4;
68 if ((p[i] | p[i + 1] | p[i + 2] | p[i + 3]) != 0)
69 return 0;
70 }
71 return 1;
72}
#define k
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 lmmp_param_assert(x)
定义 lmmp.h:509
int lmmp_cmp_(mp_srcptr numa, mp_srcptr numb, mp_size_t n)
比较函数
定义 cmp.c:26
int lmmp_zero_q_(mp_srcptr p, mp_size_t n)
判零函数
定义 cmp.c:55
#define numb
#define n