-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_2.go
65 lines (55 loc) · 1 KB
/
log_2.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
package main
import (
"fmt"
"math/big"
)
func shiftY(y *big.Int, one *big.Int, two *big.Int) (*big.Int, int) {
i := 0
for y.Cmp(two) >= 0 {
i++
y.Rsh(y, 1)
}
for y.Cmp(one) < 0 {
i--
y.Lsh(y, 1)
}
return y, i
}
func getLog2(y *big.Int, precision uint, one *big.Int, two *big.Int) (*big.Int, error) {
if y.Sign() < 0 {
return nil, fmt.Errorf("negative input to GetLog2: %s", y.String())
}
x := big.NewInt(0).Set(y)
z_2, shifted := shiftY(x, one, two)
r := big.NewInt(0)
var sum_m uint = 0
log2loop:
for {
if z_2.Cmp(one) == 0 {
break log2loop
}
z := big.NewInt(0)
z.Mul(z_2, z_2)
z.Rsh(z, uint(precision))
sum_m += 1
if sum_m > precision {
break
}
if z.Cmp(two) >= 0 {
r.Add(r, big.NewInt(0).Rsh(one, sum_m))
z_2.Rsh(z, 1)
} else {
z_2.Set(z)
}
}
sumed := big.NewInt(int64(shifted))
if shifted < 0 {
sumed.Neg(sumed)
sumed.Lsh(sumed, precision)
sumed.Neg(sumed)
} else {
sumed.Lsh(sumed, precision)
}
sumed.Add(sumed, r)
return sumed, nil
}