]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/powerpc/powerpc64/power8/fpu/s_cosf.S
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / powerpc / powerpc64 / power8 / fpu / s_cosf.S
CommitLineData
b2980e3c 1/* Optimized cosf(). PowerPC64/POWER8 version.
688903eb 2 Copyright (C) 2017-2018 Free Software Foundation, Inc.
b2980e3c
PC
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19#include <sysdep.h>
20#define _ERRNO_H 1
21#include <bits/errno.h>
216933b2 22#include <libm-alias-float.h>
b2980e3c
PC
23
24#define FRAMESIZE (FRAME_MIN_SIZE+16)
25
26#define FLOAT_EXPONENT_SHIFT 23
27#define FLOAT_EXPONENT_BIAS 127
28#define INTEGER_BITS 3
29
30#define PI_4 0x3f490fdb /* PI/4 */
31#define NINEPI_4 0x40e231d6 /* 9 * PI/4 */
32#define TWO_PN5 0x3d000000 /* 2^-5 */
33#define TWO_PN27 0x32000000 /* 2^-27 */
34#define INFINITY 0x7f800000
35#define TWO_P23 0x4b000000 /* 2^23 */
36#define FX_FRACTION_1_28 0x9249250 /* 0x100000000 / 28 + 1 */
37
38 /* Implements the function
39
40 float [fp1] cosf (float [fp1] x) */
41
42 .machine power8
d5b41185 43ENTRY (__cosf, 4)
b2980e3c
PC
44 addis r9,r2,L(anchor)@toc@ha
45 addi r9,r9,L(anchor)@toc@l
46
47 lis r4,PI_4@h
48 ori r4,r4,PI_4@l
49
50 xscvdpspn v0,v1
51 mfvsrd r8,v0
52 rldicl r3,r8,32,33 /* Remove sign bit. */
53
54 cmpw r3,r4
55 bge L(greater_or_equal_pio4)
56
57 lis r4,TWO_PN5@h
58 ori r4,r4,TWO_PN5@l
59
60 cmpw r3,r4
61 blt L(less_2pn5)
62
63 /* Chebyshev polynomial of the form:
64 * 1.0+x^2*(C0+x^2*(C1+x^2*(C2+x^2*(C3+x^2*C4)))). */
65
66 lfd fp9,(L(C0)-L(anchor))(r9)
67 lfd fp10,(L(C1)-L(anchor))(r9)
68 lfd fp11,(L(C2)-L(anchor))(r9)
69 lfd fp12,(L(C3)-L(anchor))(r9)
70 lfd fp13,(L(C4)-L(anchor))(r9)
71
72 fmul fp2,fp1,fp1 /* x^2 */
73 lfd fp3,(L(DPone)-L(anchor))(r9)
74
75 fmadd fp4,fp2,fp13,fp12 /* C3+x^2*C4 */
76 fmadd fp4,fp2,fp4,fp11 /* C2+x^2*(C3+x^2*C4) */
77 fmadd fp4,fp2,fp4,fp10 /* C1+x^2*(C2+x^2*(C3+x^2*C4)) */
78 fmadd fp4,fp2,fp4,fp9 /* C0+x^2*(C1+x^2*(C2+x^2*(C3+x^2*C4))) */
79 fmadd fp1,fp2,fp4,fp3 /* 1.0+x^2*(C0+x^2*(C1+x^2*(C2+x^2*(C3+x^2*C4)))) */
80 frsp fp1,fp1 /* Round to single precision. */
81
82 blr
83
84 .balign 16
85L(greater_or_equal_pio4):
86 lis r4,NINEPI_4@h
87 ori r4,r4,NINEPI_4@l
88 cmpw r3,r4
89 bge L(greater_or_equal_9pio4)
90
91 /* Calculate quotient of |x|/(PI/4). */
92 lfd fp2,(L(invpio4)-L(anchor))(r9)
93 fabs fp1,fp1 /* |x| */
94 fmul fp2,fp1,fp2 /* |x|/(PI/4) */
95 fctiduz fp2,fp2
96 mfvsrd r3,v2 /* n = |x| mod PI/4 */
97
98 /* Now use that quotient to find |x| mod (PI/2). */
99 addi r7,r3,1
100 rldicr r5,r7,2,60 /* ((n+1) >> 1) << 3 */
101 addi r6,r9,(L(pio2_table)-L(anchor))
102 lfdx fp4,r5,r6
103 fsub fp1,fp1,fp4
104
105 .balign 16
106L(reduced):
107 /* Now we are in the range -PI/4 to PI/4. */
108
109 /* Work out if we are in a positive or negative primary interval. */
110 addi r7,r7,2
111 rldicl r4,r7,62,63 /* ((n+3) >> 2) & 1 */
112
113 /* Load a 1.0 or -1.0. */
114 addi r5,r9,(L(ones)-L(anchor))
115 sldi r4,r4,3
116 lfdx fp0,r4,r5
117
118 /* Are we in the primary interval of sin or cos? */
119 andi. r4,r7,0x2
120 bne L(cos)
121
122 /* Chebyshev polynomial of the form:
123 x+x^3*(S0+x^2*(S1+x^2*(S2+x^2*(S3+x^2*S4)))). */
124
125 lfd fp9,(L(S0)-L(anchor))(r9)
126 lfd fp10,(L(S1)-L(anchor))(r9)
127 lfd fp11,(L(S2)-L(anchor))(r9)
128 lfd fp12,(L(S3)-L(anchor))(r9)
129 lfd fp13,(L(S4)-L(anchor))(r9)
130
131 fmul fp2,fp1,fp1 /* x^2 */
132 fmul fp3,fp2,fp1 /* x^3 */
133
134 fmadd fp4,fp2,fp13,fp12 /* S3+x^2*S4 */
135 fmadd fp4,fp2,fp4,fp11 /* S2+x^2*(S3+x^2*S4) */
136 fmadd fp4,fp2,fp4,fp10 /* S1+x^2*(S2+x^2*(S3+x^2*S4)) */
137 fmadd fp4,fp2,fp4,fp9 /* S0+x^2*(S1+x^2*(S2+x^2*(S3+x^2*S4))) */
138 fmadd fp4,fp3,fp4,fp1 /* x+x^3*(S0+x^2*(S1+x^2*(S2+x^2*(S3+x^2*S4)))) */
139 fmul fp4,fp4,fp0 /* Add in the sign. */
140 frsp fp1,fp4 /* Round to single precision. */
141
142 blr
143
144 .balign 16
145L(cos):
146 /* Chebyshev polynomial of the form:
147 1.0+x^2*(C0+x^2*(C1+x^2*(C2+x^2*(C3+x^2*C4)))). */
148
149 lfd fp9,(L(C0)-L(anchor))(r9)
150 lfd fp10,(L(C1)-L(anchor))(r9)
151 lfd fp11,(L(C2)-L(anchor))(r9)
152 lfd fp12,(L(C3)-L(anchor))(r9)
153 lfd fp13,(L(C4)-L(anchor))(r9)
154
155 fmul fp2,fp1,fp1 /* x^2 */
156 lfd fp3,(L(DPone)-L(anchor))(r9)
157
158 fmadd fp4,fp2,fp13,fp12 /* C3+x^2*C4 */
159 fmadd fp4,fp2,fp4,fp11 /* C2+x^2*(C3+x^2*C4) */
160 fmadd fp4,fp2,fp4,fp10 /* C1+x^2*(C2+x^2*(C3+x^2*C4)) */
161 fmadd fp4,fp2,fp4,fp9 /* C0+x^2*(C1+x^2*(C2+x^2*(C3+x^2*C4))) */
162 fmadd fp4,fp2,fp4,fp3 /* 1.0 + x^2*(C0+x^2*(C1+x^2*(C2+x^2*(C3+x^2*C4)))) */
163 fmul fp4,fp4,fp0 /* Add in the sign. */
164 frsp fp1,fp4 /* Round to single precision. */
165
166 blr
167
168 .balign 16
169L(greater_or_equal_9pio4):
170 lis r4,INFINITY@h
171 ori r4,r4,INFINITY@l
172 cmpw r3,r4
173 bge L(inf_or_nan)
174
175 lis r4,TWO_P23@h
176 ori r4,r4,TWO_P23@l
177 cmpw r3,r4
178 bge L(greater_or_equal_2p23)
179
180 fabs fp1,fp1 /* |x| */
181
182 /* Calculate quotient of |x|/(PI/4). */
183 lfd fp2,(L(invpio4)-L(anchor))(r9)
184
185 lfd fp3,(L(DPone)-L(anchor))(r9)
186 lfd fp4,(L(DPhalf)-L(anchor))(r9)
187 fmul fp2,fp1,fp2 /* |x|/(PI/4) */
188 friz fp2,fp2 /* n = floor(|x|/(PI/4)) */
189
190 /* Calculate (n + 1) / 2. */
191 fadd fp2,fp2,fp3 /* n + 1 */
192 fmul fp3,fp2,fp4 /* (n + 1) / 2 */
193 friz fp3,fp3
194
195 lfd fp4,(L(pio2hi)-L(anchor))(r9)
196 lfd fp5,(L(pio2lo)-L(anchor))(r9)
197
198 fmul fp6,fp4,fp3
199 fadd fp6,fp6,fp1
200 fmadd fp1,fp5,fp3,fp6
201
202 fctiduz fp2,fp2
203 mfvsrd r7,v2 /* n + 1 */
204
205 b L(reduced)
206
207 .balign 16
208L(inf_or_nan):
209 bne L(skip_errno_setting) /* Is a NAN? */
210
211 /* We delayed the creation of the stack frame, as well as the saving of
212 the link register, because only at this point, we are sure that
213 doing so is actually needed. */
214
215 stfd fp1,-8(r1)
216
217 /* Save the link register. */
218 mflr r0
219 std r0,16(r1)
220 cfi_offset(lr, 16)
221
222 /* Create the stack frame. */
223 stdu r1,-FRAMESIZE(r1)
224 cfi_adjust_cfa_offset(FRAMESIZE)
225
226 bl JUMPTARGET(__errno_location)
227 nop
228
229 /* Restore the stack frame. */
230 addi r1,r1,FRAMESIZE
231 cfi_adjust_cfa_offset(-FRAMESIZE)
232 /* Restore the link register. */
233 ld r0,16(r1)
234 mtlr r0
235
236 lfd fp1,-8(r1)
237
238 /* errno = EDOM */
239 li r4,EDOM
240 stw r4,0(r3)
241
242L(skip_errno_setting):
243 fsub fp1,fp1,fp1 /* x - x */
244 blr
245
246 .balign 16
247L(greater_or_equal_2p23):
248 fabs fp1,fp1
249
250 srwi r4,r3,FLOAT_EXPONENT_SHIFT
251 subi r4,r4,FLOAT_EXPONENT_BIAS
252
253 /* We reduce the input modulo pi/4, so we need 3 bits of integer
254 to determine where in 2*pi we are. Index into our array
255 accordingly. */
256 addi r4,r4,INTEGER_BITS
257
258 /* To avoid an expensive divide, for the range we care about (0 - 127)
259 we can transform x/28 into:
260
261 x/28 = (x * ((0x100000000 / 28) + 1)) >> 32
262
263 mulhwu returns the top 32 bits of the 64 bit result, doing the
264 shift for us in the same instruction. The top 32 bits are undefined,
265 so we have to mask them. */
266
267 lis r6,FX_FRACTION_1_28@h
268 ori r6,r6,FX_FRACTION_1_28@l
269 mulhwu r5,r4,r6
270 clrldi r5,r5,32
271
272 /* Get our pointer into the invpio4_table array. */
273 sldi r4,r5,3
274 addi r6,r9,(L(invpio4_table)-L(anchor))
275 add r4,r4,r6
276
277 lfd fp2,0(r4)
278 lfd fp3,8(r4)
279 lfd fp4,16(r4)
280 lfd fp5,24(r4)
281
282 fmul fp6,fp2,fp1
283 fmul fp7,fp3,fp1
284 fmul fp8,fp4,fp1
285 fmul fp9,fp5,fp1
286
287 /* Mask off larger integer bits in highest double word that we don't
288 care about to avoid losing precision when combining with smaller
289 values. */
290 fctiduz fp10,fp6
291 mfvsrd r7,v10
292 rldicr r7,r7,0,(63-INTEGER_BITS)
293 mtvsrd v10,r7
294 fcfidu fp10,fp10 /* Integer bits. */
295
296 fsub fp6,fp6,fp10 /* highest -= integer bits */
297
298 /* Work out the integer component, rounded down. Use the top two
299 limbs for this. */
300 fadd fp10,fp6,fp7 /* highest + higher */
301
302 fctiduz fp10,fp10
303 mfvsrd r7,v10
304 andi. r0,r7,1
305 fcfidu fp10,fp10
306
307 /* Subtract integer component from highest limb. */
308 fsub fp12,fp6,fp10
309
310 beq L(even_integer)
311
312 /* Our integer component is odd, so we are in the -PI/4 to 0 primary
313 region. We need to shift our result down by PI/4, and to do this
314 in the mod (4/PI) space we simply subtract 1. */
315 lfd fp11,(L(DPone)-L(anchor))(r9)
316 fsub fp12,fp12,fp11
317
318 /* Now add up all the limbs in order. */
319 fadd fp12,fp12,fp7
320 fadd fp12,fp12,fp8
321 fadd fp12,fp12,fp9
322
323 /* And finally multiply by pi/4. */
324 lfd fp13,(L(pio4)-L(anchor))(r9)
325 fmul fp1,fp12,fp13
326
327 addi r7,r7,1
328 b L(reduced)
329
330L(even_integer):
331 lfd fp11,(L(DPone)-L(anchor))(r9)
332
333 /* Now add up all the limbs in order. */
334 fadd fp12,fp12,fp7
335 fadd fp12,r12,fp8
336 fadd fp12,r12,fp9
337
338 /* We need to check if the addition of all the limbs resulted in us
339 overflowing 1.0. */
340 fcmpu 0,fp12,fp11
341 bgt L(greater_than_one)
342
343 /* And finally multiply by pi/4. */
344 lfd fp13,(L(pio4)-L(anchor))(r9)
345 fmul fp1,fp12,fp13
346
347 addi r7,r7,1
348 b L(reduced)
349
350L(greater_than_one):
351 /* We did overflow 1.0 when adding up all the limbs. Add 1.0 to our
352 integer, and subtract 1.0 from our result. Since that makes the
353 integer component odd, we need to subtract another 1.0 as
354 explained above. */
355 addi r7,r7,1
356
357 lfd fp11,(L(DPtwo)-L(anchor))(r9)
358 fsub fp12,fp12,fp11
359
360 /* And finally multiply by pi/4. */
361 lfd fp13,(L(pio4)-L(anchor))(r9)
362 fmul fp1,fp12,fp13
363
364 addi r7,r7,1
365 b L(reduced)
366
367 .balign 16
368L(less_2pn5):
369 lis r4,TWO_PN27@h
370 ori r4,r4,TWO_PN27@l
371
372 cmpw r3,r4
373 blt L(less_2pn27)
374
375 /* A simpler Chebyshev approximation is close enough for this range:
376 1.0+x^2*(CC0+x^3*CC1). */
377
378 lfd fp10,(L(CC0)-L(anchor))(r9)
379 lfd fp11,(L(CC1)-L(anchor))(r9)
380
381 fmul fp2,fp1,fp1 /* x^2 */
382 fmul fp3,fp2,fp1 /* x^3 */
383 lfd fp1,(L(DPone)-L(anchor))(r9)
384
385 fmadd fp4,fp3,fp11,fp10 /* CC0+x^3*CC1 */
386 fmadd fp1,fp2,fp4,fp1 /* 1.0+x^2*(CC0+x^3*CC1) */
387
388 frsp fp1,fp1 /* Round to single precision. */
389
390 blr
391
392 .balign 16
393L(less_2pn27):
394 /* Handle some special cases:
395
396 cosf(subnormal) raises inexact
397 cosf(min_normalized) raises inexact
398 cosf(normalized) raises inexact. */
399
400 lfd fp2,(L(DPone)-L(anchor))(r9)
401
402 fabs fp1,fp1 /* |x| */
403 fsub fp1,fp2,fp1 /* 1.0-|x| */
404
405 frsp fp1,fp1
406
407 blr
408
409END (__cosf)
410
411 .section .rodata, "a"
412
413 .balign 8
414
415L(anchor):
416
417 /* Chebyshev constants for sin, range -PI/4 - PI/4. */
418L(S0): .8byte 0xbfc5555555551cd9
419L(S1): .8byte 0x3f81111110c2688b
420L(S2): .8byte 0xbf2a019f8b4bd1f9
421L(S3): .8byte 0x3ec71d7264e6b5b4
422L(S4): .8byte 0xbe5a947e1674b58a
423
424 /* Chebyshev constants for cos, range 2^-27 - 2^-5. */
425L(CC0): .8byte 0xbfdfffffff5cc6fd
426L(CC1): .8byte 0x3fa55514b178dac5
427
428 /* Chebyshev constants for cos, range -PI/4 - PI/4. */
429L(C0): .8byte 0xbfdffffffffe98ae
430L(C1): .8byte 0x3fa55555545c50c7
431L(C2): .8byte 0xbf56c16b348b6874
432L(C3): .8byte 0x3efa00eb9ac43cc0
433L(C4): .8byte 0xbe923c97dd8844d7
434
435L(invpio2):
436 .8byte 0x3fe45f306dc9c883 /* 2/PI */
437
438L(invpio4):
439 .8byte 0x3ff45f306dc9c883 /* 4/PI */
440
441L(invpio4_table):
442 .8byte 0x0000000000000000
443 .8byte 0x3ff45f306c000000
444 .8byte 0x3e3c9c882a000000
445 .8byte 0x3c54fe13a8000000
446 .8byte 0x3aaf47d4d0000000
447 .8byte 0x38fbb81b6c000000
448 .8byte 0x3714acc9e0000000
449 .8byte 0x3560e4107c000000
450 .8byte 0x33bca2c756000000
451 .8byte 0x31fbd778ac000000
452 .8byte 0x300b7246e0000000
453 .8byte 0x2e5d2126e8000000
454 .8byte 0x2c97003248000000
455 .8byte 0x2ad77504e8000000
456 .8byte 0x290921cfe0000000
457 .8byte 0x274deb1cb0000000
458 .8byte 0x25829a73e0000000
459 .8byte 0x23fd1046be000000
460 .8byte 0x2224baed10000000
461 .8byte 0x20709d338e000000
462 .8byte 0x1e535a2f80000000
463 .8byte 0x1cef904e64000000
464 .8byte 0x1b0d639830000000
465 .8byte 0x1964ce7d24000000
466 .8byte 0x17b908bf16000000
467
468L(pio4):
469 .8byte 0x3fe921fb54442d18 /* PI/4 */
470
471/* PI/2 as a sum of two doubles. We only use 32 bits of the upper limb
472 to avoid losing significant bits when multiplying with up to
473 (2^22)/(pi/2). */
474L(pio2hi):
475 .8byte 0xbff921fb54400000
476
477L(pio2lo):
478 .8byte 0xbdd0b4611a626332
479
480L(pio2_table):
481 .8byte 0
482 .8byte 0x3ff921fb54442d18 /* 1 * PI/2 */
483 .8byte 0x400921fb54442d18 /* 2 * PI/2 */
484 .8byte 0x4012d97c7f3321d2 /* 3 * PI/2 */
485 .8byte 0x401921fb54442d18 /* 4 * PI/2 */
486 .8byte 0x401f6a7a2955385e /* 5 * PI/2 */
487 .8byte 0x4022d97c7f3321d2 /* 6 * PI/2 */
488 .8byte 0x4025fdbbe9bba775 /* 7 * PI/2 */
489 .8byte 0x402921fb54442d18 /* 8 * PI/2 */
490 .8byte 0x402c463abeccb2bb /* 9 * PI/2 */
491 .8byte 0x402f6a7a2955385e /* 10 * PI/2 */
492
493L(small):
494 .8byte 0x3cd0000000000000 /* 2^-50 */
495
496L(ones):
497 .8byte 0x3ff0000000000000 /* +1.0 */
498 .8byte 0xbff0000000000000 /* -1.0 */
499
500L(DPhalf):
501 .8byte 0x3fe0000000000000 /* 0.5 */
502
503L(DPone):
504 .8byte 0x3ff0000000000000 /* 1.0 */
505
506L(DPtwo):
507 .8byte 0x4000000000000000 /* 2.0 */
508
216933b2 509libm_alias_float (__cos, cos)