Evolutionary algorithm to find Quake III's fast inverse square hack with Python
multigrad.blogspot.com
multigrad.blogspot.com
His algorithm and the result (along with a plot of relative error) can be found on his blog [2].
[1]: See, for example, this 256-byte raycaster https://www.youtube.com/watch?v=R35UuntQQF8
Put it on the left hand side, or don't overlay it over the scrollbar.
Damn #hipstercoders
infact on modern desktop/console hardware invsqrt hack is really bad. it causes type aliasing which is 'expensive' in native software terms. the x86(-64) chips underlying them all now has a dedicated instruction for this, as do the PPCs that came in the generation before... still of value on ARM though which is becoming more and more important.
It's not perfect for this application though because it doesn't bitwise operations or use execution time as a fitness function.
more or less: one 8 bit additon, one 8 bit subtraction, two table lookups for the squares and one 16 bit subtraction.
http://codebase64.org/doku.php?id=base:fast_8bit_multiplicat...
But the C64 had also a shifting operation.
ROL rotate left
ROR rotate right
ASL arithmetic shift left
LSR logic shift right
But for it to work for all 2-complement coded values we need to ensure that the MSB is copied back into itself to keep the value the same sign, which can be done with another shift.
; Divide a signed 16-bit value by 2
LDA MEM+1 ;Load the MSB
ASL A ;Copy the sign bit into C
ROR MEM+1 ;restore MSB
ROR MEM+0 ;Rotate the LSB
It's a bunch of cycles but nothing compared to a generic division.[1]: http://www.hackersdelight.org/
[2]: http://www.amazon.com/Hackers-Delight-Edition-Henry-Warren/d...
(I used to be really into game programming when I was in my teens.)
#define AND_THEN_A_MIRACLE_OCCURS (0x5f3759df)http://jheriko-rtw.blogspot.co.uk/2009/04/understanding-and-...
As already mentioned in another comment it has been taken further by Řrřola who has optimised a version with the newton step including the constants involved in that.
However this is different. It's not a person rediscovering it, but an algorithm which happens to do exactly the same thing but is provably not unique or patented. It's a different algorithm that just happens to converge on the same process.
This is perfectly analogous to patents on real world inventions - there are plenty of cases of different machines that do the same thing. You can't patent the output of an algorithm, just the process by which it gets to that output.
The (supposed) purpose is to give an incentive to inventors to freely broadcasting their inventions, by giving them a monopoly on it so they can recoup their investment. Without that incentive, inventors would keep their inventions trade secrets.
Misleading title.