Add sqrt instruction
This commit is contained in:
@@ -68,6 +68,9 @@ var opCodeByName = map[string]vm.OpCodeType{
|
|||||||
"rett": vm.OpRetT,
|
"rett": vm.OpRetT,
|
||||||
"returniffalse": vm.OpReturnIfFalse,
|
"returniffalse": vm.OpReturnIfFalse,
|
||||||
"retf": vm.OpRetF,
|
"retf": vm.OpRetF,
|
||||||
|
|
||||||
|
"squareroot": vm.OpSquareRoot,
|
||||||
|
"sqrt": vm.OpSqrt,
|
||||||
}
|
}
|
||||||
|
|
||||||
var nameByOpCode = map[vm.OpCodeType]string{
|
var nameByOpCode = map[vm.OpCodeType]string{
|
||||||
@@ -110,4 +113,6 @@ var nameByOpCode = map[vm.OpCodeType]string{
|
|||||||
vm.OpRet: "ret",
|
vm.OpRet: "ret",
|
||||||
vm.OpRetT: "rett",
|
vm.OpRetT: "rett",
|
||||||
vm.OpRetF: "retf",
|
vm.OpRetF: "retf",
|
||||||
|
|
||||||
|
vm.OpSqrt: "sqrt",
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ var opCodes = []vm.OpCodeType{
|
|||||||
vm.OpRet,
|
vm.OpRet,
|
||||||
vm.OpRetT,
|
vm.OpRetT,
|
||||||
vm.OpRetF,
|
vm.OpRetF,
|
||||||
|
|
||||||
|
vm.OpSqrt,
|
||||||
}
|
}
|
||||||
|
|
||||||
func randOpCode() vm.OpCodeType {
|
func randOpCode() vm.OpCodeType {
|
||||||
|
|||||||
15
grow/defs/sqrt.yaml
Normal file
15
grow/defs/sqrt.yaml
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
global_memory_size: 1
|
||||||
|
function_memory_size: 1
|
||||||
|
instruction_limit: 2
|
||||||
|
samples:
|
||||||
|
- in: [0]
|
||||||
|
out: [0]
|
||||||
|
|
||||||
|
- in: [4]
|
||||||
|
out: [2]
|
||||||
|
|
||||||
|
- in: [144]
|
||||||
|
out: [12]
|
||||||
|
|
||||||
|
- in: [175]
|
||||||
|
out: [13]
|
||||||
@@ -519,3 +519,27 @@ functions:
|
|||||||
|
|
||||||
expectGlobalMemory(t, state, 0, 7)
|
expectGlobalMemory(t, state, 0, 7)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSqrt(t *testing.T) {
|
||||||
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
|
functions:
|
||||||
|
- - [mov, g0, 1]
|
||||||
|
- [sqrt, g0]
|
||||||
|
|
||||||
|
- [mov, g1, 4]
|
||||||
|
- [sqrt, g1]
|
||||||
|
|
||||||
|
- [mov, g2, 144]
|
||||||
|
- [sqrt, g2]
|
||||||
|
|
||||||
|
- [mov, g3, 175]
|
||||||
|
- [sqrt, g3]
|
||||||
|
`)
|
||||||
|
|
||||||
|
expectGlobalMemory(t, state, 0, 1)
|
||||||
|
expectGlobalMemory(t, state, 1, 2)
|
||||||
|
expectGlobalMemory(t, state, 2, 12)
|
||||||
|
expectGlobalMemory(t, state, 3, 13)
|
||||||
|
}
|
||||||
|
|||||||
@@ -68,6 +68,10 @@ const (
|
|||||||
OpRetT = OpReturnIfTrue
|
OpRetT = OpReturnIfTrue
|
||||||
OpReturnIfFalse = 0x00000802
|
OpReturnIfFalse = 0x00000802
|
||||||
OpRetF = OpReturnIfFalse
|
OpRetF = OpReturnIfFalse
|
||||||
|
|
||||||
|
OpSquareRoot = 0x00000900
|
||||||
|
OpSqRt = OpSquareRoot
|
||||||
|
OpSqrt = OpSquareRoot
|
||||||
)
|
)
|
||||||
|
|
||||||
type OperandNumericType int
|
type OperandNumericType int
|
||||||
@@ -118,4 +122,6 @@ var OperandsByOpCode = map[OpCodeType][]OperandNumericType{
|
|||||||
OpRet: []OperandNumericType{},
|
OpRet: []OperandNumericType{},
|
||||||
OpRetT: []OperandNumericType{},
|
OpRetT: []OperandNumericType{},
|
||||||
OpRetF: []OperandNumericType{},
|
OpRetF: []OperandNumericType{},
|
||||||
|
|
||||||
|
OpSqrt: []OperandNumericType{OperandReference},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package vm
|
package vm
|
||||||
|
|
||||||
|
import "math"
|
||||||
|
|
||||||
type opHandler func(*State, *Instruction)
|
type opHandler func(*State, *Instruction)
|
||||||
|
|
||||||
var opHandlers = map[OpCodeType]opHandler{
|
var opHandlers = map[OpCodeType]opHandler{
|
||||||
@@ -42,6 +44,8 @@ var opHandlers = map[OpCodeType]opHandler{
|
|||||||
OpReturn: (*State).handleReturn,
|
OpReturn: (*State).handleReturn,
|
||||||
OpReturnIfTrue: (*State).handleReturnIfTrue,
|
OpReturnIfTrue: (*State).handleReturnIfTrue,
|
||||||
OpReturnIfFalse: (*State).handleReturnIfFalse,
|
OpReturnIfFalse: (*State).handleReturnIfFalse,
|
||||||
|
|
||||||
|
OpSqrt: (*State).handleSqrt,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) handleNoOp(instr *Instruction) {
|
func (state *State) handleNoOp(instr *Instruction) {
|
||||||
@@ -232,3 +236,8 @@ func (state *State) handleReturnIfFalse(instr *Instruction) {
|
|||||||
state.handleReturn(instr)
|
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)))))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user