-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Converting a float/double
to byte/sbyte
or short/ushort
does not correctly account for overflow
#461
Comments
See dotnet/roslyn#7333 (comment) and surrounding discussion for more details. |
CC. @gafter |
It looks like ARM might have similar problems given the conversion instructions I see are available. |
More noticeable errors can be seen with values such as C# has started using |
This behavior is unspecified per the ECMA-335 spec. If overflow occurs converting a floating-point type to an integer, or if the floating-point value |
Hmmm, that's unfortunate. Perhaps this is something we could normalize if we ever provided a "strict"/"determinstic" vs "fast" mode for floating-point operations. |
Issue
Take the following program:
Both Intel and AMD produce the same result (whether using the x87 FPU or SSE+):
Both inputs generate the same assembly.
SSE+:
x87 FPU:
The SSE+ instructions that support converting (
cvtsd2si
,cvtss2si
,cvttsd2si
, andcvttss2si
) all deal with only 32-bit or 64-bit results.The x87 FPU instructions that support converting (
fist
andfistp
) supports 16-bit, 32-bit, or 64-bit results. However, it looks like even under the legacy JIT, it still emitscvtts*2si
rather thanfist
/fistp
Given that the instruction emitted only supports 32-bit/64-bit results, the results are
65535
(0xFFFF) and65536
(0x10000). These, when cast toshort
and then sign-extended back to int are-1
and0
, respectively.Proposed Fix
The .NET Runtime should correctly account for overflow and return a "correct" value in this scenario. On x86, the "indefinite integer value" is defined to be:
The text was updated successfully, but these errors were encountered: