mirror of
https://github.com/videolan/vlc.git
synced 2024-12-13 19:54:18 +08:00
a632b0dfe2
According to the manpage, jrand48() must return a signed long integer uniformly distributed over the interval [-2^31, 2^31). The old implementation first computed iterate48(), which returns a positive 48-bit integer (as 'uint64_t', then cast to 'int64_t'). Once right-shifted by 16 bits, the result is guaranteed to be a 32-bit positive integer as 'int64_t'. It is then (implicitly) cast to 'long' to match the return type. When the 32th bit is 0 (i.e. the value fits in 31-bit), then everything is ok. However, when the 32nd bit is 1, then there are two cases (which are both incorrect): - if the 'long' type is 32-bit on the platform, then conversion from 'int64_t' to 'long' is undefined; - if the 'long' type is more than 32-bit, then the resulting value will be a positive integer in the interval [0, 2^32), whereas jrand48() must return a value in the interval [-2^31, 2^31).
68 lines
1.9 KiB
C
68 lines
1.9 KiB
C
/*****************************************************************************
|
|
* nrand48.c: POSIX erand48(), jrand48() and nrand48() replacements
|
|
*****************************************************************************
|
|
* Copyright © 2010 Rémi Denis-Courmont
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation; either version 2.1 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
|
|
*****************************************************************************/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
# include <config.h>
|
|
#endif
|
|
|
|
#include <inttypes.h>
|
|
|
|
static uint64_t iterate48 (unsigned short subi[3])
|
|
{
|
|
const uint64_t a = UINT64_C(0x5DEECE66D);
|
|
const unsigned c = 13;
|
|
const uint64_t mask = UINT64_C(0xFFFFFFFFFFFF); // 48 bits
|
|
|
|
uint64_t x = ((uint64_t)subi[0] << 32)
|
|
| ((uint32_t)subi[1] << 16)
|
|
| subi[2];
|
|
|
|
x *= a;
|
|
x += c;
|
|
x &= mask;
|
|
|
|
subi[0] = (x >> 32) & 0xFFFF;
|
|
subi[1] = (x >> 16) & 0xFFFF;
|
|
subi[2] = (x >> 0) & 0XFFFF;
|
|
|
|
return x;
|
|
}
|
|
|
|
double erand48 (unsigned short subi[3])
|
|
{
|
|
uint64_t r = iterate48 (subi);
|
|
return ((double)r) / 281474976710655.;
|
|
}
|
|
|
|
long jrand48 (unsigned short subi[3])
|
|
{
|
|
union {
|
|
uint32_t u;
|
|
int32_t i;
|
|
} v;
|
|
v.u = iterate48(subi) >> 16;
|
|
return v.i;
|
|
}
|
|
|
|
long nrand48 (unsigned short subi[3])
|
|
{
|
|
return iterate48 (subi) >> 17;
|
|
}
|