4 // Copyright (c) 2000 - 2003 Intel Corporation
5 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
12 // * Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
15 // * Redistributions in binary form must reproduce the above copyright
16 // notice, this list of conditions and the following disclaimer in the
17 // documentation and/or other materials provided with the distribution.
19 // * The name of Intel Corporation may not be used to endorse or promote
20 // products derived from this software without specific prior written
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS
27 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
31 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING
32 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 // Intel Corporation is the author of this code, and requests that all
36 // problem reports or change requests be submitted to it directly at
37 // http://www.intel.com/software/products/opensource/libraries/num.htm.
40 //==============================================================
41 // 02/02/00 Initial version
42 // 08/17/00 New and much faster algorithm.
43 // 08/31/00 Avoided bank conflicts on loads, shortened |x|=1 path,
44 // fixed mfb split issue stalls.
45 // 12/19/00 Fixed small arg cases to force inexact, or inexact and underflow.
46 // 08/02/02 New and much faster algorithm II
47 // 02/06/03 Reordered header: .section, .global, .proc, .align
50 //=========================================
51 // The asin function computes the principal value of the arc sine of x.
52 // asin(0) returns 0, asin(1) returns pi/2, asin(-1) returns -pi/2.
53 // A domain error occurs for arguments not in the range [-1,+1].
55 // The asin function returns the arc sine in the range [-pi/2, +pi/2] radians.
59 // Return asin(x) = +/-0.0
61 // 2. 0.0 < |x| < 0.625
62 // Return asin(x) = x + x^3 *PolA(x^2)
63 // where PolA(x^2) = A3 + A5*x^2 + A7*x^4 +...+ A35*x^32
65 // 3. 0.625 <=|x| < 1.0
66 // Return asin(x) = sign(x) * ( Pi/2 - sqrt(R) * PolB(R))
68 // PolB(R) = B0 + B1*R + B2*R^2 +...+B12*R^12
70 // sqrt(R) is approximated using the following sequence:
71 // y0 = (1 + eps)/sqrt(R) - initial approximation by frsqrta,
73 // Then 3 iterations are used to refine the result:
88 // S3 approximates sqrt(R) with enough accuracy for this algorithm
90 // So, the result should be reconstracted as follows:
91 // asin(x) = sign(x) * (Pi/2 - S3*PolB(R))
93 // But for optimization perposes the reconstruction step is slightly
95 // asin(x) = sign(x)*(Pi/2 - PolB(R)*S2) + sign(x)*d2*S2*PolB(R)
98 // Return asin(x) = sign(x)*Pi/2
100 // 5. 1.0 < |x| <= +INF
101 // A domain error occurs for arguments not in the range [-1,+1]
104 // Return asin(x) = QNaN
107 // Return asin(x) = x + x^3,
110 // Normalize input in f8 and return to the very beginning of the function
113 //==============================================================
114 // Floating Point registers used:
116 // f6, f7, f9 -> f15, f32 -> f63
118 // General registers used:
119 // r3, r21 -> r31, r32 -> r38
121 // Predicate registers used:
126 //=========================================
127 // integer registers used
149 GR_Parameter_RESULT = r37
150 GR_Parameter_TAG = r38
152 // floating point registers used
215 //==============================================================
218 LOCAL_OBJECT_START(asin_base_range_table)
219 // Ai: Polynomial coefficients for the asin(x), |x| < .625000
220 // Bi: Polynomial coefficients for the asin(x), |x| > .625000
221 data8 0xBFDAAB56C01AE468 //A29
222 data8 0x3FE1C470B76A5B2B //A31
223 data8 0xBFDC5FF82A0C4205 //A33
224 data8 0x3FC71FD88BFE93F0 //A35
225 data8 0xB504F333F9DE6487, 0x00003FFF //B0
226 data8 0xAAAAAAAAAAAAFC18, 0x00003FFC //A3
227 data8 0x3F9F1C71BC4A7823 //A9
228 data8 0x3F96E8BBAAB216B2 //A11
229 data8 0x3F91C4CA1F9F8A98 //A13
230 data8 0x3F8C9DDCEDEBE7A6 //A15
231 data8 0x3F877784442B1516 //A17
232 data8 0x3F859C0491802BA2 //A19
233 data8 0x9999999998C88B8F, 0x00003FFB //A5
234 data8 0x3F6BD7A9A660BF5E //A21
235 data8 0x3F9FC1659340419D //A23
236 data8 0xB6DB6DB798149BDF, 0x00003FFA //A7
237 data8 0xBFB3EF18964D3ED3 //A25
238 data8 0x3FCD285315542CF2 //A27
239 data8 0xF15BEEEFF7D2966A, 0x00003FFB //B1
240 data8 0x3EF0DDA376D10FB3 //B10
241 data8 0xBEB83CAFE05EBAC9 //B11
242 data8 0x3F65FFB67B513644 //B4
243 data8 0x3F5032FBB86A4501 //B5
244 data8 0x3F392162276C7CBA //B6
245 data8 0x3F2435949FD98BDF //B7
246 data8 0xD93923D7FA08341C, 0x00003FF9 //B2
247 data8 0x3F802995B6D90BDB //B3
248 data8 0x3F10DF86B341A63F //B8
249 data8 0xC90FDAA22168C235, 0x00003FFF // Pi/2
250 data8 0x3EFA3EBD6B0ECB9D //B9
251 data8 0x3EDE18BA080E9098 //B12
252 LOCAL_OBJECT_END(asin_base_range_table)
256 GLOBAL_LIBM_ENTRY(asin)
259 getf.d rXBits = f8 // grab bits of input value
260 // set p12 = 1 if x is a NaN, denormal, or zero
261 fclass.m p12, p0 = f8, 0xcf
265 addl rTblAddr = @ltoff(asin_base_range_table),gp
266 // 1 - x = 1 - |x| for positive x
267 fms.s1 f1mX = f1, f1, f8
268 addl rHalf = 0xFFFE, r0 // exponent of 1/2
272 addl r0625 = 0x3FE4, r0 // high 16 bits of 0.625
273 // set p8 = 1 if x < 0
274 fcmp.lt.s1 p8, p9 = f8, f0
275 shl rSign = rSign, 63 // sign bit
278 // point to the beginning of the table
279 ld8 rTblAddr = [rTblAddr]
280 // 1 + x = 1 - |x| for negative x
281 fma.s1 f1pX = f1, f1, f8
282 adds rOne = 0x3FF, r0
286 andcm rAbsXBits = rXBits, rSign // bits of |x|
287 fmerge.s fSignX = f8, f1 // signum(x)
288 shl r0625 = r0625, 48 // bits of DP representation of 0.625
291 setf.exp fHalf = rHalf // load A2 to FP reg
292 fma.s1 fXSqr = f8, f8, f0 // x^2
293 // branch on special path if x is a NaN, denormal, or zero
294 (p12) br.cond.spnt asin_special
298 adds rPiBy2Ptr = 272, rTblAddr
300 shl rOne = rOne, 52 // bits of 1.0
303 adds rTmpPtr1 = 16, rTblAddr
305 // set p6 = 1 if |x| < 0.625
306 cmp.lt p6, p7 = rAbsXBits, r0625
310 ldfpd fA29, fA31 = [rTblAddr] // A29, fA31
311 // 1 - x = 1 - |x| for positive x
312 (p9) fms.s1 fR = f1, f1, f8
313 // point to coefficient of "near 1" polynomial
314 (p7) adds rTmpPtr2 = 176, rTblAddr
317 ldfpd fA33, fA35 = [rTmpPtr1], 16 // A33, fA35
318 // 1 + x = 1 - |x| for negative x
319 (p8) fma.s1 fR = f1, f1, f8
320 (p6) adds rTmpPtr2 = 48, rTblAddr
324 ldfe fB0 = [rTmpPtr1], 16 // B0
329 adds rTmpPtr3 = 16, rTmpPtr2
330 // set p10 = 1 if |x| = 1.0
331 cmp.eq p10, p0 = rAbsXBits, rOne
332 // branch on special path for |x| = 1.0
333 (p10) br.cond.spnt asin_abs_1
337 ldfe fA3 = [rTmpPtr2], 48 // A3 or B1
339 adds rTmpPtr1 = 64, rTmpPtr3
342 ldfpd fA9, fA11 = [rTmpPtr3], 16 // A9, A11 or B10, B11
343 // set p11 = 1 if |x| > 1.0
344 cmp.gt p11, p0 = rAbsXBits, rOne
345 // branch on special path for |x| > 1.0
346 (p11) br.cond.spnt asin_abs_gt_1
350 ldfpd fA17, fA19 = [rTmpPtr2], 16 // A17, A19 or B6, B7
351 // initial approximation of 1 / sqrt(1 - x)
352 frsqrta.s1 f1mXRcp, p0 = f1mX
356 ldfpd fA13, fA15 = [rTmpPtr3] // A13, A15 or B4, B5
357 fma.s1 fXCube = fXSqr, f8, f0 // x^3
362 ldfe fA5 = [rTmpPtr2], 48 // A5 or B2
363 // initial approximation of 1 / sqrt(1 + x)
364 frsqrta.s1 f1pXRcp, p0 = f1pX
368 ldfpd fA21, fA23 = [rTmpPtr1], 16 // A21, A23 or B3, B8
369 fma.s1 fXQuadr = fXSqr, fXSqr, f0 // x^4
374 ldfe fA7 = [rTmpPtr1] // A7 or Pi/2
375 fma.s1 fRSqr = fR, fR, f0 // R^2
379 ldfpd fA25, fA27 = [rTmpPtr2] // A25, A27 or B9, B12
381 (p6) br.cond.spnt asin_base_range;
387 (p9) fma.s1 fH = fHalf, f1mXRcp, f0 // H0 for x > 0
392 (p9) fma.s1 fS = f1mX, f1mXRcp, f0 // S0 for x > 0
398 (p8) fma.s1 fH = fHalf, f1pXRcp, f0 // H0 for x < 0
403 (p8) fma.s1 fS = f1pX, f1pXRcp, f0 // S0 for x > 0
409 fma.s1 fRQuadr = fRSqr, fRSqr, f0 // R^4
415 fma.s1 fB11 = fB11, fR, fB10
420 fma.s1 fB1 = fB1, fR, fB0
426 fma.s1 fB5 = fB5, fR, fB4
431 fma.s1 fB7 = fB7, fR, fB6
437 fma.s1 fB3 = fB3, fR, fB2
443 fnma.s1 fD = fH, fS, fHalf // d0 = 1/2 - H0*S0
449 fma.s1 fR8 = fRQuadr, fRQuadr, f0 // R^4
454 fma.s1 fB9 = fB9, fR, fB8
460 fma.s1 fB12 = fB12, fRSqr, fB11
465 fma.s1 fB7 = fB7, fRSqr, fB5
471 fma.s1 fB3 = fB3, fRSqr, fB1
477 fma.s1 fH = fH, fD, fH // H1 = H0 + H0*d0
482 fma.s1 fS = fS, fD, fS // S1 = S0 + S0*d0
488 fma.s1 fPiBy2 = fPiBy2, fSignX, f0 // signum(x)*Pi/2
494 fma.s1 fB12 = fB12, fRSqr, fB9
499 fma.s1 fB7 = fB7, fRQuadr, fB3
505 fnma.s1 fD = fH, fS, fHalf // d1 = 1/2 - H1*S1
510 fnma.s1 fSignedS = fSignX, fS, f0 // -signum(x)*S1
516 fma.s1 fCloseTo1Pol = fB12, fR8, fB7
522 fma.s1 fH = fH, fD, fH // H2 = H1 + H1*d1
527 fma.s1 fS = fS, fD, fS // S2 = S1 + S1*d1
533 // -signum(x)* S2 = -signum(x)*(S1 + S1*d1)
534 fma.s1 fSignedS = fSignedS, fD, fSignedS
540 fnma.s1 fD = fH, fS, fHalf // d2 = 1/2 - H2*S2
546 // signum(x)*(Pi/2 - PolB*S2)
547 fma.s1 fPiBy2 = fSignedS, fCloseTo1Pol, fPiBy2
552 // -signum(x)*PolB * S2
553 fma.s1 fCloseTo1Pol = fSignedS, fCloseTo1Pol, f0
559 // final result for 0.625 <= |x| < 1
560 fma.d.s0 f8 = fCloseTo1Pol, fD, fPiBy2
561 // exit here for 0.625 <= |x| < 1
567 // here if |x| < 0.625
572 fma.s1 fA33 = fA33, fXSqr, fA31
577 fma.s1 fA15 = fA15, fXSqr, fA13
583 fma.s1 fA29 = fA29, fXSqr, fA27
588 fma.s1 fA25 = fA25, fXSqr, fA23
594 fma.s1 fA21 = fA21, fXSqr, fA19
599 fma.s1 fA9 = fA9, fXSqr, fA7
605 fma.s1 fA5 = fA5, fXSqr, fA3
611 fma.s1 fA35 = fA35, fXQuadr, fA33
616 fma.s1 fA17 = fA17, fXQuadr, fA15
622 fma.s1 fX8 = fXQuadr, fXQuadr, f0 // x^8
627 fma.s1 fA25 = fA25, fXQuadr, fA21
633 fma.s1 fA9 = fA9, fXQuadr, fA5
639 fma.s1 fA35 = fA35, fXQuadr, fA29
644 fma.s1 fA17 = fA17, fXSqr, fA11
650 fma.s1 fX16 = fX8, fX8, f0 // x^16
656 fma.s1 fA35 = fA35, fX8, fA25
661 fma.s1 fA17 = fA17, fX8, fA9
667 fma.s1 fBaseP = fA35, fX16, fA17
673 // final result for |x| < 0.625
674 fma.d.s0 f8 = fBaseP, fXCube, f8
675 // exit here for |x| < 0.625 path
681 // asin(x) = sign(x) * Pi/2
685 ldfe fPiBy2 = [rPiBy2Ptr] // Pi/2
692 // result for |x| = 1.0
693 fma.d.s0 f8 = fPiBy2, fSignX, f0
694 // exit here for |x| = 1.0
699 // here if x is a NaN, denormal, or zero
704 // set p12 = 1 if x is a NaN
705 fclass.m p12, p0 = f8, 0xc3
710 // smallest positive DP normalized number
711 movl rDenoBound = 0x0010000000000000
716 // set p13 = 1 if x = 0.0
717 fclass.m p13, p0 = f8, 0x07
727 // load smallest normal to FP reg
728 setf.d fDenoBound = rDenoBound
729 // answer if x is a NaN
730 (p12) fma.d.s0 f8 = f8,f1,f0
731 // exit here if x is a NaN
738 // exit here if x = 0.0
742 // if we still here then x is denormal or unnormal
745 // absolute value of normalized x
746 fmerge.s fNormX = f1, fNormX
752 // set p14 = 1 if normalized x is greater than or
753 // equal to the smallest denormalized value
754 // So, if p14 is set to 1 it means that we deal with
755 // unnormal rather than with "true" denormal
756 fcmp.ge.s1 p14, p0 = fNormX, fDenoBound
762 (p14) fcmp.eq.s0 p6, p0 = f8, f0 // Set D flag if x unnormal
767 // normalize unnormal input
768 (p14) fnorm.s1 f8 = f8
769 // return to the main path
770 (p14) br.cond.sptk asin_unnormal_back
773 // if we still here it means that input is "true" denormal
776 // final result if x is denormal
777 fma.d.s0 f8 = f8, fXSqr, f8
778 // exit here if x is denormal
784 // error handler should be called
788 alloc r32 = ar.pfs, 0, 3, 4, 0 // get some registers
789 fmerge.s FR_X = f8,f8
793 mov GR_Parameter_TAG = 61 // error code
794 frcpa.s0 FR_RESULT, p0 = f0,f0
795 // call error handler routine
796 br.cond.sptk __libm_error_region
799 GLOBAL_LIBM_END(asin)
800 libm_alias_double_other (asin, asin)
804 LOCAL_LIBM_ENTRY(__libm_error_region)
807 add GR_Parameter_Y=-32,sp // Parameter 2 value
809 .save ar.pfs,GR_SAVE_PFS
810 mov GR_SAVE_PFS=ar.pfs // Save ar.pfs
814 add sp=-64,sp // Create new stack
816 mov GR_SAVE_GP=gp // Save gp
819 stfd [GR_Parameter_Y] = FR_Y,16 // STORE Parameter 2 on stack
820 add GR_Parameter_X = 16,sp // Parameter 1 address
822 mov GR_SAVE_B0=b0 // Save b0
826 stfd [GR_Parameter_X] = FR_X // STORE Parameter 1 on stack
827 add GR_Parameter_RESULT = 0,GR_Parameter_Y // Parameter 3 address
831 stfd [GR_Parameter_Y] = FR_RESULT // STORE Parameter 3 on stack
832 add GR_Parameter_Y = -16,GR_Parameter_Y
833 br.call.sptk b0=__libm_error_support# // Call error handling function
836 add GR_Parameter_RESULT = 48,sp
841 ldfd f8 = [GR_Parameter_RESULT] // Get return result off stack
843 add sp = 64,sp // Restore stack pointer
844 mov b0 = GR_SAVE_B0 // Restore return address
847 mov gp = GR_SAVE_GP // Restore gp
848 mov ar.pfs = GR_SAVE_PFS // Restore ar.pfs
849 br.ret.sptk b0 // Return
852 LOCAL_LIBM_END(__libm_error_region)
853 .type __libm_error_support#,@function
854 .global __libm_error_support#