1 /* $OpenBSD: s_cosl.c,v 1.1 2008/12/09 20:00:35 martynas Exp $ */
3 * Copyright (c) 2007 Steven G. Kargl
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Compute cos(x) for x where x is reduced to y = x - k * pi / 2.
30 * Limited testing on pseudorandom numbers drawn within [-2e8:4e8] shows
31 * an accuracy of <= 0.7412 ULP.
34 #include <sys/types.h>
35 #include <machine/ieee.h>
39 #include "math_private.h"
41 #if LDBL_MANT_DIG == 64
44 #elif LDBL_MANT_DIG == 113
48 #error "Unsupported long double format"
51 static const long double two24 = 1.67772160000000000000e+07L;
61 double xd[NX], yd[PREC];
67 /* If x = +-0 or x is a subnormal number, then cos(x) = 1 */
68 if (z.bits.ext_exp == 0)
71 /* If x = NaN or Inf, then cos(x) = NaN. */
72 if (z.bits.ext_exp == 32767)
73 return ((x - x) / (x - x));
75 /* Optimize the case where x is already within range. */
77 return (__kernel_cosl(z.e, 0));
79 /* Split z.e into a 24-bit representation. */
80 e0 = ilogbl(z.e) - 23;
81 z.e = scalbnl(z.e, -e0);
82 for (i = 0; i < NX; i++) {
83 xd[i] = (double)((int32_t)z.e);
84 z.e = (z.e - xd[i]) * two24;
87 /* yd contains the pieces of xd rem pi/2 such that |yd| < pi/4. */
88 e0 = __kernel_rem_pio2(xd, yd, e0, NX, PREC);
91 hi = (long double)yd[0] + yd[1];
92 lo = yd[1] - (hi - yd[0]);
95 t = (long double)yd[2] + yd[1];
97 lo = yd[0] - (hi - t);
102 hi = __kernel_cosl(hi, lo);
105 hi = - __kernel_sinl(hi, lo, 1);
108 hi = - __kernel_cosl(hi, lo);
111 hi = __kernel_sinl(hi, lo, 1);