[music-dsp] fast sin(x) function

Benjamin Rosseaux benjamin at 0ok.de
Thu Jun 12 10:09:33 EDT 2008


No, the fsin FPU opcode is on my Intel Core 2 Duo T7500 ~40% slower than 
my FastSIN routine. I've benchmarked it.  100000000 iterations, fsin 
opcode about 5507 milliseconds and my FastSIN about 3370 milliseconds,

Benjamin 'BeRo' Rosseaux

Stefan Stenzel schrieb:
>
> So what about the simple "fsin" instruction ?
> Shouldn't this be faster?
>
> Stefan
>
> Benjamin Rosseaux wrote:
>>
>> Hello,
>>
>> My following FastSIN routine is maybe the right thing for you.  The 
>> code should be portable easily to C/C++/whatever. I'm using it in my 
>> softsynth for my demoscene productions. And i think, the routine 
>> could be more optimized, if I had more time for my softsynth.
>>
>> function FastSIN(X:single):single; {$ifdef cpu386}assembler; 
>> stdcall;{$endif}
>> const PI0D5:single=PI*0.5;
>>      PI2:single=PI*2;
>>      PIF:single=PI;
>>      DPI2:single=1/(PI*2);
>>      SinConst1:single=-2.39e-08;
>>      SinConst2:single=2.7526e-06;
>>      SinConst3:single=-1.98409e-04;
>>      SinConst4:single=8.3333315e-03;
>>      SinConst5:single=-1.666666664e-01;
>> {$IFDEF CPU386}
>> asm
>> FLD dword PTR X
>>
>> // X:=abs(frac(((frac(X*DPI2)*PI2)+PI2)*DPI2)*PI2);
>> FLD dword PTR PI2
>> FXCH ST(1)
>> FPREM
>> FXCH ST(1)
>> FSTP ST(0)
>> FADD dword PTR PI2
>> FLD dword PTR PI2
>> FXCH ST(1)
>> FPREM
>> FXCH ST(1)
>> FSTP ST(0)
>> FABS
>> FST dword PTR [ESP-8]
>>
>> // if X>PI0D5 then X:=PI-X;
>> PUSH EAX
>> MOV EAX,dword PTR [ESP-4]
>> CMP EAX,dword PTR PI0D5
>> JLE @DoNotMirror
>>  FLDPI
>>  FSUB ST(0),ST(1)
>>  FSTP ST(1)
>> @DoNotMirror:
>> POP EAX
>>
>> // ASqr:=sqr(X);
>> FLD ST(0)
>> FMUL ST(0),ST(0)
>>
>> // 
>> result:=((((((((((SinConst1*ASqr)+SinConst2)*ASqr)+SinConst3)*ASqr)+SinConst4)*ASqr)+SinConst5)*ASqr)+1)*X; 
>>
>> FLD dword PTR SinConst1
>> FMUL ST(0),ST(1)
>> FADD dword PTR SinConst2
>> FMUL ST(0),ST(1)
>> FADD dword PTR SinConst3
>> FMUL ST(0),ST(1)
>> FADD dword PTR SinConst4
>> FMUL ST(0),ST(1)
>> FADD dword PTR SinConst5
>> FMUL ST(0),ST(1)
>> FSTP ST(1)
>> FLD1
>> FADDP ST(1),ST(0)
>> FMUL ST(0),ST(1)
>> FSTP ST(1)
>> end;
>> {$ELSE}
>> var XCasted:longint absolute X;
>>    PI0D5Casted:longint absolute PI0D5;
>>    ASqr:single;
>> begin
>> X:=abs(frac(((frac(X*DPI2)*PI2)+PI2)*DPI2)*PI2);
>> if XCasted>PI0D5Casted then begin
>>  X:=PI-X;
>> end;
>> ASqr:=sqr(X);
>> result:=((((((((((SinConst1*ASqr)+SinConst2)*ASqr)+SinConst3)*ASqr)+SinConst4)*ASqr)+SinConst5)*ASqr)+1)*X; 
>>
>> end;
>> {$ENDIF}
>>
>> Benjamin 'BeRo' Rosseaux
>> from the demogroup farbrausch
>>
>>
>> -- 
>> dupswapdrop -- the music-dsp mailing list and website: subscription 
>> info, FAQ, source code archive, list archive, book reviews, dsp links 
>> http://music.columbia.edu/cmc/music-dsp 
>> http://music.columbia.edu/mailman/listinfo/music-dsp
>>
>
> -- 
> dupswapdrop -- the music-dsp mailing list and website: subscription 
> info, FAQ, source code archive, list archive, book reviews, dsp links 
> http://music.columbia.edu/cmc/music-dsp 
> http://music.columbia.edu/mailman/listinfo/music-dsp



More information about the music-dsp mailing list