Add sqrt instruction

This commit is contained in:
Ian Gulliver
2021-11-21 17:04:58 -08:00
parent 05c62a92e1
commit 5ae34243b4
6 changed files with 61 additions and 0 deletions

View File

@@ -1,5 +1,7 @@
package vm
import "math"
type opHandler func(*State, *Instruction)
var opHandlers = map[OpCodeType]opHandler{
@@ -42,6 +44,8 @@ var opHandlers = map[OpCodeType]opHandler{
OpReturn: (*State).handleReturn,
OpReturnIfTrue: (*State).handleReturnIfTrue,
OpReturnIfFalse: (*State).handleReturnIfFalse,
OpSqrt: (*State).handleSqrt,
}
func (state *State) handleNoOp(instr *Instruction) {
@@ -232,3 +236,8 @@ func (state *State) handleReturnIfFalse(instr *Instruction) {
state.handleReturn(instr)
}
}
func (state *State) handleSqrt(instr *Instruction) {
in := state.readUnsigned(instr.Operands[0])
state.writeUnsigned(instr.Operands[0], uint64(math.Round(math.Sqrt(float64(in)))))
}