1 /* $OpenBSD: s_tanl.c,v 1.1 2008/12/09 20:00:35 martynas Exp $ */
3 * Copyright (c) 2007 Steven G. Kargl
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Compute tan(x) for x where x is reduced to y = x - k * pi / 2.
30 * Limited testing on pseudorandom numbers drawn within [0:4e8] shows
31 * an accuracy of <= 1.5 ULP where 247024 values of x out of 40 million
32 * possibles resulted in tan(x) that exceeded 0.5 ULP (ie., 0.6%).
35 #include <sys/types.h>
36 #include <machine/ieee.h>
40 #include "math_private.h"
42 #if LDBL_MANT_DIG == 64
45 #elif LDBL_MANT_DIG == 113
49 #error "Unsupported long double format"
52 static const long double two24
= 1.67772160000000000000e+07L;
62 double xd
[NX
], yd
[PREC
];
69 /* If x = +-0 or x is subnormal, then tan(x) = x. */
70 if (z
.bits
.ext_exp
== 0)
73 /* If x = NaN or Inf, then tan(x) = NaN. */
74 if (z
.bits
.ext_exp
== 32767)
75 return ((x
- x
) / (x
- x
));
77 /* Optimize the case where x is already within range. */
79 hi
= __kernel_tanl(z
.e
, 0, 0);
80 return (s
? -hi
: hi
);
83 /* Split z.e into a 24-bit representation. */
84 e0
= ilogbl(z
.e
) - 23;
85 z
.e
= scalbnl(z
.e
, -e0
);
86 for (i
= 0; i
< NX
; i
++) {
87 xd
[i
] = (double)((int32_t)z
.e
);
88 z
.e
= (z
.e
- xd
[i
]) * two24
;
91 /* yd contains the pieces of xd rem pi/2 such that |yd| < pi/4. */
92 e0
= __kernel_rem_pio2(xd
, yd
, e0
, NX
, PREC
);
95 hi
= (long double)yd
[0] + yd
[1];
96 lo
= yd
[1] - (hi
- yd
[0]);
99 t
= (long double)yd
[2] + yd
[1];
101 lo
= yd
[0] - (hi
- t
);
107 hi
= __kernel_tanl(hi
, lo
, 0);
111 hi
= __kernel_tanl(hi
, lo
, 1);
115 return (s
? -hi
: hi
);