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/04/00 Unwind support added
44 // 12/27/00 Improved speed
45 // 02/21/01 Updated to call tanl
47 // 02/10/03 Reordered header: .section, .global, .proc, .align
50 //==============================================================
51 // double tan(double x);
52 // double cot(double x);
54 // Overview of operation
55 //==============================================================
56 // If the input value in radians is |x| >= 1.xxxxx 2^10 call the
57 // older slower version.
59 // The new algorithm is used when |x| <= 1.xxxxx 2^9.
61 // Represent the input X as Nfloat * pi/2 + r
62 // where r can be negative and |r| <= pi/4
65 // Nfloat = round_int(tan_W)
67 // tan_r = x - Nfloat * (pi/2)_hi
68 // a) tan_r = tan_r - Nfloat * (pi/2)_lo (for tan)
69 // b) tan_r = Nfloat * (pi/2)_lo - tan_r (for cot)
71 // We have two paths: p8, when Nfloat is even and p9. when Nfloat is odd.
72 // a) for tan: p8: tan(X) = tan(r)
73 // p9: tan(X) = -cot(r)
74 // b) for cot: p9: cot(X) = cot(r)
75 // p8: cot(X) = -tan(r)
77 // Each is evaluated as a series. The p9 path requires 1/r.
79 // The coefficients used in the series are stored in a table as
80 // are the pi constants.
83 //==============================================================
85 // predicate registers used:
88 // floating-point registers used:
92 // general registers used
96 //==============================================================
97 TAN_INV_PI_BY_2_2TO64 = f10
101 TAN_W_2TO64_RSH = f14
104 tan_Inv_Pi_by_2 = f32
189 /////////////////////////////////////////////////////////////
191 tan_GR_sig_inv_pi_by_2 = r14
192 tan_GR_rshf_2to64 = r15
193 tan_GR_exp_2tom64 = r16
199 tan_GR_N_odd_even = r22
210 GR_Parameter_RESULT = r38
211 GR_Parameter_Tag = r39
218 LOCAL_OBJECT_START(double_tan_constants)
219 data8 0xC90FDAA22168C234, 0x00003FFF // pi/2 hi
220 data8 0xBEEA54580DDEA0E1 // P14
221 data8 0x3ED3021ACE749A59 // P15
222 data8 0xBEF312BD91DC8DA1 // P12
223 data8 0x3EFAE9AFC14C5119 // P13
224 data8 0x3F2F342BF411E769 // P8
225 data8 0x3F1A60FC9F3B0227 // P9
226 data8 0x3EFF246E78E5E45B // P10
227 data8 0x3F01D9D2E782875C // P11
228 data8 0x3F8226E34C4499B6 // P4
229 data8 0x3F6D6D3F12C236AC // P5
230 data8 0x3F57DA1146DCFD8B // P6
231 data8 0x3F43576410FE3D75 // P7
232 data8 0x3FD5555555555555 // P0
233 data8 0x3FC11111111111C2 // P1
234 data8 0x3FABA1BA1BA0E850 // P2
235 data8 0x3F9664F4886725A7 // P3
236 LOCAL_OBJECT_END(double_tan_constants)
238 LOCAL_OBJECT_START(double_Q_tan_constants)
239 data8 0xC4C6628B80DC1CD1, 0x00003FBF // pi/2 lo
240 data8 0x3E223A73BA576E48 // Q8
241 data8 0x3DF54AD8D1F2CA43 // Q9
242 data8 0x3EF66A8EE529A6AA // Q4
243 data8 0x3EC2281050410EE6 // Q5
244 data8 0x3E8D6BB992CC3CF5 // Q6
245 data8 0x3E57F88DE34832E4 // Q7
246 data8 0x3FD5555555555555 // Q0
247 data8 0x3F96C16C16C16DB8 // Q1
248 data8 0x3F61566ABBFFB489 // Q2
249 data8 0x3F2BBD77945C1733 // Q3
250 data8 0x3D927FB33E2B0E04 // Q10
251 LOCAL_OBJECT_END(double_Q_tan_constants)
256 ////////////////////////////////////////////////////////
258 LOCAL_LIBM_ENTRY(cot)
259 // The initial fnorm will take any unmasked faults and
260 // normalize any single/double unorms
263 cmp.eq p12, p11 = r0, r0 // set p12=1, p11=0 for cot
264 movl tan_GR_sig_inv_pi_by_2 = 0xA2F9836E4E44152A // significand of 2/pi
267 addl tan_AD = @ltoff(double_tan_constants), gp
268 movl tan_GR_rshf_2to64 = 0x47e8000000000000 // 1.1000 2^(63+63+1)
273 mov tan_GR_exp_2tom64 = 0xffff-64 // exponent of scaling factor 2^-64
274 movl tan_GR_rshf = 0x43e8000000000000 // 1.1000 2^63 for right shift
277 ld8 tan_AD = [tan_AD]
278 fnorm.s0 tan_NORM_f8 = f8
279 br.cond.sptk COMMON_PATH
285 GLOBAL_IEEE754_ENTRY(tan)
286 // The initial fnorm will take any unmasked faults and
287 // normalize any single/double unorms
290 cmp.eq p11, p12 = r0, r0 // set p11=1, p12=0 for tan
291 movl tan_GR_sig_inv_pi_by_2 = 0xA2F9836E4E44152A // significand of 2/pi
294 addl tan_AD = @ltoff(double_tan_constants), gp
295 movl tan_GR_rshf_2to64 = 0x47e8000000000000 // 1.1000 2^(63+63+1)
300 mov tan_GR_exp_2tom64 = 0xffff-64 // exponent of scaling factor 2^-64
301 movl tan_GR_rshf = 0x43e8000000000000 // 1.1000 2^63 for right shift
304 ld8 tan_AD = [tan_AD]
305 fnorm.s0 tan_NORM_f8 = f8
311 // Common path for both tan and cot
313 // Form two constants we need
314 // 2/pi * 2^1 * 2^63, scaled by 2^64 since we just loaded the significand
315 // 1.1000...000 * 2^(63+63+1) to right shift int(W) into the significand
317 setf.sig TAN_INV_PI_BY_2_2TO64 = tan_GR_sig_inv_pi_by_2
318 setf.d TAN_RSHF_2TO64 = tan_GR_rshf_2to64
319 mov tan_GR_17_ones = 0x1ffff ;;
323 // Form another constant
324 // 2^-64 for scaling Nfloat
325 // 1.1000...000 * 2^63, the right shift constant
327 setf.exp TAN_2TOM64 = tan_GR_exp_2tom64
328 adds tan_ADQ = double_Q_tan_constants - double_tan_constants, tan_AD
329 (p11) fclass.m.unc p6,p0 = f8, 0x07 // Test for x=0 (tan)
334 // Form another constant
335 // 2^-64 for scaling Nfloat
336 // 1.1000...000 * 2^63, the right shift constant
338 setf.d TAN_RSHF = tan_GR_rshf
339 ldfe tan_Pi_by_2_hi = [tan_AD],16
340 fclass.m.unc p7,p0 = f8, 0x23 // Test for x=inf
345 ldfe tan_Pi_by_2_lo = [tan_ADQ],16
346 fclass.m.unc p8,p0 = f8, 0xc3 // Test for x=nan
347 (p6) br.ret.spnt b0 ;; // Exit for x=0 (tan only)
351 ldfpd tan_P14,tan_P15 = [tan_AD],16
352 (p7) frcpa.s0 f8,p9=f0,f0 // Set qnan indef if x=inf
353 mov tan_GR_10009 = 0x10009
356 ldfpd tan_Q8,tan_Q9 = [tan_ADQ],16
358 (p7) br.ret.spnt b0 ;; // Exit for x=inf
362 ldfpd tan_P12,tan_P13 = [tan_AD],16
363 (p12) fclass.m.unc p6,p0 = f8, 0x07 // Test for x=0 (cot)
367 ldfpd tan_Q4,tan_Q5 = [tan_ADQ],16
368 (p8) fma.d.s0 f8=f8,f1,f8 // Set qnan if x=nan
369 (p8) br.ret.spnt b0 ;; // Exit for x=nan
373 getf.exp tan_signexp = tan_NORM_f8
374 ldfpd tan_P8,tan_P9 = [tan_AD],16
375 fmerge.s arg_copy = f8, f8 ;; // Save input for error call
378 // Multiply x by scaled 2/pi and add large const to shift integer part of W to
379 // rightmost bits of significand
381 alloc r32=ar.pfs,0,4,4,0
382 ldfpd tan_Q6,tan_Q7 = [tan_ADQ],16
383 fma.s1 TAN_W_2TO64_RSH = tan_NORM_f8,TAN_INV_PI_BY_2_2TO64,TAN_RSHF_2TO64
387 ldfpd tan_P10,tan_P11 = [tan_AD],16
388 and tan_exp = tan_GR_17_ones, tan_signexp
389 (p6) frcpa.s0 f8, p0 = f1, f8 ;; // cot(+-0) = +-Inf
393 // p7 is true if we must call DBX TAN
394 // p7 is true if f8 exp is > 0x10009 (which includes all ones
397 ldfpd tan_Q0,tan_Q1 = [tan_ADQ],16
398 cmp.ge.unc p7,p0 = tan_exp,tan_GR_10009
399 (p7) br.cond.spnt TAN_DBX ;;
404 ldfpd tan_P4,tan_P5 = [tan_AD],16
405 (p6) mov GR_Parameter_Tag = 226 // (cot)
406 (p6) br.cond.spnt __libm_error_region ;; // call error support if cot(+-0)
411 ldfpd tan_Q2,tan_Q3 = [tan_ADQ],16
418 // TAN_NFLOAT = Round_Int_Nearest(tan_W)
420 ldfpd tan_P6,tan_P7 = [tan_AD],16
421 fms.s1 TAN_NFLOAT = TAN_W_2TO64_RSH,TAN_2TOM64,TAN_RSHF
427 ldfd tan_Q10 = [tan_ADQ]
434 ldfpd tan_P0,tan_P1 = [tan_AD],16
441 getf.sig tan_GR_n = TAN_W_2TO64_RSH
442 ldfpd tan_P2,tan_P3 = [tan_AD]
446 // tan_r = -tan_Nfloat * tan_Pi_by_2_hi + x
448 (p12) add tan_GR_n = 0x1, tan_GR_n // N = N + 1 (for cot)
449 fnma.s1 tan_r = TAN_NFLOAT, tan_Pi_by_2_hi, tan_NORM_f8
457 and tan_GR_N_odd_even = 0x1, tan_GR_n ;;
459 cmp.eq.unc p8,p9 = tan_GR_N_odd_even, r0 ;;
463 .pred.rel "mutex", p11, p12
464 // tan_r = tan_r -tan_Nfloat * tan_Pi_by_2_lo (tan)
467 (p11) fnma.s1 tan_r = TAN_NFLOAT, tan_Pi_by_2_lo, tan_r
470 // tan_r = -(tan_r -tan_Nfloat * tan_Pi_by_2_lo) (cot)
473 (p12) fms.s1 tan_r = TAN_NFLOAT, tan_Pi_by_2_lo, tan_r
480 fma.s1 tan_rsq = tan_r, tan_r, f0
487 (p9) frcpa.s1 tan_y0, p0 = f1,tan_r
494 (p8) fma.s1 tan_v18 = tan_rsq, tan_P15, tan_P14
499 (p8) fma.s1 tan_v4 = tan_rsq, tan_P1, tan_P0
507 (p8) fma.s1 tan_v16 = tan_rsq, tan_P13, tan_P12
512 (p8) fma.s1 tan_v17 = tan_rsq, tan_rsq, f0
520 (p8) fma.s1 tan_v12 = tan_rsq, tan_P9, tan_P8
525 (p8) fma.s1 tan_v13 = tan_rsq, tan_P11, tan_P10
533 (p8) fma.s1 tan_v7 = tan_rsq, tan_P5, tan_P4
538 (p8) fma.s1 tan_v8 = tan_rsq, tan_P7, tan_P6
546 (p9) fnma.s1 tan_d = tan_r, tan_y0, f1
551 (p8) fma.s1 tan_v5 = tan_rsq, tan_P3, tan_P2
559 (p9) fma.s1 tan_z11 = tan_rsq, tan_Q9, tan_Q8
564 (p9) fma.s1 tan_z12 = tan_rsq, tan_rsq, f0
571 (p8) fma.s1 tan_v15 = tan_v17, tan_v18, tan_v16
576 (p9) fma.s1 tan_z7 = tan_rsq, tan_Q5, tan_Q4
583 (p8) fma.s1 tan_v11 = tan_v17, tan_v13, tan_v12
588 (p9) fma.s1 tan_z8 = tan_rsq, tan_Q7, tan_Q6
596 (p8) fma.s1 tan_v14 = tan_v17, tan_v17, f0
601 (p9) fma.s1 tan_z3 = tan_rsq, tan_Q1, tan_Q0
610 (p8) fma.s1 tan_v3 = tan_v17, tan_v5, tan_v4
615 (p8) fma.s1 tan_v6 = tan_v17, tan_v8, tan_v7
623 (p9) fma.s1 tan_y1 = tan_y0, tan_d, tan_y0
628 (p9) fma.s1 tan_dsq = tan_d, tan_d, f0
635 (p9) fma.s1 tan_z10 = tan_z12, tan_Q10, tan_z11
640 (p9) fma.s1 tan_z9 = tan_z12, tan_z12,f0
647 (p9) fma.s1 tan_z4 = tan_rsq, tan_Q3, tan_Q2
652 (p9) fma.s1 tan_z6 = tan_z12, tan_z8, tan_z7
660 (p8) fma.s1 tan_v10 = tan_v14, tan_v15, tan_v11
668 (p9) fma.s1 tan_y2 = tan_y1, tan_d, tan_y0
673 (p9) fma.s1 tan_d4 = tan_dsq, tan_dsq, tan_d
680 (p8) fma.s1 tan_v2 = tan_v14, tan_v6, tan_v3
685 (p8) fma.s1 tan_v9 = tan_v14, tan_v14, f0
692 (p9) fma.s1 tan_z2 = tan_z12, tan_z4, tan_z3
697 (p9) fma.s1 tan_z5 = tan_z9, tan_z10, tan_z6
704 (p9) fma.s1 tan_inv_r = tan_d4, tan_y2, tan_y0
709 (p8) fma.s1 tan_rcube = tan_rsq, tan_r, f0
717 (p8) fma.s1 tan_v1 = tan_v9, tan_v10, tan_v2
722 (p9) fma.s1 tan_z1 = tan_z9, tan_z5, tan_z2
730 (p8) fma.d.s0 f8 = tan_v1, tan_rcube, tan_r
735 (p9) fms.d.s0 f8 = tan_r, tan_z1, tan_inv_r
738 GLOBAL_IEEE754_END(tan)
740 LOCAL_LIBM_ENTRY(__libm_callout)
747 .save ar.pfs,GR_SAVE_PFS
748 mov GR_SAVE_PFS=ar.pfs
763 (p11) br.cond.sptk.many call_tanl ;;
766 // Here if we should call cotl
770 br.call.sptk.many b0=__libm_cotl# ;;
782 mov ar.pfs = GR_SAVE_PFS
787 // Here if we should call tanl
792 br.call.sptk.many b0=__libm_tanl# ;;
804 mov ar.pfs = GR_SAVE_PFS
809 LOCAL_LIBM_END(__libm_callout)
811 .type __libm_tanl#,@function
813 .type __libm_cotl#,@function
816 LOCAL_LIBM_ENTRY(__libm_error_region)
821 add GR_Parameter_Y=-32,sp // Parameter 2 value
823 .save ar.pfs,GR_SAVE_PFS
824 mov GR_SAVE_PFS=ar.pfs // Save ar.pfs
828 add sp=-64,sp // Create new stack
830 mov GR_SAVE_GP=gp // Save gp
835 stfd [GR_Parameter_Y] = f1,16 // STORE Parameter 2 on stack
836 add GR_Parameter_X = 16,sp // Parameter 1 address
838 mov GR_SAVE_B0=b0 // Save b0
844 stfd [GR_Parameter_X] = arg_copy // STORE Parameter 1 on stack
845 add GR_Parameter_RESULT = 0,GR_Parameter_Y // Parameter 3 address
849 stfd [GR_Parameter_Y] = f8 // STORE Parameter 3 on stack
850 add GR_Parameter_Y = -16,GR_Parameter_Y
851 br.call.sptk b0=__libm_error_support# // Call error handling function
856 add GR_Parameter_RESULT = 48,sp
861 ldfd f8 = [GR_Parameter_RESULT] // Get return result off stack
863 add sp = 64,sp // Restore stack pointer
864 mov b0 = GR_SAVE_B0 // Restore return address
867 mov gp = GR_SAVE_GP // Restore gp
868 mov ar.pfs = GR_SAVE_PFS // Restore ar.pfs
869 br.ret.sptk b0 // Return
872 LOCAL_LIBM_END(__libm_error_region)
874 .type __libm_error_support#,@function
875 .global __libm_error_support#