2012-07-20 22:51:06 +04:00
|
|
|
/*
|
2016-09-02 00:32:49 +03:00
|
|
|
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
|
2012-07-20 22:51:06 +04:00
|
|
|
*
|
2016-09-02 00:32:49 +03:00
|
|
|
* This source code is subject to the terms of the BSD 2 Clause License and
|
|
|
|
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
|
|
|
|
* was not distributed with this source code in the LICENSE file, you can
|
|
|
|
* obtain it at www.aomedia.org/license/software. If the Alliance for Open
|
|
|
|
* Media Patent License 1.0 was not distributed with this source code in the
|
|
|
|
* PATENTS file, you can obtain it at www.aomedia.org/license/patent.
|
2012-07-20 22:51:06 +04:00
|
|
|
*/
|
|
|
|
|
2013-09-05 19:45:56 +04:00
|
|
|
#ifndef TEST_ACM_RANDOM_H_
|
|
|
|
#define TEST_ACM_RANDOM_H_
|
2012-07-20 22:51:06 +04:00
|
|
|
|
2013-04-05 06:00:31 +04:00
|
|
|
#include "third_party/googletest/src/include/gtest/gtest.h"
|
2012-07-20 22:51:06 +04:00
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
#include "aom/aom_integer.h"
|
2012-07-24 21:19:44 +04:00
|
|
|
|
2016-08-23 02:08:15 +03:00
|
|
|
namespace libaom_test {
|
2012-07-20 22:51:06 +04:00
|
|
|
|
|
|
|
class ACMRandom {
|
|
|
|
public:
|
2013-04-05 06:00:31 +04:00
|
|
|
ACMRandom() : random_(DeterministicSeed()) {}
|
2012-06-28 22:43:58 +04:00
|
|
|
|
2013-04-05 06:00:31 +04:00
|
|
|
explicit ACMRandom(int seed) : random_(seed) {}
|
2012-07-20 22:51:06 +04:00
|
|
|
|
2016-08-12 03:46:05 +03:00
|
|
|
void Reset(int seed) { random_.Reseed(seed); }
|
2016-08-15 20:27:19 +03:00
|
|
|
|
|
|
|
uint32_t Rand31(void) {
|
|
|
|
return random_.Generate(testing::internal::Random::kMaxRange);
|
|
|
|
}
|
|
|
|
|
2013-12-13 22:05:40 +04:00
|
|
|
uint16_t Rand16(void) {
|
|
|
|
const uint32_t value =
|
|
|
|
random_.Generate(testing::internal::Random::kMaxRange);
|
2014-12-02 02:10:00 +03:00
|
|
|
return (value >> 15) & 0xffff;
|
2013-12-13 22:05:40 +04:00
|
|
|
}
|
2012-07-20 22:51:06 +04:00
|
|
|
|
2016-04-15 21:35:56 +03:00
|
|
|
int16_t Rand9Signed(void) {
|
|
|
|
// Use 9 bits: values between 255 (0x0FF) and -256 (0x100).
|
|
|
|
const uint32_t value = random_.Generate(512);
|
2016-05-27 20:33:56 +03:00
|
|
|
return static_cast<int16_t>(value) - 256;
|
2016-04-15 21:35:56 +03:00
|
|
|
}
|
|
|
|
|
2012-07-20 22:51:06 +04:00
|
|
|
uint8_t Rand8(void) {
|
2013-04-05 06:00:31 +04:00
|
|
|
const uint32_t value =
|
|
|
|
random_.Generate(testing::internal::Random::kMaxRange);
|
|
|
|
// There's a bit more entropy in the upper bits of this implementation.
|
2014-11-27 02:17:49 +03:00
|
|
|
return (value >> 23) & 0xff;
|
2012-07-20 22:51:06 +04:00
|
|
|
}
|
|
|
|
|
2013-04-19 00:05:38 +04:00
|
|
|
uint8_t Rand8Extremes(void) {
|
|
|
|
// Returns a random value near 0 or near 255, to better exercise
|
|
|
|
// saturation behavior.
|
|
|
|
const uint8_t r = Rand8();
|
|
|
|
return r < 128 ? r << 4 : r >> 4;
|
|
|
|
}
|
|
|
|
|
2016-08-12 03:46:05 +03:00
|
|
|
int PseudoUniform(int range) { return random_.Generate(range); }
|
2012-07-20 22:51:06 +04:00
|
|
|
|
2016-08-12 03:46:05 +03:00
|
|
|
int operator()(int n) { return PseudoUniform(n); }
|
2012-07-20 22:51:06 +04:00
|
|
|
|
2016-08-12 03:46:05 +03:00
|
|
|
static int DeterministicSeed(void) { return 0xbaba; }
|
2013-04-05 06:00:31 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
testing::internal::Random random_;
|
2012-07-20 22:51:06 +04:00
|
|
|
};
|
|
|
|
|
2016-08-23 02:08:15 +03:00
|
|
|
} // namespace libaom_test
|
2012-07-20 22:51:06 +04:00
|
|
|
|
2013-09-05 19:45:56 +04:00
|
|
|
#endif // TEST_ACM_RANDOM_H_
|