Reputation: 167
I try to calculate arctangent of double precision float value, which I hold in xmm register. With normal float pointing it is possible to use old x87 instruction FPATAN, but how can I do this with double ?
Upvotes: 2
Views: 1316
Reputation: 365277
You can still copy data from xmm to x87 to use instructions like fpatan
, but usually you should call a math library function. (fpatan
is so slow that replacing it with many simple instructions is still good.) Wikipedia suggests looking at Netlib for a freely redistributable C implementation. (Obviously the easiest way is to just call the function in libm on whatever system you're using.)
If you are going to do it, don't use static storage for the memory you bounce through; use a temporary on the stack.
Also note that fpatan
takes 2 inputs, because it implements the atan2
library function, giving a result in the appropriate quadrant depending on the sign of both inputs.
; assuming you did sub rsp, 24 or something earlier in your function
movsd [rsp], xmm1
fld qword [rsp] ; st0 = xmm1
movsd [rsp], xmm0
fld qword [rsp] ; st0 = xmm0, st1 = xmm1
fpatan ; st0 = arctan(xmm1/xmm0)
fstp qword [rsp] ; x87 stack is empty again
movsd xmm0, [rsp] ; xmm0 = arctan(xmm1/xmm0)
; and then add rsp, 24 at some point before returning
Upvotes: 5