xref: /netbsd-src/lib/libm/src/s_sinl.c (revision 345cf9fb81bd0411c53e25d62cd93bdcaa865312)
1 /*	$NetBSD: s_sinl.c,v 1.2 2024/01/26 12:32:49 ryoon Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2007 Steven G. Kargl
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice unmodified, this list of conditions, and the following
14  *    disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: s_sinl.c,v 1.2 2024/01/26 12:32:49 ryoon Exp $");
33 
34 #include "namespace.h"
35 #include <float.h>
36 #ifdef __i386__
37 #include <ieeefp.h>
38 #endif
39 
40 #ifdef __weak_alias
41 __weak_alias(sinl, _sinl)
42 #endif
43 
44 #include "math.h"
45 #include "math_private.h"
46 
47 #ifdef __HAVE_LONG_DOUBLE
48 
49 #if LDBL_MANT_DIG == 64
50 #include "../ld80/e_rem_pio2l.h"
51 #include "../ld80/k_sinl.c"
52 #elif LDBL_MANT_DIG == 113
53 #include "../ld128/e_rem_pio2l.h"
54 #include "../ld128/k_sinl.c"
55 #else
56 #error "Unsupported long double format"
57 #endif
58 
59 long double
60 sinl(long double x)
61 {
62 	union ieee_ext_u z;
63 	int e0, s;
64 	long double y[2];
65 	long double hi, lo;
66 
67 	z.extu_ld = x;
68 	s = z.extu_sign;
69 	z.extu_sign = 0;
70 
71 	/* If x = +-0 or x is a subnormal number, then sin(x) = x */
72 	if (z.extu_exp == 0)
73 		return (x);
74 
75 	/* If x = NaN or Inf, then sin(x) = NaN. */
76 	if (z.extu_exp == 32767)
77 		return ((x - x) / (x - x));
78 
79 	ENTERI();
80 
81 	/* Optimize the case where x is already within range. */
82 	if (z.extu_ld < M_PI_4) {
83 		hi = __kernel_sinl(z.extu_ld, 0, 0);
84 		RETURNI(s ? -hi : hi);
85 	}
86 
87 	e0 = __ieee754_rem_pio2l(x, y);
88 	hi = y[0];
89 	lo = y[1];
90 
91 	switch (e0 & 3) {
92 	case 0:
93 	    hi = __kernel_sinl(hi, lo, 1);
94 	    break;
95 	case 1:
96 	    hi = __kernel_cosl(hi, lo);
97 	    break;
98 	case 2:
99 	    hi = - __kernel_sinl(hi, lo, 1);
100 	    break;
101 	case 3:
102 	    hi = - __kernel_cosl(hi, lo);
103 	    break;
104 	}
105 
106 	RETURNI(hi);
107 }
108 
109 #else
110 
111 long double
112 sinl(long double x)
113 {
114 	return sin(x);
115 }
116 
117 #endif /* __HAVE_LONG_DOUBLE */
118