diff --git a/Makefile b/Makefile index d008f54..e41cee0 100644 --- a/Makefile +++ b/Makefile @@ -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" diff --git a/pkg/math/pow.cell b/pkg/math/pow.cell new file mode 100644 index 0000000..c52f052 --- /dev/null +++ b/pkg/math/pow.cell @@ -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 +} diff --git a/tests/examples/pow.cell b/tests/examples/pow.cell new file mode 100644 index 0000000..d6af183 --- /dev/null +++ b/tests/examples/pow.cell @@ -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 +} \ No newline at end of file