]> git.saurik.com Git - apple/libc.git/blob - gen/FreeBSD/rand48.h.patch
c305cb764a601ca225fcbf9b14fad2d181fc24b2
[apple/libc.git] / gen / FreeBSD / rand48.h.patch
1 --- rand48.h.orig 2003-05-20 15:21:02.000000000 -0700
2 +++ rand48.h 2005-11-03 14:06:17.000000000 -0800
3 @@ -19,8 +19,6 @@
4 #include <math.h>
5 #include <stdlib.h>
6
7 -void _dorand48(unsigned short[3]);
8 -
9 #define RAND48_SEED_0 (0x330e)
10 #define RAND48_SEED_1 (0xabcd)
11 #define RAND48_SEED_2 (0x1234)
12 @@ -29,4 +27,55 @@
13 #define RAND48_MULT_2 (0x0005)
14 #define RAND48_ADD (0x000b)
15
16 +typedef unsigned long long uint48;
17 +
18 +extern uint48 _rand48_seed;
19 +extern uint48 _rand48_mult;
20 +extern uint48 _rand48_add;
21 +
22 +#define TOUINT48(x,y,z) \
23 + ((uint48)(x) + (((uint48)(y)) << 16) + (((uint48)(z)) << 32))
24 +
25 +#define RAND48_SEED TOUINT48(RAND48_SEED_0, RAND48_SEED_1, RAND48_SEED_2)
26 +#define RAND48_MULT TOUINT48(RAND48_MULT_0, RAND48_MULT_1, RAND48_MULT_2)
27 +
28 +#define LOADRAND48(l,x) \
29 + (l) = TOUINT48((x)[0], (x)[1], (x)[2])
30 +
31 +#define STORERAND48(l,x) \
32 + (x)[0] = (unsigned short)(l); \
33 + (x)[1] = (unsigned short)((l) >> 16); \
34 + (x)[2] = (unsigned short)((l) >> 32)
35 +
36 +#define _DORAND48(l) \
37 + (l) = (l) * _rand48_mult + _rand48_add
38 +
39 +#define DORAND48(l,x) \
40 + LOADRAND48(l, x); \
41 + _DORAND48(l); \
42 + STORERAND48(l, x)
43 +
44 +#include "fpmath.h"
45 +
46 +/*
47 + * Optimization for speed: avoid int-to-double conversion. Assume doubles
48 + * are IEEE-754 and insert the bits directly. To normalize, the (1 << 52)
49 + * is the hidden bit, which the first set bit is shifted to.
50 + */
51 +#define ERAND48_BEGIN \
52 + union { \
53 + union IEEEd2bits ieee; \
54 + unsigned long long l; \
55 + } u; \
56 + int s
57 +
58 +#define ERAND48_END(x) \
59 + u.l = ((x) & 0xffffffffffffULL); \
60 + if (u.l == 0) \
61 + return 0.0; \
62 + u.l <<= 5; \
63 + for(s = 0; !(u.l & (1LL << 52)); s++, u.l <<= 1) {} \
64 + u.ieee.bits.exp = 1022 - s; \
65 + return u.ieee.d
66 +
67 #endif /* _RAND48_H_ */