xref: /freebsd/lib/msun/src/s_sincosl.c (revision 20d425842a4f80f9efc1de3f7a3b043435fe4b39)
1e1b98d07SMichal Meloun /*-
2e1b98d07SMichal Meloun  * Copyright (c) 2007, 2010-2013 Steven G. Kargl
3e1b98d07SMichal Meloun  * All rights reserved.
4e1b98d07SMichal Meloun  *
5e1b98d07SMichal Meloun  * Redistribution and use in source and binary forms, with or without
6e1b98d07SMichal Meloun  * modification, are permitted provided that the following conditions
7e1b98d07SMichal Meloun  * are met:
8e1b98d07SMichal Meloun  * 1. Redistributions of source code must retain the above copyright
9e1b98d07SMichal Meloun  *    notice unmodified, this list of conditions, and the following
10e1b98d07SMichal Meloun  *    disclaimer.
11e1b98d07SMichal Meloun  * 2. Redistributions in binary form must reproduce the above copyright
12e1b98d07SMichal Meloun  *    notice, this list of conditions and the following disclaimer in the
13e1b98d07SMichal Meloun  *    documentation and/or other materials provided with the distribution.
14e1b98d07SMichal Meloun  *
15e1b98d07SMichal Meloun  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16e1b98d07SMichal Meloun  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17e1b98d07SMichal Meloun  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18e1b98d07SMichal Meloun  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19e1b98d07SMichal Meloun  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20e1b98d07SMichal Meloun  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21e1b98d07SMichal Meloun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22e1b98d07SMichal Meloun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23e1b98d07SMichal Meloun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24e1b98d07SMichal Meloun  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25e1b98d07SMichal Meloun  *
26e1b98d07SMichal Meloun  * s_sinl.c and s_cosl.c merged by Steven G. Kargl.
27e1b98d07SMichal Meloun  */
28e1b98d07SMichal Meloun 
29e1b98d07SMichal Meloun #include <sys/cdefs.h>
30e1b98d07SMichal Meloun __FBSDID("$FreeBSD$");
31e1b98d07SMichal Meloun 
32e1b98d07SMichal Meloun #include <float.h>
33e1b98d07SMichal Meloun #ifdef __i386__
34e1b98d07SMichal Meloun #include <ieeefp.h>
35e1b98d07SMichal Meloun #endif
36e1b98d07SMichal Meloun 
37e1b98d07SMichal Meloun #include "math.h"
38e1b98d07SMichal Meloun #include "math_private.h"
39e1b98d07SMichal Meloun #include "k_sincosl.h"
40e1b98d07SMichal Meloun 
41e1b98d07SMichal Meloun #if LDBL_MANT_DIG == 64
42e1b98d07SMichal Meloun #include "../ld80/e_rem_pio2l.h"
43e1b98d07SMichal Meloun #elif LDBL_MANT_DIG == 113
44e1b98d07SMichal Meloun #include "../ld128/e_rem_pio2l.h"
45e1b98d07SMichal Meloun #else
46e1b98d07SMichal Meloun #error "Unsupported long double format"
47e1b98d07SMichal Meloun #endif
48e1b98d07SMichal Meloun 
49e1b98d07SMichal Meloun void
50e1b98d07SMichal Meloun sincosl(long double x, long double *sn, long double *cs)
51e1b98d07SMichal Meloun {
52e1b98d07SMichal Meloun 	union IEEEl2bits z;
53*20d42584SDimitry Andric 	int e0;
54e1b98d07SMichal Meloun 	long double y[2];
55e1b98d07SMichal Meloun 
56e1b98d07SMichal Meloun 	z.e = x;
57e1b98d07SMichal Meloun 	z.bits.sign = 0;
58e1b98d07SMichal Meloun 
59e1b98d07SMichal Meloun 	ENTERV();
60e1b98d07SMichal Meloun 
61e1b98d07SMichal Meloun 	/* Optimize the case where x is already within range. */
62e1b98d07SMichal Meloun 	if (z.e < M_PI_4) {
63e1b98d07SMichal Meloun 		/*
64e1b98d07SMichal Meloun 		 * If x = +-0 or x is a subnormal number, then sin(x) = x and
65e1b98d07SMichal Meloun 		 * cos(x) = 1.
66e1b98d07SMichal Meloun 		 */
67e1b98d07SMichal Meloun 		if (z.bits.exp == 0) {
68e1b98d07SMichal Meloun 			*sn = x;
69e1b98d07SMichal Meloun 			*cs = 1;
70e1b98d07SMichal Meloun 		} else
71e1b98d07SMichal Meloun 			__kernel_sincosl(x, 0, 0, sn, cs);
72e1b98d07SMichal Meloun 		RETURNV();
73e1b98d07SMichal Meloun 	}
74e1b98d07SMichal Meloun 
75e1b98d07SMichal Meloun 	/* If x = NaN or Inf, then sin(x) and cos(x) are NaN. */
76e1b98d07SMichal Meloun 	if (z.bits.exp == 32767) {
77e1b98d07SMichal Meloun 		*sn = x - x;
78e1b98d07SMichal Meloun 		*cs = x - x;
79e1b98d07SMichal Meloun 		RETURNV();
80e1b98d07SMichal Meloun 	}
81e1b98d07SMichal Meloun 
82e1b98d07SMichal Meloun 	/* Range reduction. */
83e1b98d07SMichal Meloun 	e0 = __ieee754_rem_pio2l(x, y);
84e1b98d07SMichal Meloun 
85e1b98d07SMichal Meloun 	switch (e0 & 3) {
86e1b98d07SMichal Meloun 	case 0:
87e1b98d07SMichal Meloun 		__kernel_sincosl(y[0], y[1], 1, sn, cs);
88e1b98d07SMichal Meloun 		break;
89e1b98d07SMichal Meloun 	case 1:
90e1b98d07SMichal Meloun 		__kernel_sincosl(y[0], y[1], 1, cs, sn);
91e1b98d07SMichal Meloun 		*cs = -*cs;
92e1b98d07SMichal Meloun 		break;
93e1b98d07SMichal Meloun 	case 2:
94e1b98d07SMichal Meloun 		__kernel_sincosl(y[0], y[1], 1, sn, cs);
95e1b98d07SMichal Meloun 		*sn = -*sn;
96e1b98d07SMichal Meloun 		*cs = -*cs;
97e1b98d07SMichal Meloun 		break;
98e1b98d07SMichal Meloun 	default:
99e1b98d07SMichal Meloun 		__kernel_sincosl(y[0], y[1], 1, cs, sn);
100e1b98d07SMichal Meloun 		*sn = -*sn;
101e1b98d07SMichal Meloun 	}
102e1b98d07SMichal Meloun 
103e1b98d07SMichal Meloun 	RETURNV();
104e1b98d07SMichal Meloun }
105