xref: /netbsd-src/lib/libm/src/s_sinl.c (revision b0c6c153909fe2ba392c312e6735dc2dc0f2de99)
1 /*	$NetBSD: s_sinl.c,v 1.3 2024/04/03 18:53:42 christos 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.3 2024/04/03 18:53:42 christos Exp $");
33 
34 #include "namespace.h"
35 #include <float.h>
36 
37 #ifdef __weak_alias
__weak_alias(sinl,_sinl)38 __weak_alias(sinl, _sinl)
39 #endif
40 
41 #include "math.h"
42 #include "math_private.h"
43 
44 #ifdef __HAVE_LONG_DOUBLE
45 
46 #if LDBL_MANT_DIG == 64
47 #include "../ld80/e_rem_pio2l.h"
48 #include "../ld80/k_sinl.c"
49 #elif LDBL_MANT_DIG == 113
50 #include "../ld128/e_rem_pio2l.h"
51 #include "../ld128/k_sinl.c"
52 #else
53 #error "Unsupported long double format"
54 #endif
55 
56 long double
57 sinl(long double x)
58 {
59 	union ieee_ext_u z;
60 	int e0, s;
61 	long double y[2];
62 	long double hi, lo;
63 
64 	z.extu_ld = x;
65 	s = z.extu_sign;
66 	z.extu_sign = 0;
67 
68 	/* If x = +-0 or x is a subnormal number, then sin(x) = x */
69 	if (z.extu_exp == 0)
70 		return (x);
71 
72 	/* If x = NaN or Inf, then sin(x) = NaN. */
73 	if (z.extu_exp == 32767)
74 		return ((x - x) / (x - x));
75 
76 	ENTERI();
77 
78 	/* Optimize the case where x is already within range. */
79 	if (z.extu_ld < M_PI_4) {
80 		hi = __kernel_sinl(z.extu_ld, 0, 0);
81 		RETURNI(s ? -hi : hi);
82 	}
83 
84 	e0 = __ieee754_rem_pio2l(x, y);
85 	hi = y[0];
86 	lo = y[1];
87 
88 	switch (e0 & 3) {
89 	case 0:
90 	    hi = __kernel_sinl(hi, lo, 1);
91 	    break;
92 	case 1:
93 	    hi = __kernel_cosl(hi, lo);
94 	    break;
95 	case 2:
96 	    hi = - __kernel_sinl(hi, lo, 1);
97 	    break;
98 	case 3:
99 	    hi = - __kernel_cosl(hi, lo);
100 	    break;
101 	}
102 
103 	RETURNI(hi);
104 }
105 
106 #else
107 
108 long double
109 sinl(long double x)
110 {
111 	return sin(x);
112 }
113 
114 #endif /* __HAVE_LONG_DOUBLE */
115