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