acm_random.h 2.7 KB

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