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