OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #ifndef SkRandom_DEFINED | 10 #ifndef SkRandom_DEFINED |
11 #define SkRandom_DEFINED | 11 #define SkRandom_DEFINED |
12 | 12 |
13 #include "SkScalar.h" | 13 #include "SkScalar.h" |
14 | 14 |
15 #ifdef SK_SUPPORT_LEGACY_SK64 | |
16 #include "Sk64.h" | |
17 #endif | |
18 | |
19 /** \class SkLCGRandom | 15 /** \class SkLCGRandom |
20 | 16 |
21 Utility class that implements pseudo random 32bit numbers using a fast | 17 Utility class that implements pseudo random 32bit numbers using a fast |
22 linear equation. Unlike rand(), this class holds its own seed (initially | 18 linear equation. Unlike rand(), this class holds its own seed (initially |
23 set to 0), so that multiple instances can be used with no side-effects. | 19 set to 0), so that multiple instances can be used with no side-effects. |
24 */ | 20 */ |
25 class SkLCGRandom { | 21 class SkLCGRandom { |
26 public: | 22 public: |
27 SkLCGRandom() : fSeed(0) {} | 23 SkLCGRandom() : fSeed(0) {} |
28 SkLCGRandom(uint32_t seed) : fSeed(seed) {} | 24 SkLCGRandom(uint32_t seed) : fSeed(seed) {} |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 } | 123 } |
128 | 124 |
129 /** | 125 /** |
130 * Return the next pseudo random number as a signed 64bit value. | 126 * Return the next pseudo random number as a signed 64bit value. |
131 */ | 127 */ |
132 int64_t next64() { | 128 int64_t next64() { |
133 int64_t hi = this->nextS(); | 129 int64_t hi = this->nextS(); |
134 return (hi << 32) | this->nextU(); | 130 return (hi << 32) | this->nextU(); |
135 } | 131 } |
136 | 132 |
137 #ifdef SK_SUPPORT_LEGACY_SK64 | |
138 SK_ATTR_DEPRECATED("use next64()") | |
139 void next64(Sk64* a) { | |
140 SkASSERT(a); | |
141 a->set(this->nextS(), this->nextU()); | |
142 } | |
143 #endif | |
144 /** | 133 /** |
145 * Return the current seed. This allows the caller to later reset to the | 134 * Return the current seed. This allows the caller to later reset to the |
146 * same seed (using setSeed) so it can generate the same sequence. | 135 * same seed (using setSeed) so it can generate the same sequence. |
147 */ | 136 */ |
148 int32_t getSeed() const { return fSeed; } | 137 int32_t getSeed() const { return fSeed; } |
149 | 138 |
150 /** Set the seed of the random object. The seed is initialized to 0 when the | 139 /** Set the seed of the random object. The seed is initialized to 0 when the |
151 object is first created, and is updated each time the next pseudo random | 140 object is first created, and is updated each time the next pseudo random |
152 number is requested. | 141 number is requested. |
153 */ | 142 */ |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 } | 277 } |
289 | 278 |
290 /** | 279 /** |
291 * Return the next pseudo random number as a signed 64bit value. | 280 * Return the next pseudo random number as a signed 64bit value. |
292 */ | 281 */ |
293 int64_t next64() { | 282 int64_t next64() { |
294 int64_t hi = this->nextS(); | 283 int64_t hi = this->nextS(); |
295 return (hi << 32) | this->nextU(); | 284 return (hi << 32) | this->nextU(); |
296 } | 285 } |
297 | 286 |
298 #ifdef SK_SUPPORT_LEGACY_SK64 | |
299 SK_ATTR_DEPRECATED("use next64()") | |
300 void next64(Sk64* a) { | |
301 SkASSERT(a); | |
302 a->set(this->nextS(), this->nextU()); | |
303 } | |
304 #endif | |
305 | |
306 /** Reset the random object. | 287 /** Reset the random object. |
307 */ | 288 */ |
308 void setSeed(uint32_t seed) { init(seed); } | 289 void setSeed(uint32_t seed) { init(seed); } |
309 | 290 |
310 private: | 291 private: |
311 // Initialize state variables with LCG. | 292 // Initialize state variables with LCG. |
312 // We must ensure that both J and K are non-zero, otherwise the | 293 // We must ensure that both J and K are non-zero, otherwise the |
313 // multiply-with-carry step will forevermore return zero. | 294 // multiply-with-carry step will forevermore return zero. |
314 void init(uint32_t seed) { | 295 void init(uint32_t seed) { |
315 fK = NextLCG(seed); | 296 fK = NextLCG(seed); |
(...skipping 18 matching lines...) Expand all Loading... |
334 enum { | 315 enum { |
335 kKMul = 30345, | 316 kKMul = 30345, |
336 kJMul = 18000, | 317 kJMul = 18000, |
337 }; | 318 }; |
338 | 319 |
339 uint32_t fK; | 320 uint32_t fK; |
340 uint32_t fJ; | 321 uint32_t fJ; |
341 }; | 322 }; |
342 | 323 |
343 #endif | 324 #endif |
OLD | NEW |