kf_tan.c 2.9 KB

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