An unexpected bug may come from code that looks like the following:
S32 My32BitInt = somenumber;
U32 Other32BitInt = anothernumber;
U64 MyLargeNumber = (U64)Other32BitInt + (U64)My32BitInt;
Spot the bug?
The code might not execute as expected - I don't know if this is spec or not, but the cast to U64 might not be a zero extend as you might expect - as the target is unsigned. In the above instance, we encountered a bug where the compiler was using cdq (sign extend), which when the value happens to be large enough, ends up adding a very large number.
To ensure zero extend, cast to unsigned of the same bit size beforehand:
U64 myval = (U64)(U32)My32BitInt;
No comments:
Post a Comment