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