xref: /aosp_15_r20/external/libvpx/test/acm_random.h (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
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 VPX_TEST_ACM_RANDOM_H_
12 #define VPX_TEST_ACM_RANDOM_H_
13 
14 #include <assert.h>
15 
16 #include <limits>
17 
18 #include "gtest/gtest.h"
19 
20 #include "vpx/vpx_integer.h"
21 
22 namespace libvpx_test {
23 
24 class ACMRandom {
25  public:
ACMRandom()26   ACMRandom() : random_(DeterministicSeed()) {}
27 
ACMRandom(int seed)28   explicit ACMRandom(int seed) : random_(seed) {}
29 
Reset(int seed)30   void Reset(int seed) { random_.Reseed(seed); }
Rand16()31   uint16_t Rand16() {
32     const uint32_t value =
33         random_.Generate(testing::internal::Random::kMaxRange);
34     return (value >> 15) & 0xffff;
35   }
36 
Rand20Signed()37   int32_t Rand20Signed() {
38     // Use 20 bits: values between 524287 and -524288.
39     const uint32_t value = random_.Generate(1048576);
40     return static_cast<int32_t>(value) - 524288;
41   }
42 
Rand16Signed()43   int16_t Rand16Signed() {
44     // Use 16 bits: values between 32767 and -32768.
45     return static_cast<int16_t>(random_.Generate(65536));
46   }
47 
Rand12()48   uint16_t Rand12() {
49     const uint32_t value =
50         random_.Generate(testing::internal::Random::kMaxRange);
51     // There's a bit more entropy in the upper bits of this implementation.
52     return (value >> 19) & 0xfff;
53   }
54 
Rand8()55   uint8_t Rand8() {
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 >> 23) & 0xff;
60   }
61 
Rand8Extremes()62   uint8_t Rand8Extremes() {
63     // Returns a random value near 0 or near 255, to better exercise
64     // saturation behavior.
65     const uint8_t r = Rand8();
66     return static_cast<uint8_t>((r < 128) ? r << 4 : r >> 4);
67   }
68 
RandRange(const uint32_t range)69   uint32_t RandRange(const uint32_t range) {
70     // testing::internal::Random::Generate provides values in the range
71     // testing::internal::Random::kMaxRange.
72     assert(range <= testing::internal::Random::kMaxRange);
73     return random_.Generate(range);
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 libvpx_test
87 
88 #endif  // VPX_TEST_ACM_RANDOM_H_
89