-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbp.go
106 lines (93 loc) · 1.55 KB
/
bbp.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
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
101
102
103
104
105
106
package pidentities
import (
. "github.com/solidifylabs/specops" //lint:ignore ST1001 SpecOps DSL is designed to be dot-imported
"github.com/solidifylabs/specops/stack"
)
// BBP implements the Bailey–Borwein–Plouffe formula.
func BBP() Code {
return convert(bbp)
}
func bbp() (Code, uint8) {
const bits = 252
// https://en.wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%93Plouffe_formula
const (
_ = Inverted(DUP1) + iota // 16k
eightK
fourK
one
four
five
six
eight
bigone
bigTwo
bigFour
sum
)
const (
swapsixteenK = Inverted(SWAP1) + iota
swapEightK
swapFourK
)
code := Code{
PUSH0, // 16k
PUSH0, // 8k
PUSH0, // 4k
PUSH(1),
PUSH(4),
PUSH(5),
PUSH(6),
PUSH(8),
Fn(SHL, PUSH(bits), one),
Fn(MUL, bigone, PUSH(2)),
Fn(MUL, bigone, four),
PUSH0, // sum
}
fracs := Code{
Fn(SUB,
Fn(DIV,
bigFour,
Fn(ADD, eightK, one),
),
Fn(ADD,
Fn(DIV,
bigTwo,
Fn(ADD, eightK, four),
),
Fn(ADD,
Fn(DIV,
bigone,
Fn(ADD, eightK, five),
),
Fn(DIV,
bigone,
Fn(ADD, eightK, six),
),
),
),
),
}
return append(
code,
stack.ExpectDepth(12),
JUMPDEST("loop"),
stack.SetDepth(12),
Fn(ADD,
Fn(SHR, fourK, fracs),
/* sum on top*/
),
Fn(swapEightK,
Fn(ADD, eightK, eight),
), POP,
Fn(swapsixteenK,
Fn(SHL, one, eightK),
), POP,
Fn(swapFourK,
Fn(SHR, one, eightK),
), // Deliberately not popping, to use in loop check
Fn(JUMPI,
PUSH("loop"),
Fn(GT, PUSH(1+bits*4) /* old 4k */),
),
), bits
}