xref: /netbsd-src/lib/libm/src/s_sincosl.c (revision 4c3eb207d36f67d31994830c0a694161fc1ca39b)
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 #if 0
31 __FBSDID("$FreeBSD: head/lib/msun/src/s_sincosl.c 319047 2017-05-28 06:13:38Z mmel $");
32 #endif
33 #if defined(LIBM_SCCS) && !defined(lint)
34 __RCSID("$NetBSD: s_sincosl.c,v 1.2 2022/08/27 17:15:52 christos Exp $");
35 #endif
36 
37 #include "namespace.h"
38 #include <float.h>
39 #ifdef __i386__
40 #include <ieeefp.h>
41 #endif
42 
43 #include "math.h"
44 #include "math_private.h"
45 
46 #ifdef __HAVE_LONG_DOUBLE
47 
48 #include "k_sincosl.h"
49 
50 #if LDBL_MANT_DIG == 64
51 #include "../ld80/e_rem_pio2l.h"
52 #elif LDBL_MANT_DIG == 113
53 #include "../ld128/e_rem_pio2l.h"
54 #else
55 #error "Unsupported long double format"
56 #endif
57 
58 #ifdef __weak_alias
59 __weak_alias(sincosl,_sincosl)
60 #endif
61 
62 void
63 sincosl(long double x, long double *sn, long double *cs)
64 {
65 	union ieee_ext_u z;
66 	int e0;
67 	long double y[2];
68 
69 	z.extu_ld = x;
70 	z.extu_sign = 0;
71 
72 	ENTERV();
73 
74 	/* Optimize the case where x is already within range. */
75 	if (z.extu_ld < M_PI_4) {
76 		/*
77 		 * If x = +-0 or x is a subnormal number, then sin(x) = x and
78 		 * cos(x) = 1.
79 		 */
80 		if (z.extu_exp == 0) {
81 			*sn = x;
82 			*cs = 1;
83 		} else
84 			__kernel_sincosl(x, 0, 0, sn, cs);
85 		RETURNV();
86 	}
87 
88 	/* If x = NaN or Inf, then sin(x) and cos(x) are NaN. */
89 	if (z.extu_exp == 32767) {
90 		*sn = x - x;
91 		*cs = x - x;
92 		RETURNV();
93 	}
94 
95 	/* Range reduction. */
96 	e0 = __ieee754_rem_pio2l(x, y);
97 
98 	switch (e0 & 3) {
99 	case 0:
100 		__kernel_sincosl(y[0], y[1], 1, sn, cs);
101 		break;
102 	case 1:
103 		__kernel_sincosl(y[0], y[1], 1, cs, sn);
104 		*cs = -*cs;
105 		break;
106 	case 2:
107 		__kernel_sincosl(y[0], y[1], 1, sn, cs);
108 		*sn = -*sn;
109 		*cs = -*cs;
110 		break;
111 	default:
112 		__kernel_sincosl(y[0], y[1], 1, cs, sn);
113 		*sn = -*sn;
114 	}
115 
116 	RETURNV();
117 }
118 #endif
119