-
Notifications
You must be signed in to change notification settings - Fork 4
/
math.fj
100 lines (90 loc) · 2.23 KB
/
math.fj
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// ---------- Arithmetical Macros
// carry is both input and output
ns bit {
// Unsafe for dst==carry (but there is no reason in calling it that way)
// Complexity: 2@+6
// {carry:dst}++
// carry,dst are bits.
def inc1 dst, carry @ continue {
.inc.inc1_with_carry0_jump dst, carry, continue
continue:
}
ns inc {
// Unsafe for dst==carry (but there is no reason in calling it that way)
// If carry was 0, jump to carry0_jump.
// Complexity: 2@+6
// {carry:dst}++
// carry,dst are bits.
def inc1_with_carry0_jump dst, carry, carry0_jump @ end {
..if0 carry, carry0_jump
..not dst
..if0 dst, end
..not carry
end:
}
}
// Time Complexity: 5@+12 in average
// Space Complexity: n(2@+6)
// x[:n]++
// x is a bit[:n]
def inc n, x @ carry, end {
.one carry
rep(n, i) .inc.inc1_with_carry0_jump x+i*dw, carry, end
;end
carry:
.bit
end:
}
// Time Complexity: 2n + 5@+12
// Space Complexity: n(2@+6)
// x[:n]--
// x is a bit[:n]
def dec n, x {
.not n, x
.inc n, x
.not n, x
}
// Time Complexity: n + 5@+12
// Space Complexity: n(2@+6)
// x[:n]--
// x is a bit[:n]
def neg n, x {
.not n, x
.inc n, x
}
// Unsafe for dst==carry (but there is no reason in calling it that way)
// Complexity: 8@+14
// {carry:dst} += src
// dst,src,carry are bits.
def add1 dst, src, carry @ _src {
.mov _src, src
.inc1 dst, _src
.inc1 dst, carry
.or carry, _src
stl.skip
_src:
.bit
}
// Complexity: n(8@+14)
// dst[:n] += src[:n]
// dst,src are bit[:n]
def add n, dst, src @ carry {
.zero carry
rep(n, i) .add1 dst+i*dw, src+i*dw, carry
stl.skip
carry:
.bit
}
// Complexity: n(8@+16)
// dst[:n] -= src[:n]
// dst,src are bit[:n]
def sub n, dst, src @ carry {
.not n, src
.one carry
rep(n, i) .add1 dst+i*dw, src+i*dw, carry
.not n, src
stl.skip
carry:
.bit
}
}