xref: /aosp_15_r20/external/libopus/dnn/kiss99.c (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1*a58d3d2aSXin Li /*Daala video codec
2*a58d3d2aSXin Li Copyright (c) 2012 Daala project contributors.  All rights reserved.
3*a58d3d2aSXin Li Author: Timothy B. Terriberry
4*a58d3d2aSXin Li 
5*a58d3d2aSXin Li Redistribution and use in source and binary forms, with or without
6*a58d3d2aSXin Li modification, are permitted provided that the following conditions are met:
7*a58d3d2aSXin Li 
8*a58d3d2aSXin Li - Redistributions of source code must retain the above copyright notice, this
9*a58d3d2aSXin Li   list of conditions and the following disclaimer.
10*a58d3d2aSXin Li 
11*a58d3d2aSXin Li - Redistributions in binary form must reproduce the above copyright notice,
12*a58d3d2aSXin Li   this list of conditions and the following disclaimer in the documentation
13*a58d3d2aSXin Li   and/or other materials provided with the distribution.
14*a58d3d2aSXin Li 
15*a58d3d2aSXin Li THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
16*a58d3d2aSXin Li AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*a58d3d2aSXin Li IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18*a58d3d2aSXin Li DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
19*a58d3d2aSXin Li FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*a58d3d2aSXin Li DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21*a58d3d2aSXin Li SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22*a58d3d2aSXin Li CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23*a58d3d2aSXin Li OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24*a58d3d2aSXin Li OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
25*a58d3d2aSXin Li 
26*a58d3d2aSXin Li #ifdef HAVE_CONFIG_H
27*a58d3d2aSXin Li #include "config.h"
28*a58d3d2aSXin Li #endif
29*a58d3d2aSXin Li 
30*a58d3d2aSXin Li #include "kiss99.h"
31*a58d3d2aSXin Li 
kiss99_srand(kiss99_ctx * _this,const unsigned char * _data,int _ndata)32*a58d3d2aSXin Li void kiss99_srand(kiss99_ctx *_this,const unsigned char *_data,int _ndata){
33*a58d3d2aSXin Li   int i;
34*a58d3d2aSXin Li   _this->z=362436069;
35*a58d3d2aSXin Li   _this->w=521288629;
36*a58d3d2aSXin Li   _this->jsr=123456789;
37*a58d3d2aSXin Li   _this->jcong=380116160;
38*a58d3d2aSXin Li   for(i=3;i<_ndata;i+=4){
39*a58d3d2aSXin Li     _this->z^=_data[i-3];
40*a58d3d2aSXin Li     _this->w^=_data[i-2];
41*a58d3d2aSXin Li     _this->jsr^=_data[i-1];
42*a58d3d2aSXin Li     _this->jcong^=_data[i];
43*a58d3d2aSXin Li     kiss99_rand(_this);
44*a58d3d2aSXin Li   }
45*a58d3d2aSXin Li   if(i-3<_ndata)_this->z^=_data[i-3];
46*a58d3d2aSXin Li   if(i-2<_ndata)_this->w^=_data[i-2];
47*a58d3d2aSXin Li   if(i-1<_ndata)_this->jsr^=_data[i-1];
48*a58d3d2aSXin Li   /*Fix any potential short cycles that show up.
49*a58d3d2aSXin Li     These are not too likely, given the way we initialize the state, but they
50*a58d3d2aSXin Li      are technically possible, so let us go ahead and eliminate that
51*a58d3d2aSXin Li      possibility.
52*a58d3d2aSXin Li     See Gregory G. Rose: "KISS: A Bit Too Simple", Cryptographic Communications
53*a58d3d2aSXin Li      No. 10, pp. 123---137, 2018.*/
54*a58d3d2aSXin Li   if(_this->z==0||_this->z==0x9068FFFF)_this->z++;
55*a58d3d2aSXin Li   if(_this->w==0||_this->w==0x464FFFFF)_this->w++;
56*a58d3d2aSXin Li   if(_this->jsr==0)_this->jsr++;
57*a58d3d2aSXin Li }
58*a58d3d2aSXin Li 
kiss99_rand(kiss99_ctx * _this)59*a58d3d2aSXin Li uint32_t kiss99_rand(kiss99_ctx *_this){
60*a58d3d2aSXin Li   uint32_t znew;
61*a58d3d2aSXin Li   uint32_t wnew;
62*a58d3d2aSXin Li   uint32_t mwc;
63*a58d3d2aSXin Li   uint32_t shr3;
64*a58d3d2aSXin Li   uint32_t cong;
65*a58d3d2aSXin Li   znew=36969*(_this->z&0xFFFF)+(_this->z>>16);
66*a58d3d2aSXin Li   wnew=18000*(_this->w&0xFFFF)+(_this->w>>16);
67*a58d3d2aSXin Li   mwc=(znew<<16)+wnew;
68*a58d3d2aSXin Li   /*We swap the 13 and 17 from the original 1999 algorithm to produce a single
69*a58d3d2aSXin Li      cycle of maximal length, matching KISS11.
70*a58d3d2aSXin Li     We are not actually using KISS11 because of the impractically large (16 MB)
71*a58d3d2aSXin Li      internal state of the full algorithm.*/
72*a58d3d2aSXin Li   shr3=_this->jsr^(_this->jsr<<13);
73*a58d3d2aSXin Li   shr3^=shr3>>17;
74*a58d3d2aSXin Li   shr3^=shr3<<5;
75*a58d3d2aSXin Li   cong=69069*_this->jcong+1234567;
76*a58d3d2aSXin Li   _this->z=znew;
77*a58d3d2aSXin Li   _this->w=wnew;
78*a58d3d2aSXin Li   _this->jsr=shr3;
79*a58d3d2aSXin Li   _this->jcong=cong;
80*a58d3d2aSXin Li   return (mwc^cong)+shr3;
81*a58d3d2aSXin Li }
82