]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/msun/src/s_cosl.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / lib / msun / src / s_cosl.c
1 /*-
2  * Copyright (c) 2007 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 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 /*
31  * Compute cos(x) for x where x is reduced to y = x - k * pi / 2.
32  * Limited testing on pseudorandom numbers drawn within [-2e8:4e8] shows
33  * an accuracy of <= 0.7412 ULP.
34  */
35
36 #include <float.h>
37
38 #include "math.h"
39 #include "math_private.h"
40 #include "fpmath.h"
41
42 #if LDBL_MANT_DIG == 64
43 #define NX      3
44 #define PREC    2
45 #elif LDBL_MANT_DIG == 113
46 #define NX      5
47 #define PREC    3
48 #else
49 #error "Unsupported long double format"
50 #endif
51
52 static const long double two24 = 1.67772160000000000000e+07L;
53
54 long double
55 cosl(long double x)
56 {
57         union IEEEl2bits z;
58         int i, e0;
59         double xd[NX], yd[PREC];
60         long double hi, lo;
61
62         z.e = x;
63         z.bits.sign = 0;
64
65         /* If x = +-0 or x is a subnormal number, then cos(x) = 1 */
66         if (z.bits.exp == 0)
67                 return (1.0);
68
69         /* If x = NaN or Inf, then cos(x) = NaN. */
70         if (z.bits.exp == 32767)
71                 return ((x - x) / (x - x));
72
73         /* Optimize the case where x is already within range. */
74         if (z.e < M_PI_4)
75                 return (__kernel_cosl(z.e, 0));
76
77         /* Split z.e into a 24-bit representation. */
78         e0 = ilogbl(z.e) - 23;
79         z.e = scalbnl(z.e, -e0);
80         for (i = 0; i < NX; i++) {
81                 xd[i] = (double)((int32_t)z.e);
82                 z.e = (z.e - xd[i]) * two24;
83         }
84         
85         /* yd contains the pieces of xd rem pi/2 such that |yd| < pi/4. */
86         e0 = __kernel_rem_pio2(xd, yd, e0, NX, PREC);
87
88 #if PREC == 2
89         hi = (long double)yd[0] + yd[1];
90         lo = yd[1] - (hi - yd[0]);
91 #else /* PREC == 3 */
92         long double t;
93         t = (long double)yd[2] + yd[1];
94         hi = t + yd[0];
95         lo = yd[0] - (hi - t);
96 #endif
97
98         switch (e0 & 3) {
99         case 0:
100             hi = __kernel_cosl(hi, lo);
101             break;
102         case 1:
103             hi = - __kernel_sinl(hi, lo, 1);
104             break;
105         case 2:
106             hi = - __kernel_cosl(hi, lo);
107             break;
108         case 3:
109             hi = __kernel_sinl(hi, lo, 1);
110             break;
111         }
112         
113         return (hi);
114 }