xref: /aosp_15_r20/external/libaom/av1/encoder/random.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2017, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #ifndef AOM_AV1_ENCODER_RANDOM_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AV1_ENCODER_RANDOM_H_
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include <stdint.h>
16*77c1e3ccSAndroid Build Coastguard Worker 
17*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
18*77c1e3ccSAndroid Build Coastguard Worker extern "C" {
19*77c1e3ccSAndroid Build Coastguard Worker #endif
20*77c1e3ccSAndroid Build Coastguard Worker 
21*77c1e3ccSAndroid Build Coastguard Worker // Advance the generator to its next state, and generate the next 32-bit output.
22*77c1e3ccSAndroid Build Coastguard Worker // Note that the low bits of this output are comparatively low-quality, so users
23*77c1e3ccSAndroid Build Coastguard Worker // of this function should ensure that the high bits factor through to their
24*77c1e3ccSAndroid Build Coastguard Worker // outputs.
lcg_next(uint32_t * state)25*77c1e3ccSAndroid Build Coastguard Worker static inline uint32_t lcg_next(uint32_t *state) {
26*77c1e3ccSAndroid Build Coastguard Worker   *state = (uint32_t)(*state * 1103515245ULL + 12345);
27*77c1e3ccSAndroid Build Coastguard Worker   return *state;
28*77c1e3ccSAndroid Build Coastguard Worker }
29*77c1e3ccSAndroid Build Coastguard Worker 
30*77c1e3ccSAndroid Build Coastguard Worker // Generate a random number in the range [0, 32768).
lcg_rand16(uint32_t * state)31*77c1e3ccSAndroid Build Coastguard Worker static inline uint32_t lcg_rand16(uint32_t *state) {
32*77c1e3ccSAndroid Build Coastguard Worker   return (lcg_next(state) / 65536) % 32768;
33*77c1e3ccSAndroid Build Coastguard Worker }
34*77c1e3ccSAndroid Build Coastguard Worker 
35*77c1e3ccSAndroid Build Coastguard Worker // Generate a random number in the range [0, n)
36*77c1e3ccSAndroid Build Coastguard Worker // This is implemented as (rand() * n) / <range of RNG> rather than
37*77c1e3ccSAndroid Build Coastguard Worker // rand() % n, for a few reasons: This implementation is faster and less biased,
38*77c1e3ccSAndroid Build Coastguard Worker // and if is a power of 2, this uses the higher-quality top bits from the RNG
39*77c1e3ccSAndroid Build Coastguard Worker // output rather than the lower-quality bottom bits.
lcg_randint(uint32_t * state,uint32_t n)40*77c1e3ccSAndroid Build Coastguard Worker static inline uint32_t lcg_randint(uint32_t *state, uint32_t n) {
41*77c1e3ccSAndroid Build Coastguard Worker   uint64_t v = ((uint64_t)lcg_next(state) * n) >> 32;
42*77c1e3ccSAndroid Build Coastguard Worker   return (uint32_t)v;
43*77c1e3ccSAndroid Build Coastguard Worker }
44*77c1e3ccSAndroid Build Coastguard Worker 
45*77c1e3ccSAndroid Build Coastguard Worker // Generate a random number in the range [lo, hi)
lcg_randrange(uint32_t * state,uint32_t lo,uint32_t hi)46*77c1e3ccSAndroid Build Coastguard Worker static inline uint32_t lcg_randrange(uint32_t *state, uint32_t lo,
47*77c1e3ccSAndroid Build Coastguard Worker                                      uint32_t hi) {
48*77c1e3ccSAndroid Build Coastguard Worker   assert(lo < hi);
49*77c1e3ccSAndroid Build Coastguard Worker   return lo + lcg_randint(state, hi - lo);
50*77c1e3ccSAndroid Build Coastguard Worker }
51*77c1e3ccSAndroid Build Coastguard Worker 
52*77c1e3ccSAndroid Build Coastguard Worker // Pick k distinct numbers from the set {0, ..., n-1}
53*77c1e3ccSAndroid Build Coastguard Worker // All possible sets of k numbers, and all possible orderings of those numbers,
54*77c1e3ccSAndroid Build Coastguard Worker // are equally likely.
55*77c1e3ccSAndroid Build Coastguard Worker //
56*77c1e3ccSAndroid Build Coastguard Worker // Note: The algorithm used here uses resampling to avoid choosing repeated
57*77c1e3ccSAndroid Build Coastguard Worker // values. This works well as long as n >> k, but can potentially lead to many
58*77c1e3ccSAndroid Build Coastguard Worker // resampling attempts if n is equal to or only slightly larger than k.
lcg_pick(int n,int k,int * out,unsigned int * seed)59*77c1e3ccSAndroid Build Coastguard Worker static inline void lcg_pick(int n, int k, int *out, unsigned int *seed) {
60*77c1e3ccSAndroid Build Coastguard Worker   assert(0 <= k && k <= n);
61*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < k; i++) {
62*77c1e3ccSAndroid Build Coastguard Worker     int v;
63*77c1e3ccSAndroid Build Coastguard Worker 
64*77c1e3ccSAndroid Build Coastguard Worker   // Inner resampling loop
65*77c1e3ccSAndroid Build Coastguard Worker   // We have to use a goto here because C does not have a multi-level continue
66*77c1e3ccSAndroid Build Coastguard Worker   // statement
67*77c1e3ccSAndroid Build Coastguard Worker   resample:
68*77c1e3ccSAndroid Build Coastguard Worker     v = (int)lcg_randint(seed, n);
69*77c1e3ccSAndroid Build Coastguard Worker     for (int j = 0; j < i; j++) {
70*77c1e3ccSAndroid Build Coastguard Worker       if (v == out[j]) {
71*77c1e3ccSAndroid Build Coastguard Worker         // Repeated v, resample
72*77c1e3ccSAndroid Build Coastguard Worker         goto resample;
73*77c1e3ccSAndroid Build Coastguard Worker       }
74*77c1e3ccSAndroid Build Coastguard Worker     }
75*77c1e3ccSAndroid Build Coastguard Worker 
76*77c1e3ccSAndroid Build Coastguard Worker     // New v, accept
77*77c1e3ccSAndroid Build Coastguard Worker     out[i] = v;
78*77c1e3ccSAndroid Build Coastguard Worker   }
79*77c1e3ccSAndroid Build Coastguard Worker }
80*77c1e3ccSAndroid Build Coastguard Worker 
81*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
82*77c1e3ccSAndroid Build Coastguard Worker }  // extern "C"
83*77c1e3ccSAndroid Build Coastguard Worker #endif
84*77c1e3ccSAndroid Build Coastguard Worker 
85*77c1e3ccSAndroid Build Coastguard Worker #endif  // AOM_AV1_ENCODER_RANDOM_H_
86