k_tanf.c revision 151961
1/* k_tanf.c -- float version of k_tan.c
2 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3 */
4
5/*
6 * ====================================================
7 * Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
8 *
9 * Permission to use, copy, modify, and distribute this
10 * software is freely granted, provided that this notice
11 * is preserved.
12 * ====================================================
13 */
14
15#ifndef lint
16static char rcsid[] = "$FreeBSD: head/lib/msun/src/k_tanf.c 151961 2005-11-02 05:37:31Z bde $";
17#endif
18
19#include "math.h"
20#include "math_private.h"
21static const float
22one   =  1.0000000000e+00, /* 0x3f800000 */
23pio4  =  7.8539812565e-01, /* 0x3f490fda */
24pio4lo=  3.7748947079e-08, /* 0x33222168 */
25T[] =  {
26  3.3333334327e-01, /* 0x3eaaaaab */
27  1.3333334029e-01, /* 0x3e088889 */
28  5.3968254477e-02, /* 0x3d5d0dd1 */
29  2.1869488060e-02, /* 0x3cb327a4 */
30  8.8632395491e-03, /* 0x3c11371f */
31  3.5920790397e-03, /* 0x3b6b6916 */
32  1.4562094584e-03, /* 0x3abede48 */
33  5.8804126456e-04, /* 0x3a1a26c8 */
34  2.4646313977e-04, /* 0x398137b9 */
35  7.8179444245e-05, /* 0x38a3f445 */
36  7.1407252108e-05, /* 0x3895c07a */
37 -1.8558637748e-05, /* 0xb79bae5f */
38  2.5907305826e-05, /* 0x37d95384 */
39};
40
41float
42__kernel_tanf(float x, float y, int iy)
43{
44	float z,r,v,w,s;
45	int32_t ix,hx;
46	GET_FLOAT_WORD(hx,x);
47	ix = hx&0x7fffffff;	/* high word of |x| */
48	if(ix<0x31800000) {			/* x < 2**-28 */
49		if ((int) x == 0) {		/* generate inexact */
50			if ((ix | (iy + 1)) == 0)
51				return one / fabsf(x);
52			else {
53				if (iy == 1)
54					return x;
55				else {	/* compute -1 / (x+y) carefully */
56					float a, t;
57
58					z = w = x + y;
59					GET_FLOAT_WORD(ix, z);
60					SET_FLOAT_WORD(z, ix & 0xfffff000);
61					v = y - (z - x);
62					t = a = -one / w;
63					GET_FLOAT_WORD(ix, t);
64					SET_FLOAT_WORD(t, ix & 0xfffff000);
65					s = one + t * z;
66					return t + a * (s + t * v);
67				}
68			}
69		}
70	}
71	if(ix>=0x3f2ca140) { 			/* |x|>=0.6744 */
72	    if(hx<0) {x = -x; y = -y;}
73	    z = pio4-x;
74	    w = pio4lo-y;
75	    x = z+w; y = 0.0;
76	}
77	z	=  x*x;
78	w 	=  z*z;
79    /* Break x^5*(T[1]+x^2*T[2]+...) into
80     *	  x^5(T[1]+x^4*T[3]+...+x^20*T[11]) +
81     *	  x^5(x^2*(T[2]+x^4*T[4]+...+x^22*[T12]))
82     */
83	r = T[1]+w*(T[3]+w*(T[5]+w*(T[7]+w*(T[9]+w*T[11]))));
84	v = z*(T[2]+w*(T[4]+w*(T[6]+w*(T[8]+w*(T[10]+w*T[12])))));
85	s = z*x;
86	r = y + z*(s*(r+v)+y);
87	r += T[0]*s;
88	w = x+r;
89	if(ix>=0x3f2ca140) {
90	    v = (float)iy;
91	    return (float)(1-((hx>>30)&2))*(v-(float)2.0*(x-(w*w/(w+v)-r)));
92	}
93	if(iy==1) return w;
94	else {		/* if allow error up to 2 ulp,
95			   simply return -1.0/(x+r) here */
96     /*  compute -1.0/(x+r) accurately */
97	    float a,t;
98	    int32_t i;
99	    z  = w;
100	    GET_FLOAT_WORD(i,z);
101	    SET_FLOAT_WORD(z,i&0xfffff000);
102	    v  = r-(z - x); 	/* z+v = r+x */
103	    t = a  = -(float)1.0/w;	/* a = -1.0/w */
104	    GET_FLOAT_WORD(i,t);
105	    SET_FLOAT_WORD(t,i&0xfffff000);
106	    s  = (float)1.0+t*z;
107	    return t+a*(s+t*v);
108	}
109}
110