Skip to content

Commit 0deaac1

Browse files
committed
support math pow for primitive types
1 parent e3db547 commit 0deaac1

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ test/example:
121121
${CELL} -d -t riscv tests/examples/switch.cell && ckb-debugger --bin switch${exe} | grep "five"
122122
${CELL} -d -t riscv tests/examples/number-literal.cell && ckb-debugger --bin number-literal${exe}
123123
${CELL} -d -t riscv tests/examples/to-string.cell && ckb-debugger --bin to-string${exe}
124+
${CELL} -d -t riscv tests/examples/pow.cell && ckb-debugger --bin pow${exe}
124125
${CELL} -d -t riscv tests/examples/return.cell && ckb-debugger --bin return${exe}
125126
${CELL} -d -t riscv tests/examples/named-ret-type.cell && ckb-debugger --bin named-ret-type${exe} | grep "0"
126127
${CELL} -d -t riscv tests/examples/func.cell && ckb-debugger --bin func${exe} | grep "999"

pkg/math/pow.cell

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package math
2+
3+
type uint8 uint8
4+
type uint16 uint16
5+
type uint32 uint32
6+
type uint64 uint64
7+
type uint128 uint128
8+
type uint256 uint256
9+
10+
// x ** y
11+
func (x uint8) pow(y uint64) uint256 {
12+
if y == 0 {
13+
return 1u256
14+
}
15+
ret := uint256(x)
16+
for i := 1; i < y; i += 2 {
17+
ret *= ret
18+
}
19+
mod := y % 2
20+
if mod == 1 {
21+
ret *= uint256(x)
22+
}
23+
return ret
24+
}
25+
26+
func (x uint16) pow(y uint64) uint256 {
27+
if y == 0 {
28+
return 1u256
29+
}
30+
ret := uint256(x)
31+
for i := 1; i < y; i += 2 {
32+
ret *= ret
33+
}
34+
mod := y % 2
35+
if mod == 1 {
36+
ret *= uint256(x)
37+
}
38+
return ret
39+
}
40+
41+
func (x uint32) pow(y uint64) uint256 {
42+
if y == 0 {
43+
return 1u256
44+
}
45+
ret := uint256(x)
46+
for i := 1; i < y; i += 2 {
47+
ret *= ret
48+
}
49+
mod := y % 2
50+
if mod == 1 {
51+
ret *= uint256(x)
52+
}
53+
return ret
54+
}
55+
56+
func (x uint64) pow(y uint64) uint256 {
57+
if y == 0 {
58+
return 1u256
59+
}
60+
ret := uint256(x)
61+
for i := 1; i < y; i += 2 {
62+
ret *= ret
63+
}
64+
mod := y % 2
65+
if mod == 1 {
66+
ret *= uint256(x)
67+
}
68+
return ret
69+
}
70+
71+
func (x uint128) pow(y uint64) uint256 {
72+
if y == 0 {
73+
return 1u256
74+
}
75+
ret := uint256(x)
76+
for i := 1; i < y; i += 2 {
77+
ret *= ret
78+
}
79+
mod := y % 2
80+
if mod == 1 {
81+
ret *= uint256(x)
82+
}
83+
return ret
84+
}
85+
86+
func (x uint256) pow(y uint64) uint256 {
87+
if y == 0 {
88+
return 1u256
89+
}
90+
ret := uint256(x)
91+
for i := 1; i < y; i += 2 {
92+
ret *= ret
93+
}
94+
mod := y % 2
95+
if mod == 1 {
96+
ret *= uint256(x)
97+
}
98+
return ret
99+
}

tests/examples/pow.cell

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import (
2+
"math"
3+
)
4+
5+
func u8() {
6+
a := 10u8.pow(3)
7+
if a != 1000u256 {
8+
panic("error")
9+
}
10+
}
11+
12+
func u16() {
13+
a := 10u16.pow(2)
14+
if a != 100u256 {
15+
panic("error")
16+
}
17+
}
18+
19+
func u32() {
20+
a := 10u32.pow(2)
21+
if a != 100u256 {
22+
panic("error")
23+
}
24+
}
25+
26+
func u64() {
27+
a := 10u64.pow(2)
28+
if a != 100u256 {
29+
panic("error")
30+
}
31+
}
32+
33+
func u128() {
34+
a := 10u128.pow(2)
35+
if a != 100u256 {
36+
panic("error")
37+
}
38+
}
39+
40+
func u256() {
41+
a := 10u256.pow(2)
42+
if a != 100u256 {
43+
panic("error")
44+
}
45+
}
46+
47+
func main() {
48+
u8()
49+
u16()
50+
u32()
51+
u64()
52+
u128()
53+
u256()
54+
return 0
55+
}

0 commit comments

Comments
 (0)