blob: eb2353d4af34c83f2128227864c03f7b6063bd24 [file] [log] [blame]
Johannc0e80952012-07-20 11:51:06 -07001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Johannc0e80952012-07-20 11:51:06 -07003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * 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.
Johannc0e80952012-07-20 11:51:06 -070010 */
11
Yaowu Xuafffa3d2013-09-05 08:45:56 -070012#ifndef TEST_ACM_RANDOM_H_
13#define TEST_ACM_RANDOM_H_
Johannc0e80952012-07-20 11:51:06 -070014
James Zernc4195e02013-04-04 19:00:31 -070015#include "third_party/googletest/src/include/gtest/gtest.h"
Johannc0e80952012-07-20 11:51:06 -070016
Yaowu Xuf883b422016-08-30 14:01:10 -070017#include "aom/aom_integer.h"
Johannfce8f062012-07-24 10:19:44 -070018
Yaowu Xuc27fc142016-08-22 16:08:15 -070019namespace libaom_test {
Johannc0e80952012-07-20 11:51:06 -070020
21class ACMRandom {
22 public:
James Zernc4195e02013-04-04 19:00:31 -070023 ACMRandom() : random_(DeterministicSeed()) {}
Johannfbea8972012-06-28 11:43:58 -070024
James Zernc4195e02013-04-04 19:00:31 -070025 explicit ACMRandom(int seed) : random_(seed) {}
Johannc0e80952012-07-20 11:51:06 -070026
clang-format3a826f12016-08-11 17:46:05 -070027 void Reset(int seed) { random_.Reseed(seed); }
Yaowu Xu253c0012016-08-15 10:27:19 -070028
29 uint32_t Rand31(void) {
30 return random_.Generate(testing::internal::Random::kMaxRange);
31 }
32
Yaowu Xued7e3d22013-12-13 10:05:40 -080033 uint16_t Rand16(void) {
34 const uint32_t value =
35 random_.Generate(testing::internal::Random::kMaxRange);
James Zern615230b2014-12-01 15:10:00 -080036 return (value >> 15) & 0xffff;
Yaowu Xued7e3d22013-12-13 10:05:40 -080037 }
Johannc0e80952012-07-20 11:51:06 -070038
Johann8c02a362016-04-15 11:35:56 -070039 int16_t Rand9Signed(void) {
40 // Use 9 bits: values between 255 (0x0FF) and -256 (0x100).
41 const uint32_t value = random_.Generate(512);
James Zern13d48c42016-05-27 10:33:56 -070042 return static_cast<int16_t>(value) - 256;
Johann8c02a362016-04-15 11:35:56 -070043 }
44
Johannc0e80952012-07-20 11:51:06 -070045 uint8_t Rand8(void) {
James Zernc4195e02013-04-04 19:00:31 -070046 const uint32_t value =
47 random_.Generate(testing::internal::Random::kMaxRange);
48 // There's a bit more entropy in the upper bits of this implementation.
James Zern30631932014-11-26 15:17:49 -080049 return (value >> 23) & 0xff;
Johannc0e80952012-07-20 11:51:06 -070050 }
51
John Koleszara9ebbcc2013-04-18 13:05:38 -070052 uint8_t Rand8Extremes(void) {
53 // Returns a random value near 0 or near 255, to better exercise
54 // saturation behavior.
55 const uint8_t r = Rand8();
56 return r < 128 ? r << 4 : r >> 4;
57 }
58
clang-format3a826f12016-08-11 17:46:05 -070059 int PseudoUniform(int range) { return random_.Generate(range); }
Johannc0e80952012-07-20 11:51:06 -070060
clang-format3a826f12016-08-11 17:46:05 -070061 int operator()(int n) { return PseudoUniform(n); }
Johannc0e80952012-07-20 11:51:06 -070062
clang-format3a826f12016-08-11 17:46:05 -070063 static int DeterministicSeed(void) { return 0xbaba; }
James Zernc4195e02013-04-04 19:00:31 -070064
65 private:
66 testing::internal::Random random_;
Johannc0e80952012-07-20 11:51:06 -070067};
68
Yaowu Xuc27fc142016-08-22 16:08:15 -070069} // namespace libaom_test
Johannc0e80952012-07-20 11:51:06 -070070
Yaowu Xuafffa3d2013-09-05 08:45:56 -070071#endif // TEST_ACM_RANDOM_H_