1/*
2 *  Copyright (c) 2011 The WebRTC 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
11
12/*
13 * This file contains implementations of the randomization functions
14 * WebRtcSpl_RandU()
15 * WebRtcSpl_RandN()
16 * WebRtcSpl_RandUArray()
17 *
18 * The description header can be found in signal_processing_library.h
19 *
20 */
21
22#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
23
24static const uint32_t kMaxSeedUsed = 0x80000000;
25
26static const int16_t kRandNTable[] = {
27    9178,    -7260,       40,    10189,     4894,    -3531,   -13779,    14764,
28   -4008,    -8884,    -8990,     1008,     7368,     5184,     3251,    -5817,
29   -9786,     5963,     1770,     8066,    -7135,    10772,    -2298,     1361,
30    6484,     2241,    -8633,      792,      199,    -3344,     6553,   -10079,
31  -15040,       95,    11608,   -12469,    14161,    -4176,     2476,     6403,
32   13685,   -16005,     6646,     2239,    10916,    -3004,     -602,    -3141,
33    2142,    14144,    -5829,     5305,     8209,     4713,     2697,    -5112,
34   16092,    -1210,    -2891,    -6631,    -5360,   -11878,    -6781,    -2739,
35   -6392,      536,    10923,    10872,     5059,    -4748,    -7770,     5477,
36      38,    -1025,    -2892,     1638,     6304,    14375,   -11028,     1553,
37   -1565,    10762,     -393,     4040,     5257,    12310,     6554,    -4799,
38    4899,    -6354,     1603,    -1048,    -2220,     8247,     -186,    -8944,
39  -12004,     2332,     4801,    -4933,     6371,      131,     8614,    -5927,
40   -8287,   -22760,     4033,   -15162,     3385,     3246,     3153,    -5250,
41    3766,      784,     6494,      -62,     3531,    -1582,    15572,      662,
42   -3952,     -330,    -3196,      669,     7236,    -2678,    -6569,    23319,
43   -8645,     -741,    14830,   -15976,     4903,      315,   -11342,    10311,
44    1858,    -7777,     2145,     5436,     5677,     -113,   -10033,      826,
45   -1353,    17210,     7768,      986,    -1471,     8291,    -4982,     8207,
46  -14911,    -6255,    -2449,   -11881,    -7059,   -11703,    -4338,     8025,
47    7538,    -2823,   -12490,     9470,    -1613,    -2529,   -10092,    -7807,
48    9480,     6970,   -12844,     5123,     3532,     4816,     4803,    -8455,
49   -5045,    14032,    -4378,    -1643,     5756,   -11041,    -2732,   -16618,
50   -6430,   -18375,    -3320,     6098,     5131,    -4269,    -8840,     2482,
51   -7048,     1547,   -21890,    -6505,    -7414,     -424,   -11722,     7955,
52    1653,   -17299,     1823,      473,    -9232,     3337,     1111,      873,
53    4018,    -8982,     9889,     3531,   -11763,    -3799,     7373,    -4539,
54    3231,     7054,    -8537,     7616,     6244,    16635,      447,    -2915,
55   13967,      705,    -2669,    -1520,    -1771,   -16188,     5956,     5117,
56    6371,    -9936,    -1448,     2480,     5128,     7550,    -8130,     5236,
57    8213,    -6443,     7707,    -1950,   -13811,     7218,     7031,    -3883,
58      67,     5731,    -2874,    13480,    -3743,     9298,    -3280,     3552,
59   -4425,      -18,    -3785,    -9988,    -5357,     5477,   -11794,     2117,
60    1416,    -9935,     3376,      802,    -5079,    -8243,    12652,       66,
61    3653,    -2368,     6781,   -21895,    -7227,     2487,     7839,     -385,
62    6646,    -7016,    -4658,     5531,    -1705,      834,      129,     3694,
63   -1343,     2238,   -22640,    -6417,   -11139,    11301,    -2945,    -3494,
64   -5626,      185,    -3615,    -2041,    -7972,    -3106,      -60,   -23497,
65   -1566,    17064,     3519,     2518,      304,    -6805,   -10269,     2105,
66    1936,     -426,     -736,    -8122,    -1467,     4238,    -6939,   -13309,
67     360,     7402,    -7970,    12576,     3287,    12194,    -6289,   -16006,
68    9171,     4042,    -9193,     9123,    -2512,     6388,    -4734,    -8739,
69    1028,    -5406,    -1696,     5889,     -666,    -4736,     4971,     3565,
70    9362,    -6292,     3876,    -3652,   -19666,     7523,    -4061,      391,
71  -11773,     7502,    -3763,     4929,    -9478,    13278,     2805,     4496,
72    7814,    16419,    12455,   -14773,     2127,    -2746,     3763,     4847,
73    3698,     6978,     4751,    -6957,    -3581,      -45,     6252,     1513,
74   -4797,    -7925,    11270,    16188,    -2359,    -5269,     9376,   -10777,
75    7262,    20031,    -6515,    -2208,    -5353,     8085,    -1341,    -1303,
76    7333,     5576,     3625,     5763,    -7931,     9833,    -3371,   -10305,
77    6534,   -13539,    -9971,      997,     8464,    -4064,    -1495,     1857,
78   13624,     5458,     9490,   -11086,    -4524,    12022,     -550,     -198,
79     408,    -8455,    -7068,    10289,     9712,    -3366,     9028,    -7621,
80   -5243,     2362,     6909,     4672,    -4933,    -1799,     4709,    -4563,
81     -62,     -566,     1624,    -7010,    14730,   -17791,    -3697,    -2344,
82   -1741,     7099,    -9509,    -6855,    -1989,     3495,    -2289,     2031,
83   12784,      891,    14189,    -3963,    -5683,      421,   -12575,     1724,
84  -12682,    -5970,    -8169,     3143,    -1824,    -5488,    -5130,     8536,
85   12799,      794,     5738,     3459,   -11689,     -258,    -3738,    -3775,
86   -8742,     2333,     8312,    -9383,    10331,    13119,     8398,    10644,
87  -19433,    -6446,   -16277,   -11793,    16284,     9345,    15222,    15834,
88    2009,    -7349,      130,   -14547,      338,    -5998,     3337,    21492,
89    2406,     7703,     -951,    11196,     -564,     3406,     2217,     4806,
90    2374,    -5797,    11839,     8940,   -11874,    18213,     2855,    10492
91};
92
93static uint32_t IncreaseSeed(uint32_t* seed) {
94  seed[0] = (seed[0] * ((int32_t)69069) + 1) & (kMaxSeedUsed - 1);
95  return seed[0];
96}
97
98int16_t WebRtcSpl_RandU(uint32_t* seed) {
99  return (int16_t)(IncreaseSeed(seed) >> 16);
100}
101
102int16_t WebRtcSpl_RandN(uint32_t* seed) {
103  return kRandNTable[IncreaseSeed(seed) >> 23];
104}
105
106// Creates an array of uniformly distributed variables.
107int16_t WebRtcSpl_RandUArray(int16_t* vector,
108                             int16_t vector_length,
109                             uint32_t* seed) {
110  int i;
111  for (i = 0; i < vector_length; i++) {
112    vector[i] = WebRtcSpl_RandU(seed);
113  }
114  return vector_length;
115}
116