e_exp.S 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Written by:
  3. * J.T. Conklin ([email protected])
  4. * Public domain.
  5. */
  6. #include <i387/bsd_asm.h>
  7. /* e^x = 2^(x * log2(e)) */
  8. ENTRY(exp)
  9. /*
  10. * If x is +-Inf, then the subtraction would give Inf-Inf = NaN.
  11. * Avoid this. Also avoid it if x is NaN for convenience.
  12. */
  13. movl 8(%esp),%eax
  14. andl $0x7fffffff,%eax
  15. cmpl $0x7ff00000,%eax
  16. jae x_Inf_or_NaN
  17. fldl 4(%esp)
  18. /*
  19. * Extended precision is needed to reduce the maximum error from
  20. * hundreds of ulps to less than 1 ulp. Switch to it if necessary.
  21. * We may as well set the rounding mode to to-nearest and mask traps
  22. * if we switch.
  23. */
  24. fstcw 4(%esp)
  25. movl 4(%esp),%eax
  26. andl $0x0300,%eax
  27. cmpl $0x0300,%eax /* RC == 0 && PC == 3? */
  28. je 1f /* jump if mode is good */
  29. movl $0x137f,8(%esp)
  30. fldcw 8(%esp)
  31. 1:
  32. fldl2e
  33. fmulp /* x * log2(e) */
  34. fst %st(1)
  35. frndint /* int(x * log2(e)) */
  36. fst %st(2)
  37. fsubrp /* fract(x * log2(e)) */
  38. f2xm1 /* 2^(fract(x * log2(e))) - 1 */
  39. fld1
  40. faddp /* 2^(fract(x * log2(e))) */
  41. fscale /* e^x */
  42. fstp %st(1)
  43. je 1f
  44. fldcw 4(%esp)
  45. 1:
  46. ret
  47. x_Inf_or_NaN:
  48. /*
  49. * Return 0 if x is -Inf. Otherwise just return x; when x is Inf
  50. * this gives Inf, and when x is a NaN this gives the same result
  51. * as (x + x) (x quieted).
  52. */
  53. cmpl $0xfff00000,8(%esp)
  54. jne x_not_minus_Inf
  55. cmpl $0,4(%esp)
  56. jne x_not_minus_Inf
  57. fldz
  58. ret
  59. x_not_minus_Inf:
  60. fldl 4(%esp)
  61. ret
  62. END(exp)
  63. //
  64. /* Enable stack protection */
  65. #if defined(__ELF__)
  66. .section .note.GNU-stack,"",%progbits
  67. #endif