1 /*-
2 * Copyright (c) 2017,2023 Steven G. Kargl
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
10 * disclaimer.
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.
14 *
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.
25 */
26
27 /*
28 * See ../src/s_cospi.c for implementation details.
29 */
30 #define INLINE_KERNEL_SINDF
31 #define INLINE_KERNEL_COSDF
32
33 #include "math.h"
34 #include "math_private.h"
35 #include "k_cosf.c"
36 #include "k_sinf.c"
37
38 #define __kernel_cospif(x) (__kernel_cosdf(M_PI * (x)))
39 #define __kernel_sinpif(x) (__kernel_sindf(M_PI * (x)))
40
41 volatile static const float vzero = 0;
42
43 float
cospif(float x)44 cospif(float x)
45 {
46 float ax, c;
47 uint32_t ix, j0;
48
49 GET_FLOAT_WORD(ix, x);
50 ix = ix & 0x7fffffff;
51 SET_FLOAT_WORD(ax, ix);
52
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. */
57 if ((int)ax == 0)
58 return (1);
59 }
60 return (__kernel_cospif(ax));
61 }
62
63 if (ix < 0x3f000000) /* |x| < 0.5 */
64 c = __kernel_sinpif(0.5F - ax);
65 else if (ix < 0x3f400000) { /* |x| < 0.75 */
66 if (ix == 0x3f000000)
67 return (0);
68 c = -__kernel_sinpif(ax - 0.5F);
69 } else
70 c = -__kernel_cospif(1 - ax);
71 return (c);
72 }
73
74 if (ix < 0x4b000000) { /* 1 <= |x| < 0x1p23 */
75 FFLOORF(x, j0, ix); /* Integer part of ax. */
76 ax -= x;
77 GET_FLOAT_WORD(ix, ax);
78
79 if (ix < 0x3f000000) { /* |x| < 0.5 */
80 if (ix < 0x3e800000) /* |x| < 0.25 */
81 c = ix == 0 ? 1 : __kernel_cospif(ax);
82 else
83 c = __kernel_sinpif(0.5F - ax);
84 } else {
85 if (ix < 0x3f400000) { /* |x| < 0.75 */
86 if (ix == 0x3f000000)
87 return (0);
88 c = -__kernel_sinpif(ax - 0.5F);
89 } else
90 c = -__kernel_cospif(1 - ax);
91 }
92
93 j0 = (uint32_t)x;
94 return (j0 & 1 ? -c : c);
95 }
96
97 /* x = +-inf or nan. */
98 if (ix >= 0x7f800000)
99 return (vzero / vzero);
100
101 /*
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.
105 */
106 return (ix < 0x4b800000 ? ((ix & 1) ? -1 : 1) : 1);
107 }
108