Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

If we're talking real-world languages, the code is perfectly well-defined. It's undefined behaviour in Abstract C, the C of the Specification, which to my knowledge nobody has ever implemented, but Quake 3 was not written in Abstract C. It was written in Visual C++ 2003, gcc 2.95, and some other specific C implementation.

The code snippet that the article claims is C:

  int32_t compute_magic(void) {
    double sigma = 0.0450465;
    double expression = 1.5 * pow(2.0, 23.0) * (127.0 - sigma);
    int32_t i = expression;
    return i;
  }
which, as far as I can tell, is perfectly well-defined Abstract C.


Specifically, I think the original code will always do what you expect as long as sizeof(long) == sizeof(float), and the alignment is the same. In reality no compiler is gonna do weird stuff unless your hardware target is weird.


Examples of such a weird hardware target include Linux and MacOS, which use LP64: https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_m...

If you use a union, your code won't read or write out of bounds, since it will save space for the larger type. Ideally you would also specify _Float32_t and int32_t for some pseudoportability (at least before endianness gets involved), although I'm aware this was not available in 1999.


compiler engineers have been disappointing our 'surely no compiler would ever be so perverse as to do x' expectations for 25 years now, and it doesn't seem that they're likely to stop soon


Minor correction: Q3 came out in 99, so it was probably written in either VC++ 97 or VC6.

Otherwise, carry on.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: