aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNicolas Schodet2024-03-09 19:35:48 +0100
committerNicolas Schodet2024-03-27 21:25:05 +0100
commit109e9777bb91360fee67d20263886d14cb97b23a (patch)
tree76b05285e7fe5d497fe297a7a0992d28bed41b18
parent3cbab8ace2adcd3cf7df5f000c03dd8e8c4032e8 (diff)
Fix float to integer conversion
When converting a float to an integer, if the integer is not able to represent the value, the behavior is implementation defined. The original firmware source code depends on this implementation defined behavior from the C compiler. The LEGO MINDSTORMS NXT Executable File Specification does not define what is the expected behavior for the firmware because it was released for version 1.03, before float was supported. However it says: "At the scalar level, data type conversions behave identically to type casts in ANSI C.". For NXT Improved Firmware, I decided to follow the behavior of the original firmware binary, which is: - Negative values to unsigned numbers: large positive integer (two's complement). - Round to nearest for SETOUT instruction, truncate for other operations.
-rw-r--r--src/c_cmd.c84
1 files changed, 50 insertions, 34 deletions
diff --git a/src/c_cmd.c b/src/c_cmd.c
index 876297e..470abbf 100644
--- a/src/c_cmd.c
+++ b/src/c_cmd.c
@@ -3341,37 +3341,45 @@ void * cCmdResolveIODataArg(DATA_ARG DataArg, ULONG Offset, TYPE_CODE * TypeCode
void cCmdSetValFlt(void * pVal, TYPE_CODE TypeCode, float NewVal)
{
+ /*
+ * According to C standard, converting a float to integer is implementation
+ * defined when the number is out of integer bounds.
+ *
+ * To match original LEGO firmware, when casting float as integer, always
+ * convert to signed value, then cast as destination type.
+ */
if (pVal)
{
- switch (TypeCode)
+ if (TypeCode == TC_FLOAT)
+ *(float*)pVal = NewVal;
+ else
{
- case TC_ULONG:
- case TC_SLONG:
- {
- *(ULONG*)pVal = NewVal;
- }
- break;
+ SLONG i = NewVal;
- case TC_UWORD:
- case TC_SWORD:
+ switch (TypeCode)
{
- *(UWORD*)pVal = (UWORD)NewVal;
- }
- break;
+ case TC_ULONG:
+ case TC_SLONG:
+ {
+ *(ULONG*)pVal = i;
+ }
+ break;
- case TC_UBYTE:
- case TC_SBYTE:
- {
- *(UBYTE*)pVal = (UBYTE)NewVal;
- }
- break;
+ case TC_UWORD:
+ case TC_SWORD:
+ {
+ *(UWORD*)pVal = i;
+ }
+ break;
- case TC_FLOAT:
- {
- *(float*)pVal = (float)NewVal;
+ case TC_UBYTE:
+ case TC_SBYTE:
+ {
+ *(UBYTE*)pVal = i;
+ }
+ break;
}
- break;
}
}
@@ -3460,7 +3468,14 @@ ULONG cCmdGetFloat(void * pVal) {
else {
tempVal -= 0.5f;
}
- return (ULONG)tempVal;
+ /*
+ * According to C standard, converting a float to integer is implementation
+ * defined when the number is out of integer bounds.
+ *
+ * To match original LEGO firmware, when casting float as integer, always
+ * convert to signed value, then cast as destination type.
+ */
+ return (ULONG)(SLONG)tempVal;
}
ULONG cCmdGetVal(void * pVal, TYPE_CODE TypeCode)
@@ -5648,20 +5663,21 @@ NXT_STATUS cCmdMove(DATA_ARG Arg1, DATA_ARG Arg2)
}
else if(tc2 == TC_FLOAT && tc1 <= TC_LAST_INT_SCALAR) { // float to int
moveFloatInt++;
+ /*
+ * According to C standard, converting a float to integer is implementation
+ * defined when the number is out of integer bounds.
+ *
+ * To match original LEGO firmware, when casting float as integer, always
+ * convert to signed value, then cast as destination type.
+ */
pArg1= VarsCmd.pDataspace + TOC1Ptr->DSOffset;
pArg2= VarsCmd.pDataspace + TOC2Ptr->DSOffset;
- if (tc1 == TC_SLONG)
- *(SLONG*)pArg1 = *(float*)pArg2;
- else if (tc1 == TC_ULONG)
- *(ULONG*)pArg1 = *(float*)pArg2;
- else if (tc1 == TC_SBYTE)
- *(SBYTE*)pArg1 = *(float*)pArg2;
- else if (tc1 == TC_UBYTE)
- *(UBYTE*)pArg1 = *(float*)pArg2;
- else if (tc1 == TC_UWORD)
- *(UWORD*)pArg1 = *(float*)pArg2;
+ if (tc1 == TC_SLONG || tc1 == TC_ULONG)
+ *(SLONG*)pArg1 = (SLONG)*(float*)pArg2;
+ else if (tc1 == TC_SBYTE || tc1 == TC_UBYTE)
+ *(SBYTE*)pArg1 = (SLONG)*(float*)pArg2;
else
- *(SWORD*)pArg1 = *(float*)pArg2;
+ *(SWORD*)pArg1 = (SLONG)*(float*)pArg2;
Status= NO_ERR;
}
//!!! Optimized move for arrays of ints.