LAMMP 4.2.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
sqrt_1.c
浏览该文件的文档.
1/**
2 * Copyright (C) 2026 HJimmyK(Jericho Knox)
3 *
4 * This file is part of LAMMP.
5 *
6 * LAMMP is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU Lesser General Public License (LGPL) as published
8 * by the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed WITHOUT ANY WARRANTY.
12 *
13 * See <https://www.gnu.org/licenses/>.
14 */
15
16#include <math.h>
17
18#include "../../../include/lammp/impl/mparam.h"
19#include "../../../include/lammp/numth.h"
20
21
23 ulong is;
24
25 is = (ulong)sqrt((double)a);
26
27 is -= (is * is > a);
28 if (is == (1ULL << 32))
29 is--;
30 return is;
31}
32
39
42 mp_limb_t rl, s, q, al, u;
44
45 rl = lmmp_sqrt_1_(&s, numa[1]);
46 al = numa[0];
47
48 //(r:alh)/2
49 rl = rl << 31 | al >> 33;
50 q = rl / s;
51 q -= q >> 32;
52
53 u = rl - s * q;
54 s = s << 32 | q;
55 rh = u >> 31;
56 rl = (u << 33) | (al & (((mp_limb_t)1 << 33) - 1));
57
58 q *= q;
59 rh -= rl < q;
60 rl -= q;
61 if (rh < 0) {
62 rl += s;
63 rh += rl < s;
64 --s;
65 rl += s;
66 rh += rl < s;
67 }
68
69 dsts[0] = s;
70 dstr[0] = rl;
71 return rh;
72}
mp_limb_t * mp_ptr
Definition lmmp.h:117
int64_t mp_slimb_t
Definition lmmp.h:115
const mp_limb_t * mp_srcptr
Definition lmmp.h:118
uint64_t mp_limb_t
Definition lmmp.h:113
#define lmmp_param_assert(x)
Definition lmmp.h:496
#define LIMB_B_4
Definition mparam.h:159
#define s
#define n
uint64_t ulong
Definition numth.h:32
mp_limb_t lmmp_sqrt_1_(mp_ptr dsts, mp_limb_t x)
计算算术平方根 floor(sqrt(x))
Definition sqrt_1.c:33
ulong lmmp_sqrt_ulong_(ulong a)
Copyright (C) 2026 HJimmyK(Jericho Knox)
Definition sqrt_1.c:22
mp_limb_t lmmp_sqrt_2_(mp_ptr dsts, mp_ptr dstr, mp_srcptr numa)
计算算术平方根 floor(sqrt([numa,2]))
Definition sqrt_1.c:40