s_atanl.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* @(#)s_atan.c 5.1 93/09/24 */
  2. /* FreeBSD: head/lib/msun/src/s_atan.c 176451 2008-02-22 02:30:36Z das */
  3. /*
  4. * ====================================================
  5. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  6. *
  7. * Developed at SunPro, a Sun Microsystems, Inc. business.
  8. * Permission to use, copy, modify, and distribute this
  9. * software is freely granted, provided that this notice
  10. * is preserved.
  11. * ====================================================
  12. */
  13. #include "cdefs-compat.h"
  14. //__FBSDID("$FreeBSD: src/lib/msun/src/s_atanl.c,v 1.1 2008/07/31 22:41:26 das Exp $");
  15. /*
  16. * See comments in s_atan.c.
  17. * Converted to long double by David Schultz <[email protected]>.
  18. */
  19. #include <float.h>
  20. #include <openlibm_math.h>
  21. #include "invtrig.h"
  22. #include "math_private.h"
  23. static const long double
  24. one = 1.0,
  25. huge = 1.0e300;
  26. DLLEXPORT long double
  27. atanl(long double x)
  28. {
  29. union IEEEl2bits u;
  30. long double w,s1,s2,z;
  31. int id;
  32. int16_t expsign, expt;
  33. int32_t expman;
  34. u.e = x;
  35. expsign = u.xbits.expsign;
  36. expt = expsign & 0x7fff;
  37. if(expt >= ATAN_CONST) { /* if |x| is large, atan(x)~=pi/2 */
  38. if(expt == BIAS + LDBL_MAX_EXP &&
  39. ((u.bits.manh&~LDBL_NBIT)|u.bits.manl)!=0)
  40. return x+x; /* NaN */
  41. if(expsign>0) return atanhi[3]+atanlo[3];
  42. else return -atanhi[3]-atanlo[3];
  43. }
  44. /* Extract the exponent and the first few bits of the mantissa. */
  45. /* XXX There should be a more convenient way to do this. */
  46. expman = (expt << 8) | ((u.bits.manh >> (MANH_SIZE - 9)) & 0xff);
  47. if (expman < ((BIAS - 2) << 8) + 0xc0) { /* |x| < 0.4375 */
  48. if (expt < ATAN_LINEAR) { /* if |x| is small, atanl(x)~=x */
  49. if(huge+x>one) return x; /* raise inexact */
  50. }
  51. id = -1;
  52. } else {
  53. x = fabsl(x);
  54. if (expman < (BIAS << 8) + 0x30) { /* |x| < 1.1875 */
  55. if (expman < ((BIAS - 1) << 8) + 0x60) { /* 7/16 <=|x|<11/16 */
  56. id = 0; x = (2.0*x-one)/(2.0+x);
  57. } else { /* 11/16<=|x|< 19/16 */
  58. id = 1; x = (x-one)/(x+one);
  59. }
  60. } else {
  61. if (expman < ((BIAS + 1) << 8) + 0x38) { /* |x| < 2.4375 */
  62. id = 2; x = (x-1.5)/(one+1.5*x);
  63. } else { /* 2.4375 <= |x| < 2^ATAN_CONST */
  64. id = 3; x = -1.0/x;
  65. }
  66. }}
  67. /* end of argument reduction */
  68. z = x*x;
  69. w = z*z;
  70. /* break sum aT[i]z**(i+1) into odd and even poly */
  71. s1 = z*T_even(w);
  72. s2 = w*T_odd(w);
  73. if (id<0) return x - x*(s1+s2);
  74. else {
  75. z = atanhi[id] - ((x*(s1+s2) - atanlo[id]) - x);
  76. return (expsign<0)? -z:z;
  77. }
  78. }