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
22
ulong
lmmp_sqrt_ulong_
(
ulong
a
) {
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
33
mp_limb_t
lmmp_sqrt_1_
(
mp_ptr
dsts
,
mp_limb_t
x
) {
34
lmmp_param_assert
(
x
>=
LIMB_B_4
);
35
mp_limb_t
s
=
lmmp_sqrt_ulong_
(
x
);
36
*
dsts
=
s
;
37
return
x
-
s
*
s
;
38
}
39
40
mp_limb_t
lmmp_sqrt_2_
(
mp_ptr
dsts
,
mp_ptr
dstr
,
mp_srcptr
numa
) {
41
lmmp_param_assert
(
numa
[1] >=
LIMB_B_4
);
42
mp_limb_t
rl
,
s
,
q
,
al
,
u
;
43
mp_slimb_t
rh
;
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_ptr
mp_limb_t * mp_ptr
Definition
lmmp.h:117
mp_slimb_t
int64_t mp_slimb_t
Definition
lmmp.h:115
mp_srcptr
const mp_limb_t * mp_srcptr
Definition
lmmp.h:118
mp_limb_t
uint64_t mp_limb_t
Definition
lmmp.h:113
lmmp_param_assert
#define lmmp_param_assert(x)
Definition
lmmp.h:496
LIMB_B_4
#define LIMB_B_4
Definition
mparam.h:159
s
#define s
n
#define n
ulong
uint64_t ulong
Definition
numth.h:32
lmmp_sqrt_1_
mp_limb_t lmmp_sqrt_1_(mp_ptr dsts, mp_limb_t x)
计算算术平方根 floor(sqrt(x))
Definition
sqrt_1.c:33
lmmp_sqrt_ulong_
ulong lmmp_sqrt_ulong_(ulong a)
Copyright (C) 2026 HJimmyK(Jericho Knox)
Definition
sqrt_1.c:22
lmmp_sqrt_2_
mp_limb_t lmmp_sqrt_2_(mp_ptr dsts, mp_ptr dstr, mp_srcptr numa)
计算算术平方根 floor(sqrt([numa,2]))
Definition
sqrt_1.c:40
src
lammp
numth
sqrt_1.c
生成于 2026年 八月 9日 星期日 02:53:58 , 为 LAMMP使用
1.9.8