Skip to content

Commit 80e5e1d

Browse files
committed
refactor(executor/opcode): float/double comparisons
1 parent 7d982b3 commit 80e5e1d

File tree

1 file changed

+44
-40
lines changed

1 file changed

+44
-40
lines changed

src/executor/op_code.rs

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,6 @@ got: {:?}",
663663
},
664664

665665
Self::Dcmp(nan_handling) => {
666-
// TODO: Floating-point comparison in accordance with IEEE 754?
667-
668666
let op2 = if let StackValue::Double(d) =
669667
frame.operand_stack.pop().unwrap()
670668
{
@@ -680,24 +678,28 @@ got: {:?}",
680678
panic!("expected double on top");
681679
};
682680

683-
if op1.is_nan() || op2.is_nan() {
684-
match nan_handling {
685-
FloatCmp::Pg => frame
686-
.operand_stack
687-
.push(StackValue::Int(1))
688-
.unwrap(),
689-
FloatCmp::Pl => frame
690-
.operand_stack
691-
.push(StackValue::Int(-1))
692-
.unwrap(),
681+
match op1.partial_cmp(&op2) {
682+
Some(Ordering::Greater) => {
683+
frame.operand_stack.push(StackValue::Int(1)).unwrap();
684+
},
685+
Some(Ordering::Equal) => {
686+
frame.operand_stack.push(StackValue::Int(0)).unwrap();
687+
},
688+
Some(Ordering::Less) => {
689+
frame.operand_stack.push(StackValue::Int(-1)).unwrap();
690+
},
691+
None => {
692+
match nan_handling {
693+
FloatCmp::Pg => frame
694+
.operand_stack
695+
.push(StackValue::Int(1))
696+
.unwrap(),
697+
FloatCmp::Pl => frame
698+
.operand_stack
699+
.push(StackValue::Int(-1))
700+
.unwrap(),
701+
}
693702
}
694-
} else if op1 > op2 {
695-
frame.operand_stack.push(StackValue::Int(1)).unwrap();
696-
} else if op1 == op2 {
697-
frame.operand_stack.push(StackValue::Int(0)).unwrap();
698-
} else {
699-
// op1 < op2
700-
frame.operand_stack.push(StackValue::Int(-1)).unwrap();
701703
}
702704

703705
Update::None
@@ -973,41 +975,43 @@ got: {:?}",
973975
},
974976

975977
Self::Fcmp(nan_handling) => {
976-
// TODO: Floating-point comparison in accordance with IEEE 754?
977-
978978
let op2 = if let StackValue::Float(d) =
979979
frame.operand_stack.pop().unwrap()
980980
{
981981
d
982982
} else {
983-
panic!("expected double on top");
983+
panic!("expected float on top");
984984
};
985985
let op1 = if let StackValue::Float(d) =
986986
frame.operand_stack.pop().unwrap()
987987
{
988988
d
989989
} else {
990-
panic!("expected double on top");
990+
panic!("expected float on top");
991991
};
992992

993-
if op1.is_nan() || op2.is_nan() {
994-
match nan_handling {
995-
FloatCmp::Pg => frame
996-
.operand_stack
997-
.push(StackValue::Int(1))
998-
.unwrap(),
999-
FloatCmp::Pl => frame
1000-
.operand_stack
1001-
.push(StackValue::Int(-1))
1002-
.unwrap(),
993+
match op1.partial_cmp(&op2) {
994+
Some(Ordering::Greater) => {
995+
frame.operand_stack.push(StackValue::Int(1)).unwrap();
996+
},
997+
Some(Ordering::Equal) => {
998+
frame.operand_stack.push(StackValue::Int(0)).unwrap();
999+
},
1000+
Some(Ordering::Less) => {
1001+
frame.operand_stack.push(StackValue::Int(-1)).unwrap();
1002+
},
1003+
None => {
1004+
match nan_handling {
1005+
FloatCmp::Pg => frame
1006+
.operand_stack
1007+
.push(StackValue::Int(1))
1008+
.unwrap(),
1009+
FloatCmp::Pl => frame
1010+
.operand_stack
1011+
.push(StackValue::Int(-1))
1012+
.unwrap(),
1013+
}
10031014
}
1004-
} else if op1 > op2 {
1005-
frame.operand_stack.push(StackValue::Int(1)).unwrap();
1006-
} else if op1 == op2 {
1007-
frame.operand_stack.push(StackValue::Int(0)).unwrap();
1008-
} else {
1009-
// op1 < op2
1010-
frame.operand_stack.push(StackValue::Int(-1)).unwrap();
10111015
}
10121016

10131017
Update::None

0 commit comments

Comments
 (0)