Explicitly change a floating point division with a constant into a
authorager@chromium.org <ager@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Thu, 20 Nov 2008 11:33:50 +0000 (11:33 +0000)
committerager@chromium.org <ager@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Thu, 20 Nov 2008 11:33:50 +0000 (11:33 +0000)
multiplication by its inverse.

In optimized builds GCC does this on its own, but this may be useful
when using other compilers.
Review URL: http://codereview.chromium.org/11524

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@805 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/runtime.cc

index 5e60c0d..71b5fed 100644 (file)
@@ -3163,9 +3163,9 @@ static Object* Runtime_Math_random(Arguments args) {
   // double in the range [0, RAND_MAX + 1) obtained by adding the
   // high-order bits in the range [0, RAND_MAX] with the low-order
   // bits in the range [0, 1).
-  double lo = static_cast<double>(random()) / (RAND_MAX + 1.0);
+  double lo = static_cast<double>(random()) * (1.0 / (RAND_MAX + 1.0));
   double hi = static_cast<double>(random());
-  double result = (hi + lo) / (RAND_MAX + 1.0);
+  double result = (hi + lo) * (1.0 / (RAND_MAX + 1.0));
   ASSERT(result >= 0 && result < 1);
   return Heap::AllocateHeapNumber(result);
 }