Performance of tables slower than built in?
Ola Fosheim Grøstad
ola.fosheim.grostad at gmail.com
Fri May 24 16:47:40 UTC 2019
With linear interpolation you get roughly the same result with
floats, a little more efficient too (half the memory and a bit
faster):
__gshared float[512+1] QuarterSinTab;
void init(){
const auto res = QuarterSinTab.length-1;
for(int i = 0; i < res; i++)
QuarterSinTab[i] = sin(PI*(0.5*i/cast(double)res));
QuarterSinTab[$-1] = sin(PI*0.5);
}
auto fastQuarterLookup(float x){
const uint mantissa = cast(uint)( (x - floor(x)) *
(cast(float)(1U<<24)) );
const float sign = cast(float)(1 -
cast(int)((mantissa>>22)&2));
const uint phase =
(mantissa^((1U<<23)-((mantissa>>22)&1)))&((1U<<23) -1);
const uint quarterphase = (phase>>13)&511;
const float frac =
cast(float)(phase&((1U<<13)-1))*cast(float)(1.0f/(1U<<13));
return sign*((1.0f-frac)*QuarterSinTab[quarterphase] +
frac*QuarterSinTab[quarterphase+1]);
}
More information about the Digitalmars-d-learn
mailing list