aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Vesely2014-11-30 12:04:23 -0600
committerJan Vesely2015-02-10 14:24:35 -0600
commit6ce06202ddcb979db47b67a508db96ed049f0768 (patch)
treef274cb6b0e5b95140650fdb259e3e51be7ad9a12 /xf86drmRandom.c
parentccbb9aa887f992359335ecf2d26919b04e14e63f (diff)
downloadexternal-libdrm-6ce06202ddcb979db47b67a508db96ed049f0768.tar.gz
external-libdrm-6ce06202ddcb979db47b67a508db96ed049f0768.tar.xz
external-libdrm-6ce06202ddcb979db47b67a508db96ed049f0768.zip
random: Use unsigned long for seed
v2: Remove unrelated change in main() This is more consistent with the rest, and avoids potential undefined behavior (signed overflow) ind drmRandom() Signed-off-by: Jan Vesely <jan.vesely@rutgers.edu> Reviewed-by: Ian Romanick <idr@freedesktop.org> Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Diffstat (limited to 'xf86drmRandom.c')
-rw-r--r--xf86drmRandom.c12
1 files changed, 6 insertions, 6 deletions
diff --git a/xf86drmRandom.c b/xf86drmRandom.c
index ecab9e2d..94922ad7 100644
--- a/xf86drmRandom.c
+++ b/xf86drmRandom.c
@@ -98,7 +98,7 @@ typedef struct RandomState {
98 unsigned long q; /* m div a */ 98 unsigned long q; /* m div a */
99 unsigned long r; /* m mod a */ 99 unsigned long r; /* m mod a */
100 unsigned long check; 100 unsigned long check;
101 long seed; 101 unsigned long seed;
102} RandomState; 102} RandomState;
103 103
104#if RANDOM_MAIN 104#if RANDOM_MAIN
@@ -147,13 +147,13 @@ int drmRandomDestroy(void *state)
147unsigned long drmRandom(void *state) 147unsigned long drmRandom(void *state)
148{ 148{
149 RandomState *s = (RandomState *)state; 149 RandomState *s = (RandomState *)state;
150 long hi; 150 unsigned long hi;
151 long lo; 151 unsigned long lo;
152 152
153 hi = s->seed / s->q; 153 hi = s->seed / s->q;
154 lo = s->seed % s->q; 154 lo = s->seed % s->q;
155 s->seed = s->a * lo - s->r * hi; 155 s->seed = s->a * lo - s->r * hi;
156 if (s->seed <= 0) s->seed += s->m; 156 if ((s->a * lo) <= (s->r * hi)) s->seed += s->m;
157 157
158 return s->seed; 158 return s->seed;
159} 159}
@@ -166,7 +166,7 @@ double drmRandomDouble(void *state)
166} 166}
167 167
168#if RANDOM_MAIN 168#if RANDOM_MAIN
169static void check_period(long seed) 169static void check_period(unsigned long seed)
170{ 170{
171 unsigned long count = 0; 171 unsigned long count = 0;
172 unsigned long initial; 172 unsigned long initial;
@@ -178,7 +178,7 @@ static void check_period(long seed)
178 while (initial != drmRandom(state)) { 178 while (initial != drmRandom(state)) {
179 if (!++count) break; 179 if (!++count) break;
180 } 180 }
181 printf("With seed of %10ld, period = %10lu (0x%08lx)\n", 181 printf("With seed of %10lu, period = %10lu (0x%08lx)\n",
182 seed, count, count); 182 seed, count, count);
183 drmRandomDestroy(state); 183 drmRandomDestroy(state);
184} 184}