Index: src/conversions-inl.h |
diff --git a/src/conversions-inl.h b/src/conversions-inl.h |
index ba7220a4a62b90151b41275e3dbfaa23f3b878ff..36c7015177c1a31aa41a3ad0eaf219e26e4252bf 100644 |
--- a/src/conversions-inl.h |
+++ b/src/conversions-inl.h |
@@ -59,6 +59,30 @@ static inline int FastD2I(double x) { |
} |
+// The fast double-to-unsigned-int conversion routine does not guarantee |
+// rounding towards zero. |
+static inline unsigned int FastD2UI(double x) { |
+ // There is no unsigned version of lrint, so there is no fast path |
+ // in this function as there is in FastD2I. Using lrint doesn't work |
+ // for values of 2^31 and above. |
+ |
+ // Convert "small enough" doubles to uint32_t by fixing the 32 |
+ // least significant non-fractional bits in the low 32 bits of the |
+ // double, and reading them from there. |
+ const double k2Pow52 = 4503599627370496.0; |
+ bool negative = x < 0; |
+ if (negative) { x = -x; } |
Mads Ager (chromium)
2010/03/03 13:16:50
Remove the braces or split on multiple lines.
|
+ if (x < k2Pow52) { |
+ x += k2Pow52; |
+ uint32_t result; |
+ memcpy(&result, &x, sizeof(result)); // Copy low 32 bits. |
+ return negative ? ~result + 1 : result; |
+ } |
+ // Large number (outside uint32 range), Infinity or NaN. |
+ return 0x80000000u; // Return integer indefinite. |
+} |
+ |
+ |
static inline double DoubleToInteger(double x) { |
if (isnan(x)) return 0; |
if (!isfinite(x) || x == 0) return x; |