4 // Copyright (c) 2002 - 2005, 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/01/02 Initial version
42 // 02/18/02 Large arguments processing routine is excluded.
43 // External interface entry points are added
44 // 02/26/02 Added temporary return of results in r8, r9
45 // 03/13/02 Corrected restore of predicate registers
46 // 03/19/02 Added stack unwind around call to __libm_cisf_large
47 // 09/05/02 Work range is widened by reduction strengthen (2 parts of Pi/16)
48 // 02/10/03 Reordered header: .section, .global, .proc, .align
49 // 02/11/04 cisf is moved to the separate file.
50 // 03/31/05 Reformatted delimiters between data tables
53 //==============================================================
54 // 1) void sincosf(float, float*s, float*c)
55 // 2) __libm_sincosf - internal LIBM function, that accepts
56 // argument in f8 and returns cosine through f8, sine through f9
59 // Overview of operation
60 //==============================================================
64 // Reduce x to region -1/2*pi/2^k ===== 0 ===== +1/2*pi/2^k where k=4
65 // divide x by pi/2^k.
66 // Multiply by 2^k/pi.
67 // nfloat = Round result to integer (round-to-nearest)
69 // r = x - nfloat * pi/2^k
70 // Do this as (x - nfloat * HIGH(pi/2^k)) - nfloat * LOW(pi/2^k) for increased accuracy.
71 // pi/2^k is stored as two numbers that when added make pi/2^k.
72 // pi/2^k = HIGH(pi/2^k) + LOW(pi/2^k)
73 // HIGH part is rounded to zero, LOW - to nearest
75 // x = (nfloat * pi/2^k) + r
76 // r is small enough that we can use a polynomial approximation
77 // and is referred to as the reduced argument.
81 // Take the unreduced part and remove the multiples of 2pi.
82 // So nfloat = nfloat (with lower k+1 bits cleared) + lower k+1 bits
84 // nfloat (with lower k+1 bits cleared) is a multiple of 2^(k+1)
86 // nfloat * pi/2^k = N * 2^(k+1) * pi/2^k + (lower k+1 bits) * pi/2^k
87 // nfloat * pi/2^k = N * 2 * pi + (lower k+1 bits) * pi/2^k
88 // nfloat * pi/2^k = N2pi + M * pi/2^k
91 // Sin(x) = Sin((nfloat * pi/2^k) + r)
92 // = Sin(nfloat * pi/2^k) * Cos(r) + Cos(nfloat * pi/2^k) * Sin(r)
94 // Sin(nfloat * pi/2^k) = Sin(N2pi + Mpi/2^k)
95 // = Sin(N2pi)Cos(Mpi/2^k) + Cos(N2pi)Sin(Mpi/2^k)
98 // Cos(nfloat * pi/2^k) = Cos(N2pi + Mpi/2^k)
99 // = Cos(N2pi)Cos(Mpi/2^k) + Sin(N2pi)Sin(Mpi/2^k)
102 // Sin(x) = Sin(Mpi/2^k) Cos(r) + Cos(Mpi/2^k) Sin(r)
108 // There are 2^(k+1) Sin entries in a table.
109 // There are 2^(k+1) Cos entries in a table.
111 // Get Sin(Mpi/2^k) and Cos(Mpi/2^k) by table lookup.
116 // Calculate Cos(r) and Sin(r) by polynomial approximation.
118 // Cos(r) = 1 + r^2 q1 + r^4 q2 = Series for Cos
119 // Sin(r) = r + r^3 p1 + r^5 p2 = Series for Sin
121 // and the coefficients q1, q2 and p1, p2 are stored in a table
125 // Sin(x) = Sin(Mpi/2^k) Cos(r) + Cos(Mpi/2^k) Sin(r)
129 // S[m] = Sin(Mpi/2^k) and C[m] = Cos(Mpi/2^k)
137 // Sin(r) = r + rcub * P
138 // = r + r^3p1 + r^5p2 = Sin(r)
142 // Answer = S[m] Cos(r) + C[m] P
144 // Cos(r) = 1 + rsq Q
145 // Cos(r) = 1 + r^2 Q
146 // Cos(r) = 1 + r^2 (q1 + r^2q2)
147 // Cos(r) = 1 + r^2q1 + r^4q2
149 // S[m] Cos(r) = S[m](1 + rsq Q)
150 // S[m] Cos(r) = S[m] + S[m] rsq Q
151 // S[m] Cos(r) = S[m] + s_rsq Q
152 // Q = S[m] + s_rsq Q
156 // Answer = Q + C[m] P
160 //==============================================================
161 // general input registers:
165 // predicate registers used:
168 // floating-point registers used
173 //==============================================================
183 cisf_int_Nfloat = f12
191 cisf_Inv_Pi_by_16 = f33
192 cisf_Pi_by_16_hi = f34
193 cisf_Pi_by_16_lo = f35
195 cisf_Inv_Pi_by_64 = f36
196 cisf_Pi_by_64_hi = f37
197 cisf_Pi_by_64_lo = f38
217 cisf_SIG_INV_PI_BY_16_2TO61 = f52
218 cisf_RSHF_2TO61 = f53
222 cisf_W_2TO61_RSH = f57
239 /////////////////////////////////////////////////////////////
246 cisf_AD_beta_table = r37
252 cisf_GR_sig_inv_pi_by_16 = r14
253 cisf_GR_rshf_2to61 = r15
255 cisf_GR_exp_2tom61 = r17
260 cisf_GR_32m_sin = r41
264 cisf_GR_32m_cos = r43
281 LOCAL_OBJECT_START(double_cisf_pi)
282 data8 0xC90FDAA22168C234, 0x00003FFC // pi/16 1st part
283 data8 0xC4C6628B80DC1CD1, 0x00003FBC // pi/16 2nd part
284 LOCAL_OBJECT_END(double_cisf_pi)
286 // Coefficients for polynomials
287 LOCAL_OBJECT_START(double_cisf_pq_k4)
288 data8 0x3F810FABB668E9A2 // P2
289 data8 0x3FA552E3D6DE75C9 // Q2
290 data8 0xBFC555554447BC7F // P1
291 data8 0xBFDFFFFFC447610A // Q1
292 LOCAL_OBJECT_END(double_cisf_pq_k4)
294 // Sincos table (S[m], C[m])
295 LOCAL_OBJECT_START(double_sin_cos_beta_k4)
296 data8 0x0000000000000000 // sin ( 0 Pi / 16 )
297 data8 0x3FF0000000000000 // cos ( 0 Pi / 16 )
299 data8 0x3FC8F8B83C69A60B // sin ( 1 Pi / 16 )
300 data8 0x3FEF6297CFF75CB0 // cos ( 1 Pi / 16 )
302 data8 0x3FD87DE2A6AEA963 // sin ( 2 Pi / 16 )
303 data8 0x3FED906BCF328D46 // cos ( 2 Pi / 16 )
305 data8 0x3FE1C73B39AE68C8 // sin ( 3 Pi / 16 )
306 data8 0x3FEA9B66290EA1A3 // cos ( 3 Pi / 16 )
308 data8 0x3FE6A09E667F3BCD // sin ( 4 Pi / 16 )
309 data8 0x3FE6A09E667F3BCD // cos ( 4 Pi / 16 )
311 data8 0x3FEA9B66290EA1A3 // sin ( 5 Pi / 16 )
312 data8 0x3FE1C73B39AE68C8 // cos ( 5 Pi / 16 )
314 data8 0x3FED906BCF328D46 // sin ( 6 Pi / 16 )
315 data8 0x3FD87DE2A6AEA963 // cos ( 6 Pi / 16 )
317 data8 0x3FEF6297CFF75CB0 // sin ( 7 Pi / 16 )
318 data8 0x3FC8F8B83C69A60B // cos ( 7 Pi / 16 )
320 data8 0x3FF0000000000000 // sin ( 8 Pi / 16 )
321 data8 0x0000000000000000 // cos ( 8 Pi / 16 )
323 data8 0x3FEF6297CFF75CB0 // sin ( 9 Pi / 16 )
324 data8 0xBFC8F8B83C69A60B // cos ( 9 Pi / 16 )
326 data8 0x3FED906BCF328D46 // sin ( 10 Pi / 16 )
327 data8 0xBFD87DE2A6AEA963 // cos ( 10 Pi / 16 )
329 data8 0x3FEA9B66290EA1A3 // sin ( 11 Pi / 16 )
330 data8 0xBFE1C73B39AE68C8 // cos ( 11 Pi / 16 )
332 data8 0x3FE6A09E667F3BCD // sin ( 12 Pi / 16 )
333 data8 0xBFE6A09E667F3BCD // cos ( 12 Pi / 16 )
335 data8 0x3FE1C73B39AE68C8 // sin ( 13 Pi / 16 )
336 data8 0xBFEA9B66290EA1A3 // cos ( 13 Pi / 16 )
338 data8 0x3FD87DE2A6AEA963 // sin ( 14 Pi / 16 )
339 data8 0xBFED906BCF328D46 // cos ( 14 Pi / 16 )
341 data8 0x3FC8F8B83C69A60B // sin ( 15 Pi / 16 )
342 data8 0xBFEF6297CFF75CB0 // cos ( 15 Pi / 16 )
344 data8 0x0000000000000000 // sin ( 16 Pi / 16 )
345 data8 0xBFF0000000000000 // cos ( 16 Pi / 16 )
347 data8 0xBFC8F8B83C69A60B // sin ( 17 Pi / 16 )
348 data8 0xBFEF6297CFF75CB0 // cos ( 17 Pi / 16 )
350 data8 0xBFD87DE2A6AEA963 // sin ( 18 Pi / 16 )
351 data8 0xBFED906BCF328D46 // cos ( 18 Pi / 16 )
353 data8 0xBFE1C73B39AE68C8 // sin ( 19 Pi / 16 )
354 data8 0xBFEA9B66290EA1A3 // cos ( 19 Pi / 16 )
356 data8 0xBFE6A09E667F3BCD // sin ( 20 Pi / 16 )
357 data8 0xBFE6A09E667F3BCD // cos ( 20 Pi / 16 )
359 data8 0xBFEA9B66290EA1A3 // sin ( 21 Pi / 16 )
360 data8 0xBFE1C73B39AE68C8 // cos ( 21 Pi / 16 )
362 data8 0xBFED906BCF328D46 // sin ( 22 Pi / 16 )
363 data8 0xBFD87DE2A6AEA963 // cos ( 22 Pi / 16 )
365 data8 0xBFEF6297CFF75CB0 // sin ( 23 Pi / 16 )
366 data8 0xBFC8F8B83C69A60B // cos ( 23 Pi / 16 )
368 data8 0xBFF0000000000000 // sin ( 24 Pi / 16 )
369 data8 0x0000000000000000 // cos ( 24 Pi / 16 )
371 data8 0xBFEF6297CFF75CB0 // sin ( 25 Pi / 16 )
372 data8 0x3FC8F8B83C69A60B // cos ( 25 Pi / 16 )
374 data8 0xBFED906BCF328D46 // sin ( 26 Pi / 16 )
375 data8 0x3FD87DE2A6AEA963 // cos ( 26 Pi / 16 )
377 data8 0xBFEA9B66290EA1A3 // sin ( 27 Pi / 16 )
378 data8 0x3FE1C73B39AE68C8 // cos ( 27 Pi / 16 )
380 data8 0xBFE6A09E667F3BCD // sin ( 28 Pi / 16 )
381 data8 0x3FE6A09E667F3BCD // cos ( 28 Pi / 16 )
383 data8 0xBFE1C73B39AE68C8 // sin ( 29 Pi / 16 )
384 data8 0x3FEA9B66290EA1A3 // cos ( 29 Pi / 16 )
386 data8 0xBFD87DE2A6AEA963 // sin ( 30 Pi / 16 )
387 data8 0x3FED906BCF328D46 // cos ( 30 Pi / 16 )
389 data8 0xBFC8F8B83C69A60B // sin ( 31 Pi / 16 )
390 data8 0x3FEF6297CFF75CB0 // cos ( 31 Pi / 16 )
392 data8 0x0000000000000000 // sin ( 32 Pi / 16 )
393 data8 0x3FF0000000000000 // cos ( 32 Pi / 16 )
394 LOCAL_OBJECT_END(double_sin_cos_beta_k4)
398 GLOBAL_IEEE754_ENTRY(sincosf)
399 // cis_GR_sig_inv_pi_by_16 = significand of 16/pi
401 alloc GR_SAVE_PFS = ar.pfs, 0, 21, 0, 0
402 movl cisf_GR_sig_inv_pi_by_16 = 0xA2F9836E4E44152A // 16/pi signd
405 // cis_GR_rshf_2to61 = 1.1000 2^(63+63-2)
407 addl cisf_AD_1 = @ltoff(double_cisf_pi), gp
408 movl cisf_GR_rshf_2to61 = 0x47b8000000000000 // 1.1 2^(63+63-2)
412 ld8 cisf_AD_1 = [cisf_AD_1]
413 fnorm.s1 cisf_NORM_f8 = cisf_Arg
414 cmp.eq p13, p14 = r0, r0 // p13 set for sincos
416 // cis_GR_exp_2tom61 = exponent of scaling factor 2^-61
418 mov cisf_GR_exp_2tom61 = 0xffff-61
420 br.cond.sptk _CISF_COMMON
422 GLOBAL_IEEE754_END(sincosf)
423 libm_alias_float_other (__sincos, sincos)
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#