1 /* 2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved. 3 * 4 * This source code is subject to the terms of the BSD 2 Clause License and 5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License 6 * was not distributed with this source code in the LICENSE file, you can 7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open 8 * Media Patent License 1.0 was not distributed with this source code in the 9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent. 10 */ 11 12 #ifndef AOM_TEST_ACM_RANDOM_H_ 13 #define AOM_TEST_ACM_RANDOM_H_ 14 15 #include "gtest/gtest.h" 16 17 #include "aom/aom_integer.h" 18 19 namespace libaom_test { 20 21 class ACMRandom { 22 public: ACMRandom()23 ACMRandom() : random_(DeterministicSeed()) {} 24 ACMRandom(int seed)25 explicit ACMRandom(int seed) : random_(seed) {} 26 Reset(int seed)27 void Reset(int seed) { random_.Reseed(seed); } 28 29 // Generates a random 31-bit unsigned integer from [0, 2^31). Rand31()30 uint32_t Rand31() { 31 return random_.Generate(testing::internal::Random::kMaxRange); 32 } 33 Rand16()34 uint16_t Rand16() { 35 const uint32_t value = 36 random_.Generate(testing::internal::Random::kMaxRange); 37 // There's a bit more entropy in the upper bits of this implementation. 38 return (value >> 15) & 0xffff; 39 } 40 Rand16Signed()41 int16_t Rand16Signed() { return static_cast<int16_t>(Rand16()); } 42 Rand15()43 int16_t Rand15() { 44 const uint32_t value = 45 random_.Generate(testing::internal::Random::kMaxRange); 46 // There's a bit more entropy in the upper bits of this implementation. 47 return (value >> 16) & 0x7fff; 48 } 49 Rand15Signed()50 int16_t Rand15Signed() { 51 // Use 15 bits: values between 16383 (0x3FFF) and -16384 (0xC000). 52 return static_cast<int16_t>(Rand15()) - (1 << 14); 53 } 54 Rand12()55 uint16_t Rand12() { 56 const uint32_t value = 57 random_.Generate(testing::internal::Random::kMaxRange); 58 // There's a bit more entropy in the upper bits of this implementation. 59 return (value >> 19) & 0xfff; 60 } 61 Rand8()62 uint8_t Rand8() { 63 const uint32_t value = 64 random_.Generate(testing::internal::Random::kMaxRange); 65 // There's a bit more entropy in the upper bits of this implementation. 66 return (value >> 23) & 0xff; 67 } 68 Rand8Extremes()69 uint8_t Rand8Extremes() { 70 // Returns a random value near 0 or near 255, to better exercise 71 // saturation behavior. 72 const uint8_t r = Rand8(); 73 return static_cast<uint8_t>((r < 128) ? r << 4 : r >> 4); 74 } 75 PseudoUniform(int range)76 int PseudoUniform(int range) { return random_.Generate(range); } 77 operator()78 int operator()(int n) { return PseudoUniform(n); } 79 DeterministicSeed()80 static int DeterministicSeed() { return 0xbaba; } 81 82 private: 83 testing::internal::Random random_; 84 }; 85 86 } // namespace libaom_test 87 88 #endif // AOM_TEST_ACM_RANDOM_H_ 89