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 "third_party/googletest/src/googletest/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(void)30 uint32_t Rand31(void) { 31 return random_.Generate(testing::internal::Random::kMaxRange); 32 } 33 Rand16(void)34 uint16_t Rand16(void) { 35 const uint32_t value = 36 random_.Generate(testing::internal::Random::kMaxRange); 37 return (value >> 15) & 0xffff; 38 } 39 Rand15Signed(void)40 int16_t Rand15Signed(void) { 41 const uint32_t value = 42 random_.Generate(testing::internal::Random::kMaxRange); 43 return (value >> 17) & 0xffff; 44 } 45 Rand12(void)46 uint16_t Rand12(void) { 47 const uint32_t value = 48 random_.Generate(testing::internal::Random::kMaxRange); 49 // There's a bit more entropy in the upper bits of this implementation. 50 return (value >> 19) & 0xfff; 51 } 52 Rand9Signed(void)53 int16_t Rand9Signed(void) { 54 // Use 9 bits: values between 255 (0x0FF) and -256 (0x100). 55 const uint32_t value = random_.Generate(512); 56 return static_cast<int16_t>(value) - 256; 57 } 58 Rand8(void)59 uint8_t Rand8(void) { 60 const uint32_t value = 61 random_.Generate(testing::internal::Random::kMaxRange); 62 // There's a bit more entropy in the upper bits of this implementation. 63 return (value >> 23) & 0xff; 64 } 65 Rand8Extremes(void)66 uint8_t Rand8Extremes(void) { 67 // Returns a random value near 0 or near 255, to better exercise 68 // saturation behavior. 69 const uint8_t r = Rand8(); 70 return r < 128 ? r << 4 : r >> 4; 71 } 72 PseudoUniform(int range)73 int PseudoUniform(int range) { return random_.Generate(range); } 74 operator()75 int operator()(int n) { return PseudoUniform(n); } 76 DeterministicSeed(void)77 static int DeterministicSeed(void) { return 0xbaba; } 78 79 private: 80 testing::internal::Random random_; 81 }; 82 83 } // namespace libaom_test 84 85 #endif // AOM_TEST_ACM_RANDOM_H_ 86