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