fp.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Copyright (c) 2003, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #include "lib/math/fp.h"
  7. #include <math.h>
  8. /**
  9. * Returns the natural logarithm of d base e. We defined this wrapper here so
  10. * to avoid conflicts with old versions of tor_log(), which were named log().
  11. */
  12. double
  13. tor_mathlog(double d)
  14. {
  15. return log(d);
  16. }
  17. /** Return the long integer closest to <b>d</b>. We define this wrapper
  18. * here so that not all users of math.h need to use the right incantations
  19. * to get the c99 functions. */
  20. long
  21. tor_lround(double d)
  22. {
  23. #if defined(HAVE_LROUND)
  24. return lround(d);
  25. #elif defined(HAVE_RINT)
  26. return (long)rint(d);
  27. #else
  28. return (long)(d > 0 ? d + 0.5 : ceil(d - 0.5));
  29. #endif /* defined(HAVE_LROUND) || ... */
  30. }
  31. /** Return the 64-bit integer closest to d. We define this wrapper here so
  32. * that not all users of math.h need to use the right incantations to get the
  33. * c99 functions. */
  34. int64_t
  35. tor_llround(double d)
  36. {
  37. #if defined(HAVE_LLROUND)
  38. return (int64_t)llround(d);
  39. #elif defined(HAVE_RINT)
  40. return (int64_t)rint(d);
  41. #else
  42. return (int64_t)(d > 0 ? d + 0.5 : ceil(d - 0.5));
  43. #endif /* defined(HAVE_LLROUND) || ... */
  44. }
  45. /** Cast a given double value to a int64_t. Return 0 if number is NaN.
  46. * Returns either INT64_MIN or INT64_MAX if number is outside of the int64_t
  47. * range. */
  48. int64_t
  49. clamp_double_to_int64(double number)
  50. {
  51. int exponent;
  52. #if defined(MINGW_ANY) && GCC_VERSION >= 409
  53. /*
  54. Mingw's math.h uses gcc's __builtin_choose_expr() facility to declare
  55. isnan, isfinite, and signbit. But as implemented in at least some
  56. versions of gcc, __builtin_choose_expr() can generate type warnings
  57. even from branches that are not taken. So, suppress those warnings.
  58. */
  59. #define PROBLEMATIC_FLOAT_CONVERSION_WARNING
  60. DISABLE_GCC_WARNING(float-conversion)
  61. #endif /* defined(MINGW_ANY) && GCC_VERSION >= 409 */
  62. /*
  63. With clang 4.0 we apparently run into "double promotion" warnings here,
  64. since clang thinks we're promoting a double to a long double.
  65. */
  66. #if defined(__clang__)
  67. #if __has_warning("-Wdouble-promotion")
  68. #define PROBLEMATIC_DOUBLE_PROMOTION_WARNING
  69. DISABLE_GCC_WARNING(double-promotion)
  70. #endif
  71. #endif /* defined(__clang__) */
  72. /* NaN is a special case that can't be used with the logic below. */
  73. if (isnan(number)) {
  74. return 0;
  75. }
  76. /* Time to validate if result can overflows a int64_t value. Fun with
  77. * float! Find that exponent exp such that
  78. * number == x * 2^exp
  79. * for some x with abs(x) in [0.5, 1.0). Note that this implies that the
  80. * magnitude of number is strictly less than 2^exp.
  81. *
  82. * If number is infinite, the call to frexp is legal but the contents of
  83. * are exponent unspecified. */
  84. frexp(number, &exponent);
  85. /* If the magnitude of number is strictly less than 2^63, the truncated
  86. * version of number is guaranteed to be representable. The only
  87. * representable integer for which this is not the case is INT64_MIN, but
  88. * it is covered by the logic below. */
  89. if (isfinite(number) && exponent <= 63) {
  90. return (int64_t)number;
  91. }
  92. /* Handle infinities and finite numbers with magnitude >= 2^63. */
  93. return signbit(number) ? INT64_MIN : INT64_MAX;
  94. #ifdef PROBLEMATIC_DOUBLE_PROMOTION_WARNING
  95. ENABLE_GCC_WARNING(double-promotion)
  96. #endif
  97. #ifdef PROBLEMATIC_FLOAT_CONVERSION_WARNING
  98. ENABLE_GCC_WARNING(float-conversion)
  99. #endif
  100. }