1 /* Optimized sinf(). PowerPC64/POWER8 version.
2 Copyright (C) 2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
21 #include <bits/errno.h>
23 #define FRAMESIZE (FRAME_MIN_SIZE+16)
25 #define FLOAT_EXPONENT_SHIFT 23
26 #define FLOAT_EXPONENT_BIAS 127
27 #define INTEGER_BITS 3
29 #define PI_4 0x3f490fdb /* PI/4 */
30 #define NINEPI_4 0x40e231d6 /* 9 * PI/4 */
31 #define TWO_PN5 0x3d000000 /* 2^-5 */
32 #define TWO_PN27 0x32000000 /* 2^-27 */
33 #define INFINITY 0x7f800000
34 #define TWO_P23 0x4b000000 /* 2^27 */
35 #define FX_FRACTION_1_28 0x9249250 /* 0x100000000 / 28 + 1 */
37 /* Implements the function
39 float [fp1] sinf (float [fp1] x) */
43 addis r9,r2,L(anchor)@toc@ha
44 addi r9,r9,L(anchor)@toc@l
51 rldicl r3,r8,32,33 /* Remove sign bit. */
54 bge L(greater_or_equal_pio4)
62 /* Chebyshev polynomial of the form:
63 * x+x^3*(S0+x^2*(S1+x^2*(S2+x^2*(S3+x^2*S4)))). */
65 lfd fp9,(L(S0)-L(anchor))(r9)
66 lfd fp10,(L(S1)-L(anchor))(r9)
67 lfd fp11,(L(S2)-L(anchor))(r9)
68 lfd fp12,(L(S3)-L(anchor))(r9)
69 lfd fp13,(L(S4)-L(anchor))(r9)
71 fmul fp2,fp1,fp1 /* x^2 */
72 fmul fp3,fp2,fp1 /* x^3 */
74 fmadd fp4,fp2,fp13,fp12 /* S3+x^2*S4 */
75 fmadd fp4,fp2,fp4,fp11 /* S2+x^2*(S3+x^2*S4) */
76 fmadd fp4,fp2,fp4,fp10 /* S1+x^2*(S2+x^2*(S3+x^2*S4)) */
77 fmadd fp4,fp2,fp4,fp9 /* S0+x^2*(S1+x^2*(S2+x^2*(S3+x^2*S4))) */
78 fmadd fp1,fp3,fp4,fp1 /* x+x^3*(S0+x^2*(S1+x^2*(S2+x^2*(S3+x^2*S4)))) */
79 frsp fp1,fp1 /* Round to single precision. */
84 L(greater_or_equal_pio4):
88 bge L(greater_or_equal_9pio4)
90 /* Calculate quotient of |x|/(PI/4). */
91 lfd fp2,(L(invpio4)-L(anchor))(r9)
92 fabs fp1,fp1 /* |x| */
93 fmul fp2,fp1,fp2 /* |x|/(PI/4) */
95 mfvsrd r3,v2 /* n = |x| mod PI/4 */
97 /* Now use that quotient to find |x| mod (PI/2). */
99 rldicr r5,r7,2,60 /* ((n+1) >> 1) << 3 */
100 addi r6,r9,(L(pio2_table)-L(anchor))
106 /* Now we are in the range -PI/4 to PI/4. */
108 /* Work out if we are in a positive or negative primary interval. */
109 rldicl r4,r7,62,63 /* ((n+1) >> 2) & 1 */
111 /* We are operating on |x|, so we need to add back the original
113 rldicl r8,r8,33,63 /* (x >> 31) & 1, ie the sign bit. */
114 xor r4,r4,r8 /* 0 if result should be positive,
117 /* Load a 1.0 or -1.0. */
118 addi r5,r9,(L(ones)-L(anchor))
122 /* Are we in the primary interval of sin or cos? */
126 /* Chebyshev polynomial of the form:
127 x+x^3*(S0+x^2*(S1+x^2*(S2+x^2*(S3+x^2*S4)))). */
129 lfd fp9,(L(S0)-L(anchor))(r9)
130 lfd fp10,(L(S1)-L(anchor))(r9)
131 lfd fp11,(L(S2)-L(anchor))(r9)
132 lfd fp12,(L(S3)-L(anchor))(r9)
133 lfd fp13,(L(S4)-L(anchor))(r9)
135 fmul fp2,fp1,fp1 /* x^2 */
136 fmul fp3,fp2,fp1 /* x^3 */
138 fmadd fp4,fp2,fp13,fp12 /* S3+x^2*S4 */
139 fmadd fp4,fp2,fp4,fp11 /* S2+x^2*(S3+x^2*S4) */
140 fmadd fp4,fp2,fp4,fp10 /* S1+x^2*(S2+x^2*(S3+x^2*S4)) */
141 fmadd fp4,fp2,fp4,fp9 /* S0+x^2*(S1+x^2*(S2+x^2*(S3+x^2*S4))) */
142 fmadd fp4,fp3,fp4,fp1 /* x+x^3*(S0+x^2*(S1+x^2*(S2+x^2*(S3+x^2*S4)))) */
143 fmul fp4,fp4,fp0 /* Add in the sign. */
144 frsp fp1,fp4 /* Round to single precision. */
150 /* Chebyshev polynomial of the form:
151 1.0+x^2*(C0+x^2*(C1+x^2*(C2+x^2*(C3+x^2*C4)))). */
153 lfd fp9,(L(C0)-L(anchor))(r9)
154 lfd fp10,(L(C1)-L(anchor))(r9)
155 lfd fp11,(L(C2)-L(anchor))(r9)
156 lfd fp12,(L(C3)-L(anchor))(r9)
157 lfd fp13,(L(C4)-L(anchor))(r9)
159 fmul fp2,fp1,fp1 /* x^2 */
160 lfd fp3,(L(DPone)-L(anchor))(r9)
162 fmadd fp4,fp2,fp13,fp12 /* C3+x^2*C4 */
163 fmadd fp4,fp2,fp4,fp11 /* C2+x^2*(C3+x^2*C4) */
164 fmadd fp4,fp2,fp4,fp10 /* C1+x^2*(C2+x^2*(C3+x^2*C4)) */
165 fmadd fp4,fp2,fp4,fp9 /* C0+x^2*(C1+x^2*(C2+x^2*(C3+x^2*C4))) */
166 fmadd fp4,fp2,fp4,fp3 /* 1.0 + x^2*(C0+x^2*(C1+x^2*(C2+x^2*(C3+x^2*C4)))) */
167 fmul fp4,fp4,fp0 /* Add in the sign. */
168 frsp fp1,fp4 /* Round to single precision. */
173 L(greater_or_equal_9pio4):
182 bge L(greater_or_equal_2p23)
184 fabs fp1,fp1 /* |x| */
186 /* Calculate quotient of |x|/(PI/4). */
187 lfd fp2,(L(invpio4)-L(anchor))(r9)
189 lfd fp3,(L(DPone)-L(anchor))(r9)
190 lfd fp4,(L(DPhalf)-L(anchor))(r9)
191 fmul fp2,fp1,fp2 /* |x|/(PI/4) */
192 friz fp2,fp2 /* n = floor(|x|/(PI/4)) */
194 /* Calculate (n + 1) / 2. */
195 fadd fp2,fp2,fp3 /* n + 1 */
196 fmul fp3,fp2,fp4 /* (n + 1) / 2 */
199 lfd fp4,(L(pio2hi)-L(anchor))(r9)
200 lfd fp5,(L(pio2lo)-L(anchor))(r9)
204 fmadd fp1,fp5,fp3,fp6
207 mfvsrd r7,v2 /* n + 1 */
213 bne L(skip_errno_setting) /* Is a NAN? */
215 /* We delayed the creation of the stack frame, as well as the saving of
216 the link register, because only at this point, we are sure that
217 doing so is actually needed. */
221 /* Save the link register. */
226 /* Create the stack frame. */
227 stdu r1,-FRAMESIZE(r1)
228 cfi_adjust_cfa_offset(FRAMESIZE)
230 bl JUMPTARGET(__errno_location)
233 /* Restore the stack frame. */
235 cfi_adjust_cfa_offset(-FRAMESIZE)
236 /* Restore the link register. */
246 L(skip_errno_setting):
247 fsub fp1,fp1,fp1 /* x - x */
251 L(greater_or_equal_2p23):
254 srwi r4,r3,FLOAT_EXPONENT_SHIFT
255 subi r4,r4,FLOAT_EXPONENT_BIAS
257 /* We reduce the input modulo pi/4, so we need 3 bits of integer
258 to determine where in 2*pi we are. Index into our array
260 addi r4,r4,INTEGER_BITS
262 /* To avoid an expensive divide, for the range we care about (0 - 127)
263 we can transform x/28 into:
265 x/28 = (x * ((0x100000000 / 28) + 1)) >> 32
267 mulhwu returns the top 32 bits of the 64 bit result, doing the
268 shift for us in the same instruction. The top 32 bits are undefined,
269 so we have to mask them. */
271 lis r6,FX_FRACTION_1_28@h
272 ori r6,r6,FX_FRACTION_1_28@l
276 /* Get our pointer into the invpio4_table array. */
278 addi r6,r9,(L(invpio4_table)-L(anchor))
291 /* Mask off larger integer bits in highest double word that we don't
292 care about to avoid losing precision when combining with smaller
296 rldicr r7,r7,0,(63-INTEGER_BITS)
298 fcfidu fp10,fp10 /* Integer bits. */
300 fsub fp6,fp6,fp10 /* highest -= integer bits */
302 /* Work out the integer component, rounded down. Use the top two
304 fadd fp10,fp6,fp7 /* highest + higher */
311 /* Subtract integer component from highest limb. */
316 /* Our integer component is odd, so we are in the -PI/4 to 0 primary
317 region. We need to shift our result down by PI/4, and to do this
318 in the mod (4/PI) space we simply subtract 1. */
319 lfd fp11,(L(DPone)-L(anchor))(r9)
322 /* Now add up all the limbs in order. */
327 /* And finally multiply by pi/4. */
328 lfd fp13,(L(pio4)-L(anchor))(r9)
335 lfd fp11,(L(DPone)-L(anchor))(r9)
337 /* Now add up all the limbs in order. */
342 /* We need to check if the addition of all the limbs resulted in us
345 bgt L(greater_than_one)
347 /* And finally multiply by pi/4. */
348 lfd fp13,(L(pio4)-L(anchor))(r9)
355 /* We did overflow 1.0 when adding up all the limbs. Add 1.0 to our
356 integer, and subtract 1.0 from our result. Since that makes the
357 integer component odd, we need to subtract another 1.0 as
361 lfd fp11,(L(DPtwo)-L(anchor))(r9)
364 /* And finally multiply by pi/4. */
365 lfd fp13,(L(pio4)-L(anchor))(r9)
379 /* A simpler Chebyshev approximation is close enough for this range:
380 x+x^3*(SS0+x^2*SS1). */
382 lfd fp10,(L(SS0)-L(anchor))(r9)
383 lfd fp11,(L(SS1)-L(anchor))(r9)
385 fmul fp2,fp1,fp1 /* x^2 */
386 fmul fp3,fp2,fp1 /* x^3 */
388 fmadd fp4,fp2,fp11,fp10 /* SS0+x^2*SS1 */
389 fmadd fp1,fp3,fp4,fp1 /* x+x^3*(SS0+x^2*SS1) */
391 frsp fp1,fp1 /* Round to single precision. */
400 /* Handle some special cases:
402 sinf(subnormal) raises inexact/underflow
403 sinf(min_normalized) raises inexact/underflow
404 sinf(normalized) raises inexact. */
406 lfd fp2,(L(small)-L(anchor))(r9)
408 fmul fp2,fp1,fp2 /* x * small */
409 fsub fp1,fp1,fp2 /* x - x * small */
421 .section .rodata, "a"
427 /* Chebyshev constants for sin, range -PI/4 - PI/4. */
428 L(S0): .8byte 0xbfc5555555551cd9
429 L(S1): .8byte 0x3f81111110c2688b
430 L(S2): .8byte 0xbf2a019f8b4bd1f9
431 L(S3): .8byte 0x3ec71d7264e6b5b4
432 L(S4): .8byte 0xbe5a947e1674b58a
434 /* Chebyshev constants for sin, range 2^-27 - 2^-5. */
435 L(SS0): .8byte 0xbfc555555543d49d
436 L(SS1): .8byte 0x3f8110f475cec8c5
438 /* Chebyshev constants for cos, range -PI/4 - PI/4. */
439 L(C0): .8byte 0xbfdffffffffe98ae
440 L(C1): .8byte 0x3fa55555545c50c7
441 L(C2): .8byte 0xbf56c16b348b6874
442 L(C3): .8byte 0x3efa00eb9ac43cc0
443 L(C4): .8byte 0xbe923c97dd8844d7
446 .8byte 0x3fe45f306dc9c883 /* 2/PI */
449 .8byte 0x3ff45f306dc9c883 /* 4/PI */
452 .8byte 0x0000000000000000
453 .8byte 0x3ff45f306c000000
454 .8byte 0x3e3c9c882a000000
455 .8byte 0x3c54fe13a8000000
456 .8byte 0x3aaf47d4d0000000
457 .8byte 0x38fbb81b6c000000
458 .8byte 0x3714acc9e0000000
459 .8byte 0x3560e4107c000000
460 .8byte 0x33bca2c756000000
461 .8byte 0x31fbd778ac000000
462 .8byte 0x300b7246e0000000
463 .8byte 0x2e5d2126e8000000
464 .8byte 0x2c97003248000000
465 .8byte 0x2ad77504e8000000
466 .8byte 0x290921cfe0000000
467 .8byte 0x274deb1cb0000000
468 .8byte 0x25829a73e0000000
469 .8byte 0x23fd1046be000000
470 .8byte 0x2224baed10000000
471 .8byte 0x20709d338e000000
472 .8byte 0x1e535a2f80000000
473 .8byte 0x1cef904e64000000
474 .8byte 0x1b0d639830000000
475 .8byte 0x1964ce7d24000000
476 .8byte 0x17b908bf16000000
479 .8byte 0x3fe921fb54442d18 /* PI/4 */
481 /* PI/2 as a sum of two doubles. We only use 32 bits of the upper limb
482 to avoid losing significant bits when multiplying with up to
485 .8byte 0xbff921fb54400000
488 .8byte 0xbdd0b4611a626332
492 .8byte 0x3ff921fb54442d18 /* 1 * PI/2 */
493 .8byte 0x400921fb54442d18 /* 2 * PI/2 */
494 .8byte 0x4012d97c7f3321d2 /* 3 * PI/2 */
495 .8byte 0x401921fb54442d18 /* 4 * PI/2 */
496 .8byte 0x401f6a7a2955385e /* 5 * PI/2 */
497 .8byte 0x4022d97c7f3321d2 /* 6 * PI/2 */
498 .8byte 0x4025fdbbe9bba775 /* 7 * PI/2 */
499 .8byte 0x402921fb54442d18 /* 8 * PI/2 */
500 .8byte 0x402c463abeccb2bb /* 9 * PI/2 */
501 .8byte 0x402f6a7a2955385e /* 10 * PI/2 */
504 .8byte 0x3cd0000000000000 /* 2^-50 */
507 .8byte 0x3ff0000000000000 /* +1.0 */
508 .8byte 0xbff0000000000000 /* -1.0 */
511 .8byte 0x3fe0000000000000 /* 0.5 */
514 .8byte 0x3ff0000000000000 /* 1.0 */
517 .8byte 0x4000000000000000 /* 2.0 */
519 weak_alias(__sinf, sinf)