xref: /freebsd/lib/msun/src/s_sincosl.c (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
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/cdefs.h>
30 #include <float.h>
31 #ifdef __i386__
32 #include <ieeefp.h>
33 #endif
34 
35 #include "math.h"
36 #include "math_private.h"
37 #include "k_sincosl.h"
38 
39 #if LDBL_MANT_DIG == 64
40 #include "../ld80/e_rem_pio2l.h"
41 #elif LDBL_MANT_DIG == 113
42 #include "../ld128/e_rem_pio2l.h"
43 #else
44 #error "Unsupported long double format"
45 #endif
46 
47 void
48 sincosl(long double x, long double *sn, long double *cs)
49 {
50 	union IEEEl2bits z;
51 	int e0;
52 	long double y[2];
53 
54 	z.e = x;
55 	z.bits.sign = 0;
56 
57 	ENTERV();
58 
59 	/* Optimize the case where x is already within range. */
60 	if (z.e < M_PI_4) {
61 		/*
62 		 * If x = +-0 or x is a subnormal number, then sin(x) = x and
63 		 * cos(x) = 1.
64 		 */
65 		if (z.bits.exp == 0) {
66 			*sn = x;
67 			*cs = 1;
68 		} else
69 			__kernel_sincosl(x, 0, 0, sn, cs);
70 		RETURNV();
71 	}
72 
73 	/* If x = NaN or Inf, then sin(x) and cos(x) are NaN. */
74 	if (z.bits.exp == 32767) {
75 		*sn = x - x;
76 		*cs = x - x;
77 		RETURNV();
78 	}
79 
80 	/* Range reduction. */
81 	e0 = __ieee754_rem_pio2l(x, y);
82 
83 	switch (e0 & 3) {
84 	case 0:
85 		__kernel_sincosl(y[0], y[1], 1, sn, cs);
86 		break;
87 	case 1:
88 		__kernel_sincosl(y[0], y[1], 1, cs, sn);
89 		*cs = -*cs;
90 		break;
91 	case 2:
92 		__kernel_sincosl(y[0], y[1], 1, sn, cs);
93 		*sn = -*sn;
94 		*cs = -*cs;
95 		break;
96 	default:
97 		__kernel_sincosl(y[0], y[1], 1, cs, sn);
98 		*sn = -*sn;
99 	}
100 
101 	RETURNV();
102 }
103