-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmath.fc
More file actions
69 lines (53 loc) · 997 Bytes
/
math.fc
File metadata and controls
69 lines (53 loc) · 997 Bytes
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
{-
math.func
Extends FunC's arithmetic operations.
-}
(int) math::pow(int n, int e) {
if (e == 0) {
return 1;
}
if (e == 1) {
return n;
}
int p = math::pow(n, (e / 2));
p = p * p;
if ((e % 2) == 1) {
p = p * n;
}
return p;
}
int math::sqrt(int x) inline {
if (x == 0) { return x; }
int r = 181;
int xx = x;
if (xx >= 0x100000000000000000000000000000000) {
xx >>= 128;
r <<= 64;
}
if (xx >= 0x10000000000000000) {
xx >>= 64;
r <<= 32;
}
if (xx >= 0x100000000) {
xx >>= 32;
r <<= 16;
}
if (xx >= 0x10000) {
xx >>= 16;
r <<= 8;
}
r = (r * (xx + 65536)) >> 18;
repeat(7) {
r = (r + x / r) >> 1;
}
int r1 = x / r;
return (r < r1 ? r : r1);
}
(int) math::avg(int x, int y) inline {
return (x + y) / 2;
}
(int) math::exp(int x) inline {
return (x >= 0 ? 1 << x : 1 >> (x * -1));
}
(int) math::log2(int x) asm "UBITSIZE DEC";
(int) math::mod (int x, int y) asm "MOD";