1 /*- 2 * Copyright (c) 2007, 2010-2013 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 * s_sinl.c and s_cosl.c merged by Steven G. Kargl. 27 */ 28 29 #include <sys/types.h> 30 #include <machine/ieee.h> 31 #include <float.h> 32 #include <math.h> 33 34 #include "math_private.h" 35 36 #if LDBL_MANT_DIG == 64 37 #include "../ld80/k_sincosl.h" 38 #define NX 3 39 #define PREC 2 40 #elif LDBL_MANT_DIG == 113 41 #include "../ld128/k_sincosl.h" 42 #define NX 5 43 #define PREC 3 44 #else 45 #error "Unsupported long double format" 46 #endif 47 48 static const long double two24 = 1.67772160000000000000e+07L; 49 50 void 51 sincosl(long double x, long double *sn, long double *cs) 52 { 53 union { 54 long double e; 55 struct ieee_ext bits; 56 } z; 57 int i, e0; 58 double xd[NX], yd[PREC]; 59 long double hi, lo; 60 61 z.e = x; 62 z.bits.ext_sign = 0; 63 64 /* Optimize the case where x is already within range. */ 65 if (z.e < M_PI_4) { 66 /* 67 * If x = +-0 or x is a subnormal number, then sin(x) = x and 68 * cos(x) = 1. 69 */ 70 if (z.bits.ext_exp == 0) { 71 *sn = x; 72 *cs = 1; 73 } else 74 __kernel_sincosl(x, 0, 0, sn, cs); 75 return; 76 } 77 78 /* If x = NaN or Inf, then sin(x) and cos(x) are NaN. */ 79 if (z.bits.ext_exp == 32767) { 80 *sn = x - x; 81 *cs = x - x; 82 return; 83 } 84 85 /* Split z.e into a 24-bit representation. */ 86 e0 = ilogbl(z.e) - 23; 87 z.e = scalbnl(z.e, -e0); 88 for (i = 0; i < NX; i++) { 89 xd[i] = (double)((int32_t)z.e); 90 z.e = (z.e - xd[i]) * two24; 91 } 92 93 /* yd contains the pieces of xd rem pi/2 such that |yd| < pi/4. */ 94 e0 = __kernel_rem_pio2(xd, yd, e0, NX, PREC); 95 96 #if PREC == 2 97 hi = (long double)yd[0] + yd[1]; 98 lo = yd[1] - (hi - yd[0]); 99 #else /* PREC == 3 */ 100 long double t; 101 t = (long double)yd[2] + yd[1]; 102 hi = t + yd[0]; 103 lo = yd[0] - (hi - t); 104 #endif 105 106 switch (e0 & 3) { 107 case 0: 108 __kernel_sincosl(hi, lo, 1, sn, cs); 109 break; 110 case 1: 111 __kernel_sincosl(hi, lo, 1, cs, sn); 112 *cs = -*cs; 113 break; 114 case 2: 115 __kernel_sincosl(hi, lo, 1, sn, cs); 116 *sn = -*sn; 117 *cs = -*cs; 118 break; 119 default: 120 __kernel_sincosl(hi, lo, 1, cs, sn); 121 *sn = -*sn; 122 } 123 } 124