1.file "sincos.s"
2
3
4// Copyright (c) 2000 - 2005, Intel Corporation
5// All rights reserved.
6//
7//
8// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
11//
12// * Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14//
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.
18//
19// * The name of Intel Corporation may not be used to endorse or promote
20// products derived from this software without specific prior written
21// permission.
22
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.
34//
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.
38//
39// History
40//==============================================================
41// 02/02/00 Initial version
42// 04/02/00 Unwind support added.
43// 06/16/00 Updated tables to enforce symmetry
44// 08/31/00 Saved 2 cycles in main path, and 9 in other paths.
45// 09/20/00 The updated tables regressed to an old version, so reinstated them
46// 10/18/00 Changed one table entry to ensure symmetry
47// 01/03/01 Improved speed, fixed flag settings for small arguments.
48// 02/18/02 Large arguments processing routine excluded
49// 05/20/02 Cleaned up namespace and sf0 syntax
50// 06/03/02 Insure inexact flag set for large arg result
51// 09/05/02 Work range is widened by reduction strengthen (3 parts of Pi/16)
52// 02/10/03 Reordered header: .section, .global, .proc, .align
53// 08/08/03 Improved performance
54// 10/28/04 Saved sincos_r_sincos to avoid clobber by dynamic loader
55// 03/31/05 Reformatted delimiters between data tables
56
57// API
58//==============================================================
59// double sin( double x);
60// double cos( double x);
61//
62// Overview of operation
63//==============================================================
64//
65// Step 1
66// ======
67// Reduce x to region -1/2*pi/2^k ===== 0 ===== +1/2*pi/2^k  where k=4
68//    divide x by pi/2^k.
69//    Multiply by 2^k/pi.
70//    nfloat = Round result to integer (round-to-nearest)
71//
72// r = x -  nfloat * pi/2^k
73//    Do this as ((((x -  nfloat * HIGH(pi/2^k))) -
74//                        nfloat * LOW(pi/2^k)) -
75//                        nfloat * LOWEST(pi/2^k) for increased accuracy.
76//    pi/2^k is stored as two numbers that when added make pi/2^k.
77//       pi/2^k = HIGH(pi/2^k) + LOW(pi/2^k)
78//    HIGH and LOW parts are rounded to zero values,
79//    and LOWEST is rounded to nearest one.
80//
81// x = (nfloat * pi/2^k) + r
82//    r is small enough that we can use a polynomial approximation
83//    and is referred to as the reduced argument.
84//
85// Step 3
86// ======
87// Take the unreduced part and remove the multiples of 2pi.
88// So nfloat = nfloat (with lower k+1 bits cleared) + lower k+1 bits
89//
90//    nfloat (with lower k+1 bits cleared) is a multiple of 2^(k+1)
91//    N * 2^(k+1)
92//    nfloat * pi/2^k = N * 2^(k+1) * pi/2^k + (lower k+1 bits) * pi/2^k
93//    nfloat * pi/2^k = N * 2 * pi + (lower k+1 bits) * pi/2^k
94//    nfloat * pi/2^k = N2pi + M * pi/2^k
95//
96//
97// Sin(x) = Sin((nfloat * pi/2^k) + r)
98//        = Sin(nfloat * pi/2^k) * Cos(r) + Cos(nfloat * pi/2^k) * Sin(r)
99//
100//          Sin(nfloat * pi/2^k) = Sin(N2pi + Mpi/2^k)
101//                               = Sin(N2pi)Cos(Mpi/2^k) + Cos(N2pi)Sin(Mpi/2^k)
102//                               = Sin(Mpi/2^k)
103//
104//          Cos(nfloat * pi/2^k) = Cos(N2pi + Mpi/2^k)
105//                               = Cos(N2pi)Cos(Mpi/2^k) + Sin(N2pi)Sin(Mpi/2^k)
106//                               = Cos(Mpi/2^k)
107//
108// Sin(x) = Sin(Mpi/2^k) Cos(r) + Cos(Mpi/2^k) Sin(r)
109//
110//
111// Step 4
112// ======
113// 0 <= M < 2^(k+1)
114// There are 2^(k+1) Sin entries in a table.
115// There are 2^(k+1) Cos entries in a table.
116//
117// Get Sin(Mpi/2^k) and Cos(Mpi/2^k) by table lookup.
118//
119//
120// Step 5
121// ======
122// Calculate Cos(r) and Sin(r) by polynomial approximation.
123//
124// Cos(r) = 1 + r^2 q1  + r^4 q2 + r^6 q3 + ... = Series for Cos
125// Sin(r) = r + r^3 p1  + r^5 p2 + r^7 p3 + ... = Series for Sin
126//
127// and the coefficients q1, q2, ... and p1, p2, ... are stored in a table
128//
129//
130// Calculate
131// Sin(x) = Sin(Mpi/2^k) Cos(r) + Cos(Mpi/2^k) Sin(r)
132//
133// as follows
134//
135//    S[m] = Sin(Mpi/2^k) and C[m] = Cos(Mpi/2^k)
136//    rsq = r*r
137//
138//
139//    P = p1 + r^2p2 + r^4p3 + r^6p4
140//    Q = q1 + r^2q2 + r^4q3 + r^6q4
141//
142//       rcub = r * rsq
143//       Sin(r) = r + rcub * P
144//              = r + r^3p1  + r^5p2 + r^7p3 + r^9p4 + ... = Sin(r)
145//
146//            The coefficients are not exactly these values, but almost.
147//
148//            p1 = -1/6  = -1/3!
149//            p2 = 1/120 =  1/5!
150//            p3 = -1/5040 = -1/7!
151//            p4 = 1/362889 = 1/9!
152//
153//       P =  r + rcub * P
154//
155//    Answer = S[m] Cos(r) + [Cm] P
156//
157//       Cos(r) = 1 + rsq Q
158//       Cos(r) = 1 + r^2 Q
159//       Cos(r) = 1 + r^2 (q1 + r^2q2 + r^4q3 + r^6q4)
160//       Cos(r) = 1 + r^2q1 + r^4q2 + r^6q3 + r^8q4 + ...
161//
162//       S[m] Cos(r) = S[m](1 + rsq Q)
163//       S[m] Cos(r) = S[m] + Sm rsq Q
164//       S[m] Cos(r) = S[m] + s_rsq Q
165//       Q         = S[m] + s_rsq Q
166//
167// Then,
168//
169//    Answer = Q + C[m] P
170
171
172// Registers used
173//==============================================================
174// general input registers:
175// r14 -> r26
176// r32 -> r35
177
178// predicate registers used:
179// p6 -> p11
180
181// floating-point registers used
182// f9 -> f15
183// f32 -> f61
184
185// Assembly macros
186//==============================================================
187sincos_NORM_f8                 = f9
188sincos_W                       = f10
189sincos_int_Nfloat              = f11
190sincos_Nfloat                  = f12
191
192sincos_r                       = f13
193sincos_rsq                     = f14
194sincos_rcub                    = f15
195sincos_save_tmp                = f15
196
197sincos_Inv_Pi_by_16            = f32
198sincos_Pi_by_16_1              = f33
199sincos_Pi_by_16_2              = f34
200
201sincos_Inv_Pi_by_64            = f35
202
203sincos_Pi_by_16_3              = f36
204
205sincos_r_exact                 = f37
206
207sincos_Sm                      = f38
208sincos_Cm                      = f39
209
210sincos_P1                      = f40
211sincos_Q1                      = f41
212sincos_P2                      = f42
213sincos_Q2                      = f43
214sincos_P3                      = f44
215sincos_Q3                      = f45
216sincos_P4                      = f46
217sincos_Q4                      = f47
218
219sincos_P_temp1                 = f48
220sincos_P_temp2                 = f49
221
222sincos_Q_temp1                 = f50
223sincos_Q_temp2                 = f51
224
225sincos_P                       = f52
226sincos_Q                       = f53
227
228sincos_srsq                    = f54
229
230sincos_SIG_INV_PI_BY_16_2TO61  = f55
231sincos_RSHF_2TO61              = f56
232sincos_RSHF                    = f57
233sincos_2TOM61                  = f58
234sincos_NFLOAT                  = f59
235sincos_W_2TO61_RSH             = f60
236
237fp_tmp                         = f61
238
239/////////////////////////////////////////////////////////////
240
241sincos_GR_sig_inv_pi_by_16     = r14
242sincos_GR_rshf_2to61           = r15
243sincos_GR_rshf                 = r16
244sincos_GR_exp_2tom61           = r17
245sincos_GR_n                    = r18
246sincos_GR_m                    = r19
247sincos_GR_32m                  = r19
248sincos_GR_all_ones             = r19
249sincos_AD_1                    = r20
250sincos_AD_2                    = r21
251sincos_exp_limit               = r22
252sincos_r_signexp               = r23
253sincos_r_17_ones               = r24
254sincos_r_sincos                = r25
255sincos_r_exp                   = r26
256
257GR_SAVE_PFS                    = r33
258GR_SAVE_B0                     = r34
259GR_SAVE_GP                     = r35
260GR_SAVE_r_sincos               = r36
261
262
263RODATA
264
265// Pi/16 parts
266.align 16
267LOCAL_OBJECT_START(double_sincos_pi)
268   data8 0xC90FDAA22168C234, 0x00003FFC // pi/16 1st part
269   data8 0xC4C6628B80DC1CD1, 0x00003FBC // pi/16 2nd part
270   data8 0xA4093822299F31D0, 0x00003F7A // pi/16 3rd part
271LOCAL_OBJECT_END(double_sincos_pi)
272
273// Coefficients for polynomials
274LOCAL_OBJECT_START(double_sincos_pq_k4)
275   data8 0x3EC71C963717C63A // P4
276   data8 0x3EF9FFBA8F191AE6 // Q4
277   data8 0xBF2A01A00F4E11A8 // P3
278   data8 0xBF56C16C05AC77BF // Q3
279   data8 0x3F8111111110F167 // P2
280   data8 0x3FA555555554DD45 // Q2
281   data8 0xBFC5555555555555 // P1
282   data8 0xBFDFFFFFFFFFFFFC // Q1
283LOCAL_OBJECT_END(double_sincos_pq_k4)
284
285// Sincos table (S[m], C[m])
286LOCAL_OBJECT_START(double_sin_cos_beta_k4)
287
288data8 0x0000000000000000 , 0x00000000 // sin( 0 pi/16)  S0
289data8 0x8000000000000000 , 0x00003fff // cos( 0 pi/16)  C0
290//
291data8 0xc7c5c1e34d3055b3 , 0x00003ffc // sin( 1 pi/16)  S1
292data8 0xfb14be7fbae58157 , 0x00003ffe // cos( 1 pi/16)  C1
293//
294data8 0xc3ef1535754b168e , 0x00003ffd // sin( 2 pi/16)  S2
295data8 0xec835e79946a3146 , 0x00003ffe // cos( 2 pi/16)  C2
296//
297data8 0x8e39d9cd73464364 , 0x00003ffe // sin( 3 pi/16)  S3
298data8 0xd4db3148750d181a , 0x00003ffe // cos( 3 pi/16)  C3
299//
300data8 0xb504f333f9de6484 , 0x00003ffe // sin( 4 pi/16)  S4
301data8 0xb504f333f9de6484 , 0x00003ffe // cos( 4 pi/16)  C4
302//
303data8 0xd4db3148750d181a , 0x00003ffe // sin( 5 pi/16)  C3
304data8 0x8e39d9cd73464364 , 0x00003ffe // cos( 5 pi/16)  S3
305//
306data8 0xec835e79946a3146 , 0x00003ffe // sin( 6 pi/16)  C2
307data8 0xc3ef1535754b168e , 0x00003ffd // cos( 6 pi/16)  S2
308//
309data8 0xfb14be7fbae58157 , 0x00003ffe // sin( 7 pi/16)  C1
310data8 0xc7c5c1e34d3055b3 , 0x00003ffc // cos( 7 pi/16)  S1
311//
312data8 0x8000000000000000 , 0x00003fff // sin( 8 pi/16)  C0
313data8 0x0000000000000000 , 0x00000000 // cos( 8 pi/16)  S0
314//
315data8 0xfb14be7fbae58157 , 0x00003ffe // sin( 9 pi/16)  C1
316data8 0xc7c5c1e34d3055b3 , 0x0000bffc // cos( 9 pi/16)  -S1
317//
318data8 0xec835e79946a3146 , 0x00003ffe // sin(10 pi/16)  C2
319data8 0xc3ef1535754b168e , 0x0000bffd // cos(10 pi/16)  -S2
320//
321data8 0xd4db3148750d181a , 0x00003ffe // sin(11 pi/16)  C3
322data8 0x8e39d9cd73464364 , 0x0000bffe // cos(11 pi/16)  -S3
323//
324data8 0xb504f333f9de6484 , 0x00003ffe // sin(12 pi/16)  S4
325data8 0xb504f333f9de6484 , 0x0000bffe // cos(12 pi/16)  -S4
326//
327data8 0x8e39d9cd73464364 , 0x00003ffe // sin(13 pi/16) S3
328data8 0xd4db3148750d181a , 0x0000bffe // cos(13 pi/16) -C3
329//
330data8 0xc3ef1535754b168e , 0x00003ffd // sin(14 pi/16) S2
331data8 0xec835e79946a3146 , 0x0000bffe // cos(14 pi/16) -C2
332//
333data8 0xc7c5c1e34d3055b3 , 0x00003ffc // sin(15 pi/16) S1
334data8 0xfb14be7fbae58157 , 0x0000bffe // cos(15 pi/16) -C1
335//
336data8 0x0000000000000000 , 0x00000000 // sin(16 pi/16) S0
337data8 0x8000000000000000 , 0x0000bfff // cos(16 pi/16) -C0
338//
339data8 0xc7c5c1e34d3055b3 , 0x0000bffc // sin(17 pi/16) -S1
340data8 0xfb14be7fbae58157 , 0x0000bffe // cos(17 pi/16) -C1
341//
342data8 0xc3ef1535754b168e , 0x0000bffd // sin(18 pi/16) -S2
343data8 0xec835e79946a3146 , 0x0000bffe // cos(18 pi/16) -C2
344//
345data8 0x8e39d9cd73464364 , 0x0000bffe // sin(19 pi/16) -S3
346data8 0xd4db3148750d181a , 0x0000bffe // cos(19 pi/16) -C3
347//
348data8 0xb504f333f9de6484 , 0x0000bffe // sin(20 pi/16) -S4
349data8 0xb504f333f9de6484 , 0x0000bffe // cos(20 pi/16) -S4
350//
351data8 0xd4db3148750d181a , 0x0000bffe // sin(21 pi/16) -C3
352data8 0x8e39d9cd73464364 , 0x0000bffe // cos(21 pi/16) -S3
353//
354data8 0xec835e79946a3146 , 0x0000bffe // sin(22 pi/16) -C2
355data8 0xc3ef1535754b168e , 0x0000bffd // cos(22 pi/16) -S2
356//
357data8 0xfb14be7fbae58157 , 0x0000bffe // sin(23 pi/16) -C1
358data8 0xc7c5c1e34d3055b3 , 0x0000bffc // cos(23 pi/16) -S1
359//
360data8 0x8000000000000000 , 0x0000bfff // sin(24 pi/16) -C0
361data8 0x0000000000000000 , 0x00000000 // cos(24 pi/16) S0
362//
363data8 0xfb14be7fbae58157 , 0x0000bffe // sin(25 pi/16) -C1
364data8 0xc7c5c1e34d3055b3 , 0x00003ffc // cos(25 pi/16) S1
365//
366data8 0xec835e79946a3146 , 0x0000bffe // sin(26 pi/16) -C2
367data8 0xc3ef1535754b168e , 0x00003ffd // cos(26 pi/16) S2
368//
369data8 0xd4db3148750d181a , 0x0000bffe // sin(27 pi/16) -C3
370data8 0x8e39d9cd73464364 , 0x00003ffe // cos(27 pi/16) S3
371//
372data8 0xb504f333f9de6484 , 0x0000bffe // sin(28 pi/16) -S4
373data8 0xb504f333f9de6484 , 0x00003ffe // cos(28 pi/16) S4
374//
375data8 0x8e39d9cd73464364 , 0x0000bffe // sin(29 pi/16) -S3
376data8 0xd4db3148750d181a , 0x00003ffe // cos(29 pi/16) C3
377//
378data8 0xc3ef1535754b168e , 0x0000bffd // sin(30 pi/16) -S2
379data8 0xec835e79946a3146 , 0x00003ffe // cos(30 pi/16) C2
380//
381data8 0xc7c5c1e34d3055b3 , 0x0000bffc // sin(31 pi/16) -S1
382data8 0xfb14be7fbae58157 , 0x00003ffe // cos(31 pi/16) C1
383//
384data8 0x0000000000000000 , 0x00000000 // sin(32 pi/16) S0
385data8 0x8000000000000000 , 0x00003fff // cos(32 pi/16) C0
386LOCAL_OBJECT_END(double_sin_cos_beta_k4)
387
388.section .text
389
390////////////////////////////////////////////////////////
391// There are two entry points: sin and cos
392
393
394// If from sin, p8 is true
395// If from cos, p9 is true
396
397GLOBAL_IEEE754_ENTRY(sin)
398
399{ .mlx
400      getf.exp      sincos_r_signexp    = f8
401      movl sincos_GR_sig_inv_pi_by_16   = 0xA2F9836E4E44152A // signd of 16/pi
402}
403{ .mlx
404      addl          sincos_AD_1         = @ltoff(double_sincos_pi), gp
405      movl sincos_GR_rshf_2to61         = 0x47b8000000000000 // 1.1 2^(63+63-2)
406}
407;;
408
409{ .mfi
410      ld8           sincos_AD_1         = [sincos_AD_1]
411      fnorm.s0      sincos_NORM_f8      = f8  // Normalize argument
412      cmp.eq        p8,p9               = r0, r0 // set p8 (clear p9) for sin
413}
414{ .mib
415      mov           sincos_GR_exp_2tom61  = 0xffff-61 // exponent of scale 2^-61
416      mov           sincos_r_sincos       = 0x0 // sincos_r_sincos = 0 for sin
417      br.cond.sptk  _SINCOS_COMMON  // go to common part
418}
419;;
420
421GLOBAL_IEEE754_END(sin)
422libm_alias_double_other (__sin, sin)
423
424GLOBAL_IEEE754_ENTRY(cos)
425
426{ .mlx
427      getf.exp      sincos_r_signexp    = f8
428      movl sincos_GR_sig_inv_pi_by_16   = 0xA2F9836E4E44152A // signd of 16/pi
429}
430{ .mlx
431      addl          sincos_AD_1         = @ltoff(double_sincos_pi), gp
432      movl sincos_GR_rshf_2to61         = 0x47b8000000000000 // 1.1 2^(63+63-2)
433}
434;;
435
436{ .mfi
437      ld8           sincos_AD_1         = [sincos_AD_1]
438      fnorm.s1      sincos_NORM_f8      = f8 // Normalize argument
439      cmp.eq        p9,p8               = r0, r0 // set p9 (clear p8) for cos
440}
441{ .mib
442      mov           sincos_GR_exp_2tom61  = 0xffff-61 // exp of scale 2^-61
443      mov           sincos_r_sincos       = 0x8 // sincos_r_sincos = 8 for cos
444      nop.b         999
445}
446;;
447
448////////////////////////////////////////////////////////
449// All entry points end up here.
450// If from sin, sincos_r_sincos is 0 and p8 is true
451// If from cos, sincos_r_sincos is 8 = 2^(k-1) and p9 is true
452// We add sincos_r_sincos to N
453
454///////////// Common sin and cos part //////////////////
455_SINCOS_COMMON:
456
457
458// Form two constants we need
459//  16/pi * 2^-2 * 2^63, scaled by 2^61 since we just loaded the significand
460//  1.1000...000 * 2^(63+63-2) to right shift int(W) into the low significand
461{ .mfi
462      setf.sig      sincos_SIG_INV_PI_BY_16_2TO61 = sincos_GR_sig_inv_pi_by_16
463      fclass.m      p6,p0                         = f8, 0xe7 // if x = 0,inf,nan
464      mov           sincos_exp_limit              = 0x1001a
465}
466{ .mlx
467      setf.d        sincos_RSHF_2TO61   = sincos_GR_rshf_2to61
468      movl          sincos_GR_rshf      = 0x43e8000000000000 // 1.1 2^63
469}                                                            // Right shift
470;;
471
472// Form another constant
473//  2^-61 for scaling Nfloat
474// 0x1001a is register_bias + 27.
475// So if f8 >= 2^27, go to large argument routines
476{ .mfi
477      alloc         r32                 = ar.pfs, 1, 4, 0, 0
478      fclass.m      p11,p0              = f8, 0x0b // Test for x=unorm
479      mov           sincos_GR_all_ones  = -1 // For "inexect" constant create
480}
481{ .mib
482      setf.exp      sincos_2TOM61       = sincos_GR_exp_2tom61
483      nop.i         999
484(p6)  br.cond.spnt  _SINCOS_SPECIAL_ARGS
485}
486;;
487
488// Load the two pieces of pi/16
489// Form another constant
490//  1.1000...000 * 2^63, the right shift constant
491{ .mmb
492      ldfe          sincos_Pi_by_16_1   = [sincos_AD_1],16
493      setf.d        sincos_RSHF         = sincos_GR_rshf
494(p11) br.cond.spnt  _SINCOS_UNORM       // Branch if x=unorm
495}
496;;
497
498_SINCOS_COMMON2:
499// Return here if x=unorm
500// Create constant used to set inexact
501{ .mmi
502      ldfe          sincos_Pi_by_16_2   = [sincos_AD_1],16
503      setf.sig      fp_tmp              = sincos_GR_all_ones
504      nop.i         999
505};;
506
507// Select exponent (17 lsb)
508{ .mfi
509      ldfe          sincos_Pi_by_16_3   = [sincos_AD_1],16
510      nop.f         999
511      dep.z         sincos_r_exp        = sincos_r_signexp, 0, 17
512};;
513
514// Polynomial coefficients (Q4, P4, Q3, P3, Q2, Q1, P2, P1) loading
515// p10 is true if we must call routines to handle larger arguments
516// p10 is true if f8 exp is >= 0x1001a (2^27)
517{ .mmb
518      ldfpd         sincos_P4,sincos_Q4 = [sincos_AD_1],16
519      cmp.ge        p10,p0              = sincos_r_exp,sincos_exp_limit
520(p10) br.cond.spnt  _SINCOS_LARGE_ARGS // Go to "large args" routine
521};;
522
523// sincos_W          = x * sincos_Inv_Pi_by_16
524// Multiply x by scaled 16/pi and add large const to shift integer part of W to
525//   rightmost bits of significand
526{ .mfi
527      ldfpd         sincos_P3,sincos_Q3 = [sincos_AD_1],16
528      fma.s1 sincos_W_2TO61_RSH = sincos_NORM_f8,sincos_SIG_INV_PI_BY_16_2TO61,sincos_RSHF_2TO61
529      nop.i         999
530};;
531
532// get N = (int)sincos_int_Nfloat
533// sincos_NFLOAT = Round_Int_Nearest(sincos_W)
534// This is done by scaling back by 2^-61 and subtracting the shift constant
535{ .mmf
536      getf.sig      sincos_GR_n         = sincos_W_2TO61_RSH
537      ldfpd         sincos_P2,sincos_Q2 = [sincos_AD_1],16
538      fms.s1 sincos_NFLOAT = sincos_W_2TO61_RSH,sincos_2TOM61,sincos_RSHF
539};;
540
541// sincos_r          = -sincos_Nfloat * sincos_Pi_by_16_1 + x
542{ .mfi
543      ldfpd         sincos_P1,sincos_Q1 = [sincos_AD_1],16
544      fnma.s1 sincos_r = sincos_NFLOAT, sincos_Pi_by_16_1, sincos_NORM_f8
545      nop.i         999
546};;
547
548// Add 2^(k-1) (which is in sincos_r_sincos) to N
549{ .mmi
550      add           sincos_GR_n         = sincos_GR_n, sincos_r_sincos
551;;
552// Get M (least k+1 bits of N)
553      and           sincos_GR_m         = 0x1f,sincos_GR_n
554      nop.i         999
555};;
556
557// sincos_r          = sincos_r -sincos_Nfloat * sincos_Pi_by_16_2
558{ .mfi
559      nop.m         999
560      fnma.s1 sincos_r = sincos_NFLOAT, sincos_Pi_by_16_2,  sincos_r
561      shl           sincos_GR_32m       = sincos_GR_m,5
562};;
563
564// Add 32*M to address of sin_cos_beta table
565// For sin denorm. - set uflow
566{ .mfi
567      add           sincos_AD_2         = sincos_GR_32m, sincos_AD_1
568(p8)  fclass.m.unc  p10,p0              = f8,0x0b
569      nop.i         999
570};;
571
572// Load Sin and Cos table value using obtained index m  (sincosf_AD_2)
573{ .mfi
574      ldfe          sincos_Sm           = [sincos_AD_2],16
575      nop.f         999
576      nop.i         999
577};;
578
579// get rsq = r*r
580{ .mfi
581      ldfe          sincos_Cm           = [sincos_AD_2]
582      fma.s1        sincos_rsq          = sincos_r, sincos_r,   f0 // r^2 = r*r
583      nop.i         999
584}
585{ .mfi
586      nop.m         999
587      fmpy.s0       fp_tmp              = fp_tmp,fp_tmp // forces inexact flag
588      nop.i         999
589};;
590
591// sincos_r_exact = sincos_r -sincos_Nfloat * sincos_Pi_by_16_3
592{ .mfi
593      nop.m         999
594      fnma.s1 sincos_r_exact = sincos_NFLOAT, sincos_Pi_by_16_3, sincos_r
595      nop.i         999
596};;
597
598// Polynomials calculation
599// P_1 = P4*r^2 + P3
600// Q_2 = Q4*r^2 + Q3
601{ .mfi
602      nop.m         999
603      fma.s1        sincos_P_temp1      = sincos_rsq, sincos_P4, sincos_P3
604      nop.i         999
605}
606{ .mfi
607      nop.m         999
608      fma.s1        sincos_Q_temp1      = sincos_rsq, sincos_Q4, sincos_Q3
609      nop.i         999
610};;
611
612// get rcube = r^3 and S[m]*r^2
613{ .mfi
614      nop.m         999
615      fmpy.s1       sincos_srsq         = sincos_Sm,sincos_rsq
616      nop.i         999
617}
618{ .mfi
619      nop.m         999
620      fmpy.s1       sincos_rcub         = sincos_r_exact, sincos_rsq
621      nop.i         999
622};;
623
624// Polynomials calculation
625// Q_2 = Q_1*r^2 + Q2
626// P_1 = P_1*r^2 + P2
627{ .mfi
628      nop.m         999
629      fma.s1        sincos_Q_temp2      = sincos_rsq, sincos_Q_temp1, sincos_Q2
630      nop.i         999
631}
632{ .mfi
633      nop.m         999
634      fma.s1        sincos_P_temp2      = sincos_rsq, sincos_P_temp1, sincos_P2
635      nop.i         999
636};;
637
638// Polynomials calculation
639// Q = Q_2*r^2 + Q1
640// P = P_2*r^2 + P1
641{ .mfi
642      nop.m         999
643      fma.s1        sincos_Q            = sincos_rsq, sincos_Q_temp2, sincos_Q1
644      nop.i         999
645}
646{ .mfi
647      nop.m         999
648      fma.s1        sincos_P            = sincos_rsq, sincos_P_temp2, sincos_P1
649      nop.i         999
650};;
651
652// Get final P and Q
653// Q = Q*S[m]*r^2 + S[m]
654// P = P*r^3 + r
655{ .mfi
656      nop.m         999
657      fma.s1        sincos_Q            = sincos_srsq,sincos_Q, sincos_Sm
658      nop.i         999
659}
660{ .mfi
661      nop.m         999
662      fma.s1        sincos_P            = sincos_rcub,sincos_P, sincos_r_exact
663      nop.i         999
664};;
665
666// If sin(denormal), force underflow to be set
667{ .mfi
668      nop.m         999
669(p10) fmpy.d.s0     fp_tmp              = sincos_NORM_f8,sincos_NORM_f8
670      nop.i         999
671};;
672
673// Final calculation
674// result = C[m]*P + Q
675{ .mfb
676      nop.m         999
677      fma.d.s0      f8                  = sincos_Cm, sincos_P, sincos_Q
678      br.ret.sptk   b0  // Exit for common path
679};;
680
681////////// x = 0/Inf/NaN path //////////////////
682_SINCOS_SPECIAL_ARGS:
683.pred.rel "mutex",p8,p9
684// sin(+/-0) = +/-0
685// sin(Inf)  = NaN
686// sin(NaN)  = NaN
687{ .mfi
688      nop.m         999
689(p8)  fma.d.s0      f8                  = f8, f0, f0 // sin(+/-0,NaN,Inf)
690      nop.i         999
691}
692// cos(+/-0) = 1.0
693// cos(Inf)  = NaN
694// cos(NaN)  = NaN
695{ .mfb
696      nop.m         999
697(p9)  fma.d.s0      f8                  = f8, f0, f1 // cos(+/-0,NaN,Inf)
698      br.ret.sptk   b0 // Exit for x = 0/Inf/NaN path
699};;
700
701_SINCOS_UNORM:
702// Here if x=unorm
703{ .mfb
704      getf.exp      sincos_r_signexp    = sincos_NORM_f8 // Get signexp of x
705      fcmp.eq.s0    p11,p0              = f8, f0  // Dummy op to set denorm flag
706      br.cond.sptk  _SINCOS_COMMON2     // Return to main path
707};;
708
709GLOBAL_IEEE754_END(cos)
710libm_alias_double_other (__cos, cos)
711
712//////////// x >= 2^27 - large arguments routine call ////////////
713LOCAL_LIBM_ENTRY(__libm_callout_sincos)
714_SINCOS_LARGE_ARGS:
715.prologue
716{ .mfi
717      mov           GR_SAVE_r_sincos    = sincos_r_sincos // Save sin or cos
718      nop.f         999
719.save ar.pfs,GR_SAVE_PFS
720      mov           GR_SAVE_PFS         = ar.pfs
721}
722;;
723
724{ .mfi
725      mov           GR_SAVE_GP          = gp
726      nop.f         999
727.save b0, GR_SAVE_B0
728      mov           GR_SAVE_B0          = b0
729}
730
731.body
732{ .mbb
733      setf.sig      sincos_save_tmp     = sincos_GR_all_ones// inexact set
734      nop.b         999
735(p8)  br.call.sptk.many b0              = __libm_sin_large# // sin(large_X)
736
737};;
738
739{ .mbb
740      cmp.ne        p9,p0               = GR_SAVE_r_sincos, r0 // set p9 if cos
741      nop.b         999
742(p9)  br.call.sptk.many b0              = __libm_cos_large# // cos(large_X)
743};;
744
745{ .mfi
746      mov           gp                  = GR_SAVE_GP
747      fma.d.s0      f8                  = f8, f1, f0 // Round result to double
748      mov           b0                  = GR_SAVE_B0
749}
750// Force inexact set
751{ .mfi
752      nop.m         999
753      fmpy.s0       sincos_save_tmp     = sincos_save_tmp, sincos_save_tmp
754      nop.i         999
755};;
756
757{ .mib
758      nop.m         999
759      mov           ar.pfs              = GR_SAVE_PFS
760      br.ret.sptk   b0 // Exit for large arguments routine call
761};;
762
763LOCAL_LIBM_END(__libm_callout_sincos)
764
765.type    __libm_sin_large#,@function
766.global  __libm_sin_large#
767.type    __libm_cos_large#,@function
768.global  __libm_cos_large#
769