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

Unified Diff: src/v8.cc

Issue 1599019: Change Math.random() to return 32 bits of random goodness, instead of 30 rand... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 10 years, 8 months 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/v8.h ('k') | src/x64/codegen-x64.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/v8.cc
===================================================================
--- src/v8.cc (revision 4353)
+++ src/v8.cc (working copy)
@@ -208,14 +208,27 @@
return Heap::IdleNotification();
}
-static const uint32_t kRandomPositiveSmiMax = 0x3fffffff;
-Smi* V8::RandomPositiveSmi() {
- uint32_t random = Random();
- ASSERT(static_cast<uint32_t>(Smi::kMaxValue) >= kRandomPositiveSmiMax);
- // kRandomPositiveSmiMax must match the value being divided
- // by in math.js.
- return Smi::FromInt(random & kRandomPositiveSmiMax);
+// Use a union type to avoid type-aliasing optimizations in GCC.
+typedef union {
+ double double_value;
+ uint64_t uint64_t_value;
+} double_int_union;
+
+
+Object* V8::FillHeapNumberWithRandom(Object* heap_number) {
+ uint64_t random_bits = Random();
+ // Make a double* from address (heap_number + sizeof(double)).
+ double_int_union* r = reinterpret_cast<double_int_union*>(
+ reinterpret_cast<char*>(heap_number) +
+ HeapNumber::kValueOffset - kHeapObjectTag);
+ // Create a random number between 0.0 and 1.0 by putting random bits into
+ // the mantissa of 1.0 and subtracting 1.0.
+ r->double_value = 1.0;
+ r->uint64_t_value |= (random_bits << 20);
+ r->double_value -= 1.0; // Force into the range [0.0, 1.0).
+
+ return heap_number;
}
} } // namespace v8::internal
« no previous file with comments | « src/v8.h ('k') | src/x64/codegen-x64.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698