4 // Copyright (c) 2000 - 2003, Intel Corporation
5 // All rights reserved.
7 // Contributed 2000 by the Intel Numerics Group, Intel Corporation
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are
13 // * Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
16 // * Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
20 // * The name of Intel Corporation may not be used to endorse or promote
21 // products derived from this software without specific prior written
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS
28 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
32 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING
33 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 // Intel Corporation is the author of this code, and requests that all
37 // problem reports or change requests be submitted to it directly at
38 // http://www.intel.com/software/products/opensource/libraries/num.htm.
41 //==============================================================
42 // 02/02/00 Initial version
43 // 04/13/00 Improved speed
44 // 04/19/00 Removed the qualifying predicate from the fmerge.s that
45 // takes the absolute value.
46 // 06/16/00 Reassigned FP registers to eliminate stalls on loads
47 // 08/30/00 Saved 5 cycles in main path by rearranging large argument logic
48 // and delaying use of result of fcmp in load by 1 group
49 // 05/20/02 Cleaned up namespace and sf0 syntax
50 // 08/20/02 Use atan2 algorithm with x=1 for better accuracy
51 // 02/06/03 Reordered header: .section, .global, .proc, .align
54 //==============================================================
55 // double atan(double Y)
57 // Overview of operation
58 //==============================================================
60 // The atan function returns values in the interval [-pi/2,+pi/2].
62 // The algorithm used is the atan2(Y,X) algorithm where we fix X=1.0.
64 // There are two basic paths: swap true and swap false.
65 // atan2(Y,X) ==> atan2(V/U) where U >= V. If Y > X, we must swap.
67 // p6 swap True |Y| > |X|
68 // p7 swap False |Y| <= |X|
71 // Simple trigonometric identities show
73 // |Y|<=1.0, V=Y, U=1.0 atan2(Y,X) = sgnY * (0 + atan(V/U))
76 // |Y|>1.0, V=1.0, U=Y atan2(Y,X) = sgnY * (pi/2 - atan(V/U))
79 // We compute atan(V/U) from the identity
80 // atan(z) + atan([(V/U)-z] / [1+(V/U)z])
81 // where z is a limited precision approximation (16 bits) to V/U
83 // z is calculated with the assistance of the frcpa instruction.
85 // atan(z) is calculated by a polynomial z + z^3 * p(w), w=z^2
86 // where p(w) = P0+P1*w+...+P22*w^22
88 // Let d = [(V/U)-z] / [1+(V/U)z]) = (V-U*z)/(U+V*z)
90 // Approximate atan(d) by d + P0*d^3
91 // Let F = 1/(U+V*z) * (1-a), where |a|< 2^-8.8.
92 // Compute q(a) = 1 + a + ... + a^5.
93 // Then F*q(a) approximates the reciprocal to more than 50 bits.
96 //==============================================================
98 // atan(SNAN) = quieted SNAN
99 // atan(+-inf) = +- pi/2
103 //==============================================================
105 // predicate registers used:
108 // floating-point registers used:
112 // general registers used
116 //==============================================================
168 atan2_sgn_pi_by_2 = f69
205 atan2_alpha_sq = f100
211 atan2_alpha_cub = f105
224 atan2_sig_near_one = f116
225 atan2_near_one = f116
227 /////////////////////////////////////////////////////////////
234 LOCAL_OBJECT_START(atan2_tb1)
235 data8 0xA21922DC45605EA1 , 0x00003FFA // P11
236 data8 0xB199DD6D2675C40F , 0x0000BFFA // P10
237 data8 0xC2F01E5DDD100DBE , 0x00003FFA // P9
238 data8 0xD78F28FC2A592781 , 0x0000BFFA // P8
239 data8 0xF0F03ADB3FC930D3 , 0x00003FFA // P7
240 data8 0x88887EBB209E3543 , 0x0000BFFB // P6
241 data8 0x9D89D7D55C3287A5 , 0x00003FFB // P5
242 data8 0xBA2E8B9793955C77 , 0x0000BFFB // P4
243 data8 0xE38E38E320A8A098 , 0x00003FFB // P3
244 data8 0x9249249247E37913 , 0x0000BFFC // P2
245 data8 0xCCCCCCCCCCC906CD , 0x00003FFC // P1
246 data8 0xAAAAAAAAAAAAA8A9 , 0x0000BFFD // P0
247 data8 0x0000000000000000 , 0x00000000 // pad to avoid bank conflict
248 LOCAL_OBJECT_END(atan2_tb1)
250 LOCAL_OBJECT_START(atan2_tb2)
251 data8 0xCE585A259BD8374C , 0x00003FF0 // P21
252 data8 0x9F90FB984D8E39D0 , 0x0000BFF3 // P20
253 data8 0x9D3436AABE218776 , 0x00003FF5 // P19
254 data8 0xDEC343E068A6D2A8 , 0x0000BFF6 // P18
255 data8 0xF396268151CFB11C , 0x00003FF7 // P17
256 data8 0xD818B4BB43D84BF2 , 0x0000BFF8 // P16
257 data8 0xA2270D30A90AA220 , 0x00003FF9 // P15
258 data8 0xD5F4F2182E7A8725 , 0x0000BFF9 // P14
259 data8 0x80D601879218B53A , 0x00003FFA // P13
260 data8 0x9297B23CCFFB291F , 0x0000BFFA // P12
261 data8 0xFE7E52D2A89995B3 , 0x0000BFEC // P22
262 data8 0xC90FDAA22168C235 , 0x00003FFF // pi/2
263 LOCAL_OBJECT_END(atan2_tb2)
269 GLOBAL_LIBM_ENTRY(atan)
273 frcpa.s1 atan2_u1_Y,p7 = f1,atan2_Y
277 addl EXP_AD_P1 = @ltoff(atan2_tb1), gp
278 fma.s1 atan2_two = f1,f1,f1
284 ld8 EXP_AD_P1 = [EXP_AD_P1]
285 frcpa.s1 atan2_u1_X,p6 = f1,atan2_X
290 fma.s1 atan2_ysq = atan2_Y,atan2_Y,f0
296 add EXP_AD_P2 = 0xd0,EXP_AD_P1
297 fmerge.s atan2_sgnY = atan2_Y,f1
304 ldfe atan2_P11 = [EXP_AD_P1],16
305 fclass.m p10,p0 = atan2_Y, 0xc3 // Test for y=nan
309 ldfe atan2_P21 = [EXP_AD_P2],16
317 ldfe atan2_P10 = [EXP_AD_P1],16
318 fnma.s1 atan2_B1Y = atan2_u1_Y, atan2_Y, atan2_two
322 ldfe atan2_P20 = [EXP_AD_P2],16
323 fma.s1 atan2_wp_Y = atan2_u1_Y, atan2_u1_Y, f0
329 ldfe atan2_P9 = [EXP_AD_P1],16
330 fma.s1 atan2_z1_X = atan2_u1_X, atan2_Y, f0
334 ldfe atan2_P19 = [EXP_AD_P2],16
335 fnma.s1 atan2_B1X = atan2_u1_X, atan2_X, atan2_two
341 ldfe atan2_P8 = [EXP_AD_P1],16
342 fma.s1 atan2_z2_X = atan2_u1_X, atan2_ysq, f0
346 ldfe atan2_P18 = [EXP_AD_P2],16
347 (p10) fma.d.s0 f8 = atan2_Y,atan2_X,f0 // If y=nan, result quietized y
348 (p10) br.ret.spnt b0 // Exit if y=nan
352 // p6 true if swap, means |y| > 1.0 or ysq > 1.0
353 // p7 true if no swap, means 1.0 >= |y| or 1.0 >= ysq
355 ldfe atan2_P7 = [EXP_AD_P1],16
356 fcmp.ge.s1 p7,p6 = f1, atan2_ysq
360 ldfe atan2_P17 = [EXP_AD_P2],16
367 ldfe atan2_P6 = [EXP_AD_P1],16
368 fma.s1 atan2_E = atan2_u1_Y, atan2_B1Y, atan2_Y
372 ldfe atan2_P16 = [EXP_AD_P2],16
373 fma.s1 atan2_B1sq_Y = atan2_B1Y, atan2_B1Y, f0
379 ldfe atan2_P5 = [EXP_AD_P1],16
380 (p7) fma.s1 atan2_wp_X = atan2_z1_X, atan2_z1_X, f0
384 ldfe atan2_P15 = [EXP_AD_P2],16
385 (p7) fma.s1 atan2_B1sq_X = atan2_B1X, atan2_B1X, f0
391 ldfe atan2_P4 = [EXP_AD_P1],16
392 (p6) fma.s1 atan2_z = atan2_u1_Y, atan2_B1Y, f0
396 ldfe atan2_P14 = [EXP_AD_P2],16
397 (p7) fma.s1 atan2_E = atan2_z2_X, atan2_B1X, atan2_X
404 ldfe atan2_P3 = [EXP_AD_P1],16
405 fcmp.eq.s0 p14,p15=atan2_X,atan2_Y // Dummy for denorm and invalid
409 ldfe atan2_P13 = [EXP_AD_P2],16
411 (p7) fma.s1 atan2_z = atan2_z1_X, atan2_B1X, f0
416 ldfe atan2_P2 = [EXP_AD_P1],16
417 (p6) fma.s1 atan2_w = atan2_wp_Y, atan2_B1sq_Y,f0
421 ldfe atan2_P12 = [EXP_AD_P2],16
422 movl rsig_near_one = 0x8000000000000001 // signif near 1.0
427 ldfe atan2_P1 = [EXP_AD_P1],16
428 fclass.m p9,p0 = atan2_Y, 0x23 // test if y inf
432 ldfe atan2_P22 = [EXP_AD_P2],16
433 (p7) fma.s1 atan2_w = atan2_wp_X, atan2_B1sq_X,f0
439 ldfe atan2_P0 = [EXP_AD_P1],16
440 frcpa.s1 atan2_F,p0 = f1, atan2_E
444 ldfe atan2_pi_by_2 = [EXP_AD_P2],16
445 (p6) fnma.s1 atan2_gV = atan2_Y, atan2_z, atan2_X
451 setf.sig atan2_sig_near_one = rsig_near_one
452 (p7) fnma.s1 atan2_gV = atan2_X, atan2_z, atan2_Y
457 (p9) fma.d.s0 f8 = atan2_sgnY, atan2_pi_by_2, f0 // +-pi/2 if y inf
458 (p9) br.ret.spnt b0 // exit if y inf, result is +-pi/2
464 fma.s1 atan2_V13 = atan2_w, atan2_P11, atan2_P10
469 fma.s1 atan2_W11 = atan2_w, atan2_P21, atan2_P20
476 fma.s1 atan2_V11 = atan2_w, atan2_P9, atan2_P8
481 fma.s1 atan2_V12 = atan2_w, atan2_w, f0
488 fma.s1 atan2_V8 = atan2_w, atan2_P7 , atan2_P6
493 fma.s1 atan2_W8 = atan2_w, atan2_P19, atan2_P18
500 fnma.s1 atan2_alpha = atan2_E, atan2_F, f1
505 fnma.s1 atan2_alpha_1 = atan2_E, atan2_F, atan2_two
513 fma.s1 atan2_V7 = atan2_w, atan2_P5 , atan2_P4
518 fma.s1 atan2_W7 = atan2_w, atan2_P17, atan2_P16
525 fma.s1 atan2_V4 = atan2_w, atan2_P3 , atan2_P2
530 fma.s1 atan2_W4 = atan2_w, atan2_P15, atan2_P14
537 fma.s1 atan2_V3 = atan2_w, atan2_P1 , atan2_P0
542 fma.s1 atan2_W3 = atan2_w, atan2_P13, atan2_P12
549 fma.s1 atan2_V10 = atan2_V12, atan2_V13, atan2_V11
554 fma.s1 atan2_gVF = atan2_gV, atan2_F, f0
561 fma.s1 atan2_alpha_sq = atan2_alpha, atan2_alpha, f0
566 fma.s1 atan2_Cp = atan2_alpha, atan2_alpha_1, f1
573 fma.s1 atan2_V9 = atan2_V12, atan2_V12, f0
578 fma.s1 atan2_W10 = atan2_V12, atan2_P22 , atan2_W11
585 fma.s1 atan2_V6 = atan2_V12, atan2_V8 , atan2_V7
590 fma.s1 atan2_W6 = atan2_V12, atan2_W8 , atan2_W7
597 fma.s1 atan2_V2 = atan2_V12, atan2_V4 , atan2_V3
602 fma.s1 atan2_W2 = atan2_V12, atan2_W4 , atan2_W3
609 fma.s1 atan2_alpha_cub = atan2_alpha, atan2_alpha_sq, f0
614 fma.s1 atan2_C = atan2_gVF, atan2_Cp, f0
621 fma.s1 atan2_W12 = atan2_V9, atan2_V9, f0
628 fma.s1 atan2_V5 = atan2_V9, atan2_V10, atan2_V6
633 fma.s1 atan2_W5 = atan2_V9, atan2_W10, atan2_W6
640 fclass.m p8,p0 = atan2_Y, 0x07 // Test for y=0
645 fma.s1 atan2_d = atan2_alpha_cub, atan2_C, atan2_C
652 fma.s1 atan2_W12 = atan2_V9, atan2_W12, f0
659 fma.s1 atan2_V1 = atan2_V9, atan2_V5, atan2_V2
664 fma.s1 atan2_W1 = atan2_V9, atan2_W5, atan2_W2
671 (p8) fmerge.s f8 = atan2_sgnY, f0 // +-0 if y=0
676 fma.s1 atan2_zcub = atan2_z, atan2_w, f0
677 (p8) br.ret.spnt b0 // Exit if y=0
683 fma.s1 atan2_pd = atan2_P0, atan2_d, f0
688 fma.s1 atan2_dsq = atan2_d, atan2_d, f0
696 fmerge.se atan2_near_one = f1, atan2_sig_near_one // Const ~1.0
701 fma.s1 atan2_Pp = atan2_W12, atan2_W1, atan2_V1
708 fma.s1 atan2_sgn_pi_by_2 = atan2_pi_by_2, atan2_sgnY, f0
713 fma.s1 atan2_A_lo = atan2_pd, atan2_dsq, atan2_d
721 fma.s1 atan2_A_hi = atan2_zcub, atan2_Pp, atan2_z
729 (p6) fma.s1 atan2_A = atan2_A_hi, f1, atan2_A_lo
732 // For |Y| <= |X| and X > 0, result is A_hi + A_lo
735 (p7) fma.d.s0 f8 = atan2_A_hi, f1, atan2_A_lo
740 // For |Y| > |X|, result is +- pi/2 - (A_hi + A_lo)
741 // We perturb A by multiplying by 1.0+1ulp as we produce the result
742 // in order to get symmetrically rounded results in directed rounding modes.
743 // If we don't do this, there are a few cases where the trailing 11 bits of
744 // the significand of the result, before converting to double, are zero. These
745 // cases do not round symmetrically in round to +infinity or round to -infinity.
748 (p6) fnma.d.s0 f8 = atan2_A, atan2_near_one, atan2_sgn_pi_by_2
753 GLOBAL_LIBM_END(atan)