• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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()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 
Rand9Signed()62   int16_t Rand9Signed() {
63     // Use 9 bits: values between 255 (0x0FF) and -256 (0x100).
64     const uint32_t value = random_.Generate(512);
65     return static_cast<int16_t>(value) - 256;
66   }
67 
Rand8()68   uint8_t Rand8() {
69     const uint32_t value =
70         random_.Generate(testing::internal::Random::kMaxRange);
71     // There's a bit more entropy in the upper bits of this implementation.
72     return (value >> 23) & 0xff;
73   }
74 
Rand8Extremes()75   uint8_t Rand8Extremes() {
76     // Returns a random value near 0 or near 255, to better exercise
77     // saturation behavior.
78     const uint8_t r = Rand8();
79     return static_cast<uint8_t>((r < 128) ? r << 4 : r >> 4);
80   }
81 
PseudoUniform(int range)82   int PseudoUniform(int range) { return random_.Generate(range); }
83 
operator()84   int operator()(int n) { return PseudoUniform(n); }
85 
DeterministicSeed()86   static int DeterministicSeed() { return 0xbaba; }
87 
88  private:
89   testing::internal::Random random_;
90 };
91 
92 }  // namespace libaom_test
93 
94 #endif  // AOM_TEST_ACM_RANDOM_H_
95