- Home /
hide your children, there's a float-to-ushort casting bug in the xcode 7.3.1 compiler
i'm super excited about this. i think it's a compiler bug.
check out this C# code:
float val = -3.67; // this value is not special, it's merely negative.
ushort v1 = (ushort)val;
ushort v2 = (ushort)(short)val;
short t3 = ( short)val;
ushort v3 = (ushort)t3;
when compiled with xcode 7.3.1 the result when running on certain iOS devices is that v2 and v3 will be the expected value, 65533, but v1 will be 0.
have tested iPad3 and a handful of iPad Mini's & iPhone 5, and they all have the bug, whereas none of the iPhone 6's we've tested repro it.
also, if xcode 7.2.x is used to compile the project, the bug does not repro on any platform.
wow, confirmed as an xcode bug.
a blank iOS project (not created from unity) repro's it with this code (which is what's generated by the C# --> cpp compiler):
float val = -3.67f;
uint16_t v1 = (((int32_t)((uint16_t)val)));
NSLog(@"the value is: %d", v1);
again specifically, on iPhone 6 the result is 65533 but on iPad 3 the result is 0.
here's another simpler repro w/o unity:
// when compiled in XCode 7.3.1,
// the code below outputs the following on iPad3:
// -4.0 --> 0, 65532
// and this on iPhone6:
// -4.0 --> 65532, 65532
float fval = -4.0f;
unsigned short us1 = (unsigned short)fval;
unsigned short us2 = (unsigned short)(short)fval;
NSLog(@"%5.1f --> %5d, %5d", fval, us1, us2);
according to stack overflow, casting a negative float to an unsigned short has undefined behavior in C. so i guess it's not technically a compiler bug.
Your answer
Follow this Question
Related Questions
Deploying Simple Project in Xcode to iOS Fails 1 Answer
Strange behavior while running in Xcode: error only when "Development Build" is unchecked 0 Answers
getting debug line numbers xcode 0 Answers
Unity3d for iOS, a lot of linker errors after upgrading to Unity 4.5.5 1 Answer
Invalid PBX project Unity 5 0 Answers