1/*	$NetBSD: s_tanl.c,v 1.3 2024/04/03 18:53:42 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.3 2024/04/03 18:53:42 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
42#include "math.h"
43#include "math_private.h"
44
45#ifdef __weak_alias
46__weak_alias(tanl, _tanl)
47#endif
48
49#ifdef __HAVE_LONG_DOUBLE
50
51#if LDBL_MANT_DIG == 64
52#include "../ld80/e_rem_pio2l.h"
53#include "../ld80/k_tanl.c"
54#elif LDBL_MANT_DIG == 113
55#include "../ld128/e_rem_pio2l.h"
56#include "../ld128/k_tanl.c"
57#else
58#error "Unsupported long double format"
59#endif
60
61long double
62tanl(long double x)
63{
64	union ieee_ext_u z;
65	int e0, s;
66	long double y[2];
67	long double hi, lo;
68
69	z.extu_ld = x;
70	s = z.extu_sign;
71	z.extu_sign = 0;
72
73	/* If x = +-0 or x is subnormal, then tan(x) = x. */
74	if (z.extu_exp == 0)
75		return (x);
76
77	/* If x = NaN or Inf, then tan(x) = NaN. */
78	if (z.extu_exp == 32767)
79		return ((x - x) / (x - x));
80
81	ENTERI();
82
83	/* Optimize the case where x is already within range. */
84	if (z.extu_ld < M_PI_4) {
85		hi = __kernel_tanl(z.extu_ld, 0, 0);
86		RETURNI(s ? -hi : hi);
87	}
88
89	e0 = __ieee754_rem_pio2l(x, y);
90	hi = y[0];
91	lo = y[1];
92
93	switch (e0 & 3) {
94	case 0:
95	case 2:
96	    hi = __kernel_tanl(hi, lo, 0);
97	    break;
98	case 1:
99	case 3:
100	    hi = __kernel_tanl(hi, lo, 1);
101	    break;
102	}
103
104	RETURNI(hi);
105}
106
107#else
108
109long double
110tanl(long double x)
111{
112	return tan(x);
113}
114
115#endif
116