From http://www.jwz.org/xscreensaver/xscreensaver-5.33.tar.gz
[xscreensaver] / utils / yarandom.h
1 /* xscreensaver, Copyright (c) 1997, 1998, 2003 by Jamie Zawinski <jwz@jwz.org>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10  */
11
12 #ifndef __YARANDOM_H__
13 #define __YARANDOM_H__
14
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18
19 #ifdef HAVE_INTTYPES_H
20 # include <inttypes.h>
21 #endif
22
23 #undef random
24 #undef rand
25 #undef drand48
26 #undef srandom
27 #undef srand
28 #undef srand48
29 #undef frand
30 #undef RAND_MAX
31
32 #ifdef VMS
33 # include "vms-gtod.h"
34 #endif
35
36 extern unsigned int ya_random (void);
37 extern void ya_rand_init (unsigned int);
38
39 #define random()   ya_random()
40 #define RAND_MAX   0xFFFFFFFF
41
42 /*#define srandom(i) ya_rand_init(0)*/
43
44 /* Define these away to keep people from using the wrong APIs in xscreensaver.
45  */
46 #define rand          __ERROR_use_random_not_rand_in_xscreensaver__
47 #define drand48       __ERROR_use_frand_not_drand48_in_xscreensaver__
48 #define srandom       __ERROR_do_not_call_srandom_in_xscreensaver__
49 #define srand         __ERROR_do_not_call_srand_in_xscreensaver__
50 #define srand48       __ERROR_do_not_call_srand48_in_xscreensaver__
51 #define ya_rand_init  __ERROR_do_not_call_ya_rand_init_in_xscreensaver__
52
53
54 #if defined (__GNUC__) && (__GNUC__ >= 2)
55  /* Implement frand using GCC's statement-expression extension. */
56
57 # define frand(f)                                                       \
58   __extension__                                                         \
59   ({ double tmp = ((((double) random()) * ((double) (f))) /             \
60                    ((double) ((unsigned int)~0)));                      \
61      tmp < 0 ? (-tmp) : tmp; })
62
63 #else /* not GCC2 - implement frand using a global variable.*/
64
65 static double _frand_tmp_;
66 # define frand(f)                                                       \
67   (_frand_tmp_ = ((((double) random()) * ((double) (f))) /              \
68                   ((double) ((unsigned int)~0))),                       \
69    _frand_tmp_ < 0 ? (-_frand_tmp_) : _frand_tmp_)
70
71 #endif /* not GCC2 */
72
73 /* Compatibility with the xlockmore RNG API
74    (note that the xlockmore hacks never expect negative numbers.)
75  */
76 #define LRAND()         ((long) (random() & 0x7fffffff))
77
78 /* The first NRAND(n) is much faster, when uint64_t is available to allow it.
79  * Especially on ARM and other processors without built-in division.
80  *
81  * n must be greater than zero.
82  *
83  * The division by RAND_MAX+1 should optimize down to a bit shift.
84  *
85  * Although the result here is never negative, this needs to return signed
86  * integers: A binary operator in C requires operands have the same type, and
87  * if one side is signed and the other is unsigned, but they're both the same
88  * size, then the signed operand is cast to unsigned, and the result is
89  * similarly unsigned. This can potentially cause problems when idioms like
90  * "NRAND(n) * RANDSIGN()" (where RANDSIGN() returns either -1 or 1) is used
91  * to get random numbers from the range (-n,n).
92  */
93 #ifdef HAVE_INTTYPES_H
94 # define NRAND(n)       ((int32_t) ((uint64_t) random() * (uint32_t) (n) / \
95                                     ((uint64_t) RAND_MAX + 1)))
96 #else
97 # define NRAND(n)       ((int) (LRAND() % (n)))
98 #endif
99
100 #define MAXRAND         (2147483648.0) /* unsigned 1<<31 as a float */
101 #define SRAND(n)        /* already seeded by screenhack.c */
102
103 #endif /* __YARANDOM_H__ */