e_exp2_template.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* Compute 2^x.
  2. Copyright (C) 2012-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <math.h>
  16. #include <math_private.h>
  17. #include <math-underflow.h>
  18. #include <float.h>
  19. FLOAT
  20. M_DECL_FUNC (__ieee754_exp2) (FLOAT x)
  21. {
  22. if (__glibc_likely (isless (x, (FLOAT) M_MAX_EXP)))
  23. {
  24. if (__builtin_expect (isgreaterequal (x, (FLOAT) (M_MIN_EXP - M_MANT_DIG
  25. - 1)), 1))
  26. {
  27. int intx = (int) x;
  28. FLOAT fractx = x - intx;
  29. FLOAT result;
  30. if (M_FABS (fractx) < M_EPSILON / 4)
  31. result = M_SCALBN (1 + fractx, intx);
  32. else
  33. result = M_SCALBN (M_EXP (M_MLIT (M_LN2) * fractx), intx);
  34. math_check_force_underflow_nonneg (result);
  35. return result;
  36. }
  37. else
  38. {
  39. /* Underflow or exact zero. */
  40. if (isinf (x))
  41. return 0;
  42. else
  43. return M_MIN * M_MIN;
  44. }
  45. }
  46. else
  47. /* Infinity, NaN or overflow. */
  48. return M_MAX * x;
  49. }
  50. declare_mgen_finite_alias (__ieee754_exp2, __exp2)