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 * sinpi(x) computes sin(pi*x) without multiplication by pi (almost). First, 29 * note that sinpi(-x) = -sinpi(x), so the algorithm considers only |x| and 30 * includes reflection symmetry by considering the sign of x on output. The 31 * method used depends on the magnitude of x. 32 * 33 * 1. For small |x|, sinpi(x) = pi * x where a sloppy threshold is used. The 34 * threshold is |x| < 0x1pN with N = -(P/2+M). P is the precision of the 35 * floating-point type and M = 2 to 4. To achieve high accuracy, pi is 36 * decomposed into high and low parts with the high part containing a 37 * number of trailing zero bits. x is also split into high and low parts. 38 * 39 * 2. For |x| < 1, argument reduction is not required and sinpi(x) is 40 * computed by calling a kernel that leverages the kernels for sin(x) 41 * ans cos(x). See k_sinpi.c and k_cospi.c for details. 42 * 43 * 3. For 1 <= |x| < 0x1p(P-1), argument reduction is required where 44 * |x| = j0 + r with j0 an integer and the remainder r satisfies 45 * 0 <= r < 1. With the given domain, a simplified inline floor(x) 46 * is used. Also, note the following identity 47 * 48 * sinpi(x) = sin(pi*(j0+r)) 49 * = sin(pi*j0) * cos(pi*r) + cos(pi*j0) * sin(pi*r) 50 * = cos(pi*j0) * sin(pi*r) 51 * = +-sinpi(r) 52 * 53 * If j0 is even, then cos(pi*j0) = 1. If j0 is odd, then cos(pi*j0) = -1. 54 * sinpi(r) is then computed via an appropriate kernel. 55 * 56 * 4. For |x| >= 0x1p(P-1), |x| is integral and sinpi(x) = copysign(0,x). 57 * 58 * 5. Special cases: 59 * 60 * sinpi(+-0) = +-0 61 * sinpi(+-n) = +-0, for positive integers n. 62 * sinpi(+-inf) = nan. Raises the "invalid" floating-point exception. 63 * sinpi(nan) = nan. Raises the "invalid" floating-point exception. 64 */ 65 66 #include <sys/cdefs.h> 67 68 #include "namespace.h" 69 __weak_alias(sinpi, _sinpi) 70 #include <float.h> 71 #include "math.h" 72 #include "math_private.h" 73 74 static const double 75 pi_hi = 3.1415926814079285e+00, /* 0x400921fb 0x58000000 */ 76 pi_lo =-2.7818135228334233e-08; /* 0xbe5dde97 0x3dcb3b3a */ 77 78 #include "k_cospi.h" 79 #include "k_sinpi.h" 80 81 static volatile const double vzero = 0; 82 83 double 84 sinpi(double x) 85 { 86 double ax, hi, lo, s; 87 uint32_t hx, ix, j0, lx; 88 89 EXTRACT_WORDS(hx, lx, x); 90 ix = hx & 0x7fffffff; 91 INSERT_WORDS(ax, ix, lx); 92 93 if (ix < 0x3ff00000) { /* |x| < 1 */ 94 if (ix < 0x3fd00000) { /* |x| < 0.25 */ 95 if (ix < 0x3e200000) { /* |x| < 0x1p-29 */ 96 if (x == 0) 97 return (x); 98 /* 99 * To avoid issues with subnormal values, 100 * scale the computation and rescale on 101 * return. 102 */ 103 INSERT_WORDS(hi, hx, 0); 104 hi *= 0x1p53; 105 lo = x * 0x1p53 - hi; 106 s = (pi_lo + pi_hi) * lo + pi_lo * hi + 107 pi_hi * hi; 108 return (s * 0x1p-53); 109 } 110 111 s = __kernel_sinpi(ax); 112 return ((hx & 0x80000000) ? -s : s); 113 } 114 115 if (ix < 0x3fe00000) /* |x| < 0.5 */ 116 s = __kernel_cospi(0.5 - ax); 117 else if (ix < 0x3fe80000) /* |x| < 0.75 */ 118 s = __kernel_cospi(ax - 0.5); 119 else 120 s = __kernel_sinpi(1 - ax); 121 return ((hx & 0x80000000) ? -s : s); 122 } 123 124 if (ix < 0x43300000) { /* 1 <= |x| < 0x1p52 */ 125 FFLOOR(x, j0, ix, lx); /* Integer part of ax. */ 126 ax -= x; 127 EXTRACT_WORDS(ix, lx, ax); 128 129 if (ix == 0) 130 s = 0; 131 else { 132 if (ix < 0x3fe00000) { /* |x| < 0.5 */ 133 if (ix < 0x3fd00000) /* |x| < 0.25 */ 134 s = __kernel_sinpi(ax); 135 else 136 s = __kernel_cospi(0.5 - ax); 137 } else { 138 if (ix < 0x3fe80000) /* |x| < 0.75 */ 139 s = __kernel_cospi(ax - 0.5); 140 else 141 s = __kernel_sinpi(1 - ax); 142 } 143 144 if (j0 > 30) 145 x -= 0x1p30; 146 j0 = (uint32_t)x; 147 if (j0 & 1) s = -s; 148 } 149 150 return ((hx & 0x80000000) ? -s : s); 151 } 152 153 /* x = +-inf or nan. */ 154 if (ix >= 0x7ff00000) 155 return (vzero / vzero); 156 157 /* 158 * |x| >= 0x1p52 is always an integer, so return +-0. 159 */ 160 return (copysign(0, x)); 161 } 162