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 // 08/17/00 New and much faster algorithm.
44 // 08/30/00 Avoided bank conflicts on loads, shortened |x|=1 and x=0 paths,
45 // fixed mfb split issue stalls.
46 // 05/20/02 Cleaned up namespace and sf0 syntax
47 // 08/02/02 New and much faster algorithm II
48 // 02/06/03 Reordered header: .section, .global, .proc, .align
51 //=========================================
52 // The acos function computes the principal value of the arc cosine of x.
53 // acos(0) returns Pi/2, acos(1) returns 0, acos(-1) returns Pi.
54 // A doman error occurs for arguments not in the range [-1,+1].
56 // The acos function returns the arc cosine in the range [0, Pi] radians.
60 // Return acos(x) = Pi/2 + x
62 // 2. 0.0 < |x| < 0.625
63 // Return acos(x) = Pi/2 - x - x^3 *PolA(x^2)
64 // where PolA(x^2) = A3 + A5*x^2 + A7*x^4 +...+ A35*x^32
66 // 3. 0.625 <=|x| < 1.0
67 // Return acos(x) = Pi/2 - asin(x) =
68 // = Pi/2 - sign(x) * ( Pi/2 - sqrt(R) * PolB(R))
70 // PolB(R) = B0 + B1*R + B2*R^2 +...+B12*R^12
72 // sqrt(R) is approximated using the following sequence:
73 // y0 = (1 + eps)/sqrt(R) - initial approximation by frsqrta,
75 // Then 3 iterations are used to refine the result:
90 // S3 approximates sqrt(R) with enough accuracy for this algorithm
92 // So, the result should be reconstracted as follows:
93 // acos(x) = Pi/2 - sign(x) * (Pi/2 - S3*PolB(R))
95 // But for optimization purposes the reconstruction step is slightly
97 // acos(x) = Cpi + sign(x)*PolB(R)*S2 - sign(x)*d2*S2*PolB(R)
98 // where Cpi = 0 if x > 0 and Cpi = Pi if x < 0
101 // Return acos(1.0) = 0.0, acos(-1.0) = Pi
103 // 5. 1.0 < |x| <= +INF
104 // A doman error occurs for arguments not in the range [-1,+1]
107 // Return acos(x) = QNaN
110 // Return acos(x) = Pi/2 - x,
113 // Normalize input in f8 and return to the very beginning of the function
116 //==============================================================
117 // Floating Point registers used:
119 // f6, f7, f9 -> f15, f32 -> f64
121 // General registers used:
122 // r3, r21 -> r31, r32 -> r38
124 // Predicate registers used:
129 //=========================================
130 // integer registers used
152 GR_Parameter_RESULT = r37
153 GR_Parameter_TAG = r38
155 // floating point registers used
220 //==============================================================
223 LOCAL_OBJECT_START(acos_base_range_table)
224 // Ai: Polynomial coefficients for the acos(x), |x| < .625000
225 // Bi: Polynomial coefficients for the acos(x), |x| > .625000
226 data8 0xBFDAAB56C01AE468 //A29
227 data8 0x3FE1C470B76A5B2B //A31
228 data8 0xBFDC5FF82A0C4205 //A33
229 data8 0x3FC71FD88BFE93F0 //A35
230 data8 0xB504F333F9DE6487, 0x00003FFF //B0
231 data8 0xAAAAAAAAAAAAFC18, 0x00003FFC //A3
232 data8 0x3F9F1C71BC4A7823 //A9
233 data8 0x3F96E8BBAAB216B2 //A11
234 data8 0x3F91C4CA1F9F8A98 //A13
235 data8 0x3F8C9DDCEDEBE7A6 //A15
236 data8 0x3F877784442B1516 //A17
237 data8 0x3F859C0491802BA2 //A19
238 data8 0x9999999998C88B8F, 0x00003FFB //A5
239 data8 0x3F6BD7A9A660BF5E //A21
240 data8 0x3F9FC1659340419D //A23
241 data8 0xB6DB6DB798149BDF, 0x00003FFA //A7
242 data8 0xBFB3EF18964D3ED3 //A25
243 data8 0x3FCD285315542CF2 //A27
244 data8 0xF15BEEEFF7D2966A, 0x00003FFB //B1
245 data8 0x3EF0DDA376D10FB3 //B10
246 data8 0xBEB83CAFE05EBAC9 //B11
247 data8 0x3F65FFB67B513644 //B4
248 data8 0x3F5032FBB86A4501 //B5
249 data8 0x3F392162276C7CBA //B6
250 data8 0x3F2435949FD98BDF //B7
251 data8 0xD93923D7FA08341C, 0x00003FF9 //B2
252 data8 0x3F802995B6D90BDB //B3
253 data8 0x3F10DF86B341A63F //B8
254 data8 0xC90FDAA22168C235, 0x00003FFF // Pi/2
255 data8 0x3EFA3EBD6B0ECB9D //B9
256 data8 0x3EDE18BA080E9098 //B12
257 LOCAL_OBJECT_END(acos_base_range_table)
260 GLOBAL_LIBM_ENTRY(acos)
263 getf.d rXBits = f8 // grab bits of input value
264 // set p12 = 1 if x is a NaN, denormal, or zero
265 fclass.m p12, p0 = f8, 0xcf
269 addl rTblAddr = @ltoff(acos_base_range_table),gp
270 // 1 - x = 1 - |x| for positive x
271 fms.s1 f1mX = f1, f1, f8
272 addl rHalf = 0xFFFE, r0 // exponent of 1/2
276 addl r0625 = 0x3FE4, r0 // high 16 bits of 0.625
277 // set p8 = 1 if x < 0
278 fcmp.lt.s1 p8, p9 = f8, f0
279 shl rSign = rSign, 63 // sign bit
282 // point to the beginning of the table
283 ld8 rTblAddr = [rTblAddr]
284 // 1 + x = 1 - |x| for negative x
285 fma.s1 f1pX = f1, f1, f8
286 adds rOne = 0x3FF, r0
290 andcm rAbsXBits = rXBits, rSign // bits of |x|
291 fmerge.s fSignX = f8, f1 // signum(x)
292 shl r0625 = r0625, 48 // bits of DP representation of 0.625
295 setf.exp fHalf = rHalf // load A2 to FP reg
296 fma.s1 fXSqr = f8, f8, f0 // x^2
297 // branch on special path if x is a NaN, denormal, or zero
298 (p12) br.cond.spnt acos_special
302 adds rPiBy2Ptr = 272, rTblAddr
304 shl rOne = rOne, 52 // bits of 1.0
307 adds rTmpPtr1 = 16, rTblAddr
309 // set p6 = 1 if |x| < 0.625
310 cmp.lt p6, p7 = rAbsXBits, r0625
314 ldfpd fA29, fA31 = [rTblAddr] // A29, fA31
315 // 1 - x = 1 - |x| for positive x
316 (p9) fms.s1 fR = f1, f1, f8
317 // point to coefficient of "near 1" polynomial
318 (p7) adds rTmpPtr2 = 176, rTblAddr
321 ldfpd fA33, fA35 = [rTmpPtr1], 16 // A33, fA35
322 // 1 + x = 1 - |x| for negative x
323 (p8) fma.s1 fR = f1, f1, f8
324 (p6) adds rTmpPtr2 = 48, rTblAddr
328 ldfe fB0 = [rTmpPtr1], 16 // B0
333 adds rTmpPtr3 = 16, rTmpPtr2
334 // set p10 = 1 if |x| = 1.0
335 cmp.eq p10, p0 = rAbsXBits, rOne
336 // branch on special path for |x| = 1.0
337 (p10) br.cond.spnt acos_abs_1
341 ldfe fA3 = [rTmpPtr2], 48 // A3 or B1
343 adds rTmpPtr1 = 64, rTmpPtr3
346 ldfpd fA9, fA11 = [rTmpPtr3], 16 // A9, A11 or B10, B11
347 // set p11 = 1 if |x| > 1.0
348 cmp.gt p11, p0 = rAbsXBits, rOne
349 // branch on special path for |x| > 1.0
350 (p11) br.cond.spnt acos_abs_gt_1
354 ldfpd fA17, fA19 = [rTmpPtr2], 16 // A17, A19 or B6, B7
355 // initial approximation of 1 / sqrt(1 - x)
356 frsqrta.s1 f1mXRcp, p0 = f1mX
360 ldfpd fA13, fA15 = [rTmpPtr3] // A13, A15 or B4, B5
361 fma.s1 fXCube = fXSqr, f8, f0 // x^3
366 ldfe fA5 = [rTmpPtr2], 48 // A5 or B2
367 // initial approximation of 1 / sqrt(1 + x)
368 frsqrta.s1 f1pXRcp, p0 = f1pX
372 ldfpd fA21, fA23 = [rTmpPtr1], 16 // A21, A23 or B3, B8
373 fma.s1 fXQuadr = fXSqr, fXSqr, f0 // x^4
378 ldfe fA7 = [rTmpPtr1] // A7 or Pi/2
379 fma.s1 fRSqr = fR, fR, f0 // R^2
383 ldfpd fA25, fA27 = [rTmpPtr2] // A25, A27 or B9, B12
385 (p6) br.cond.spnt acos_base_range;
391 (p9) fma.s1 fH = fHalf, f1mXRcp, f0 // H0 for x > 0
396 (p9) fma.s1 fS = f1mX, f1mXRcp, f0 // S0 for x > 0
402 (p8) fma.s1 fH = fHalf, f1pXRcp, f0 // H0 for x < 0
407 (p8) fma.s1 fS = f1pX, f1pXRcp, f0 // S0 for x > 0
413 fma.s1 fRQuadr = fRSqr, fRSqr, f0 // R^4
419 fma.s1 fB11 = fB11, fR, fB10
424 fma.s1 fB1 = fB1, fR, fB0
430 fma.s1 fB5 = fB5, fR, fB4
435 fma.s1 fB7 = fB7, fR, fB6
441 fma.s1 fB3 = fB3, fR, fB2
447 fnma.s1 fD = fH, fS, fHalf // d0 = 1/2 - H0*S0
453 fma.s1 fR8 = fRQuadr, fRQuadr, f0 // R^4
458 fma.s1 fB9 = fB9, fR, fB8
464 fma.s1 fB12 = fB12, fRSqr, fB11
469 fma.s1 fB7 = fB7, fRSqr, fB5
475 fma.s1 fB3 = fB3, fRSqr, fB1
481 fma.s1 fH = fH, fD, fH // H1 = H0 + H0*d0
486 fma.s1 fS = fS, fD, fS // S1 = S0 + S0*d0
492 (p9) fma.s1 fCpi = f1, f0, f0 // Cpi = 0 if x > 0
497 (p8) fma.s1 fCpi = fPiBy2, f1, fPiBy2 // Cpi = Pi if x < 0
503 fma.s1 fB12 = fB12, fRSqr, fB9
508 fma.s1 fB7 = fB7, fRQuadr, fB3
514 fnma.s1 fD = fH, fS, fHalf // d1 = 1/2 - H1*S1
519 fnma.s1 fSignedS = fSignX, fS, f0 // -signum(x)*S1
525 fma.s1 fCloseTo1Pol = fB12, fR8, fB7
531 fma.s1 fH = fH, fD, fH // H2 = H1 + H1*d1
536 fma.s1 fS = fS, fD, fS // S2 = S1 + S1*d1
542 // -signum(x)* S2 = -signum(x)*(S1 + S1*d1)
543 fma.s1 fSignedS = fSignedS, fD, fSignedS
549 fnma.s1 fD = fH, fS, fHalf // d2 = 1/2 - H2*S2
555 // Cpi + signum(x)*PolB*S2
556 fnma.s1 fCpi = fSignedS, fCloseTo1Pol, fCpi
561 // signum(x)*PolB * S2
562 fnma.s1 fCloseTo1Pol = fSignedS, fCloseTo1Pol, f0
568 // final result for 0.625 <= |x| < 1
569 fma.d.s0 f8 = fCloseTo1Pol, fD, fCpi
570 // exit here for 0.625 <= |x| < 1
576 // here if |x| < 0.625
580 ldfe fCpi = [rPiBy2Ptr] // Pi/2
581 fma.s1 fA33 = fA33, fXSqr, fA31
586 fma.s1 fA15 = fA15, fXSqr, fA13
592 fma.s1 fA29 = fA29, fXSqr, fA27
597 fma.s1 fA25 = fA25, fXSqr, fA23
603 fma.s1 fA21 = fA21, fXSqr, fA19
608 fma.s1 fA9 = fA9, fXSqr, fA7
614 fma.s1 fA5 = fA5, fXSqr, fA3
620 fma.s1 fA35 = fA35, fXQuadr, fA33
625 fma.s1 fA17 = fA17, fXQuadr, fA15
631 fma.s1 fX8 = fXQuadr, fXQuadr, f0 // x^8
636 fma.s1 fA25 = fA25, fXQuadr, fA21
642 fma.s1 fA9 = fA9, fXQuadr, fA5
648 fms.s1 fCpi = fCpi, f1, f8 // Pi/2 - x
654 fma.s1 fA35 = fA35, fXQuadr, fA29
659 fma.s1 fA17 = fA17, fXSqr, fA11
665 fma.s1 fX16 = fX8, fX8, f0 // x^16
671 fma.s1 fA35 = fA35, fX8, fA25
676 fma.s1 fA17 = fA17, fX8, fA9
682 fma.s1 fBaseP = fA35, fX16, fA17
688 // final result for |x| < 0.625
689 fnma.d.s0 f8 = fBaseP, fXCube, fCpi
690 // exit here for |x| < 0.625 path
701 ldfe fPiBy2 = [rPiBy2Ptr] // Pi/2
706 .pred.rel "mutex", p8, p9
709 // result for x = 1.0
710 (p9) fma.d.s0 f8 = f1, f0, f0 // 0.0
715 // result for x = -1.0
716 (p8) fma.d.s0 f8 = fPiBy2, f1, fPiBy2 // Pi
717 // exit here for |x| = 1.0
722 // here if x is a NaN, denormal, or zero
727 adds rPiBy2Ptr = 272, rTblAddr
728 // set p12 = 1 if x is a NaN
729 fclass.m p12, p0 = f8, 0xc3
734 // smallest positive DP normalized number
735 movl rDenoBound = 0x0010000000000000
739 ldfe fPiBy2 = [rPiBy2Ptr] // Pi/2
740 // set p13 = 1 if x = 0.0
741 fclass.m p13, p0 = f8, 0x07
751 // load smallest normal to FP reg
752 setf.d fDenoBound = rDenoBound
753 // answer if x is a NaN
754 (p12) fma.d.s0 f8 = f8,f1,f0
755 // exit here if x is a NaN
761 // absolute value of normalized x
762 fmerge.s fNormX = f1, fNormX
768 // final result for x = 0
769 (p13) fma.d.s0 f8 = fPiBy2, f1, f8
770 // exit here if x = 0.0
774 // if we still here then x is denormal or unnormal
777 // set p14 = 1 if normalized x is greater than or
778 // equal to the smallest denormalized value
779 // So, if p14 is set to 1 it means that we deal with
780 // unnormal rather than with "true" denormal
781 fcmp.ge.s1 p14, p0 = fNormX, fDenoBound
787 (p14) fcmp.eq.s0 p6, p0 = f8, f0 // Set D flag if x unnormal
792 // normalize unnormal input
793 (p14) fnorm.s1 f8 = f8
794 // return to the main path
795 (p14) br.cond.sptk acos_unnormal_back
798 // if we still here it means that input is "true" denormal
801 // final result if x is denormal
802 fms.d.s0 f8 = fPiBy2, f1, f8 // Pi/2 - x
803 // exit here if x is denormal
809 // error handler should be called
813 alloc r32 = ar.pfs, 0, 3, 4, 0 // get some registers
814 fmerge.s FR_X = f8,f8
818 mov GR_Parameter_TAG = 58 // error code
819 frcpa.s0 FR_RESULT, p0 = f0,f0
820 // call error handler routine
821 br.cond.sptk __libm_error_region
824 GLOBAL_LIBM_END(acos)
828 LOCAL_LIBM_ENTRY(__libm_error_region)
831 add GR_Parameter_Y=-32,sp // Parameter 2 value
833 .save ar.pfs,GR_SAVE_PFS
834 mov GR_SAVE_PFS=ar.pfs // Save ar.pfs
838 add sp=-64,sp // Create new stack
840 mov GR_SAVE_GP=gp // Save gp
843 stfd [GR_Parameter_Y] = FR_Y,16 // STORE Parameter 2 on stack
844 add GR_Parameter_X = 16,sp // Parameter 1 address
846 mov GR_SAVE_B0=b0 // Save b0
850 stfd [GR_Parameter_X] = FR_X // STORE Parameter 1 on stack
851 add GR_Parameter_RESULT = 0,GR_Parameter_Y // Parameter 3 address
855 stfd [GR_Parameter_Y] = FR_RESULT // STORE Parameter 3 on stack
856 add GR_Parameter_Y = -16,GR_Parameter_Y
857 br.call.sptk b0=__libm_error_support# // Call error handling function
860 add GR_Parameter_RESULT = 48,sp
865 ldfd f8 = [GR_Parameter_RESULT] // Get return result off stack
867 add sp = 64,sp // Restore stack pointer
868 mov b0 = GR_SAVE_B0 // Restore return address
871 mov gp = GR_SAVE_GP // Restore gp
872 mov ar.pfs = GR_SAVE_PFS // Restore ar.pfs
873 br.ret.sptk b0 // Return
876 LOCAL_LIBM_END(__libm_error_region)
877 .type __libm_error_support#,@function
878 .global __libm_error_support#