Live data from Hacker News

Magical Square Root Implementation In Quake III by Carmack

codemaestro.com

11–19 of 19 posts

Re: Magical Square Root Implementation In Quake III by Carmack

#11
post #10
post #7

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]

FWIW, memcpy(&anInt, &aFloat, sizeof(int)) achieves the same thing, is not turned in a real call to memcpy(), and doesn't not break the strict aliasing rule, and does not cause incorrect code to be generated with using `gcc -fstrict-aliasing`.

Re: Magical Square Root Implementation In Quake III by Carmack

#12
post #7

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.

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

#13
post #12
post #7

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.

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

#14
post #13
post #12

Earlier 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.

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

#15
post #14
post #13

Earlier 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.

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

#16
post #15
post #14

Earlier 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?

Readme, documentation, makefile(??)

Re: Magical Square Root Implementation In Quake III by Carmack

#17
post #9

Are 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; }

Why is this weird? It's the textbook method for doing hashes.

Re: Magical Square Root Implementation In Quake III by Carmack

#18
post #9

Earlier 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.

It is textbook, but the poster asked for seemingly random numbers in code. Based on all the places the invsqrt method has popped up, it also was a textbook method for doing it quickly.
Post reply on HN