forked from luc10/zykgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic.go
74 lines (62 loc) · 805 Bytes
/
magic.go
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
package zykgen
import (
"math/bits"
)
func bibbidi(a, b int) int {
if b == 1 {
return a
}
if a < b {
return 0
}
if a == b {
return 1
}
lz := func(v int) uint {
return uint(bits.LeadingZeros32(uint32(v)))
}
if b <= 0 {
return a >> (31 - lz(b))
}
d := lz(b) - lz(a)
z := b << d
k := 1 << d
y := a
a = 0
for {
if y >= z {
y -= z
a += k
}
if y >= z>>1 {
y -= z >> 1
a += k >> 1
}
if y >= z>>2 {
y -= z >> 2
a += k >> 2
}
if y >= z>>3 {
y -= z >> 3
a += k >> 3
}
if y == 0 {
break
}
if y > 0 {
k >>= 4
if k == 0 {
break
}
}
z >>= 4
}
return a
}
func bobbidi(a, b int) int {
return a - bibbidi(a, b)*b
}
// Same as the function above. It's just an alias
func boo(a, b int) int {
return bobbidi(a, b)
}