A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 119 lines 3.9 kB view raw
1/* 2 A C-program for MT19937, with initialization improved 2002/2/10. 3 Coded by Takuji Nishimura and Makoto Matsumoto. 4 This is a faster version by taking Shawn Cokus's optimization, 5 Matthe Bellew's simplification. 6 7 Before using, initialize the state by using srand(seed). 8 9 Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, 10 All rights reserved. 11 12 Redistribution and use in source and binary forms, with or without 13 modification, are permitted provided that the following conditions 14 are met: 15 16 1. Redistributions of source code must retain the above copyright 17 notice, this list of conditions and the following disclaimer. 18 19 2. Redistributions in binary form must reproduce the above copyright 20 notice, this list of conditions and the following disclaimer in the 21 documentation and/or other materials provided with the distribution. 22 23 3. The names of its contributors may not be used to endorse or promote 24 products derived from this software without specific prior written 25 permission. 26 27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 28 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 29 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 30 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 31 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 32 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 33 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 34 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 35 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 36 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 37 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 39 Any feedback is very welcome. 40 http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html 41 email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) 42*/ 43 44/* 45 Adapted to Rockbox by Jens Arnold 46*/ 47 48#include <stdlib.h> 49 50/* Period parameters */ 51#define N 624 52#define M 397 53#define MATRIX_A 0x9908b0dfUL /* constant vector a */ 54#define UMASK 0x80000000UL /* most significant w-r bits */ 55#define LMASK 0x7fffffffUL /* least significant r bits */ 56#define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) ) 57#define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1UL ? MATRIX_A : 0UL)) 58 59static unsigned long state[N]; /* the array for the state vector */ 60static int left = 0; 61static unsigned long *next; 62 63/* initializes state[N] with a seed */ 64void srand(unsigned int seed) 65{ 66 unsigned long x = seed & 0xffffffffUL; 67 unsigned long *s = state; 68 int j; 69 70 for (*s++ = x, j = 1; j < N; j++) { 71 x = (1812433253UL * (x ^ (x >> 30)) + j) & 0xffffffffUL; 72 *s++ = x; 73 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ 74 /* In the previous versions, MSBs of the seed affect */ 75 /* only MSBs of the array state[]. */ 76 /* 2002/01/09 modified by Makoto Matsumoto */ 77 } 78 left = 1; 79} 80 81static void next_state(void) 82{ 83 unsigned long *p = state; 84 int j; 85 86 /* if srand() has not been called, */ 87 /* a default initial seed is used */ 88 if (left < 0) 89 srand(5489UL); 90 91 left = N; 92 next = state; 93 94 for (j = N - M + 1; --j; p++) 95 *p = p[M] ^ TWIST(p[0], p[1]); 96 97 for (j = M; --j; p++) 98 *p = p[M-N] ^ TWIST(p[0], p[1]); 99 100 *p = p[M-N] ^ TWIST(p[0], state[0]); 101} 102 103/* generates a random number on [0,RAND_MAX]-interval */ 104int rand(void) 105{ 106 unsigned long y; 107 108 if (--left <= 0) 109 next_state(); 110 y = *next++; 111 112 /* Tempering */ 113 y ^= (y >> 11); 114 y ^= (y << 7) & 0x9d2c5680UL; 115 y ^= (y << 15) & 0xefc60000UL; 116 y ^= (y >> 18); 117 118 return ((unsigned int)y) >> 1; 119}