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 /*
308e77cc64SDavid Schultz * Limited testing on pseudorandom numbers drawn within [0:4e8] shows
318e77cc64SDavid Schultz * an accuracy of <= 1.5 ULP where 247024 values of x out of 40 million
328e77cc64SDavid Schultz * possibles resulted in tan(x) that exceeded 0.5 ULP (ie., 0.6%).
338e77cc64SDavid Schultz */
348e77cc64SDavid Schultz
358e77cc64SDavid Schultz #include <float.h>
36340076f0SSteve Kargl #ifdef __i386__
37340076f0SSteve Kargl #include <ieeefp.h>
38340076f0SSteve Kargl #endif
398e77cc64SDavid Schultz
408e77cc64SDavid Schultz #include "math.h"
418e77cc64SDavid Schultz #include "math_private.h"
428e77cc64SDavid Schultz #if LDBL_MANT_DIG == 64
43c273267eSSteve Kargl #include "../ld80/e_rem_pio2l.h"
448e77cc64SDavid Schultz #elif LDBL_MANT_DIG == 113
45c273267eSSteve Kargl #include "../ld128/e_rem_pio2l.h"
468e77cc64SDavid Schultz #else
478e77cc64SDavid Schultz #error "Unsupported long double format"
488e77cc64SDavid Schultz #endif
498e77cc64SDavid Schultz
508e77cc64SDavid Schultz long double
tanl(long double x)518e77cc64SDavid Schultz tanl(long double x)
528e77cc64SDavid Schultz {
538e77cc64SDavid Schultz union IEEEl2bits z;
54c273267eSSteve Kargl int e0, s;
55c273267eSSteve Kargl long double y[2];
568e77cc64SDavid Schultz long double hi, lo;
578e77cc64SDavid Schultz
588e77cc64SDavid Schultz z.e = x;
598e77cc64SDavid Schultz s = z.bits.sign;
608e77cc64SDavid Schultz z.bits.sign = 0;
618e77cc64SDavid Schultz
628e77cc64SDavid Schultz /* If x = +-0 or x is subnormal, then tan(x) = x. */
638e77cc64SDavid Schultz if (z.bits.exp == 0)
648e77cc64SDavid Schultz return (x);
658e77cc64SDavid Schultz
668e77cc64SDavid Schultz /* If x = NaN or Inf, then tan(x) = NaN. */
678e77cc64SDavid Schultz if (z.bits.exp == 32767)
688e77cc64SDavid Schultz return ((x - x) / (x - x));
698e77cc64SDavid Schultz
70340076f0SSteve Kargl ENTERI();
71340076f0SSteve Kargl
728e77cc64SDavid Schultz /* Optimize the case where x is already within range. */
738e77cc64SDavid Schultz if (z.e < M_PI_4) {
748e77cc64SDavid Schultz hi = __kernel_tanl(z.e, 0, 0);
75340076f0SSteve Kargl RETURNI(s ? -hi : hi);
768e77cc64SDavid Schultz }
778e77cc64SDavid Schultz
78c273267eSSteve Kargl e0 = __ieee754_rem_pio2l(x, y);
79c273267eSSteve Kargl hi = y[0];
80c273267eSSteve Kargl lo = y[1];
818e77cc64SDavid Schultz
828e77cc64SDavid Schultz switch (e0 & 3) {
838e77cc64SDavid Schultz case 0:
848e77cc64SDavid Schultz case 2:
858e77cc64SDavid Schultz hi = __kernel_tanl(hi, lo, 0);
868e77cc64SDavid Schultz break;
878e77cc64SDavid Schultz case 1:
888e77cc64SDavid Schultz case 3:
898e77cc64SDavid Schultz hi = __kernel_tanl(hi, lo, 1);
908e77cc64SDavid Schultz break;
918e77cc64SDavid Schultz }
928e77cc64SDavid Schultz
93340076f0SSteve Kargl RETURNI(hi);
948e77cc64SDavid Schultz }
95