xref: /freebsd/lib/msun/src/s_sinl.c (revision 8e77cc64315e2d8ae4a60cbd3f160a35a1359550)
18e77cc64SDavid Schultz /*-
28e77cc64SDavid Schultz  * Copyright (c) 2007 Steven G. Kargl
38e77cc64SDavid Schultz  * All rights reserved.
48e77cc64SDavid Schultz  *
58e77cc64SDavid Schultz  * Redistribution and use in source and binary forms, with or without
68e77cc64SDavid Schultz  * modification, are permitted provided that the following conditions
78e77cc64SDavid Schultz  * are met:
88e77cc64SDavid Schultz  * 1. Redistributions of source code must retain the above copyright
98e77cc64SDavid Schultz  *    notice unmodified, this list of conditions, and the following
108e77cc64SDavid Schultz  *    disclaimer.
118e77cc64SDavid Schultz  * 2. Redistributions in binary form must reproduce the above copyright
128e77cc64SDavid Schultz  *    notice, this list of conditions and the following disclaimer in the
138e77cc64SDavid Schultz  *    documentation and/or other materials provided with the distribution.
148e77cc64SDavid Schultz  *
158e77cc64SDavid Schultz  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
168e77cc64SDavid Schultz  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
178e77cc64SDavid Schultz  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
188e77cc64SDavid Schultz  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
198e77cc64SDavid Schultz  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
208e77cc64SDavid Schultz  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
218e77cc64SDavid Schultz  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
228e77cc64SDavid Schultz  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
238e77cc64SDavid Schultz  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
248e77cc64SDavid Schultz  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
258e77cc64SDavid Schultz  */
268e77cc64SDavid Schultz 
278e77cc64SDavid Schultz #include <sys/cdefs.h>
288e77cc64SDavid Schultz __FBSDID("$FreeBSD$");
298e77cc64SDavid Schultz 
308e77cc64SDavid Schultz /*
318e77cc64SDavid Schultz  * Compute sin(x) for x where x is reduced to y = x - k * pi / 2.
328e77cc64SDavid Schultz  */
338e77cc64SDavid Schultz 
348e77cc64SDavid Schultz #include <float.h>
358e77cc64SDavid Schultz 
368e77cc64SDavid Schultz #include "math.h"
378e77cc64SDavid Schultz #include "math_private.h"
388e77cc64SDavid Schultz #include "fpmath.h"
398e77cc64SDavid Schultz 
408e77cc64SDavid Schultz #if LDBL_MANT_DIG == 64
418e77cc64SDavid Schultz #define	NX	3
428e77cc64SDavid Schultz #define	PREC	2
438e77cc64SDavid Schultz #elif LDBL_MANT_DIG == 113
448e77cc64SDavid Schultz #define	NX	5
458e77cc64SDavid Schultz #define	PREC	3
468e77cc64SDavid Schultz #else
478e77cc64SDavid Schultz #error "Unsupported long double format"
488e77cc64SDavid Schultz #endif
498e77cc64SDavid Schultz 
508e77cc64SDavid Schultz static const long double two24 = 1.67772160000000000000e+07L;
518e77cc64SDavid Schultz 
528e77cc64SDavid Schultz long double
538e77cc64SDavid Schultz sinl(long double x)
548e77cc64SDavid Schultz {
558e77cc64SDavid Schultz 	union IEEEl2bits z;
568e77cc64SDavid Schultz 	int i, e0, s;
578e77cc64SDavid Schultz 	double xd[NX], yd[PREC];
588e77cc64SDavid Schultz 	long double hi, lo;
598e77cc64SDavid Schultz 
608e77cc64SDavid Schultz 	z.e = x;
618e77cc64SDavid Schultz 	s = z.bits.sign;
628e77cc64SDavid Schultz 	z.bits.sign = 0;
638e77cc64SDavid Schultz 
648e77cc64SDavid Schultz 	/* If x = +-0 or x is a subnormal number, then sin(x) = x */
658e77cc64SDavid Schultz 	if (z.bits.exp == 0)
668e77cc64SDavid Schultz 		return (x);
678e77cc64SDavid Schultz 
688e77cc64SDavid Schultz 	/* If x = NaN or Inf, then sin(x) = NaN. */
698e77cc64SDavid Schultz 	if (z.bits.exp == 32767)
708e77cc64SDavid Schultz 		return ((x - x) / (x - x));
718e77cc64SDavid Schultz 
728e77cc64SDavid Schultz 	/* Optimize the case where x is already within range. */
738e77cc64SDavid Schultz 	if (z.e < M_PI_4) {
748e77cc64SDavid Schultz 		hi = __kernel_sinl(z.e, 0, 0);
758e77cc64SDavid Schultz 		return  (s ? -hi : hi);
768e77cc64SDavid Schultz 	}
778e77cc64SDavid Schultz 
788e77cc64SDavid Schultz 	/* Split z.e into a 24-bit representation. */
798e77cc64SDavid Schultz 	e0 = ilogbl(z.e) - 23;
808e77cc64SDavid Schultz 	z.e = scalbnl(z.e, -e0);
818e77cc64SDavid Schultz 	for (i = 0; i < NX; i++) {
828e77cc64SDavid Schultz 		xd[i] = (double)((int32_t)z.e);
838e77cc64SDavid Schultz 		z.e = (z.e - xd[i]) * two24;
848e77cc64SDavid Schultz 	}
858e77cc64SDavid Schultz 
868e77cc64SDavid Schultz 	/* yd contains the pieces of xd rem pi/2 such that |yd| < pi/4. */
878e77cc64SDavid Schultz 	e0 = __kernel_rem_pio2(xd, yd, e0, NX, PREC);
888e77cc64SDavid Schultz 
898e77cc64SDavid Schultz #if PREC == 2
908e77cc64SDavid Schultz 	hi = (long double)yd[0] + yd[1];
918e77cc64SDavid Schultz 	lo = yd[1] - (hi - yd[0]);
928e77cc64SDavid Schultz #else /* PREC == 3 */
938e77cc64SDavid Schultz 	long double t;
948e77cc64SDavid Schultz 	t = (long double)yd[2] + yd[1];
958e77cc64SDavid Schultz 	hi = t + yd[0];
968e77cc64SDavid Schultz 	lo = yd[0] - (hi - t);
978e77cc64SDavid Schultz #endif
988e77cc64SDavid Schultz 
998e77cc64SDavid Schultz 	switch (e0 & 3) {
1008e77cc64SDavid Schultz 	case 0:
1018e77cc64SDavid Schultz 	    hi = __kernel_sinl(hi, lo, 1);
1028e77cc64SDavid Schultz 	    break;
1038e77cc64SDavid Schultz 	case 1:
1048e77cc64SDavid Schultz 	    hi = __kernel_cosl(hi, lo);
1058e77cc64SDavid Schultz 	    break;
1068e77cc64SDavid Schultz 	case 2:
1078e77cc64SDavid Schultz 	    hi = - __kernel_sinl(hi, lo, 1);
1088e77cc64SDavid Schultz 	    break;
1098e77cc64SDavid Schultz 	case 3:
1108e77cc64SDavid Schultz 	    hi = - __kernel_cosl(hi, lo);
1118e77cc64SDavid Schultz 	    break;
1128e77cc64SDavid Schultz 	}
1138e77cc64SDavid Schultz 
1148e77cc64SDavid Schultz 	return (s ? -hi : hi);
1158e77cc64SDavid Schultz }
116