• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef LIBGAV1_TESTS_THIRD_PARTY_LIBVPX_ACM_RANDOM_H_
12 #define LIBGAV1_TESTS_THIRD_PARTY_LIBVPX_ACM_RANDOM_H_
13 
14 #include <cassert>
15 #include <cstdint>
16 #include <limits>
17 
18 #include "gtest/gtest.h"
19 
20 namespace libvpx_test {
21 
22 class ACMRandom {
23  public:
ACMRandom()24   ACMRandom() : random_(DeterministicSeed()) {}
25 
ACMRandom(int seed)26   explicit ACMRandom(int seed) : random_(seed) {}
27 
Reset(int seed)28   void Reset(int seed) { random_.Reseed(seed); }
Rand16(void)29   uint16_t Rand16(void) {
30     const uint32_t value =
31         random_.Generate(testing::internal::Random::kMaxRange);
32     return (value >> 15) & 0xffff;
33   }
34 
Rand20Signed(void)35   int32_t Rand20Signed(void) {
36     // Use 20 bits: values between 524287 and -524288.
37     const uint32_t value = random_.Generate(1048576);
38     return static_cast<int32_t>(value) - 524288;
39   }
40 
Rand16Signed(void)41   int16_t Rand16Signed(void) {
42     // Use 16 bits: values between 32767 and -32768.
43     return static_cast<int16_t>(random_.Generate(65536));
44   }
45 
Rand13Signed(void)46   int16_t Rand13Signed(void) {
47     // Use 13 bits: values between 4095 and -4096.
48     const uint32_t value = random_.Generate(8192);
49     return static_cast<int16_t>(value) - 4096;
50   }
51 
Rand9Signed(void)52   int16_t Rand9Signed(void) {
53     // Use 9 bits: values between 255 (0x0FF) and -256 (0x100).
54     const uint32_t value = random_.Generate(512);
55     return static_cast<int16_t>(value) - 256;
56   }
57 
Rand8(void)58   uint8_t Rand8(void) {
59     const uint32_t value =
60         random_.Generate(testing::internal::Random::kMaxRange);
61     // There's a bit more entropy in the upper bits of this implementation.
62     return (value >> 23) & 0xff;
63   }
64 
Rand8Extremes(void)65   uint8_t Rand8Extremes(void) {
66     // Returns a random value near 0 or near 255, to better exercise
67     // saturation behavior.
68     const uint8_t r = Rand8();
69     return static_cast<uint8_t>((r < 128) ? r << 4 : r >> 4);
70   }
71 
RandRange(const uint32_t range)72   uint32_t RandRange(const uint32_t range) {
73     // testing::internal::Random::Generate provides values in the range
74     // testing::internal::Random::kMaxRange.
75     assert(range <= testing::internal::Random::kMaxRange);
76     return random_.Generate(range);
77   }
78 
PseudoUniform(int range)79   int PseudoUniform(int range) { return random_.Generate(range); }
80 
operator()81   int operator()(int n) { return PseudoUniform(n); }
82 
DeterministicSeed(void)83   static constexpr int DeterministicSeed(void) { return 0xbaba; }
84 
85  private:
86   testing::internal::Random random_;
87 };
88 
89 }  // namespace libvpx_test
90 
91 #endif  // LIBGAV1_TESTS_THIRD_PARTY_LIBVPX_ACM_RANDOM_H_
92