s_cospil.c revision 1.1 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
31 #ifdef __i386__
32 #include <ieeefp.h>
33 #endif
34 #include <stdint.h>
35
36 #include "math.h"
37 #include "math_private.h"
38
39 static const double
40 pi_hi = 3.1415926814079285e+00, /* 0x400921fb 0x58000000 */
41 pi_lo =-2.7818135228334233e-08; /* 0xbe5dde97 0x3dcb3b3a */
42
43 #include "k_cospil.h"
44 #include "k_sinpil.h"
45
46 static volatile const double vzero = 0;
47
48 long double
49 cospil(long double x)
50 {
51 long double ax, c;
52 uint64_t lx;
53 uint32_t j0;
54 uint16_t hx, ix;
55
56 EXTRACT_LDBL80_WORDS(hx, lx, x);
57 ix = hx & 0x7fff;
58 INSERT_LDBL80_WORDS(ax, ix, lx);
59
60 ENTERI();
61
62 if (ix < 0x3fff) { /* |x| < 1 */
63 if (ix < 0x3ffd) { /* |x| < 0.25 */
64 if (ix < 0x3fdd) { /* |x| < 0x1p-34 */
65 if ((int)x == 0)
66 RETURNI(1);
67 }
68 RETURNI(__kernel_cospil(ax));
69 }
70
71 if (ix < 0x3ffe) /* |x| < 0.5 */
72 c = __kernel_sinpil(0.5 - ax);
73 else if (lx < 0xc000000000000000ull) { /* |x| < 0.75 */
74 if (ax == 0.5)
75 RETURNI(0);
76 c = -__kernel_sinpil(ax - 0.5);
77 } else
78 c = -__kernel_cospil(1 - ax);
79 RETURNI(c);
80 }
81
82 if (ix < 0x403e) { /* 1 <= |x| < 0x1p63 */
83 FFLOORL80(x, j0, ix, lx); /* Integer part of ax. */
84 ax -= x;
85 EXTRACT_LDBL80_WORDS(ix, lx, ax);
86
87 if (ix < 0x3ffe) { /* |x| < 0.5 */
88 if (ix < 0x3ffd) /* |x| < 0.25 */
89 c = ix == 0 ? 1 : __kernel_cospil(ax);
90 else
91 c = __kernel_sinpil(0.5 - ax);
92
93 } else {
94 if (lx < 0xc000000000000000ull) { /* |x| < 0.75 */
95 if (ax == 0.5)
96 RETURNI(0);
97 c = -__kernel_sinpil(ax - 0.5);
98 } else
99 c = -__kernel_cospil(1 - ax);
100 }
101
102 if (j0 > 40)
103 x -= 0x1p40;
104 if (j0 > 30)
105 x -= 0x1p30;
106 j0 = (uint32_t)x;
107
108 RETURNI(j0 & 1 ? -c : c);
109 }
110
111 if (ix >= 0x7fff)
112 RETURNI(vzero / vzero);
113
114 /*
115 * For 0x1p63 <= |x| < 0x1p64 need to determine if x is an even
116 * or odd integer to return t = +1 or -1.
117 * For |x| >= 0x1p64, it is always an even integer, so t = 1.
118 */
119 RETURNI(ix >= 0x403f ? 1 : ((lx & 1) ? -1 : 1));
120 }
121