tor  0.4.0.1-alpha
fp.c
Go to the documentation of this file.
1 /* Copyright (c) 2003, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
12 #include "orconfig.h"
13 #include "lib/math/fp.h"
14 
15 #include <math.h>
16 
21 double
22 tor_mathlog(double d)
23 {
24  return log(d);
25 }
26 
30 long
31 tor_lround(double d)
32 {
33 #if defined(HAVE_LROUND)
34  return lround(d);
35 #elif defined(HAVE_RINT)
36  return (long)rint(d);
37 #else
38  return (long)(d > 0 ? d + 0.5 : ceil(d - 0.5));
39 #endif /* defined(HAVE_LROUND) || ... */
40 }
41 
45 int64_t
46 tor_llround(double d)
47 {
48 #if defined(HAVE_LLROUND)
49  return (int64_t)llround(d);
50 #elif defined(HAVE_RINT)
51  return (int64_t)rint(d);
52 #else
53  return (int64_t)(d > 0 ? d + 0.5 : ceil(d - 0.5));
54 #endif /* defined(HAVE_LLROUND) || ... */
55 }
56 
60 int64_t
61 clamp_double_to_int64(double number)
62 {
63  int exponent;
64 
65 #if defined(MINGW_ANY) && GCC_VERSION >= 409
66 /*
67  Mingw's math.h uses gcc's __builtin_choose_expr() facility to declare
68  isnan, isfinite, and signbit. But as implemented in at least some
69  versions of gcc, __builtin_choose_expr() can generate type warnings
70  even from branches that are not taken. So, suppress those warnings.
71 */
72 #define PROBLEMATIC_FLOAT_CONVERSION_WARNING
73 DISABLE_GCC_WARNING(float-conversion)
74 #endif /* defined(MINGW_ANY) && GCC_VERSION >= 409 */
75 
76 /*
77  With clang 4.0 we apparently run into "double promotion" warnings here,
78  since clang thinks we're promoting a double to a long double.
79  */
80 #if defined(__clang__)
81 #if __has_warning("-Wdouble-promotion")
82 #define PROBLEMATIC_DOUBLE_PROMOTION_WARNING
83 DISABLE_GCC_WARNING(double-promotion)
84 #endif
85 #endif /* defined(__clang__) */
86 
87  /* NaN is a special case that can't be used with the logic below. */
88  if (isnan(number)) {
89  return 0;
90  }
91 
92  /* Time to validate if result can overflows a int64_t value. Fun with
93  * float! Find that exponent exp such that
94  * number == x * 2^exp
95  * for some x with abs(x) in [0.5, 1.0). Note that this implies that the
96  * magnitude of number is strictly less than 2^exp.
97  *
98  * If number is infinite, the call to frexp is legal but the contents of
99  * are exponent unspecified. */
100  frexp(number, &exponent);
101 
102  /* If the magnitude of number is strictly less than 2^63, the truncated
103  * version of number is guaranteed to be representable. The only
104  * representable integer for which this is not the case is INT64_MIN, but
105  * it is covered by the logic below. */
106  if (isfinite(number) && exponent <= 63) {
107  return (int64_t)number;
108  }
109 
110  /* Handle infinities and finite numbers with magnitude >= 2^63. */
111  return signbit(number) ? INT64_MIN : INT64_MAX;
112 
113 #ifdef PROBLEMATIC_DOUBLE_PROMOTION_WARNING
114 ENABLE_GCC_WARNING(double-promotion)
115 #endif
116 #ifdef PROBLEMATIC_FLOAT_CONVERSION_WARNING
117 ENABLE_GCC_WARNING(float-conversion)
118 #endif
119 }
120 
121 /* isinf() wrapper for tor */
122 int
123 tor_isinf(double x)
124 {
125  /* Same as above, work around the "double promotion" warnings */
126 #if defined(MINGW_ANY) && GCC_VERSION >= 409
127 #define PROBLEMATIC_FLOAT_CONVERSION_WARNING
128 DISABLE_GCC_WARNING(float-conversion)
129 #endif /* defined(MINGW_ANY) && GCC_VERSION >= 409 */
130 #if defined(__clang__)
131 #if __has_warning("-Wdouble-promotion")
132 #define PROBLEMATIC_DOUBLE_PROMOTION_WARNING
133 DISABLE_GCC_WARNING(double-promotion)
134 #endif
135 #endif /* defined(__clang__) */
136  return isinf(x);
137 #ifdef PROBLEMATIC_DOUBLE_PROMOTION_WARNING
138 ENABLE_GCC_WARNING(double-promotion)
139 #endif
140 #ifdef PROBLEMATIC_FLOAT_CONVERSION_WARNING
141 ENABLE_GCC_WARNING(float-conversion)
142 #endif
143 }
144 
int64_t tor_llround(double d)
Definition: fp.c:46
double tor_mathlog(double d)
Definition: fp.c:22
Header for fp.c.
long tor_lround(double d)
Definition: fp.c:31
int64_t clamp_double_to_int64(double number)
Definition: fp.c:61