Page 1 of 1

SinCos lookup

Posted: Thu Jul 11, 2013 7:55 am
by wilbert
Sometimes you need both the Sin and Cos value of an angle.
Here's a fast lookup procedure that returns an approximation of both (SSE required).
Input angle should be between -4095 PI and 4095 PI

Code: Select all

; *** Prepare SinCosTable (4 MiB) ***

Global Dim SinCosTable.f(1048575)

Procedure SinCosTableInit()
  Protected i.i, s.f, m.f = #PI / 524288
  For i = 0 To 524287
    s = Sin(i * m)
    SinCosTable(i) = s
    SinCosTable(i + 524288) = -s
  Next
EndProcedure

SinCosTableInit()

; *** SinCos Procedure (SSE required) *** 

Procedure SinCos(Angle.f, *Sin.Float, *Cos.Float)
  !mov eax, 0x4822f983
  !movd xmm0, eax
  !mulss xmm0, [p.v_Angle]
  !cvtss2si eax, xmm0
  !lea ecx, [eax + 262144]
  !and eax, 0xfffff
  !and ecx, 0xfffff
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !mov rdx, [a_SinCosTable]
    !mov eax, [rdx + rax * 4]
    !mov ecx, [rdx + rcx * 4]
    !mov rdx, [p.p_Sin]
    !mov [rdx], eax
    !mov rdx, [p.p_Cos]
    !mov [rdx], ecx
  CompilerElse
    !mov edx, [a_SinCosTable]
    !mov eax, [edx + eax * 4]
    !mov ecx, [edx + ecx * 4]
    !mov edx, [p.p_Sin]
    !mov [edx], eax
    !mov edx, [p.p_Cos]
    !mov [edx], ecx
  CompilerEndIf
EndProcedure
Usage:

Code: Select all

Define S.f, C.f
SinCos(0.234, @S, @C)
Debug S
Debug C

Re: SinCos lookup

Posted: Thu Jul 11, 2013 1:02 pm
by Joris
Thanks, wilbert.

Dank U wilbert, ik vermoed dat dit nog best handig kan worden (FFT e.d.) hoewel ik daar nu nog niet aan toe ben.