I wish the myriad articles about this interesting hack would bother to mention that it relies on undefined behaviour. Any time you find yourself doing this: *(foo*) you're probably breaking the strict aliasing rule.
[deleted]
Magical Square Root Implementation In Quake III by Carmack
11–19 of 19 posts
Re: Magical Square Root Implementation In Quake III by Carmack
#12I wish the myriad articles about this interesting hack would bother to mention that it relies on undefined behaviour. Any time you find yourself doing this: *(foo*) you're probably breaking the strict aliasing rule.
Re: Magical Square Root Implementation In Quake III by Carmack
#13I wish the myriad articles about this interesting hack would bother to mention that it relies on undefined behaviour. Any time you find yourself doing this: *(foo*) you're probably breaking the strict aliasing rule.
The behavior is defined, if you disable the rule or your compiler doesn't follow it.
Re: Magical Square Root Implementation In Quake III by Carmack
#14Earlier quoted context omitted.
The behavior is defined, if you disable the rule or your compiler doesn't follow it.
That's the opposite of "defined", in the context of a language discussion.
Re: Magical Square Root Implementation In Quake III by Carmack
#15Earlier quoted context omitted.
That's the opposite of "defined", in the context of a language discussion.
It is a good thing that actual results rely on the compiler implementation, not the language.
Re: Magical Square Root Implementation In Quake III by Carmack
#16Earlier quoted context omitted.
It is a good thing that actual results rely on the compiler implementation, not the language.
Sure, your code will work today, with your compiler. But will it work tomorrow? Or what if your client wants to compile it on their machine, in a different operating system, with a compiler that you've never tried?
Re: Magical Square Root Implementation In Quake III by Carmack
#17Are there other weird number hacks like this one?
Look at the hashCode() implementations in http://grepcode.com/file/repository.grepcode.com/java/root/j... For example: public static int hashCode(double a[]) { if (a == null) return 0; int result = 1; for (double element : a) { long bits = Double.doubleToLongBits(element); result = 31 * result + (int)(bits ^ (bits >>> 32)); } return result; }
Re: Magical Square Root Implementation In Quake III by Carmack
#18Earlier quoted context omitted.
Look at the hashCode() implementations in http://grepcode.com/file/repository.grepcode.com/java/root/j... For example: public static int hashCode(double a[]) { if (a == null) return 0; int result = 1; for (double element : a) { long bits = Double.doubleToLongBits(element); result = 31 * result + (int)(bits ^ (bits >>> 32)); } return result; }
Why is this weird? It's the textbook method for doing hashes.