4 // Copyright (c) 2002 - 2005, Intel Corporation
5 // All rights reserved.
7 // Contributed 2002 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/01/02 Initial version
43 // 02/18/02 Large arguments processing routine is excluded.
44 // External interface entry points are added
45 // 02/26/02 Added temporary return of results in r8, r9
46 // 03/13/02 Corrected restore of predicate registers
47 // 03/19/02 Added stack unwind around call to __libm_cisf_large
48 // 09/05/02 Work range is widened by reduction strengthen (2 parts of Pi/16)
49 // 02/10/03 Reordered header: .section, .global, .proc, .align
50 // 02/11/04 cisf is moved to the separate file.
51 // 03/31/05 Reformatted delimiters between data tables
54 //==============================================================
55 // 1) void sincosf(float, float*s, float*c)
56 // 2) __libm_sincosf - internal LIBM function, that accepts
57 // argument in f8 and returns cosine through f8, sine through f9
60 // Overview of operation
61 //==============================================================
65 // Reduce x to region -1/2*pi/2^k ===== 0 ===== +1/2*pi/2^k where k=4
66 // divide x by pi/2^k.
67 // Multiply by 2^k/pi.
68 // nfloat = Round result to integer (round-to-nearest)
70 // r = x - nfloat * pi/2^k
71 // Do this as (x - nfloat * HIGH(pi/2^k)) - nfloat * LOW(pi/2^k) for increased accuracy.
72 // pi/2^k is stored as two numbers that when added make pi/2^k.
73 // pi/2^k = HIGH(pi/2^k) + LOW(pi/2^k)
74 // HIGH part is rounded to zero, LOW - to nearest
76 // x = (nfloat * pi/2^k) + r
77 // r is small enough that we can use a polynomial approximation
78 // and is referred to as the reduced argument.
82 // Take the unreduced part and remove the multiples of 2pi.
83 // So nfloat = nfloat (with lower k+1 bits cleared) + lower k+1 bits
85 // nfloat (with lower k+1 bits cleared) is a multiple of 2^(k+1)
87 // nfloat * pi/2^k = N * 2^(k+1) * pi/2^k + (lower k+1 bits) * pi/2^k
88 // nfloat * pi/2^k = N * 2 * pi + (lower k+1 bits) * pi/2^k
89 // nfloat * pi/2^k = N2pi + M * pi/2^k
92 // Sin(x) = Sin((nfloat * pi/2^k) + r)
93 // = Sin(nfloat * pi/2^k) * Cos(r) + Cos(nfloat * pi/2^k) * Sin(r)
95 // Sin(nfloat * pi/2^k) = Sin(N2pi + Mpi/2^k)
96 // = Sin(N2pi)Cos(Mpi/2^k) + Cos(N2pi)Sin(Mpi/2^k)
99 // Cos(nfloat * pi/2^k) = Cos(N2pi + Mpi/2^k)
100 // = Cos(N2pi)Cos(Mpi/2^k) + Sin(N2pi)Sin(Mpi/2^k)
103 // Sin(x) = Sin(Mpi/2^k) Cos(r) + Cos(Mpi/2^k) Sin(r)
109 // There are 2^(k+1) Sin entries in a table.
110 // There are 2^(k+1) Cos entries in a table.
112 // Get Sin(Mpi/2^k) and Cos(Mpi/2^k) by table lookup.
117 // Calculate Cos(r) and Sin(r) by polynomial approximation.
119 // Cos(r) = 1 + r^2 q1 + r^4 q2 = Series for Cos
120 // Sin(r) = r + r^3 p1 + r^5 p2 = Series for Sin
122 // and the coefficients q1, q2 and p1, p2 are stored in a table
126 // Sin(x) = Sin(Mpi/2^k) Cos(r) + Cos(Mpi/2^k) Sin(r)
130 // S[m] = Sin(Mpi/2^k) and C[m] = Cos(Mpi/2^k)
138 // Sin(r) = r + rcub * P
139 // = r + r^3p1 + r^5p2 = Sin(r)
143 // Answer = S[m] Cos(r) + C[m] P
145 // Cos(r) = 1 + rsq Q
146 // Cos(r) = 1 + r^2 Q
147 // Cos(r) = 1 + r^2 (q1 + r^2q2)
148 // Cos(r) = 1 + r^2q1 + r^4q2
150 // S[m] Cos(r) = S[m](1 + rsq Q)
151 // S[m] Cos(r) = S[m] + S[m] rsq Q
152 // S[m] Cos(r) = S[m] + s_rsq Q
153 // Q = S[m] + s_rsq Q
157 // Answer = Q + C[m] P
161 //==============================================================
162 // general input registers:
166 // predicate registers used:
169 // floating-point registers used
174 //==============================================================
184 cisf_int_Nfloat = f12
192 cisf_Inv_Pi_by_16 = f33
193 cisf_Pi_by_16_hi = f34
194 cisf_Pi_by_16_lo = f35
196 cisf_Inv_Pi_by_64 = f36
197 cisf_Pi_by_64_hi = f37
198 cisf_Pi_by_64_lo = f38
218 cisf_SIG_INV_PI_BY_16_2TO61 = f52
219 cisf_RSHF_2TO61 = f53
223 cisf_W_2TO61_RSH = f57
240 /////////////////////////////////////////////////////////////
247 cisf_AD_beta_table = r37
253 cisf_GR_sig_inv_pi_by_16 = r14
254 cisf_GR_rshf_2to61 = r15
256 cisf_GR_exp_2tom61 = r17
261 cisf_GR_32m_sin = r41
265 cisf_GR_32m_cos = r43
282 LOCAL_OBJECT_START(double_cisf_pi)
283 data8 0xC90FDAA22168C234, 0x00003FFC // pi/16 1st part
284 data8 0xC4C6628B80DC1CD1, 0x00003FBC // pi/16 2nd part
285 LOCAL_OBJECT_END(double_cisf_pi)
287 // Coefficients for polynomials
288 LOCAL_OBJECT_START(double_cisf_pq_k4)
289 data8 0x3F810FABB668E9A2 // P2
290 data8 0x3FA552E3D6DE75C9 // Q2
291 data8 0xBFC555554447BC7F // P1
292 data8 0xBFDFFFFFC447610A // Q1
293 LOCAL_OBJECT_END(double_cisf_pq_k4)
295 // Sincos table (S[m], C[m])
296 LOCAL_OBJECT_START(double_sin_cos_beta_k4)
297 data8 0x0000000000000000 // sin ( 0 Pi / 16 )
298 data8 0x3FF0000000000000 // cos ( 0 Pi / 16 )
300 data8 0x3FC8F8B83C69A60B // sin ( 1 Pi / 16 )
301 data8 0x3FEF6297CFF75CB0 // cos ( 1 Pi / 16 )
303 data8 0x3FD87DE2A6AEA963 // sin ( 2 Pi / 16 )
304 data8 0x3FED906BCF328D46 // cos ( 2 Pi / 16 )
306 data8 0x3FE1C73B39AE68C8 // sin ( 3 Pi / 16 )
307 data8 0x3FEA9B66290EA1A3 // cos ( 3 Pi / 16 )
309 data8 0x3FE6A09E667F3BCD // sin ( 4 Pi / 16 )
310 data8 0x3FE6A09E667F3BCD // cos ( 4 Pi / 16 )
312 data8 0x3FEA9B66290EA1A3 // sin ( 5 Pi / 16 )
313 data8 0x3FE1C73B39AE68C8 // cos ( 5 Pi / 16 )
315 data8 0x3FED906BCF328D46 // sin ( 6 Pi / 16 )
316 data8 0x3FD87DE2A6AEA963 // cos ( 6 Pi / 16 )
318 data8 0x3FEF6297CFF75CB0 // sin ( 7 Pi / 16 )
319 data8 0x3FC8F8B83C69A60B // cos ( 7 Pi / 16 )
321 data8 0x3FF0000000000000 // sin ( 8 Pi / 16 )
322 data8 0x0000000000000000 // cos ( 8 Pi / 16 )
324 data8 0x3FEF6297CFF75CB0 // sin ( 9 Pi / 16 )
325 data8 0xBFC8F8B83C69A60B // cos ( 9 Pi / 16 )
327 data8 0x3FED906BCF328D46 // sin ( 10 Pi / 16 )
328 data8 0xBFD87DE2A6AEA963 // cos ( 10 Pi / 16 )
330 data8 0x3FEA9B66290EA1A3 // sin ( 11 Pi / 16 )
331 data8 0xBFE1C73B39AE68C8 // cos ( 11 Pi / 16 )
333 data8 0x3FE6A09E667F3BCD // sin ( 12 Pi / 16 )
334 data8 0xBFE6A09E667F3BCD // cos ( 12 Pi / 16 )
336 data8 0x3FE1C73B39AE68C8 // sin ( 13 Pi / 16 )
337 data8 0xBFEA9B66290EA1A3 // cos ( 13 Pi / 16 )
339 data8 0x3FD87DE2A6AEA963 // sin ( 14 Pi / 16 )
340 data8 0xBFED906BCF328D46 // cos ( 14 Pi / 16 )
342 data8 0x3FC8F8B83C69A60B // sin ( 15 Pi / 16 )
343 data8 0xBFEF6297CFF75CB0 // cos ( 15 Pi / 16 )
345 data8 0x0000000000000000 // sin ( 16 Pi / 16 )
346 data8 0xBFF0000000000000 // cos ( 16 Pi / 16 )
348 data8 0xBFC8F8B83C69A60B // sin ( 17 Pi / 16 )
349 data8 0xBFEF6297CFF75CB0 // cos ( 17 Pi / 16 )
351 data8 0xBFD87DE2A6AEA963 // sin ( 18 Pi / 16 )
352 data8 0xBFED906BCF328D46 // cos ( 18 Pi / 16 )
354 data8 0xBFE1C73B39AE68C8 // sin ( 19 Pi / 16 )
355 data8 0xBFEA9B66290EA1A3 // cos ( 19 Pi / 16 )
357 data8 0xBFE6A09E667F3BCD // sin ( 20 Pi / 16 )
358 data8 0xBFE6A09E667F3BCD // cos ( 20 Pi / 16 )
360 data8 0xBFEA9B66290EA1A3 // sin ( 21 Pi / 16 )
361 data8 0xBFE1C73B39AE68C8 // cos ( 21 Pi / 16 )
363 data8 0xBFED906BCF328D46 // sin ( 22 Pi / 16 )
364 data8 0xBFD87DE2A6AEA963 // cos ( 22 Pi / 16 )
366 data8 0xBFEF6297CFF75CB0 // sin ( 23 Pi / 16 )
367 data8 0xBFC8F8B83C69A60B // cos ( 23 Pi / 16 )
369 data8 0xBFF0000000000000 // sin ( 24 Pi / 16 )
370 data8 0x0000000000000000 // cos ( 24 Pi / 16 )
372 data8 0xBFEF6297CFF75CB0 // sin ( 25 Pi / 16 )
373 data8 0x3FC8F8B83C69A60B // cos ( 25 Pi / 16 )
375 data8 0xBFED906BCF328D46 // sin ( 26 Pi / 16 )
376 data8 0x3FD87DE2A6AEA963 // cos ( 26 Pi / 16 )
378 data8 0xBFEA9B66290EA1A3 // sin ( 27 Pi / 16 )
379 data8 0x3FE1C73B39AE68C8 // cos ( 27 Pi / 16 )
381 data8 0xBFE6A09E667F3BCD // sin ( 28 Pi / 16 )
382 data8 0x3FE6A09E667F3BCD // cos ( 28 Pi / 16 )
384 data8 0xBFE1C73B39AE68C8 // sin ( 29 Pi / 16 )
385 data8 0x3FEA9B66290EA1A3 // cos ( 29 Pi / 16 )
387 data8 0xBFD87DE2A6AEA963 // sin ( 30 Pi / 16 )
388 data8 0x3FED906BCF328D46 // cos ( 30 Pi / 16 )
390 data8 0xBFC8F8B83C69A60B // sin ( 31 Pi / 16 )
391 data8 0x3FEF6297CFF75CB0 // cos ( 31 Pi / 16 )
393 data8 0x0000000000000000 // sin ( 32 Pi / 16 )
394 data8 0x3FF0000000000000 // cos ( 32 Pi / 16 )
395 LOCAL_OBJECT_END(double_sin_cos_beta_k4)
399 GLOBAL_IEEE754_ENTRY(sincosf)
400 // cis_GR_sig_inv_pi_by_16 = significand of 16/pi
402 alloc GR_SAVE_PFS = ar.pfs, 0, 21, 0, 0
403 movl cisf_GR_sig_inv_pi_by_16 = 0xA2F9836E4E44152A // 16/pi signd
406 // cis_GR_rshf_2to61 = 1.1000 2^(63+63-2)
408 addl cisf_AD_1 = @ltoff(double_cisf_pi), gp
409 movl cisf_GR_rshf_2to61 = 0x47b8000000000000 // 1.1 2^(63+63-2)
413 ld8 cisf_AD_1 = [cisf_AD_1]
414 fnorm.s1 cisf_NORM_f8 = cisf_Arg
415 cmp.eq p13, p14 = r0, r0 // p13 set for sincos
417 // cis_GR_exp_2tom61 = exponent of scaling factor 2^-61
419 mov cisf_GR_exp_2tom61 = 0xffff-61
421 br.cond.sptk _CISF_COMMON
423 GLOBAL_IEEE754_END(sincosf)
425 GLOBAL_LIBM_ENTRY(__libm_sincosf)
427 // cisf_GR_sig_inv_pi_by_16 = significand of 16/pi
428 alloc GR_SAVE_PFS = ar.pfs,0,21,0,0
429 movl cisf_GR_sig_inv_pi_by_16 = 0xA2F9836E4E44152A
431 // cisf_GR_rshf_2to61 = 1.1000 2^(63+63-2)
433 addl cisf_AD_1 = @ltoff(double_cisf_pi), gp
434 movl cisf_GR_rshf_2to61 = 0x47b8000000000000
437 // p14 set for __libm_sincos and cis
439 ld8 cisf_AD_1 = [cisf_AD_1]
440 fnorm.s1 cisf_NORM_f8 = cisf_Arg
441 cmp.eq p14, p13 = r0, r0
443 // cisf_GR_exp_2tom61 = exponent of scaling factor 2^-61
445 mov cisf_GR_exp_2tom61 = 0xffff-61
451 // Form two constants we need
452 // 16/pi * 2^-2 * 2^63, scaled by 2^61 since we just loaded the significand
453 // 1.1000...000 * 2^(63+63-2) to right shift int(W) into the low significand
454 // fcmp used to set denormal, and invalid on snans
456 setf.sig cisf_SIG_INV_PI_BY_16_2TO61 = cisf_GR_sig_inv_pi_by_16
457 fclass.m p6,p0 = cisf_Arg, 0xe7//if x=0,inf,nan
458 addl cisf_gr_tmp = -1, r0
460 // cisf_GR_rshf = 1.1000 2^63 for right shift
462 setf.d cisf_RSHF_2TO61 = cisf_GR_rshf_2to61
463 movl cisf_GR_rshf = 0x43e8000000000000
466 // Form another constant
467 // 2^-61 for scaling Nfloat
468 // 0x10017 is register_bias + 24.
469 // So if f8 >= 2^24, go to large args routine
471 getf.exp cisf_r_signexp = cisf_Arg
472 setf.exp cisf_2TOM61 = cisf_GR_exp_2tom61
473 mov cisf_exp_limit = 0x10017
476 // Load the two pieces of pi/16
477 // Form another constant
478 // 1.1000...000 * 2^63, the right shift constant
480 ldfe cisf_Pi_by_16_hi = [cisf_AD_1],16
481 setf.d cisf_RSHF = cisf_GR_rshf
482 (p6) br.cond.spnt _CISF_SPECIAL_ARGS
486 ldfe cisf_Pi_by_16_lo = [cisf_AD_1],16
487 setf.sig cisf_tmp = cisf_gr_tmp //constant for inexact set
491 // Start loading P, Q coefficients
493 ldfpd cisf_P2,cisf_Q2 = [cisf_AD_1],16
495 dep.z cisf_r_exp = cisf_r_signexp, 0, 17
498 // p10 is true if we must call routines to handle larger arguments
499 // p10 is true if f8 exp is >= 0x10017
501 ldfpd cisf_P1,cisf_Q1 = [cisf_AD_1], 16
502 cmp.ge p10, p0 = cisf_r_exp, cisf_exp_limit
503 (p10) br.cond.spnt _CISF_LARGE_ARGS // go to |x| >= 2^24 path
506 // cisf_W = x * cisf_Inv_Pi_by_16
507 // Multiply x by scaled 16/pi and add large const to shift integer part of W to
508 // rightmost bits of significand
511 fma.s1 cisf_W_2TO61_RSH = cisf_NORM_f8,cisf_SIG_INV_PI_BY_16_2TO61,cisf_RSHF_2TO61
515 // cisf_NFLOAT = Round_Int_Nearest(cisf_W)
518 fms.s1 cisf_NFLOAT = cisf_W_2TO61_RSH,cisf_2TOM61,cisf_RSHF
522 // N = (int)cisf_int_Nfloat
524 getf.sig cisf_GR_n = cisf_W_2TO61_RSH
529 // Add 2^(k-1) (which is in cisf_r_sincos) to N
530 // cisf_r = -cisf_Nfloat * cisf_Pi_by_16_hi + x
531 // cisf_r = cisf_r -cisf_Nfloat * cisf_Pi_by_16_lo
533 add cisf_GR_n_cos = 0x8, cisf_GR_n
534 fnma.s1 cisf_r = cisf_NFLOAT, cisf_Pi_by_16_hi, cisf_NORM_f8
538 //Get M (least k+1 bits of N)
540 and cisf_GR_m_sin = 0x1f,cisf_GR_n
541 and cisf_GR_m_cos = 0x1f,cisf_GR_n_cos
546 shladd cisf_AD_2_cos = cisf_GR_m_cos,4, cisf_AD_1
547 shladd cisf_AD_2_sin = cisf_GR_m_sin,4, cisf_AD_1
551 // den. input to set uflow
553 ldfpd cisf_Sm_sin, cisf_Cm_sin = [cisf_AD_2_sin]
554 ldfpd cisf_Sm_cos, cisf_Cm_cos = [cisf_AD_2_cos]
555 fclass.m.unc p10,p0 = cisf_Arg,0x0b
560 fma.s1 cisf_rsq = cisf_r, cisf_r, f0 // get r^2
565 fmpy.s0 cisf_tmp = cisf_tmp,cisf_tmp // inexact flag
572 fnma.s1 cisf_r_exact = cisf_NFLOAT, cisf_Pi_by_16_lo, cisf_r
577 fma.s1 cisf_P = cisf_rsq, cisf_P2, cisf_P1
582 fma.s1 cisf_Q = cisf_rsq, cisf_Q2, cisf_Q1
588 fmpy.s1 cisf_rcub = cisf_r_exact, cisf_rsq // get r^3
594 fmpy.s1 cisf_srsq_sin = cisf_Sm_sin,cisf_rsq
599 fmpy.s1 cisf_srsq_cos = cisf_Sm_cos,cisf_rsq
605 fma.s1 cisf_P = cisf_rcub,cisf_P,cisf_r_exact
611 fma.s1 cisf_Q_sin = cisf_srsq_sin,cisf_Q, cisf_Sm_sin
616 fma.s1 cisf_Q_cos = cisf_srsq_cos,cisf_Q, cisf_Sm_cos
620 // If den. arg, force underflow to be set
623 (p10) fmpy.s.s0 cisf_tmp = cisf_Arg,cisf_Arg
630 fma.s.s0 cisf_Sin_res = cisf_Cm_sin, cisf_P, cisf_Q_sin
636 fma.s.s0 cisf_Cos_res = cisf_Cm_cos, cisf_P, cisf_Q_cos
637 (p14) br.cond.sptk _CISF_RETURN //com. exit for __libm_sincos and cis main path
641 stfs [cisf_pResSin] = cisf_Sin_res
642 stfs [cisf_pResCos] = cisf_Cos_res
643 br.ret.sptk b0 // common exit for sincos main path
652 fma.s.s0 cisf_Sin_res = cisf_Arg, f0, f0 // sinf(+/-0,NaN,Inf)
661 fma.s.s0 cisf_Cos_res = cisf_Arg, f0, f1 // cosf(+/-0,NaN,Inf)
662 (p14) br.cond.sptk _CISF_RETURN //spec exit for __libm_sincos and cis main path
666 stfs [cisf_pResSin] = cisf_Sin_res
667 stfs [cisf_pResCos] = cisf_Cos_res
668 br.ret.sptk b0 // special exit for sincos main path
672 // NOTE! r8 and r9 used only because of compiler issue
673 // connected with float point complex function arguments pass
674 // After fix of this issue this operations can be deleted
677 getf.s r8 = cisf_Cos_res
678 getf.s r9 = cisf_Sin_res
679 br.ret.sptk b0 // exit for sincos
681 GLOBAL_LIBM_END(__libm_sincosf)
683 //// |x| > 2^24 path ///////
684 .proc _CISF_LARGE_ARGS
690 .save ar.pfs, GR_SAVE_PFS
691 mov GR_SAVE_PFS = ar.pfs
702 // Call of huge arguments sincos
706 br.call.sptk b0 = __libm_sincos_large
712 mov pr = GR_SAVE_PR, 0x1fffe
725 fma.s.s0 cisf_Cos_res = cisf_Cos_res, f1, f0
726 mov ar.pfs = GR_SAVE_PFS
728 // exit for |x| > 2^24 path (__libm_sincos and cis)
731 fma.s.s0 cisf_Sin_res = cisf_Sin_res, f1, f0
732 (p14) br.cond.sptk _CISF_RETURN
736 stfs [cisf_pResSin] = cisf_Sin_res
737 stfs [cisf_pResCos] = cisf_Cos_res
738 br.ret.sptk b0 // exit for sincos |x| > 2^24 path
741 .endp _CISF_LARGE_ARGS
743 .type __libm_sincos_large#,@function
744 .global __libm_sincos_large#