Skip to content

Commit

Permalink
support math pow for primitive types
Browse files Browse the repository at this point in the history
  • Loading branch information
douyixuan committed Sep 13, 2024
1 parent e3db547 commit 0deaac1
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ test/example:
${CELL} -d -t riscv tests/examples/switch.cell && ckb-debugger --bin switch${exe} | grep "five"
${CELL} -d -t riscv tests/examples/number-literal.cell && ckb-debugger --bin number-literal${exe}
${CELL} -d -t riscv tests/examples/to-string.cell && ckb-debugger --bin to-string${exe}
${CELL} -d -t riscv tests/examples/pow.cell && ckb-debugger --bin pow${exe}
${CELL} -d -t riscv tests/examples/return.cell && ckb-debugger --bin return${exe}
${CELL} -d -t riscv tests/examples/named-ret-type.cell && ckb-debugger --bin named-ret-type${exe} | grep "0"
${CELL} -d -t riscv tests/examples/func.cell && ckb-debugger --bin func${exe} | grep "999"
Expand Down
99 changes: 99 additions & 0 deletions pkg/math/pow.cell
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package math

type uint8 uint8
type uint16 uint16
type uint32 uint32
type uint64 uint64
type uint128 uint128
type uint256 uint256

// x ** y
func (x uint8) pow(y uint64) uint256 {
if y == 0 {
return 1u256
}
ret := uint256(x)
for i := 1; i < y; i += 2 {
ret *= ret
}
mod := y % 2
if mod == 1 {
ret *= uint256(x)
}
return ret
}

func (x uint16) pow(y uint64) uint256 {
if y == 0 {
return 1u256
}
ret := uint256(x)
for i := 1; i < y; i += 2 {
ret *= ret
}
mod := y % 2
if mod == 1 {
ret *= uint256(x)
}
return ret
}

func (x uint32) pow(y uint64) uint256 {
if y == 0 {
return 1u256
}
ret := uint256(x)
for i := 1; i < y; i += 2 {
ret *= ret
}
mod := y % 2
if mod == 1 {
ret *= uint256(x)
}
return ret
}

func (x uint64) pow(y uint64) uint256 {
if y == 0 {
return 1u256
}
ret := uint256(x)
for i := 1; i < y; i += 2 {
ret *= ret
}
mod := y % 2
if mod == 1 {
ret *= uint256(x)
}
return ret
}

func (x uint128) pow(y uint64) uint256 {
if y == 0 {
return 1u256
}
ret := uint256(x)
for i := 1; i < y; i += 2 {
ret *= ret
}
mod := y % 2
if mod == 1 {
ret *= uint256(x)
}
return ret
}

func (x uint256) pow(y uint64) uint256 {
if y == 0 {
return 1u256
}
ret := uint256(x)
for i := 1; i < y; i += 2 {
ret *= ret
}
mod := y % 2
if mod == 1 {
ret *= uint256(x)
}
return ret
}
55 changes: 55 additions & 0 deletions tests/examples/pow.cell
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import (
"math"
)

func u8() {
a := 10u8.pow(3)
if a != 1000u256 {
panic("error")
}
}

func u16() {
a := 10u16.pow(2)
if a != 100u256 {
panic("error")
}
}

func u32() {
a := 10u32.pow(2)
if a != 100u256 {
panic("error")
}
}

func u64() {
a := 10u64.pow(2)
if a != 100u256 {
panic("error")
}
}

func u128() {
a := 10u128.pow(2)
if a != 100u256 {
panic("error")
}
}

func u256() {
a := 10u256.pow(2)
if a != 100u256 {
panic("error")
}
}

func main() {
u8()
u16()
u32()
u64()
u128()
u256()
return 0
}

0 comments on commit 0deaac1

Please sign in to comment.