xref: /freebsd/lib/msun/src/s_sinl.c (revision c273267e834bf74fa2bdff4215f46b146fb4e46c)
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 #include <float.h>
318e77cc64SDavid Schultz 
328e77cc64SDavid Schultz #include "math.h"
33*c273267eSSteve Kargl #define INLINE_REM_PIO2L
348e77cc64SDavid Schultz #include "math_private.h"
358e77cc64SDavid Schultz #if LDBL_MANT_DIG == 64
36*c273267eSSteve Kargl #include "../ld80/e_rem_pio2l.h"
378e77cc64SDavid Schultz #elif LDBL_MANT_DIG == 113
38*c273267eSSteve Kargl #include "../ld128/e_rem_pio2l.h"
398e77cc64SDavid Schultz #else
408e77cc64SDavid Schultz #error "Unsupported long double format"
418e77cc64SDavid Schultz #endif
428e77cc64SDavid Schultz 
438e77cc64SDavid Schultz long double
448e77cc64SDavid Schultz sinl(long double x)
458e77cc64SDavid Schultz {
468e77cc64SDavid Schultz 	union IEEEl2bits z;
47*c273267eSSteve Kargl 	int e0, s;
48*c273267eSSteve Kargl 	long double y[2];
498e77cc64SDavid Schultz 	long double hi, lo;
508e77cc64SDavid Schultz 
518e77cc64SDavid Schultz 	z.e = x;
528e77cc64SDavid Schultz 	s = z.bits.sign;
538e77cc64SDavid Schultz 	z.bits.sign = 0;
548e77cc64SDavid Schultz 
558e77cc64SDavid Schultz 	/* If x = +-0 or x is a subnormal number, then sin(x) = x */
568e77cc64SDavid Schultz 	if (z.bits.exp == 0)
578e77cc64SDavid Schultz 		return (x);
588e77cc64SDavid Schultz 
598e77cc64SDavid Schultz 	/* If x = NaN or Inf, then sin(x) = NaN. */
608e77cc64SDavid Schultz 	if (z.bits.exp == 32767)
618e77cc64SDavid Schultz 		return ((x - x) / (x - x));
628e77cc64SDavid Schultz 
638e77cc64SDavid Schultz 	/* Optimize the case where x is already within range. */
648e77cc64SDavid Schultz 	if (z.e < M_PI_4) {
658e77cc64SDavid Schultz 		hi = __kernel_sinl(z.e, 0, 0);
668e77cc64SDavid Schultz 		return  (s ? -hi : hi);
678e77cc64SDavid Schultz 	}
688e77cc64SDavid Schultz 
69*c273267eSSteve Kargl 	e0 = __ieee754_rem_pio2l(x, y);
70*c273267eSSteve Kargl 	hi = y[0];
71*c273267eSSteve Kargl 	lo = y[1];
728e77cc64SDavid Schultz 
738e77cc64SDavid Schultz 	switch (e0 & 3) {
748e77cc64SDavid Schultz 	case 0:
758e77cc64SDavid Schultz 	    hi = __kernel_sinl(hi, lo, 1);
768e77cc64SDavid Schultz 	    break;
778e77cc64SDavid Schultz 	case 1:
788e77cc64SDavid Schultz 	    hi = __kernel_cosl(hi, lo);
798e77cc64SDavid Schultz 	    break;
808e77cc64SDavid Schultz 	case 2:
818e77cc64SDavid Schultz 	    hi = - __kernel_sinl(hi, lo, 1);
828e77cc64SDavid Schultz 	    break;
838e77cc64SDavid Schultz 	case 3:
848e77cc64SDavid Schultz 	    hi = - __kernel_cosl(hi, lo);
858e77cc64SDavid Schultz 	    break;
868e77cc64SDavid Schultz 	}
878e77cc64SDavid Schultz 
88*c273267eSSteve Kargl 	return (hi);
898e77cc64SDavid Schultz }
90