Deleted Added
full compact
s_cosl.c (176360) s_cosl.c (221234)
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

--- 11 unchanged lines hidden (view full) ---

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>
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

--- 11 unchanged lines hidden (view full) ---

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: head/lib/msun/src/s_cosl.c 176360 2008-02-17 07:33:12Z das $");
28__FBSDID("$FreeBSD: head/lib/msun/src/s_cosl.c 221234 2011-04-29 23:13:43Z kargl $");
29
30/*
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"
31 * Limited testing on pseudorandom numbers drawn within [-2e8:4e8] shows
32 * an accuracy of <= 0.7412 ULP.
33 */
34
35#include <float.h>
36
37#include "math.h"
38#define INLINE_REM_PIO2L
39#include "math_private.h"
39#include "math_private.h"
40#include "fpmath.h"
41
42#if LDBL_MANT_DIG == 64
40#if LDBL_MANT_DIG == 64
43#define NX 3
44#define PREC 2
41#include "../ld80/e_rem_pio2l.h"
45#elif LDBL_MANT_DIG == 113
42#elif LDBL_MANT_DIG == 113
46#define NX 5
47#define PREC 3
43#include "../ld128/e_rem_pio2l.h"
48#else
49#error "Unsupported long double format"
50#endif
51
44#else
45#error "Unsupported long double format"
46#endif
47
52static const long double two24 = 1.67772160000000000000e+07L;
53
54long double
55cosl(long double x)
56{
57 union IEEEl2bits z;
48long double
49cosl(long double x)
50{
51 union IEEEl2bits z;
58 int i, e0;
59 double xd[NX], yd[PREC];
52 int e0;
53 long double y[2];
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
54 long double hi, lo;
55
56 z.e = x;
57 z.bits.sign = 0;
58
59 /* If x = +-0 or x is a subnormal number, then cos(x) = 1 */
60 if (z.bits.exp == 0)
61 return (1.0);
62
63 /* If x = NaN or Inf, then cos(x) = NaN. */
64 if (z.bits.exp == 32767)
65 return ((x - x) / (x - x));
66
67 /* Optimize the case where x is already within range. */
68 if (z.e < M_PI_4)
69 return (__kernel_cosl(z.e, 0));
70
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);
71 e0 = __ieee754_rem_pio2l(x, y);
72 hi = y[0];
73 lo = y[1];
87
74
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}
75 switch (e0 & 3) {
76 case 0:
77 hi = __kernel_cosl(hi, lo);
78 break;
79 case 1:
80 hi = - __kernel_sinl(hi, lo, 1);
81 break;
82 case 2:
83 hi = - __kernel_cosl(hi, lo);
84 break;
85 case 3:
86 hi = __kernel_sinl(hi, lo, 1);
87 break;
88 }
89
90 return (hi);
91}