]> git.ipfire.org Git - thirdparty/squid.git/blob - compat/drand48.c
SourceFormat Enforcement
[thirdparty/squid.git] / compat / drand48.c
1 /*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #include "squid.h"
10
11 /* borrowed from libc/misc/drand48.c in Linux libc-5.4.46 this quick
12 * hack by Martin Hamilton <martinh@gnu.org> to make Squid build on
13 * Win32 with GNU-Win32 - sorry, folks! */
14
15 #if !HAVE_DRAND48
16
17 #define N 16
18 #define MASK ((unsigned)(1 << (N - 1)) + (1 << (N - 1)) - 1)
19 #define LOW(x) ((unsigned)(x) & MASK)
20 #define HIGH(x) LOW((x) >> N)
21 #define MUL(x, y, z) { long l = (long)(x) * (long)(y); \
22 (z)[0] = LOW(l); (z)[1] = HIGH(l); }
23 #define CARRY(x, y) ((long)(x) + (long)(y) > MASK)
24 #define ADDEQU(x, y, z) (z = CARRY(x, (y)), x = LOW(x + (y)))
25 #define X0 0x330E
26 #define X1 0xABCD
27 #define X2 0x1234
28 #define A0 0xE66D
29 #define A1 0xDEEC
30 #define A2 0x5
31 #define C 0xB
32
33 static void next(void);
34 static unsigned x[3] = {X0, X1, X2}, a[3] = {A0, A1, A2}, c = C;
35
36 double drand48(void);
37
38 double
39 drand48(void)
40 {
41 static double two16m = 1.0 / (1L << N);
42 next();
43 return (two16m * (two16m * (two16m * x[0] + x[1]) + x[2]));
44 }
45
46 static void
47 next(void)
48 {
49 unsigned p[2], q[2], r[2], carry0, carry1;
50
51 MUL(a[0], x[0], p);
52 ADDEQU(p[0], c, carry0);
53 ADDEQU(p[1], carry0, carry1);
54 MUL(a[0], x[1], q);
55 ADDEQU(p[1], q[0], carry0);
56 MUL(a[1], x[0], r);
57 x[2] = LOW(carry0 + carry1 + CARRY(p[1], r[0]) + q[1] + r[1] +
58 a[0] * x[2] + a[1] * x[1] + a[2] * x[0]);
59 x[1] = LOW(p[1] + r[0]);
60 x[0] = LOW(p[0]);
61 }
62
63 #endif /* HAVE_DRAND48 */
64