xref: /openbsd-src/lib/libm/src/s_sinl.c (revision 2f2c00629eff6a304ebffb255fc56f4fa7a1833b)
1*2f2c0062Sguenther /*	$OpenBSD: s_sinl.c,v 1.2 2016/09/12 19:47:02 guenther Exp $	*/
2390c8400Smartynas /*-
3390c8400Smartynas  * Copyright (c) 2007 Steven G. Kargl
4390c8400Smartynas  * All rights reserved.
5390c8400Smartynas  *
6390c8400Smartynas  * Redistribution and use in source and binary forms, with or without
7390c8400Smartynas  * modification, are permitted provided that the following conditions
8390c8400Smartynas  * are met:
9390c8400Smartynas  * 1. Redistributions of source code must retain the above copyright
10390c8400Smartynas  *    notice unmodified, this list of conditions, and the following
11390c8400Smartynas  *    disclaimer.
12390c8400Smartynas  * 2. Redistributions in binary form must reproduce the above copyright
13390c8400Smartynas  *    notice, this list of conditions and the following disclaimer in the
14390c8400Smartynas  *    documentation and/or other materials provided with the distribution.
15390c8400Smartynas  *
16390c8400Smartynas  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17390c8400Smartynas  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18390c8400Smartynas  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19390c8400Smartynas  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20390c8400Smartynas  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21390c8400Smartynas  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22390c8400Smartynas  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23390c8400Smartynas  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24390c8400Smartynas  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25390c8400Smartynas  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26390c8400Smartynas  */
27390c8400Smartynas 
28390c8400Smartynas /*
29390c8400Smartynas  * Compute sin(x) for x where x is reduced to y = x - k * pi / 2.
30390c8400Smartynas  */
31390c8400Smartynas 
32390c8400Smartynas #include <sys/types.h>
33390c8400Smartynas #include <machine/ieee.h>
34390c8400Smartynas #include <float.h>
35390c8400Smartynas #include <math.h>
36390c8400Smartynas 
37390c8400Smartynas #include "math_private.h"
38390c8400Smartynas 
39390c8400Smartynas #if LDBL_MANT_DIG == 64
40390c8400Smartynas #define	NX	3
41390c8400Smartynas #define	PREC	2
42390c8400Smartynas #elif LDBL_MANT_DIG == 113
43390c8400Smartynas #define	NX	5
44390c8400Smartynas #define	PREC	3
45390c8400Smartynas #else
46390c8400Smartynas #error "Unsupported long double format"
47390c8400Smartynas #endif
48390c8400Smartynas 
49390c8400Smartynas static const long double two24 = 1.67772160000000000000e+07L;
50390c8400Smartynas 
51390c8400Smartynas long double
sinl(long double x)52390c8400Smartynas sinl(long double x)
53390c8400Smartynas {
54390c8400Smartynas 	union {
55390c8400Smartynas 		long double e;
56390c8400Smartynas 		struct ieee_ext bits;
57390c8400Smartynas 	} z;
58390c8400Smartynas 	int i, e0, s;
59390c8400Smartynas 	double xd[NX], yd[PREC];
60390c8400Smartynas 	long double hi, lo;
61390c8400Smartynas 
62390c8400Smartynas 	z.e = x;
63390c8400Smartynas 	s = z.bits.ext_sign;
64390c8400Smartynas 	z.bits.ext_sign = 0;
65390c8400Smartynas 
66390c8400Smartynas 	/* If x = +-0 or x is a subnormal number, then sin(x) = x */
67390c8400Smartynas 	if (z.bits.ext_exp == 0)
68390c8400Smartynas 		return (x);
69390c8400Smartynas 
70390c8400Smartynas 	/* If x = NaN or Inf, then sin(x) = NaN. */
71390c8400Smartynas 	if (z.bits.ext_exp == 32767)
72390c8400Smartynas 		return ((x - x) / (x - x));
73390c8400Smartynas 
74390c8400Smartynas 	/* Optimize the case where x is already within range. */
75390c8400Smartynas 	if (z.e < M_PI_4) {
76390c8400Smartynas 		hi = __kernel_sinl(z.e, 0, 0);
77390c8400Smartynas 		return  (s ? -hi : hi);
78390c8400Smartynas 	}
79390c8400Smartynas 
80390c8400Smartynas 	/* Split z.e into a 24-bit representation. */
81390c8400Smartynas 	e0 = ilogbl(z.e) - 23;
82390c8400Smartynas 	z.e = scalbnl(z.e, -e0);
83390c8400Smartynas 	for (i = 0; i < NX; i++) {
84390c8400Smartynas 		xd[i] = (double)((int32_t)z.e);
85390c8400Smartynas 		z.e = (z.e - xd[i]) * two24;
86390c8400Smartynas 	}
87390c8400Smartynas 
88390c8400Smartynas 	/* yd contains the pieces of xd rem pi/2 such that |yd| < pi/4. */
89390c8400Smartynas 	e0 = __kernel_rem_pio2(xd, yd, e0, NX, PREC);
90390c8400Smartynas 
91390c8400Smartynas #if PREC == 2
92390c8400Smartynas 	hi = (long double)yd[0] + yd[1];
93390c8400Smartynas 	lo = yd[1] - (hi - yd[0]);
94390c8400Smartynas #else /* PREC == 3 */
95390c8400Smartynas 	long double t;
96390c8400Smartynas 	t = (long double)yd[2] + yd[1];
97390c8400Smartynas 	hi = t + yd[0];
98390c8400Smartynas 	lo = yd[0] - (hi - t);
99390c8400Smartynas #endif
100390c8400Smartynas 
101390c8400Smartynas 	switch (e0 & 3) {
102390c8400Smartynas 	case 0:
103390c8400Smartynas 	    hi = __kernel_sinl(hi, lo, 1);
104390c8400Smartynas 	    break;
105390c8400Smartynas 	case 1:
106390c8400Smartynas 	    hi = __kernel_cosl(hi, lo);
107390c8400Smartynas 	    break;
108390c8400Smartynas 	case 2:
109390c8400Smartynas 	    hi = - __kernel_sinl(hi, lo, 1);
110390c8400Smartynas 	    break;
111390c8400Smartynas 	case 3:
112390c8400Smartynas 	    hi = - __kernel_cosl(hi, lo);
113390c8400Smartynas 	    break;
114390c8400Smartynas 	}
115390c8400Smartynas 
116390c8400Smartynas 	return (s ? -hi : hi);
117390c8400Smartynas }
118*2f2c0062Sguenther DEF_STD(sinl);
119