| OLD | NEW |
| 1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2009 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 | 96 |
| 97 | 97 |
| 98 static uint32_t random_seed() { | 98 static uint32_t random_seed() { |
| 99 if (FLAG_random_seed == 0) { | 99 if (FLAG_random_seed == 0) { |
| 100 return random(); | 100 return random(); |
| 101 } | 101 } |
| 102 return FLAG_random_seed; | 102 return FLAG_random_seed; |
| 103 } | 103 } |
| 104 | 104 |
| 105 | 105 |
| 106 uint32_t V8::Random() { | 106 uint32_t V8::Random(Isolate* isolate) { |
| 107 ASSERT(isolate == Isolate::Current()); |
| 108 // TODO(isolates): move lo and hi to isolate |
| 107 // Random number generator using George Marsaglia's MWC algorithm. | 109 // Random number generator using George Marsaglia's MWC algorithm. |
| 108 static uint32_t hi = 0; | 110 static uint32_t hi = 0; |
| 109 static uint32_t lo = 0; | 111 static uint32_t lo = 0; |
| 110 | 112 |
| 111 // Initialize seed using the system random(). If one of the seeds | 113 // Initialize seed using the system random(). If one of the seeds |
| 112 // should ever become zero again, or if random() returns zero, we | 114 // should ever become zero again, or if random() returns zero, we |
| 113 // avoid getting stuck with zero bits in hi or lo by re-initializing | 115 // avoid getting stuck with zero bits in hi or lo by re-initializing |
| 114 // them on demand. | 116 // them on demand. |
| 115 if (hi == 0) hi = random_seed(); | 117 if (hi == 0) hi = random_seed(); |
| 116 if (lo == 0) lo = random_seed(); | 118 if (lo == 0) lo = random_seed(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 133 | 135 |
| 134 | 136 |
| 135 // Use a union type to avoid type-aliasing optimizations in GCC. | 137 // Use a union type to avoid type-aliasing optimizations in GCC. |
| 136 typedef union { | 138 typedef union { |
| 137 double double_value; | 139 double double_value; |
| 138 uint64_t uint64_t_value; | 140 uint64_t uint64_t_value; |
| 139 } double_int_union; | 141 } double_int_union; |
| 140 | 142 |
| 141 | 143 |
| 142 Object* V8::FillHeapNumberWithRandom(Object* heap_number) { | 144 Object* V8::FillHeapNumberWithRandom(Object* heap_number) { |
| 143 uint64_t random_bits = Random(); | 145 uint64_t random_bits = Random(Isolate::Current()); |
| 144 // Make a double* from address (heap_number + sizeof(double)). | 146 // Make a double* from address (heap_number + sizeof(double)). |
| 145 double_int_union* r = reinterpret_cast<double_int_union*>( | 147 double_int_union* r = reinterpret_cast<double_int_union*>( |
| 146 reinterpret_cast<char*>(heap_number) + | 148 reinterpret_cast<char*>(heap_number) + |
| 147 HeapNumber::kValueOffset - kHeapObjectTag); | 149 HeapNumber::kValueOffset - kHeapObjectTag); |
| 148 // Convert 32 random bits to 0.(32 random bits) in a double | 150 // Convert 32 random bits to 0.(32 random bits) in a double |
| 149 // by computing: | 151 // by computing: |
| 150 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)). | 152 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)). |
| 151 const double binary_million = 1048576.0; | 153 const double binary_million = 1048576.0; |
| 152 r->double_value = binary_million; | 154 r->double_value = binary_million; |
| 153 r->uint64_t_value |= random_bits; | 155 r->uint64_t_value |= random_bits; |
| 154 r->double_value -= binary_million; | 156 r->double_value -= binary_million; |
| 155 | 157 |
| 156 return heap_number; | 158 return heap_number; |
| 157 } | 159 } |
| 158 | 160 |
| 159 } } // namespace v8::internal | 161 } } // namespace v8::internal |
| OLD | NEW |