-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmathlib.func
83 lines (75 loc) · 2.57 KB
/
mathlib.func
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
{-
openlib.func – open-source extended library for Func
Copyright (C) 2023 Continuation Team
This file is part of openlib.func.
openlib.func is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
openlib.func is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with openlib.func. If not, see <https://www.gnu.org/licenses/>.
-}
#pragma version >=0.4.0;
int math::sqrt(int x) asm """
<{
// x
DUP // x x
IFNOTRET // x
181 PUSHINT // x r=181
s1 s1 PUSH2 // x r=181 xx xx
128 PUSHPOW2 // x r=181 xx xx _1=340282366920938463463374607431768211456
GEQ // x r=181 xx _f
IF:<{ // x r=181 xx
128 RSHIFT# // x r xx
SWAP // x xx r
64 LSHIFT# // x xx r
SWAP // x r xx
}> // x r xx
DUP // x r xx xx
64 PUSHPOW2 // x r xx xx _2=18446744073709551616
GEQ // x r xx _f
IF:<{ // x r xx
64 RSHIFT# // x r xx
SWAP // x xx r
32 LSHIFT# // x xx r
SWAP // x r xx
}> // x r xx
DUP // x r xx xx
32 PUSHPOW2 // x r xx xx _3=4294967296
GEQ // x r xx _f
IF:<{ // x r xx
32 RSHIFT# // x r xx
SWAP // x xx r
16 LSHIFT# // x xx r
SWAP // x r xx
}> // x r xx
DUP // x r xx xx
16 PUSHPOW2 // x r xx xx _4=65536
GEQ // x r xx _f
IF:<{ // x r xx
16 RSHIFT# // x r xx
SWAP // x xx r
8 LSHIFT# // x xx r
SWAP // x r xx
}> // x r xx
16 PUSHPOW2 // x r xx _5=65536
ADD // x r _6
MUL // x r
18 RSHIFT# // x r
{
2DUP // x r x r
DIV // x r _7
ADD // x r
1 RSHIFT# // x r
} 7 times
TUCK // r x r
DIV // r r1
2DUP // r r1 r r1
LESS // r r1 _f
-ROT CONDSELCHK // IF:<{ DROP }>ELSE<{ NIP }>
}>c CALLREF // equivalent to inline_ref
""";