summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/webkit/JavaScriptCore/wtf/RandomNumber.cpp
diff options
context:
space:
mode:
authorSimon Hausmann <simon.hausmann@nokia.com>2009-06-15 09:06:43 (GMT)
committerSimon Hausmann <simon.hausmann@nokia.com>2009-06-15 09:31:31 (GMT)
commitc411f16870f112c3407c28c22b617f613a82cff4 (patch)
tree29a1bcd590c8b31af2aab445bfe8a978dc5bf582 /src/3rdparty/webkit/JavaScriptCore/wtf/RandomNumber.cpp
parent3d77b56b32a0c53ec0bbfaa07236fedb900ff336 (diff)
downloadQt-c411f16870f112c3407c28c22b617f613a82cff4.zip
Qt-c411f16870f112c3407c28c22b617f613a82cff4.tar.gz
Qt-c411f16870f112c3407c28c22b617f613a82cff4.tar.bz2
Updated WebKit from /home/shausman/src/webkit/trunk to qtwebkit-4.6-snapshot-15062009 ( 65232bf00dc494ebfd978f998c88f58d18ecce1e )
Diffstat (limited to 'src/3rdparty/webkit/JavaScriptCore/wtf/RandomNumber.cpp')
-rw-r--r--src/3rdparty/webkit/JavaScriptCore/wtf/RandomNumber.cpp47
1 files changed, 27 insertions, 20 deletions
diff --git a/src/3rdparty/webkit/JavaScriptCore/wtf/RandomNumber.cpp b/src/3rdparty/webkit/JavaScriptCore/wtf/RandomNumber.cpp
index e569227..c94d5a4 100644
--- a/src/3rdparty/webkit/JavaScriptCore/wtf/RandomNumber.cpp
+++ b/src/3rdparty/webkit/JavaScriptCore/wtf/RandomNumber.cpp
@@ -36,6 +36,16 @@
namespace WTF {
+double weakRandomNumber()
+{
+#if COMPILER(MSVC) && defined(_CRT_RAND_S)
+ // rand_s is incredibly slow on windows so we fall back on rand for Math.random
+ return (rand() + (rand() / (RAND_MAX + 1.0))) / (RAND_MAX + 1.0);
+#else
+ return randomNumber();
+#endif
+}
+
double randomNumber()
{
#if !ENABLE(JSC_MULTIPLE_THREADS)
@@ -46,42 +56,39 @@ double randomNumber()
}
#endif
- uint32_t part1;
- uint32_t part2;
- uint64_t fullRandom;
#if COMPILER(MSVC) && defined(_CRT_RAND_S)
- rand_s(&part1);
- rand_s(&part2);
- fullRandom = part1;
- fullRandom <<= 32;
- fullRandom |= part2;
+ uint32_t bits;
+ rand_s(&bits);
+ return static_cast<double>(bits) / (static_cast<double>(std::numeric_limits<uint32_t>::max()) + 1.0);
#elif PLATFORM(DARWIN)
- part1 = arc4random();
- part2 = arc4random();
- fullRandom = part1;
- fullRandom <<= 32;
- fullRandom |= part2;
+ uint32_t bits = arc4random();
+ return static_cast<double>(bits) / (static_cast<double>(std::numeric_limits<uint32_t>::max()) + 1.0);
#elif PLATFORM(UNIX)
- part1 = random() & (RAND_MAX - 1);
- part2 = random() & (RAND_MAX - 1);
+ uint32_t part1 = random() & (RAND_MAX - 1);
+ uint32_t part2 = random() & (RAND_MAX - 1);
// random only provides 31 bits
- fullRandom = part1;
+ uint64_t fullRandom = part1;
fullRandom <<= 31;
fullRandom |= part2;
+
+ // Mask off the low 53bits
+ fullRandom &= (1LL << 53) - 1;
+ return static_cast<double>(fullRandom)/static_cast<double>(1LL << 53);
#else
- part1 = rand() & (RAND_MAX - 1);
- part2 = rand() & (RAND_MAX - 1);
+ uint32_t part1 = rand() & (RAND_MAX - 1);
+ uint32_t part2 = rand() & (RAND_MAX - 1);
// rand only provides 31 bits, and the low order bits of that aren't very random
// so we take the high 26 bits of part 1, and the high 27 bits of part2.
part1 >>= 5; // drop the low 5 bits
part2 >>= 4; // drop the low 4 bits
- fullRandom = part1;
+ uint64_t fullRandom = part1;
fullRandom <<= 27;
fullRandom |= part2;
-#endif
+
// Mask off the low 53bits
fullRandom &= (1LL << 53) - 1;
return static_cast<double>(fullRandom)/static_cast<double>(1LL << 53);
+#endif
}
}