compiler: implement syscalls for POW and SQRT opcodes

This commit is contained in:
Evgeniy Stratonikov 2021-03-02 14:19:13 +03:00
parent 578bbabd1d
commit 5f4385d3fa
2 changed files with 30 additions and 0 deletions

View file

@ -117,4 +117,20 @@ func TestOpcode(t *testing.T) {
}`
eval(t, src, big.NewInt(42))
})
t.Run("POW", func(t *testing.T) {
src := `package foo
import "github.com/nspcc-dev/neo-go/pkg/interop/math"
func Main() int {
return math.Pow(2, math.Pow(3, 2))
}`
eval(t, src, big.NewInt(512))
})
t.Run("SRQT", func(t *testing.T) {
src := `package foo
import "github.com/nspcc-dev/neo-go/pkg/interop/math"
func Main() int {
return math.Sqrt(math.Sqrt(101)) // == sqrt(10) == 3
}`
eval(t, src, big.NewInt(3))
})
}

14
pkg/interop/math/math.go Normal file
View file

@ -0,0 +1,14 @@
package math
import "github.com/nspcc-dev/neo-go/pkg/interop/neogointernal"
// Pow returns a^b using POW VM opcode.
// b must be >= 0 and <= 2^31-1.
func Pow(a, b int) int {
return neogointernal.Opcode2("POW", a, b).(int)
}
// Sqrt returns positive square root of x rounded down.
func Sqrt(x int) int {
return neogointernal.Opcode1("SQRT", x).(int)
}