Skip to content

Instantly share code, notes, and snippets.

@dobegor
Created May 31, 2018 00:02
Show Gist options
  • Save dobegor/a05ff503a74d94a44856a8d82e2059b2 to your computer and use it in GitHub Desktop.
Save dobegor/a05ff503a74d94a44856a8d82e2059b2 to your computer and use it in GitHub Desktop.
Ngaro VM float opcodes
floatHandler := func(i *vm.Instance, opcode vm.Cell) error {
switch opcode {
case -vm.OpAdd:
rhs := i.Pop()
lhs := i.Tos()
lhsf := (*float64)(unsafe.Pointer(&lhs))
*lhsf += *(*float64)(unsafe.Pointer(&rhs))
i.SetTos(lhs)
case -vm.OpSub:
rhs := i.Pop()
lhs := i.Tos()
lhsf := (*float64)(unsafe.Pointer(&lhs))
*lhsf -= *(*float64)(unsafe.Pointer(&rhs))
i.SetTos(lhs)
case -vm.OpMul:
rhs := i.Pop()
lhs := i.Tos()
lhsf := (*float64)(unsafe.Pointer(&lhs))
*lhsf *= *(*float64)(unsafe.Pointer(&rhs))
i.SetTos(lhs)
case -vm.OpDimod:
rhs := i.Pop()
lhs := i.Tos()
lhsf := (*float64)(unsafe.Pointer(&lhs))
*lhsf /= *(*float64)(unsafe.Pointer(&rhs))
i.SetTos(lhs)
case -1: //ITOF
arg := i.Tos()
f := (*float64)(unsafe.Pointer(&arg))
*f = float64(arg)
i.SetTos(arg)
fmt.Println("set tos to ", arg)
case -2: //FTOI
arg := i.Tos()
f := *(*float64)(unsafe.Pointer(&arg))
arg = vm.Cell(f)
i.SetTos(arg)
default:
return errors.New("uknown custom opcode")
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment