Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Unified Diff: src/conversions-inl.h

Issue 2577143002: [runtime] Add PositiveNumberToUint32 helper to avoid double to uint roundtrip (Closed)
Patch Set: avoid overflows Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/conversions.h ('k') | src/objects.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/conversions-inl.h
diff --git a/src/conversions-inl.h b/src/conversions-inl.h
index 2086f8184a1d212c8555d7d0fa4b24900be03fa7..d7789f9180880868baac232fbc4eeab9e58cb4d9 100644
--- a/src/conversions-inl.h
+++ b/src/conversions-inl.h
@@ -128,12 +128,26 @@ int32_t NumberToInt32(Object* number) {
return DoubleToInt32(number->Number());
}
-
uint32_t NumberToUint32(Object* number) {
if (number->IsSmi()) return Smi::cast(number)->value();
return DoubleToUint32(number->Number());
}
+uint32_t PositiveNumberToUint32(Object* number) {
+ if (number->IsSmi()) {
+ int value = Smi::cast(number)->value();
+ if (value <= 0) return 0;
+ return value;
+ }
+ DCHECK(number->IsHeapNumber());
+ double value = number->Number();
+ // Catch all values smaller than 1 and use the double-negation trick for NANs.
+ if (!(value >= 1)) return 0;
+ uint32_t max = std::numeric_limits<uint32_t>::max();
+ if (value < max) return static_cast<uint32_t>(value);
+ return max;
+}
+
int64_t NumberToInt64(Object* number) {
if (number->IsSmi()) return Smi::cast(number)->value();
return static_cast<int64_t>(number->Number());
« no previous file with comments | « src/conversions.h ('k') | src/objects.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698