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