Johann | c0e8095 | 2012-07-20 11:51:06 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Johann | c0e8095 | 2012-07-20 11:51:06 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 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. |
Johann | c0e8095 | 2012-07-20 11:51:06 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Yaowu Xu | afffa3d | 2013-09-05 08:45:56 -0700 | [diff] [blame] | 12 | #ifndef TEST_ACM_RANDOM_H_ |
| 13 | #define TEST_ACM_RANDOM_H_ |
Johann | c0e8095 | 2012-07-20 11:51:06 -0700 | [diff] [blame] | 14 | |
James Zern | c4195e0 | 2013-04-04 19:00:31 -0700 | [diff] [blame] | 15 | #include "third_party/googletest/src/include/gtest/gtest.h" |
Johann | c0e8095 | 2012-07-20 11:51:06 -0700 | [diff] [blame] | 16 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 17 | #include "aom/aom_integer.h" |
Johann | fce8f06 | 2012-07-24 10:19:44 -0700 | [diff] [blame] | 18 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 19 | namespace libaom_test { |
Johann | c0e8095 | 2012-07-20 11:51:06 -0700 | [diff] [blame] | 20 | |
| 21 | class ACMRandom { |
| 22 | public: |
James Zern | c4195e0 | 2013-04-04 19:00:31 -0700 | [diff] [blame] | 23 | ACMRandom() : random_(DeterministicSeed()) {} |
Johann | fbea897 | 2012-06-28 11:43:58 -0700 | [diff] [blame] | 24 | |
James Zern | c4195e0 | 2013-04-04 19:00:31 -0700 | [diff] [blame] | 25 | explicit ACMRandom(int seed) : random_(seed) {} |
Johann | c0e8095 | 2012-07-20 11:51:06 -0700 | [diff] [blame] | 26 | |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 27 | void Reset(int seed) { random_.Reseed(seed); } |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 28 | |
| 29 | uint32_t Rand31(void) { |
| 30 | return random_.Generate(testing::internal::Random::kMaxRange); |
| 31 | } |
| 32 | |
Yaowu Xu | ed7e3d2 | 2013-12-13 10:05:40 -0800 | [diff] [blame] | 33 | uint16_t Rand16(void) { |
| 34 | const uint32_t value = |
| 35 | random_.Generate(testing::internal::Random::kMaxRange); |
James Zern | 615230b | 2014-12-01 15:10:00 -0800 | [diff] [blame] | 36 | return (value >> 15) & 0xffff; |
Yaowu Xu | ed7e3d2 | 2013-12-13 10:05:40 -0800 | [diff] [blame] | 37 | } |
Johann | c0e8095 | 2012-07-20 11:51:06 -0700 | [diff] [blame] | 38 | |
Johann | 8c02a36 | 2016-04-15 11:35:56 -0700 | [diff] [blame] | 39 | int16_t Rand9Signed(void) { |
| 40 | // Use 9 bits: values between 255 (0x0FF) and -256 (0x100). |
| 41 | const uint32_t value = random_.Generate(512); |
James Zern | 13d48c4 | 2016-05-27 10:33:56 -0700 | [diff] [blame] | 42 | return static_cast<int16_t>(value) - 256; |
Johann | 8c02a36 | 2016-04-15 11:35:56 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Johann | c0e8095 | 2012-07-20 11:51:06 -0700 | [diff] [blame] | 45 | uint8_t Rand8(void) { |
James Zern | c4195e0 | 2013-04-04 19:00:31 -0700 | [diff] [blame] | 46 | 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 Zern | 3063193 | 2014-11-26 15:17:49 -0800 | [diff] [blame] | 49 | return (value >> 23) & 0xff; |
Johann | c0e8095 | 2012-07-20 11:51:06 -0700 | [diff] [blame] | 50 | } |
| 51 | |
John Koleszar | a9ebbcc | 2013-04-18 13:05:38 -0700 | [diff] [blame] | 52 | 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-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 59 | int PseudoUniform(int range) { return random_.Generate(range); } |
Johann | c0e8095 | 2012-07-20 11:51:06 -0700 | [diff] [blame] | 60 | |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 61 | int operator()(int n) { return PseudoUniform(n); } |
Johann | c0e8095 | 2012-07-20 11:51:06 -0700 | [diff] [blame] | 62 | |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 63 | static int DeterministicSeed(void) { return 0xbaba; } |
James Zern | c4195e0 | 2013-04-04 19:00:31 -0700 | [diff] [blame] | 64 | |
| 65 | private: |
| 66 | testing::internal::Random random_; |
Johann | c0e8095 | 2012-07-20 11:51:06 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 69 | } // namespace libaom_test |
Johann | c0e8095 | 2012-07-20 11:51:06 -0700 | [diff] [blame] | 70 | |
Yaowu Xu | afffa3d | 2013-09-05 08:45:56 -0700 | [diff] [blame] | 71 | #endif // TEST_ACM_RANDOM_H_ |