/******************************************************** * Calculating 1/sqrt(x) * ********************************************************/ // Chose the constant which minimizes the desired error // // Error // Optimal Norm 1-Norm 2-Norm OO-Norm // 1-Norm 1597203179 0.01594 0.02224 0.05055 // 2-Norm 1597294787 0.01715 0.02093 0.04482 // Infinity-Norm 1597465647 0.02339 0.02528 0.03421 // Minimize 1-norm // int const INV_SQRT_N = 1597203179; // Minimize 2-norm // int const INV_SQRT_N = 1597294787; // Minimize oo-norm int const INV_SQRT_N = 1597465647; float inv_sqrt( float x ) { int xi = *reinterpret_cast( &x ); xi = INV_SQRT_N - (xi >> 1); return *reinterpret_cast( &xi ); } // Chose the constant which minimizes the desired error // // Error // Optimal Norm 1-Norm 2-Norm OO-Norm // 1-Norm 1597292357 0.0006520 0.001078 0.002988 // 2-Norm 1597376322 0.0007246 0.0009483 0.002338 // Infinity-Norm 1597463175 0.0009549 0.001118 0.001751 // Minimize 1-norm // int const INV_SQRT_NEWTON_N = 1597292357; // Minimize 2-norm // int const INV_SQRT_NEWTON_N = 1597376322; // Minimize oo-norm int const INV_SQRT_NEWTON_N = 1597463175; float inv_sqrt_newton( float x ) { float const half_x0 = 0.5f*x; int xi = *reinterpret_cast( &x ); xi = INV_SQRT_NEWTON_N - (xi >> 1); x = *reinterpret_cast( &xi ); return x*(1.5f - half_x0*x*x); } // Chose the constant which minimizes the desired error // // Error // 1-Norm 1.000363245811462 0.0005151 // 2-Norm 1.000724768371582 0.0006122 // Infinity-Norm 1.000876311302185f 0.0008765 // Minimize 1-norm // float const MULTIPLIER = 1.000363245811462f; // Minimize 2-norm // float const MULTIPLIER = 1.000724768371582f; // Minimize oo-norm float const MULTIPLIER = 1.000876311302185f; float inv_sqrt_multiplier( float x ) { float const mx = 0.5f*MULTIPLIER*x; int xi = *reinterpret_cast( &x ); xi = INV_SQRT_NEWTON_N - (xi >> 1); x = *reinterpret_cast( &xi ); return x*(1.5f*MULTIPLIER - mx*x*x); }