3 // Copyright (C) 2000, 2001, Intel Corporation
4 // All rights reserved.
6 // Contributed 2/2/2000 by John Harrison, Ted Kubaska, Bob Norin, Shane Story,
7 // and Ping Tak Peter Tang of the Computational Software Lab, 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://developer.intel.com/opensource.
41 //==============================================================
42 // 2/02/00 Initial version
43 // 4/04/00 Unwind support added
44 // 8/15/00 Bundle added after call to __libm_error_support to properly
45 // set [the previously overwritten] GR_Parameter_RESULT.
46 // 8/17/00 Changed predicate register macro-usage to direct predicate
47 // names due to an assembler bug.
48 // 9/28/00 Updated to set invalid on SNaN inputs
49 // 1/19/01 Fixed flags for small results
52 //==============================================================
53 // double atan2(double Y, double X)
55 // Overview of operation
56 //==============================================================
58 // There are two basic paths: swap true and swap false.
59 // atan2(Y,X) ==> atan2(V/U) where U >= V. If Y > X, we must swap.
61 // p6 swap True |Y| > |X|
62 // p7 swap False |Y| <= |X|
63 // p8 X+ (If swap=True p8=p9=0)
66 // all the other predicates p10 thru p15 are false for the main path
68 // Simple trigonometric identities show
69 // Region 1 (-45 to +45 degrees):
70 // X>0, |Y|<=X, V=Y, U=X atan2(Y,X) = sgnY * (0 + atan(V/U))
72 // Region 2 (-90 to -45 degrees, and +45 to +90 degrees):
73 // X>0, |Y|>X, V=X, U=Y atan2(Y,X) = sgnY * (pi/2 - atan(V/U))
75 // Region 3 (-135 to -90 degrees, and +90 to +135 degrees):
76 // X<0, |Y|>X, V=X, U=Y atan2(Y,X) = sgnY * (pi/2 + atan(V/U))
78 // Region 4 (-180 to -135 degrees, and +135 to +180 degrees):
79 // X<0, |Y|<=X, V=Y, U=X atan2(Y,X) = sgnY * (pi - atan(V/U))
81 // So the result is always of the form atan2(Y,X) = P + sgnXY * atan(V/U)
83 // We compute atan(V/U) from the identity
84 // atan(z) + atan([(V/U)-z] / [1+(V/U)z])
85 // where z is a limited precision approximation (16 bits) to V/U
87 // z is calculated with the assistance of the frcpa instruction.
89 // atan(z) is calculated by a polynomial z + z^3 * p(w), w=z^2
90 // where p(w) = P0+P1*w+...+P22*w^22
92 // Let d = [(V/U)-z] / [1+(V/U)z]) = (V-U*z)/(U+V*z)
94 // Approximate atan(d) by d + P0*d^3
95 // Let F = 1/(U+V*z) * (1-a), where |a|< 2^-8.8.
96 // Compute q(a) = 1 + a + ... + a^5.
97 // Then F*q(a) approximates the reciprocal to more than 50 bits.
100 //==============================================================
107 // +inf +number +pi/2
108 // -inf +number -pi/2
109 // +inf -number +pi/2
110 // -inf -number -pi/2
137 // Nan anything quiet Y
138 // anything NaN quiet X
140 // atan2(+-0/+-0) sets double error tag to 37
141 // atan2(+-0/+-0) sets single error tag to 38
143 #include "libm_support.h"
146 //==============================================================
150 atan2_GR_sml_exp = r35
159 GR_Parameter_RESULT = r40
237 atan2_alpha_sq = f100
241 atan2_sml_norm = f103
245 atan2_alpha_cub = f105
262 atan2_3pi_by_4 = f120
268 // These coefficients are for atan2.
269 // You can also use this set to substitute those used in the |X| <= 1 case for atan;
270 // BUT NOT vice versa.
272 /////////////////////////////////////////////////////////////
284 ASM_TYPE_DIRECTIVE(atan2_tb1,@object)
285 data8 0xB199DD6D2675C40F , 0x0000BFFA // P10
286 data8 0xA21922DC45605EA1 , 0x00003FFA // P11
287 data8 0xD78F28FC2A592781 , 0x0000BFFA // P8
288 data8 0xC2F01E5DDD100DBE , 0x00003FFA // P9
289 data8 0x9D89D7D55C3287A5 , 0x00003FFB // P5
290 data8 0xF0F03ADB3FC930D3 , 0x00003FFA // P7
291 data8 0xF396268151CFB11C , 0x00003FF7 // P17
292 data8 0x9D3436AABE218776 , 0x00003FF5 // P19
293 data8 0x80D601879218B53A , 0x00003FFA // P13
294 data8 0xA2270D30A90AA220 , 0x00003FF9 // P15
295 data8 0xCCCCCCCCCCC906CD , 0x00003FFC // P1
296 data8 0xE38E38E320A8A098 , 0x00003FFB // P3
297 data8 0xFE7E52D2A89995B3 , 0x0000BFEC // P22
298 data8 0xC90FDAA22168C235 , 0x00003FFE // pi/4
299 ASM_SIZE_DIRECTIVE(atan2_tb1)
302 ASM_TYPE_DIRECTIVE(atan2_tb2,@object)
303 data8 0x9F90FB984D8E39D0 , 0x0000BFF3 // P20
304 data8 0xCE585A259BD8374C , 0x00003FF0 // P21
305 data8 0xBA2E8B9793955C77 , 0x0000BFFB // P4
306 data8 0x88887EBB209E3543 , 0x0000BFFB // P6
307 data8 0xD818B4BB43D84BF2 , 0x0000BFF8 // P16
308 data8 0xDEC343E068A6D2A8 , 0x0000BFF6 // P18
309 data8 0x9297B23CCFFB291F , 0x0000BFFA // P12
310 data8 0xD5F4F2182E7A8725 , 0x0000BFF9 // P14
311 data8 0xAAAAAAAAAAAAA8A9 , 0x0000BFFD // P0
312 data8 0x9249249247E37913 , 0x0000BFFC // P2
313 data8 0xC90FDAA22168C235 , 0x00003FFF // pi/2
314 data8 0xC90FDAA22168C235 , 0x00004000 // pi
315 data8 0x96cbe3f9990e91a8 , 0x00004000 // 3pi/4
316 ASM_SIZE_DIRECTIVE(atan2_tb2)
325 .global __ieee754_atan2#
328 ////////////////////////////////////////////////////////
338 .proc __ieee754_atan2#
341 // qnan snan inf norm unorm 0 -+
346 // p10 ==> quiet Y and return
347 // p11 X NAN? p12, p13
348 // p12 ==> quiet X and return
351 alloc r32 = ar.pfs,1,5,4,0
352 frcpa.s1 atan2_u1_X,p6 = f1,atan2_X
353 addl EXP_AD_P2 = @ltoff(atan2_tb2), gp
356 addl EXP_AD_P1 = @ltoff(atan2_tb1), gp
357 fclass.m.unc p10,p11 = f8, 0xc3
363 ld8 EXP_AD_P1 = [EXP_AD_P1]
364 frcpa.s1 atan2_u1_Y,p7 = f1,atan2_Y
369 fma.s1 atan2_two = f1,f1,f1
376 ld8 EXP_AD_P2 = [ EXP_AD_P2]
377 famax.s1 atan2_Umax = f8,f9
384 fmerge.s atan2_absX = f0,atan2_X
389 // p10 Y NAN, quiet and return
391 ldfe atan2_P10 = [EXP_AD_P1],16
392 fmerge.s atan2_sgnY = atan2_Y,f1
397 (p10) fma.d f8 = f8,f9,f0
404 ldfe atan2_P11 = [EXP_AD_P1],16
405 ldfe atan2_P20 = [EXP_AD_P2],16
406 fmerge.s atan2_sgnX = atan2_X,f1
412 ldfe atan2_P8 = [EXP_AD_P1],16
413 fma.s1 atan2_z1_X = atan2_u1_X, atan2_Y, f0
418 ldfe atan2_P21 = [EXP_AD_P2],16
419 fma.s1 atan2_z1_Y = atan2_u1_Y, atan2_X, f0
425 ldfe atan2_P9 = [EXP_AD_P1],16
426 fnma.s1 atan2_B1X = atan2_u1_X, atan2_X, atan2_two
431 ldfe atan2_P4 = [EXP_AD_P2],16
432 fnma.s1 atan2_B1Y = atan2_u1_Y, atan2_Y, atan2_two
437 // p6 (atan2_sT) true if swap
438 // p7 (atan2_sF) true if no swap
439 // p11 ==> Y !NAN; X NAN?
442 ldfe atan2_P5 = [EXP_AD_P1],16
443 // fcmp.eq.unc.s1 atan2_sF,atan2_sT = atan2_Umax, atan2_X
444 fcmp.eq.unc.s1 p7,p6 = atan2_Umax, atan2_X
448 ldfe atan2_P6 = [EXP_AD_P2],16
449 (p11) fclass.m.unc p12,p13 = f9, 0xc3
455 ldfe atan2_P7 = [EXP_AD_P1],16
456 ldfe atan2_P16 = [EXP_AD_P2],16
457 famin.s1 atan2_Vmin = f8,f9
461 // p8 true if X positive
462 // p9 true if X negative
463 // both are false is swap is true
465 ldfe atan2_P17 = [EXP_AD_P1],16
466 //(atan2_sF) fcmp.eq.unc.s1 p8,p9 = atan2_sgnX,f1
467 (p7) fcmp.eq.unc.s1 p8,p9 = atan2_sgnX,f1
471 ldfe atan2_P18 = [EXP_AD_P2],16
472 fma.s1 atan2_sgnXY = atan2_sgnX, atan2_sgnY, f0
479 ldfe atan2_P19 = [EXP_AD_P1],16
480 //(atan2_sF) fma.s1 atan2_wp = atan2_z1_X, atan2_z1_X, f0
481 (p7) fma.s1 atan2_wp = atan2_z1_X, atan2_z1_X, f0
485 ldfe atan2_P12 = [EXP_AD_P2],16
486 //(atan2_sT) fma.s1 atan2_wp = atan2_z1_Y, atan2_z1_Y, f0
487 (p6) fma.s1 atan2_wp = atan2_z1_Y, atan2_z1_Y, f0
494 ldfe atan2_P13 = [EXP_AD_P1],16
495 //(atan2_sF) fma.s1 atan2_z = atan2_z1_X, atan2_B1X, f0
496 (p7) fma.s1 atan2_z = atan2_z1_X, atan2_B1X, f0
500 ldfe atan2_P14 = [EXP_AD_P2],16
501 //(atan2_sT) fma.s1 atan2_z = atan2_z1_Y, atan2_B1Y, f0
502 (p6) fma.s1 atan2_z = atan2_z1_Y, atan2_B1Y, f0
509 ldfe atan2_P15 = [EXP_AD_P1],16
510 //(atan2_sF) fma.s1 atan2_B1sq = atan2_B1X, atan2_B1X, f0
511 (p7) fma.s1 atan2_B1sq = atan2_B1X, atan2_B1X, f0
515 ldfe atan2_P0 = [EXP_AD_P2],16
516 //(atan2_sT) fma.s1 atan2_B1sq = atan2_B1Y, atan2_B1Y, f0
517 (p6) fma.s1 atan2_B1sq = atan2_B1Y, atan2_B1Y, f0
523 // p12 ==> X NAN, quiet and return
525 ldfe atan2_P1 = [EXP_AD_P1],16
526 fmerge.s atan2_Umax = f0,atan2_Umax
530 ldfe atan2_P2 = [EXP_AD_P2],16
531 (p12) fma.d f8 = f9,f8,f0
538 // p11 ==> x !inf y ?
540 ldfe atan2_P3 = [EXP_AD_P1],16
541 fmerge.s atan2_Vmin = f0,atan2_Vmin
545 ldfe atan2_Pi_by_2 = [EXP_AD_P2],16
546 fclass.m.unc p10,p11 = f9, 0x23
553 ldfe atan2_P22 = [EXP_AD_P1],16
554 ldfe atan2_pi = [EXP_AD_P2],16
561 fcmp.eq.s0 p12,p13=f9,f8 // Dummy to catch denormal and invalid
568 ldfe atan2_pi_by_4 = [EXP_AD_P1],16
569 //(atan2_sT) fmerge.ns atan2_sgnXY = atan2_sgnXY, atan2_sgnXY
570 (p6) fmerge.ns atan2_sgnXY = atan2_sgnXY, atan2_sgnXY
574 ldfe atan2_3pi_by_4 = [EXP_AD_P2],16
575 fma.s1 atan2_w = atan2_wp, atan2_B1sq,f0
580 // p12 ==> x inf y inf
581 // p13 ==> x inf y !inf
584 fmerge.s atan2_z = f0, atan2_z
591 (p10) fclass.m.unc p12,p13 = f8, 0x23
596 (p11) fclass.m.unc p14,p15 = f8, 0x23
603 (p12) fcmp.eq.unc.s1 p10,p11 = atan2_sgnX,f1
610 mov atan2_GR_sml_exp = 0x1 // Small exponent for making small norm
611 (p14) fma.d f8 = atan2_sgnY, atan2_Pi_by_2, f0
616 // Make a very small normal in case need to force inexact and underflow
618 setf.exp atan2_sml_norm = atan2_GR_sml_exp
619 fma.s1 atan2_V13 = atan2_w, atan2_P11, atan2_P10
624 fma.s1 atan2_W11 = atan2_w, atan2_P21, atan2_P20
632 fma.s1 atan2_E = atan2_Vmin, atan2_z, atan2_Umax
637 fnma.s1 atan2_gamma = atan2_Umax, atan2_z, f1
644 fma.s1 atan2_V11 = atan2_w, atan2_P9, atan2_P8
649 fma.s1 atan2_V12 = atan2_w, atan2_w, f0
656 fma.s1 atan2_V7 = atan2_w, atan2_P5 , atan2_P4
661 fma.s1 atan2_V8 = atan2_w, atan2_P7 , atan2_P6
668 fma.s1 atan2_W7 = atan2_w, atan2_P17, atan2_P16
673 fma.s1 atan2_W8 = atan2_w, atan2_P19, atan2_P18
680 fma.s1 atan2_W3 = atan2_w, atan2_P13, atan2_P12
685 fma.s1 atan2_W4 = atan2_w, atan2_P15, atan2_P14
692 fma.s1 atan2_V3 = atan2_w, atan2_P1 , atan2_P0
697 fma.s1 atan2_V4 = atan2_w, atan2_P3 , atan2_P2
704 fma.s1 atan2_zcub = atan2_z, atan2_w, f0
709 fnma.s1 atan2_gV = atan2_Umax, atan2_z, atan2_Vmin
716 frcpa.s1 atan2_F,p15 = f1, atan2_E
721 fma.s1 atan2_V10 = atan2_V12, atan2_V13, atan2_V11
728 fma.s1 atan2_V6 = atan2_V12, atan2_V8 , atan2_V7
733 fma.s1 atan2_V9 = atan2_V12, atan2_V12, f0
740 fma.s1 atan2_W10 = atan2_V12, atan2_P22 , atan2_W11
745 fma.s1 atan2_W6 = atan2_V12, atan2_W8 , atan2_W7
752 fma.s1 atan2_W2 = atan2_V12, atan2_W4 , atan2_W3
757 fma.s1 atan2_V2 = atan2_V12, atan2_V4 , atan2_V3
763 // Both X and Y are INF
766 .pred.rel "mutex",p10,p11
769 (p10) fma.d f8 = atan2_sgnY, atan2_pi_by_4, f0
774 (p11) fma.d f8 = atan2_sgnY, atan2_3pi_by_4, f0
780 .pred.rel "mutex",p8,p9,p6
783 fnma.s1 atan2_alpha = atan2_E, atan2_F, f1
788 fnma.s1 atan2_alpha_1 = atan2_E, atan2_F, atan2_two
796 //(atan2_sT) fmerge.s atan2_P = atan2_Y, atan2_Pi_by_2
797 (p6) fmerge.s atan2_P = atan2_Y, atan2_Pi_by_2
802 fma.s1 atan2_gVF = atan2_gV, atan2_F, f0
810 fma.s1 atan2_V5 = atan2_V9, atan2_V10, atan2_V6
815 fma.s1 atan2_W12 = atan2_V9, atan2_V9, f0
824 (p8) fmerge.s atan2_P = atan2_sgnY, f0
829 fma.s1 atan2_W5 = atan2_V9, atan2_W10, atan2_W6
839 (p9) fmerge.s atan2_P = atan2_sgnY, atan2_pi
847 fma.s1 atan2_alpha_sq = atan2_alpha, atan2_alpha, f0
852 fma.s1 atan2_Cp = atan2_alpha, atan2_alpha_1, f1
860 fma.s1 atan2_V1 = atan2_V9, atan2_V5, atan2_V2
865 fma.s1 atan2_W12 = atan2_V9, atan2_W12, f0
871 // p13 ==> x inf y !inf
874 fma.s1 atan2_W1 = atan2_V9, atan2_W5, atan2_W2
879 (p13) fcmp.eq.unc.s1 p10,p11 = atan2_sgnX,f1
887 fma.s1 atan2_alpha_cub = atan2_alpha, atan2_alpha_sq, f0
892 fma.s1 atan2_C = atan2_gVF, atan2_Cp, f0
897 .pred.rel "mutex",p10,p11
901 (p10) fmerge.s f8 = atan2_sgnY, f0
906 (p11) fma.d f8 = atan2_sgnY, atan2_pi, f0
917 fclass.m.unc p10,p11 = f8, 0x07
924 (p8) fmerge.s atan2_sml_norm = atan2_sgnY, atan2_sml_norm
931 fma.s1 atan2_Pp = atan2_W12, atan2_W1, atan2_V1
936 fma.s1 atan2_d = atan2_alpha_cub, atan2_C, atan2_C
947 (p10) fclass.m.unc p12,p13 = f9, 0x07
952 (p11) fclass.m.unc p14,p15 = f9, 0x07
962 (p13) fcmp.eq.unc.s1 p10,p11 = atan2_sgnX,f1
963 (p12) br.spnt ATAN2_ERROR
971 fma.s1 atan2_pd = atan2_P0, atan2_d, f0
976 fma.s1 atan2_dsq = atan2_d, atan2_d, f0
983 fma.s1 atan2_A_hi = atan2_zcub, atan2_Pp, atan2_z
988 (p14) fma.d f8 = atan2_sgnY, atan2_Pi_by_2, f0
997 (p10) fmerge.s f8 = atan2_sgnY, f0
1002 (p11) fma.d f8 = atan2_sgnY, atan2_pi, f0
1003 (p11) br.ret.spnt b0
1011 fma.s1 atan2_A_lo = atan2_pd, atan2_dsq, atan2_d
1019 fma.s1 atan2_A = atan2_A_hi, f1, atan2_A_lo
1024 // Force inexact and possibly underflow if very small results
1027 (p8) fma.d atan2_FR_tmp = atan2_sgnXY, atan2_A, atan2_sml_norm
1032 fma.d f8 = atan2_sgnXY, atan2_A, atan2_P
1041 fcmp.eq.unc.s1 p10,p11 = atan2_sgnX,f1
1047 mov atan2_GR_tag = 37
1048 (p10) fmerge.s f10 = atan2_sgnY, f0
1053 (p11) fma.d f10 = atan2_sgnY, atan2_pi, f0
1058 ASM_SIZE_DIRECTIVE(atan2#)
1061 // Stack operations when calling error support.
1062 // (1) (2) (3) (call) (4)
1063 // sp -> + psp -> + psp -> + sp -> +
1065 // | | <- GR_Y R3 ->| <- GR_RESULT | -> f8
1067 // | <-GR_Y Y2->| Y2 ->| <- GR_Y |
1069 // | | <- GR_X X1 ->| |
1071 // sp-64 -> + sp -> + sp -> + +
1072 // save ar.pfs save b0 restore gp
1073 // save gp restore ar.pfs
1076 .proc __libm_error_region
1077 __libm_error_region:
1081 add GR_Parameter_Y=-32,sp // Parameter 2 value
1083 .save ar.pfs,GR_SAVE_PFS
1084 mov GR_SAVE_PFS=ar.pfs // Save ar.pfs
1088 add sp=-64,sp // Create new stack
1090 mov GR_SAVE_GP=gp // Save gp
1096 stfd [GR_Parameter_Y] = f8,16 // STORE Parameter 2 on stack
1097 add GR_Parameter_X = 16,sp // Parameter 1 address
1098 .save b0, GR_SAVE_B0
1099 mov GR_SAVE_B0=b0 // Save b0
1105 stfd [GR_Parameter_X] = f9 // STORE Parameter 1 on stack
1106 add GR_Parameter_RESULT = 0,GR_Parameter_Y // Parameter 3 address
1110 stfd [GR_Parameter_Y] = f10 // STORE Parameter 3 on stack
1111 add GR_Parameter_Y = -16,GR_Parameter_Y
1112 br.call.sptk b0=__libm_error_support# // Call error handling function
1117 add GR_Parameter_RESULT = 48,sp
1122 ldfd f8 = [GR_Parameter_RESULT] // Get return result off stack
1124 add sp = 64,sp // Restore stack pointer
1125 mov b0 = GR_SAVE_B0 // Restore return address
1128 mov gp = GR_SAVE_GP // Restore gp
1129 mov ar.pfs = GR_SAVE_PFS // Restore ar.pfs
1130 br.ret.sptk b0 // Return
1133 .endp __libm_error_region
1134 ASM_SIZE_DIRECTIVE(__libm_error_region)
1136 .type __libm_error_support#,@function
1137 .global __libm_error_support#