dri-devel.lists.freedesktop.org archive mirror
 help / color / mirror / Atom feed
From: Jan Vesely <jan.vesely@rutgers.edu>
To: Ian Romanick <idr@freedesktop.org>
Cc: dri-devel@lists.freedesktop.org
Subject: Re: [libdrm][PATCH 1/2] random: Use unsigned long for seed
Date: Mon, 09 Feb 2015 18:26:09 -0500	[thread overview]
Message-ID: <1423524369.10898.56.camel@rutgers.edu> (raw)
In-Reply-To: <54D93E99.4010107@freedesktop.org>


[-- Attachment #1.1: Type: text/plain, Size: 2655 bytes --]

On Mon, 2015-02-09 at 15:11 -0800, Ian Romanick wrote:
> On 02/09/2015 01:39 PM, Jan Vesely wrote:
> > This is more consistent with the rest, and avoids potential undefined
> > behavior (signed overflow) in drmRandom()
> > 
> > Signed-off-by: Jan Vesely <jan.vesely@rutgers.edu>
> > ---
> >  xf86drmRandom.c | 14 +++++++-------
> >  1 file changed, 7 insertions(+), 7 deletions(-)
> > 
> > diff --git a/xf86drmRandom.c b/xf86drmRandom.c
> > index ecab9e2..a084b86 100644
> > --- a/xf86drmRandom.c
> > +++ b/xf86drmRandom.c
> > @@ -98,7 +98,7 @@ typedef struct RandomState {
> >      unsigned long q;		/* m div a */
> >      unsigned long r;		/* m mod a */
> >      unsigned long check;
> > -    long          seed;
> > +    unsigned long seed;
> >  } RandomState;
> >  
> >  #if RANDOM_MAIN
> > @@ -147,13 +147,13 @@ int drmRandomDestroy(void *state)
> >  unsigned long drmRandom(void *state)
> >  {
> >      RandomState   *s = (RandomState *)state;
> > -    long          hi;
> > -    long          lo;
> > +    unsigned long hi;
> > +    unsigned long lo;
> >  
> >      hi      = s->seed / s->q;
> >      lo      = s->seed % s->q;
> >      s->seed = s->a * lo - s->r * hi;
> > -    if (s->seed <= 0) s->seed += s->m;
> > +    if ((s->a * lo) <= (s->r * hi)) s->seed += s->m;
> 
> This seems like an unrelated change.

This change is necessary. since s->seed is unsigned now it cannot be <
0. also the result of s->seed op s->q is unsigned.

> 
> >  
> >      return s->seed;
> >  }
> > @@ -166,7 +166,7 @@ double drmRandomDouble(void *state)
> >  }
> >  
> >  #if RANDOM_MAIN
> > -static void check_period(long seed)
> > +static void check_period(unsigned long seed)
> >  {
> >      unsigned long count = 0;
> >      unsigned long initial;
> > @@ -178,7 +178,7 @@ static void check_period(long seed)
> >      while (initial != drmRandom(state)) {
> >  	if (!++count) break;
> >      }
> > -    printf("With seed of %10ld, period = %10lu (0x%08lx)\n",
> > +    printf("With seed of %10lu, period = %10lu (0x%08lx)\n",
> >  	   seed, count, count);
> >      drmRandomDestroy(state);
> >  }
> > @@ -195,7 +195,7 @@ int main(void)
> >      }
> >      printf("After 10000 iterations: %lu (%lu expected): %s\n",
> >  	   rand, state->check,
> > -	   rand - state->check ? "*INCORRECT*" : "CORRECT");
> > +	   rand != state->check ? "*INCORRECT*" : "CORRECT");
> 
> So does this.

I'll drop this one in v2, it shouldn't have been included

> 
> >      drmRandomDestroy(state);
> >  
> >      printf("Checking periods...\n");
> > 
> 

-- 
Jan Vesely <jan.vesely@rutgers.edu>

[-- Attachment #1.2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

[-- Attachment #2: Type: text/plain, Size: 159 bytes --]

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

  reply	other threads:[~2015-02-09 23:26 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-09 21:39 [libdrm][PATCH 1/2] random: Use unsigned long for seed Jan Vesely
2015-02-09 21:39 ` [libdrm][PATCH 2/2] Fix gcc -Wextra warnings Jan Vesely
2015-02-09 23:12   ` Ian Romanick
2015-02-09 23:32   ` Emil Velikov
2015-02-10  0:02     ` Jan Vesely
2015-02-10  0:27       ` Emil Velikov
2015-02-10 21:37         ` Jan Vesely
2015-02-10 22:55           ` Emil Velikov
2015-02-09 23:11 ` [libdrm][PATCH 1/2] random: Use unsigned long for seed Ian Romanick
2015-02-09 23:26   ` Jan Vesely [this message]
2015-02-09 23:28   ` [libdrm][PATCH v2 " Jan Vesely
2015-02-10 18:21     ` Ian Romanick
2015-02-10  0:10 ` [libdrm][PATCH 3/2] Fix always true comparison Jan Vesely
2015-02-25 17:11   ` Jan Vesely
2015-02-25 18:41     ` Emil Velikov
2015-03-02 20:01       ` Jan Vesely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1423524369.10898.56.camel@rutgers.edu \
    --to=jan.vesely@rutgers.edu \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=idr@freedesktop.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).