From sdw at lig.net Wed Dec 23 17:13:53 2015 From: sdw at lig.net (Stephen D. Williams) Date: Wed, 23 Dec 2015 17:13:53 -0800 Subject: [FoRK] Fast inverse square root Message-ID: <567B46D1.3060600@lig.net> Interesting history of a great hack. https://en.wikipedia.org/wiki/Fast_inverse_square_root float Q_rsqrt( float number ) { long i; float x2, y; const float threehalfs = 1.5F; x2 = number * 0.5F; y = number; i = * ( long * ) &y; // evil floating point bit level hacking i = 0x5f3759df - ( i >> 1 ); // what the fuck? y = * ( float * ) &i; y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed return y; } sdw From joe at barrera.org Wed Dec 23 18:05:19 2015 From: joe at barrera.org (Joseph S. Barrera III) Date: Wed, 23 Dec 2015 18:05:19 -0800 Subject: [FoRK] Fast inverse square root In-Reply-To: <567B46D1.3060600@lig.net> References: <567B46D1.3060600@lig.net> Message-ID: Perhaps related to or derived from https://en.wikipedia.org/wiki/CORDIC On Wed, Dec 23, 2015 at 5:13 PM, Stephen D. Williams wrote: > Interesting history of a great hack. > > https://en.wikipedia.org/wiki/Fast_inverse_square_root > > float Q_rsqrt( float number ) > { > long i; > float x2, y; > const float threehalfs = 1.5F; > > x2 = number * 0.5F; > y = number; > i = * ( long * ) &y; // evil > floating point bit level hacking > i = 0x5f3759df - ( i >> 1 ); // what the > fuck? > y = * ( float * ) &i; > y = y * ( threehalfs - ( x2 * y * y ) ); // 1st > iteration > // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be > removed > > return y; > } > > > > sdw > > _______________________________________________ > FoRK mailing list > http://xent.com/mailman/listinfo/fork > From joe at barrera.org Wed Dec 23 18:07:29 2015 From: joe at barrera.org (Joseph S. Barrera III) Date: Wed, 23 Dec 2015 18:07:29 -0800 Subject: [FoRK] Fast inverse square root In-Reply-To: References: <567B46D1.3060600@lig.net> Message-ID: Oh, I see. "the fact that aliasing a float32 to an integer gives a rough approximation of its logarithm." On Wed, Dec 23, 2015 at 6:05 PM, Joseph S. Barrera III wrote: > Perhaps related to or derived from https://en.wikipedia.org/wiki/CORDIC > > > On Wed, Dec 23, 2015 at 5:13 PM, Stephen D. Williams wrote: > >> Interesting history of a great hack. >> >> https://en.wikipedia.org/wiki/Fast_inverse_square_root >> >> float Q_rsqrt( float number ) >> { >> long i; >> float x2, y; >> const float threehalfs = 1.5F; >> >> x2 = number * 0.5F; >> y = number; >> i = * ( long * ) &y; // evil >> floating point bit level hacking >> i = 0x5f3759df - ( i >> 1 ); // what the >> fuck? >> y = * ( float * ) &i; >> y = y * ( threehalfs - ( x2 * y * y ) ); // 1st >> iteration >> // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be >> removed >> >> return y; >> } >> >> >> >> sdw >> >> _______________________________________________ >> FoRK mailing list >> http://xent.com/mailman/listinfo/fork >> > > From greg at bolcer.org Wed Dec 23 21:45:33 2015 From: greg at bolcer.org (Gregory Alan Bolcer) Date: Wed, 23 Dec 2015 21:45:33 -0800 Subject: [FoRK] Fast inverse square root In-Reply-To: <567B46D1.3060600@lig.net> References: <567B46D1.3060600@lig.net> Message-ID: <567B867D.3000001@bolcer.org> 1994. Light pipes : a new method for global illumination / by James Image processing -- Digital techniques. Berney, James Patrick. On 12/23/2015 5:13 PM, Stephen D. Williams wrote: > Interesting history of a great hack. > > https://en.wikipedia.org/wiki/Fast_inverse_square_root > > float Q_rsqrt( float number ) > { > long i; > float x2, y; > const float threehalfs = 1.5F; > > x2 = number * 0.5F; > y = number; > i = * ( long * ) &y; // evil > floating point bit level hacking > i = 0x5f3759df - ( i >> 1 ); // what the > fuck? > y = * ( float * ) &i; > y = y * ( threehalfs - ( x2 * y * y ) ); // 1st > iteration > // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can > be removed > > return y; > } > > > > sdw > > _______________________________________________ > FoRK mailing list > http://xent.com/mailman/listinfo/fork > . > -- greg at bolcer.org, http://bolcer.org, c: +1.714.928.5476