xref: /freebsd/lib/msun/src/s_tanl.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
18e77cc64SDavid Schultz /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni  *
48e77cc64SDavid Schultz  * Copyright (c) 2007 Steven G. Kargl
58e77cc64SDavid Schultz  * All rights reserved.
68e77cc64SDavid Schultz  *
78e77cc64SDavid Schultz  * Redistribution and use in source and binary forms, with or without
88e77cc64SDavid Schultz  * modification, are permitted provided that the following conditions
98e77cc64SDavid Schultz  * are met:
108e77cc64SDavid Schultz  * 1. Redistributions of source code must retain the above copyright
118e77cc64SDavid Schultz  *    notice unmodified, this list of conditions, and the following
128e77cc64SDavid Schultz  *    disclaimer.
138e77cc64SDavid Schultz  * 2. Redistributions in binary form must reproduce the above copyright
148e77cc64SDavid Schultz  *    notice, this list of conditions and the following disclaimer in the
158e77cc64SDavid Schultz  *    documentation and/or other materials provided with the distribution.
168e77cc64SDavid Schultz  *
178e77cc64SDavid Schultz  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
188e77cc64SDavid Schultz  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
198e77cc64SDavid Schultz  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
208e77cc64SDavid Schultz  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
218e77cc64SDavid Schultz  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
228e77cc64SDavid Schultz  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
238e77cc64SDavid Schultz  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
248e77cc64SDavid Schultz  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
258e77cc64SDavid Schultz  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
268e77cc64SDavid Schultz  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
278e77cc64SDavid Schultz  */
288e77cc64SDavid Schultz 
298e77cc64SDavid Schultz #include <sys/cdefs.h>
308e77cc64SDavid Schultz __FBSDID("$FreeBSD$");
318e77cc64SDavid Schultz 
328e77cc64SDavid Schultz /*
338e77cc64SDavid Schultz  * Limited testing on pseudorandom numbers drawn within [0:4e8] shows
348e77cc64SDavid Schultz  * an accuracy of <= 1.5 ULP where 247024 values of x out of 40 million
358e77cc64SDavid Schultz  * possibles resulted in tan(x) that exceeded 0.5 ULP (ie., 0.6%).
368e77cc64SDavid Schultz  */
378e77cc64SDavid Schultz 
388e77cc64SDavid Schultz #include <float.h>
39340076f0SSteve Kargl #ifdef __i386__
40340076f0SSteve Kargl #include <ieeefp.h>
41340076f0SSteve Kargl #endif
428e77cc64SDavid Schultz 
438e77cc64SDavid Schultz #include "math.h"
448e77cc64SDavid Schultz #include "math_private.h"
458e77cc64SDavid Schultz #if LDBL_MANT_DIG == 64
46c273267eSSteve Kargl #include "../ld80/e_rem_pio2l.h"
478e77cc64SDavid Schultz #elif LDBL_MANT_DIG == 113
48c273267eSSteve Kargl #include "../ld128/e_rem_pio2l.h"
498e77cc64SDavid Schultz #else
508e77cc64SDavid Schultz #error "Unsupported long double format"
518e77cc64SDavid Schultz #endif
528e77cc64SDavid Schultz 
538e77cc64SDavid Schultz long double
548e77cc64SDavid Schultz tanl(long double x)
558e77cc64SDavid Schultz {
568e77cc64SDavid Schultz 	union IEEEl2bits z;
57c273267eSSteve Kargl 	int e0, s;
58c273267eSSteve Kargl 	long double y[2];
598e77cc64SDavid Schultz 	long double hi, lo;
608e77cc64SDavid Schultz 
618e77cc64SDavid Schultz 	z.e = x;
628e77cc64SDavid Schultz 	s = z.bits.sign;
638e77cc64SDavid Schultz 	z.bits.sign = 0;
648e77cc64SDavid Schultz 
658e77cc64SDavid Schultz 	/* If x = +-0 or x is subnormal, then tan(x) = x. */
668e77cc64SDavid Schultz 	if (z.bits.exp == 0)
678e77cc64SDavid Schultz 		return (x);
688e77cc64SDavid Schultz 
698e77cc64SDavid Schultz 	/* If x = NaN or Inf, then tan(x) = NaN. */
708e77cc64SDavid Schultz 	if (z.bits.exp == 32767)
718e77cc64SDavid Schultz 		return ((x - x) / (x - x));
728e77cc64SDavid Schultz 
73340076f0SSteve Kargl 	ENTERI();
74340076f0SSteve Kargl 
758e77cc64SDavid Schultz 	/* Optimize the case where x is already within range. */
768e77cc64SDavid Schultz 	if (z.e < M_PI_4) {
778e77cc64SDavid Schultz 		hi = __kernel_tanl(z.e, 0, 0);
78340076f0SSteve Kargl 		RETURNI(s ? -hi : hi);
798e77cc64SDavid Schultz 	}
808e77cc64SDavid Schultz 
81c273267eSSteve Kargl 	e0 = __ieee754_rem_pio2l(x, y);
82c273267eSSteve Kargl 	hi = y[0];
83c273267eSSteve Kargl 	lo = y[1];
848e77cc64SDavid Schultz 
858e77cc64SDavid Schultz 	switch (e0 & 3) {
868e77cc64SDavid Schultz 	case 0:
878e77cc64SDavid Schultz 	case 2:
888e77cc64SDavid Schultz 	    hi = __kernel_tanl(hi, lo, 0);
898e77cc64SDavid Schultz 	    break;
908e77cc64SDavid Schultz 	case 1:
918e77cc64SDavid Schultz 	case 3:
928e77cc64SDavid Schultz 	    hi = __kernel_tanl(hi, lo, 1);
938e77cc64SDavid Schultz 	    break;
948e77cc64SDavid Schultz 	}
958e77cc64SDavid Schultz 
96340076f0SSteve Kargl 	RETURNI(hi);
978e77cc64SDavid Schultz }
98