blob: 04d86c379bf339d1ee37e0a6f09671ab2dfea925 [file] [log] [blame]
Viet-Trung Luu96b05c12016-01-11 11:26:36 -08001/* origin: FreeBSD /usr/src/lib/msun/src/s_tanf.c */
2/*
3 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
4 * Optimized by Bruce D. Evans.
5 */
6/*
7 * ====================================================
8 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
9 *
10 * Developed at SunPro, a Sun Microsystems, Inc. business.
11 * Permission to use, copy, modify, and distribute this
12 * software is freely granted, provided that this notice
13 * is preserved.
14 * ====================================================
15 */
16
17#include "libm.h"
18
19/* Small multiples of pi/2 rounded to double precision. */
George Kulakowski17e3b042016-02-18 15:59:50 -080020static const double t1pio2 = 1 * M_PI_2, /* 0x3FF921FB, 0x54442D18 */
21 t2pio2 = 2 * M_PI_2, /* 0x400921FB, 0x54442D18 */
22 t3pio2 = 3 * M_PI_2, /* 0x4012D97C, 0x7F3321D2 */
23 t4pio2 = 4 * M_PI_2; /* 0x401921FB, 0x54442D18 */
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080024
George Kulakowski17e3b042016-02-18 15:59:50 -080025float tanf(float x) {
26 double y;
27 uint32_t ix;
28 unsigned n, sign;
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080029
George Kulakowski17e3b042016-02-18 15:59:50 -080030 GET_FLOAT_WORD(ix, x);
31 sign = ix >> 31;
32 ix &= 0x7fffffff;
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080033
George Kulakowski17e3b042016-02-18 15:59:50 -080034 if (ix <= 0x3f490fda) { /* |x| ~<= pi/4 */
35 if (ix < 0x39800000) { /* |x| < 2**-12 */
36 /* raise inexact if x!=0 and underflow if subnormal */
37 FORCE_EVAL(ix < 0x00800000 ? x / 0x1p120f : x + 0x1p120f);
38 return x;
39 }
40 return __tandf(x, 0);
41 }
42 if (ix <= 0x407b53d1) { /* |x| ~<= 5*pi/4 */
43 if (ix <= 0x4016cbe3) /* |x| ~<= 3pi/4 */
44 return __tandf((sign ? x + t1pio2 : x - t1pio2), 1);
45 else
46 return __tandf((sign ? x + t2pio2 : x - t2pio2), 0);
47 }
48 if (ix <= 0x40e231d5) { /* |x| ~<= 9*pi/4 */
49 if (ix <= 0x40afeddf) /* |x| ~<= 7*pi/4 */
50 return __tandf((sign ? x + t3pio2 : x - t3pio2), 1);
51 else
52 return __tandf((sign ? x + t4pio2 : x - t4pio2), 0);
53 }
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080054
George Kulakowski17e3b042016-02-18 15:59:50 -080055 /* tan(Inf or NaN) is NaN */
56 if (ix >= 0x7f800000)
57 return x - x;
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080058
George Kulakowski17e3b042016-02-18 15:59:50 -080059 /* argument reduction */
60 n = __rem_pio2f(x, &y);
61 return __tandf(y, n & 1);
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080062}