I found another bug in libfp. This time it is with fixsfsi.c. When casting a float to a long where the float is negative a value of -1 is always returned.
This value is equal to ULONG_MAX when the value is interpreted as signed.
Upon closer investigation I found that __fixunssfsi was checking to see if
the float interpreted as an unsigned long was larger than 0x4f800000ul it
returned ULONG_MAX. I also found that __fixunssfsi was being called by
__fixsfsi with a negative float value if the argument to __fixsfsi was
negative. A negative float has the MSB set there for evaluating to greater
than 0x4f800000ul when interpreted as an unsigned long. This there for
returns a -1.
I am not sure how unsigned floats are handled or if they even exist so I
felt that it may be incorrect to alter fixunssfsi. Attached is a diff of
fixsfsi.c with my fix and some source that illustrates the bug.
Strangely enough I am not seeing this behavior when casting to long long
even though the code is very similar. A similar error may be present in
fixsfdi but not caught in my test cases.
-Chris Takahashi
DIFF:
--- msp430-libc/src/libm/fixsfsi.old.c 2003-08-01 15:13:37.000000000 -0700
+++ msp430-libc/src/libm/fixsfsi.c 2003-08-01 15:10:59.000000000 -0700
@@ -39,6 +39,7 @@
if(a1<0)
{
+ a1 &= 0x7fffffff;
return -__fixunssfsi(a1);
}
fixsfsi.diff
Description: Binary data
test2.c
Description: Binary data
