Created
May 31, 2018 00:02
-
-
Save dobegor/a05ff503a74d94a44856a8d82e2059b2 to your computer and use it in GitHub Desktop.
Ngaro VM float opcodes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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