2 * Copyright (c) 2017,2023 Steven G. Kargl
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * See ../src/s_cospi.c for implementation details.
30 #define INLINE_KERNEL_SINDF
31 #define INLINE_KERNEL_COSDF
34 #include "math_private.h"
38 #define __kernel_cospif(x) (__kernel_cosdf(M_PI * (x)))
39 #define __kernel_sinpif(x) (__kernel_sindf(M_PI * (x)))
41 volatile static const float vzero = 0;
49 GET_FLOAT_WORD(ix, x);
51 SET_FLOAT_WORD(ax, ix);
53 if (ix < 0x3f800000) { /* |x| < 1 */
54 if (ix < 0x3e800000) { /* |x| < 0.25 */
55 if (ix < 0x38800000) { /* |x| < 0x1p-14 */
56 /* Raise inexact iff != 0. */
60 return (__kernel_cospif(ax));
63 if (ix < 0x3f000000) /* |x| < 0.5 */
64 c = __kernel_sinpif(0.5F - ax);
65 else if (ix < 0x3f400000) { /* |x| < 0.75 */
68 c = -__kernel_sinpif(ax - 0.5F);
70 c = -__kernel_cospif(1 - ax);
74 if (ix < 0x4b000000) { /* 1 <= |x| < 0x1p23 */
75 FFLOORF(x, j0, ix); /* Integer part of ax. */
77 GET_FLOAT_WORD(ix, ax);
79 if (ix < 0x3f000000) { /* |x| < 0.5 */
80 if (ix < 0x3e800000) /* |x| < 0.25 */
81 c = ix == 0 ? 1 : __kernel_cospif(ax);
83 c = __kernel_sinpif(0.5F - ax);
85 if (ix < 0x3f400000) { /* |x| < 0.75 */
88 c = -__kernel_sinpif(ax - 0.5F);
90 c = -__kernel_cospif(1 - ax);
94 return (j0 & 1 ? -c : c);
97 /* x = +-inf or nan. */
99 return (vzero / vzero);
102 * For 0x1p23 <= |x| < 0x1p24 need to determine if x is an even
103 * or odd integer to return +1 or -1.
104 * For |x| >= 0x1p24, it is always an even integer, so return 1.
106 return (ix < 0x4b800000 ? ((ix & 1) ? -1 : 1) : 1);