1176360Sdas/*-
2176360Sdas * Copyright (c) 2007 Steven G. Kargl
3176360Sdas * All rights reserved.
4176360Sdas *
5176360Sdas * Redistribution and use in source and binary forms, with or without
6176360Sdas * modification, are permitted provided that the following conditions
7176360Sdas * are met:
8176360Sdas * 1. Redistributions of source code must retain the above copyright
9176360Sdas *    notice unmodified, this list of conditions, and the following
10176360Sdas *    disclaimer.
11176360Sdas * 2. Redistributions in binary form must reproduce the above copyright
12176360Sdas *    notice, this list of conditions and the following disclaimer in the
13176360Sdas *    documentation and/or other materials provided with the distribution.
14176360Sdas *
15176360Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16176360Sdas * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17176360Sdas * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18176360Sdas * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19176360Sdas * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20176360Sdas * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21176360Sdas * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22176360Sdas * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23176360Sdas * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24176360Sdas * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25176360Sdas */
26176360Sdas
27176360Sdas#include <sys/cdefs.h>
28176360Sdas__FBSDID("$FreeBSD$");
29176360Sdas
30176360Sdas/*
31176360Sdas * Limited testing on pseudorandom numbers drawn within [0:4e8] shows
32176360Sdas * an accuracy of <= 1.5 ULP where 247024 values of x out of 40 million
33176360Sdas * possibles resulted in tan(x) that exceeded 0.5 ULP (ie., 0.6%).
34176360Sdas */
35176360Sdas
36176360Sdas#include <float.h>
37240828Skargl#ifdef __i386__
38240828Skargl#include <ieeefp.h>
39240828Skargl#endif
40176360Sdas
41176360Sdas#include "math.h"
42176360Sdas#include "math_private.h"
43176360Sdas#if LDBL_MANT_DIG == 64
44221234Skargl#include "../ld80/e_rem_pio2l.h"
45176360Sdas#elif LDBL_MANT_DIG == 113
46221234Skargl#include "../ld128/e_rem_pio2l.h"
47176360Sdas#else
48176360Sdas#error "Unsupported long double format"
49176360Sdas#endif
50176360Sdas
51176360Sdaslong double
52176360Sdastanl(long double x)
53176360Sdas{
54176360Sdas	union IEEEl2bits z;
55221234Skargl	int e0, s;
56221234Skargl	long double y[2];
57176360Sdas	long double hi, lo;
58176360Sdas
59176360Sdas	z.e = x;
60176360Sdas	s = z.bits.sign;
61176360Sdas	z.bits.sign = 0;
62176360Sdas
63176360Sdas	/* If x = +-0 or x is subnormal, then tan(x) = x. */
64176360Sdas	if (z.bits.exp == 0)
65176360Sdas		return (x);
66176360Sdas
67176360Sdas	/* If x = NaN or Inf, then tan(x) = NaN. */
68176360Sdas	if (z.bits.exp == 32767)
69176360Sdas		return ((x - x) / (x - x));
70176360Sdas
71240828Skargl	ENTERI();
72240828Skargl
73176360Sdas	/* Optimize the case where x is already within range. */
74176360Sdas	if (z.e < M_PI_4) {
75176360Sdas		hi = __kernel_tanl(z.e, 0, 0);
76240828Skargl		RETURNI(s ? -hi : hi);
77176360Sdas	}
78176360Sdas
79221234Skargl	e0 = __ieee754_rem_pio2l(x, y);
80221234Skargl	hi = y[0];
81221234Skargl	lo = y[1];
82176360Sdas
83176360Sdas	switch (e0 & 3) {
84176360Sdas	case 0:
85176360Sdas	case 2:
86176360Sdas	    hi = __kernel_tanl(hi, lo, 0);
87176360Sdas	    break;
88176360Sdas	case 1:
89176360Sdas	case 3:
90176360Sdas	    hi = __kernel_tanl(hi, lo, 1);
91176360Sdas	    break;
92176360Sdas	}
93176360Sdas
94240828Skargl	RETURNI(hi);
95176360Sdas}
96