-
Notifications
You must be signed in to change notification settings - Fork 4
/
shifts.fj
65 lines (54 loc) · 1.36 KB
/
shifts.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
// ---------- Logical Shifts
ns bit {
// Complexity: n(2@-1)
// x[:n] >>= 1
def shr n, x {
.shr n, 1, x
}
// Complexity: n(2@-1)
// x[:n] >>= times
// times is a constant.
def shr n, times, x {
rep(n-times, i) .mov x+i*dw, x+(i+times)*dw
.zero times, x+(n-times)*dw
}
// Complexity: n(2@-1)
// x[:n] >>= times (arithmetic shift right)
// times is a constant.
def shra n, times, x {
rep(n-times, i) .mov x+i*dw, x+(i+times)*dw
rep(times-1, i) .mov x+(n-1-(i+1))*dw, x+(n-1)*dw
}
// Complexity: n(2@-1)
// x[:n] <<= 1
def shl n, x {
.shl n, 1, x
}
// Complexity: n(2@-1)
// x[:n] <<= times
// times is a constant.
def shl n, times, x {
rep(n-times, i) .mov x+(n-1-i)*dw, x+(n-1-i-times)*dw
.zero times, x
}
// Complexity: n(2@-1)
// rotate x[:n] right by 1-bit
def ror n, x @ temp_bit {
.mov temp_bit, x
rep(n-1, i) .mov x+i*dw, x+(i+1)*dw
.mov x+(n-1)*dw, temp_bit
stl.skip
temp_bit:
.bit
}
// Complexity: n(2@-1)
// rotate x[:n] left by 1-bit
def rol n, x @ temp_bit {
.mov temp_bit, x+(n-1)*dw
rep(n-1, i) .mov x+(n-1-i)*dw, x+(n-1-i-1)*dw
.mov x, temp_bit
stl.skip
temp_bit:
.bit
}
}