-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallis.go
69 lines (56 loc) · 1.06 KB
/
wallis.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
package pidentities
import (
. "github.com/solidifylabs/specops" //lint:ignore ST1001 SpecOps DSL is designed to be dot-imported
"github.com/solidifylabs/specops/stack"
)
// Wallis implements the Wallis product.
func Wallis() Code {
return convert(wallis)
}
func wallis() (Code, uint8) {
const bits = 127
// https://en.wikipedia.org/wiki/Wallis_product
const (
precision = Inverted(DUP1) + iota
n
_
fourNSq
)
const (
_ = Inverted(SWAP1) + iota
swapN
swapResult
swapFourNSq
)
code := Code{
PUSH(bits),
PUSH(0x049880), // n (~25M gas)
Fn(SHL, precision, PUSH(1)), // result
JUMPDEST("loop"),
stack.SetDepth(3),
Fn(SHL,
PUSH(2),
Fn(MUL, n, n),
), // fourNSq
Fn(SHR,
precision,
Fn(MUL,
Fn(DIV,
Fn(SHL, precision, swapFourNSq),
Fn(SUB, fourNSq, PUSH(1)),
),
/* top = running product */
),
),
Fn(JUMPI,
PUSH("loop"),
Fn(LT,
PUSH(1),
Fn(swapN,
Fn(SUB, n, PUSH(1)),
)),
),
Fn(SHL, PUSH(1) /* top = result */), // identity is π/2
}
return code, bits
}