]> git.sesse.net Git - vlc/blob - compat/nrand48.c
9498ef0e625aef645302a49d0bc5df4e3175f623
[vlc] / compat / nrand48.c
1 /*****************************************************************************
2  * nrand48.c: POSIX nrand48() replacement
3  *****************************************************************************
4  * Copyright © 2010 Rémi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published
8  * by the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <inttypes.h>
26
27 long nrand48 (unsigned short subi[3])
28 {
29     const uint64_t a = UINT64_C(0x5DEECE66D16);
30     const unsigned c = 13;
31     const uint64_t mask = UINT64_C(0xFFFFFFFFFFFF); // 48 bits
32
33     uint64_t x = ((uint64_t)subi[0] << 32)
34                | ((uint32_t)subi[1] << 16)
35                | subi[2];
36
37     x *= a;
38     x += c;
39     x &= mask;
40
41     subi[0] = (x >> 32) & 0xFFFF;
42     subi[1] = (x >> 16) & 0xFFFF;
43     subi[2] = (x >>  0) & 0XFFFF;
44
45     return x >> 16;
46 }