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 // 04/04/00 Unwind support added
43 // 12/27/00 Improved speed
44 // 02/21/01 Updated to call tanl
46 // 02/10/03 Reordered header: .section, .global, .proc, .align
49 //==============================================================
50 // double tan(double x);
51 // double cot(double x);
53 // Overview of operation
54 //==============================================================
55 // If the input value in radians is |x| >= 1.xxxxx 2^10 call the
56 // older slower version.
58 // The new algorithm is used when |x| <= 1.xxxxx 2^9.
60 // Represent the input X as Nfloat * pi/2 + r
61 // where r can be negative and |r| <= pi/4
64 // Nfloat = round_int(tan_W)
66 // tan_r = x - Nfloat * (pi/2)_hi
67 // a) tan_r = tan_r - Nfloat * (pi/2)_lo (for tan)
68 // b) tan_r = Nfloat * (pi/2)_lo - tan_r (for cot)
70 // We have two paths: p8, when Nfloat is even and p9. when Nfloat is odd.
71 // a) for tan: p8: tan(X) = tan(r)
72 // p9: tan(X) = -cot(r)
73 // b) for cot: p9: cot(X) = cot(r)
74 // p8: cot(X) = -tan(r)
76 // Each is evaluated as a series. The p9 path requires 1/r.
78 // The coefficients used in the series are stored in a table as
79 // are the pi constants.
82 //==============================================================
84 // predicate registers used:
87 // floating-point registers used:
91 // general registers used
95 //==============================================================
96 TAN_INV_PI_BY_2_2TO64 = f10
100 TAN_W_2TO64_RSH = f14
103 tan_Inv_Pi_by_2 = f32
188 /////////////////////////////////////////////////////////////
190 tan_GR_sig_inv_pi_by_2 = r14
191 tan_GR_rshf_2to64 = r15
192 tan_GR_exp_2tom64 = r16
198 tan_GR_N_odd_even = r22
209 GR_Parameter_RESULT = r38
210 GR_Parameter_Tag = r39
217 LOCAL_OBJECT_START(double_tan_constants)
218 data8 0xC90FDAA22168C234, 0x00003FFF // pi/2 hi
219 data8 0xBEEA54580DDEA0E1 // P14
220 data8 0x3ED3021ACE749A59 // P15
221 data8 0xBEF312BD91DC8DA1 // P12
222 data8 0x3EFAE9AFC14C5119 // P13
223 data8 0x3F2F342BF411E769 // P8
224 data8 0x3F1A60FC9F3B0227 // P9
225 data8 0x3EFF246E78E5E45B // P10
226 data8 0x3F01D9D2E782875C // P11
227 data8 0x3F8226E34C4499B6 // P4
228 data8 0x3F6D6D3F12C236AC // P5
229 data8 0x3F57DA1146DCFD8B // P6
230 data8 0x3F43576410FE3D75 // P7
231 data8 0x3FD5555555555555 // P0
232 data8 0x3FC11111111111C2 // P1
233 data8 0x3FABA1BA1BA0E850 // P2
234 data8 0x3F9664F4886725A7 // P3
235 LOCAL_OBJECT_END(double_tan_constants)
237 LOCAL_OBJECT_START(double_Q_tan_constants)
238 data8 0xC4C6628B80DC1CD1, 0x00003FBF // pi/2 lo
239 data8 0x3E223A73BA576E48 // Q8
240 data8 0x3DF54AD8D1F2CA43 // Q9
241 data8 0x3EF66A8EE529A6AA // Q4
242 data8 0x3EC2281050410EE6 // Q5
243 data8 0x3E8D6BB992CC3CF5 // Q6
244 data8 0x3E57F88DE34832E4 // Q7
245 data8 0x3FD5555555555555 // Q0
246 data8 0x3F96C16C16C16DB8 // Q1
247 data8 0x3F61566ABBFFB489 // Q2
248 data8 0x3F2BBD77945C1733 // Q3
249 data8 0x3D927FB33E2B0E04 // Q10
250 LOCAL_OBJECT_END(double_Q_tan_constants)
255 ////////////////////////////////////////////////////////
257 LOCAL_LIBM_ENTRY(cot)
258 // The initial fnorm will take any unmasked faults and
259 // normalize any single/double unorms
262 cmp.eq p12, p11 = r0, r0 // set p12=1, p11=0 for cot
263 movl tan_GR_sig_inv_pi_by_2 = 0xA2F9836E4E44152A // significand of 2/pi
266 addl tan_AD = @ltoff(double_tan_constants), gp
267 movl tan_GR_rshf_2to64 = 0x47e8000000000000 // 1.1000 2^(63+63+1)
272 mov tan_GR_exp_2tom64 = 0xffff-64 // exponent of scaling factor 2^-64
273 movl tan_GR_rshf = 0x43e8000000000000 // 1.1000 2^63 for right shift
276 ld8 tan_AD = [tan_AD]
277 fnorm.s0 tan_NORM_f8 = f8
278 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)
739 libm_alias_double_other (__tan, tan)
742 LOCAL_LIBM_ENTRY(__libm_callout)
749 .save ar.pfs,GR_SAVE_PFS
750 mov GR_SAVE_PFS=ar.pfs
765 (p11) br.cond.sptk.many call_tanl ;;
768 // Here if we should call cotl
772 br.call.sptk.many b0=__libm_cotl# ;;
784 mov ar.pfs = GR_SAVE_PFS
789 // Here if we should call tanl
794 br.call.sptk.many b0=__libm_tanl# ;;
806 mov ar.pfs = GR_SAVE_PFS
811 LOCAL_LIBM_END(__libm_callout)
813 .type __libm_tanl#,@function
815 .type __libm_cotl#,@function
818 LOCAL_LIBM_ENTRY(__libm_error_region)
823 add GR_Parameter_Y=-32,sp // Parameter 2 value
825 .save ar.pfs,GR_SAVE_PFS
826 mov GR_SAVE_PFS=ar.pfs // Save ar.pfs
830 add sp=-64,sp // Create new stack
832 mov GR_SAVE_GP=gp // Save gp
837 stfd [GR_Parameter_Y] = f1,16 // STORE Parameter 2 on stack
838 add GR_Parameter_X = 16,sp // Parameter 1 address
840 mov GR_SAVE_B0=b0 // Save b0
846 stfd [GR_Parameter_X] = arg_copy // STORE Parameter 1 on stack
847 add GR_Parameter_RESULT = 0,GR_Parameter_Y // Parameter 3 address
851 stfd [GR_Parameter_Y] = f8 // STORE Parameter 3 on stack
852 add GR_Parameter_Y = -16,GR_Parameter_Y
853 br.call.sptk b0=__libm_error_support# // Call error handling function
858 add GR_Parameter_RESULT = 48,sp
863 ldfd f8 = [GR_Parameter_RESULT] // Get return result off stack
865 add sp = 64,sp // Restore stack pointer
866 mov b0 = GR_SAVE_B0 // Restore return address
869 mov gp = GR_SAVE_GP // Restore gp
870 mov ar.pfs = GR_SAVE_PFS // Restore ar.pfs
871 br.ret.sptk b0 // Return
874 LOCAL_LIBM_END(__libm_error_region)
876 .type __libm_error_support#,@function
877 .global __libm_error_support#