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