xref: /netbsd-src/lib/libm/src/s_tanl.c (revision 345cf9fb81bd0411c53e25d62cd93bdcaa865312)
1 /*	$NetBSD: s_tanl.c,v 1.2 2024/02/23 13:42:01 christos Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2007 Steven G. Kargl
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice unmodified, this list of conditions, and the following
14  *    disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: s_tanl.c,v 1.2 2024/02/23 13:42:01 christos Exp $");
33 
34 /*
35  * Limited testing on pseudorandom numbers drawn within [0:4e8] shows
36  * an accuracy of <= 1.5 ULP where 247024 values of x out of 40 million
37  * possibles resulted in tan(x) that exceeded 0.5 ULP (ie., 0.6%).
38  */
39 #include "namespace.h"
40 #include <float.h>
41 #ifdef __i386__
42 #include <ieeefp.h>
43 #endif
44 
45 #include "math.h"
46 #include "math_private.h"
47 
48 #ifdef __weak_alias
49 __weak_alias(tanl, _tanl)
50 #endif
51 
52 #ifdef __HAVE_LONG_DOUBLE
53 
54 #if LDBL_MANT_DIG == 64
55 #include "../ld80/e_rem_pio2l.h"
56 #include "../ld80/k_tanl.c"
57 #elif LDBL_MANT_DIG == 113
58 #include "../ld128/e_rem_pio2l.h"
59 #include "../ld128/k_tanl.c"
60 #else
61 #error "Unsupported long double format"
62 #endif
63 
64 long double
65 tanl(long double x)
66 {
67 	union ieee_ext_u z;
68 	int e0, s;
69 	long double y[2];
70 	long double hi, lo;
71 
72 	z.extu_ld = x;
73 	s = z.extu_sign;
74 	z.extu_sign = 0;
75 
76 	/* If x = +-0 or x is subnormal, then tan(x) = x. */
77 	if (z.extu_exp == 0)
78 		return (x);
79 
80 	/* If x = NaN or Inf, then tan(x) = NaN. */
81 	if (z.extu_exp == 32767)
82 		return ((x - x) / (x - x));
83 
84 	ENTERI();
85 
86 	/* Optimize the case where x is already within range. */
87 	if (z.extu_ld < M_PI_4) {
88 		hi = __kernel_tanl(z.extu_ld, 0, 0);
89 		RETURNI(s ? -hi : hi);
90 	}
91 
92 	e0 = __ieee754_rem_pio2l(x, y);
93 	hi = y[0];
94 	lo = y[1];
95 
96 	switch (e0 & 3) {
97 	case 0:
98 	case 2:
99 	    hi = __kernel_tanl(hi, lo, 0);
100 	    break;
101 	case 1:
102 	case 3:
103 	    hi = __kernel_tanl(hi, lo, 1);
104 	    break;
105 	}
106 
107 	RETURNI(hi);
108 }
109 
110 #else
111 
112 long double
113 tanl(long double x)
114 {
115 	return tan(x);
116 }
117 
118 #endif
119