PageRenderTime 850ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/mpfr/src/uceil_exp2.c

https://github.com/chrisvana/mpfr_copy
C | 65 lines | 37 code | 3 blank | 25 comment | 7 complexity | d111931e2eacc8d7d514b65094ca14e3 MD5 | raw file
Possible License(s): GPL-3.0
  1. /* __gmpfr_ceil_exp2 - returns y >= 2^d
  2. Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
  3. Contributed by the AriC and Caramel projects, INRIA.
  4. This file is part of the GNU MPFR Library.
  5. The GNU MPFR Library is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or (at your
  8. option) any later version.
  9. The GNU MPFR Library is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  12. License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
  15. http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
  16. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
  17. #include "mpfr-impl.h"
  18. /* returns y >= 2^d, assuming that d <= 1024
  19. for d integer, returns exactly 2^d
  20. */
  21. double
  22. __gmpfr_ceil_exp2 (double d)
  23. {
  24. long exp;
  25. #if _GMP_IEEE_FLOATS
  26. union ieee_double_extract x;
  27. #else
  28. struct {double d;} x;
  29. #endif
  30. MPFR_ASSERTN(d <= 1024.0);
  31. exp = (long) d;
  32. if (d != (double) exp)
  33. exp++;
  34. /* now exp = ceil(d) */
  35. x.d = 1.0;
  36. #if _GMP_IEEE_FLOATS
  37. x.s.exp = exp <= -1022 ? 1 : 1023 + exp;
  38. #else
  39. if (exp >= 0)
  40. {
  41. while (exp != 0)
  42. {
  43. x.d *= 2.0;
  44. exp--;
  45. }
  46. }
  47. else
  48. {
  49. while (exp != 0)
  50. {
  51. x.d *= (1.0 / 2.0);
  52. exp++;
  53. }
  54. }
  55. #endif
  56. return x.d;
  57. }