From patchwork Mon Feb 9 21:39:54 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Vesely X-Patchwork-Id: 5803351 Return-Path: X-Original-To: patchwork-dri-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 75257BF440 for ; Mon, 9 Feb 2015 21:40:09 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id AC6E42011D for ; Mon, 9 Feb 2015 21:40:08 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 9A6B92011B for ; Mon, 9 Feb 2015 21:40:07 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2274B6E5C2; Mon, 9 Feb 2015 13:40:06 -0800 (PST) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-qc0-f172.google.com (mail-qc0-f172.google.com [209.85.216.172]) by gabe.freedesktop.org (Postfix) with ESMTP id 5C5806E5C2 for ; Mon, 9 Feb 2015 13:40:05 -0800 (PST) Received: by mail-qc0-f172.google.com with SMTP id x3so25145793qcv.3 for ; Mon, 09 Feb 2015 13:40:04 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:subject:date:message-id; bh=nMxpoQQ4Sq0lUvmn/7ifyvBRy2ZmhK+ZvIcUBkSWi3w=; b=mnju/qcBSMlbkyuDUKLRDlIuiWQ+GqrDD9vdCYjgZjymtEgkcdrRwN47xOAG3Mi2oM oX+w8gaurhYcYRyOBRXxhlfZod4rBHC/T+pGJvXeitLDFirbOKJ8rgH9qZJpxE+CekCS ZgAaxM+nTZCv+ZS+kVLMoYum3noBL2qRUt/g3GlRf1TTaVSU9lVq3dFjSPEjfjhnxDDe yz0zgoHIcdxFX5i6oaXy/KxuB9NZ2F1PpBOHzz/v4T7p83JcZjIKNmIwnJ/Deq3jhl8e O6hCPCmpHZxEg4KeUG+XrNBw50rQCtRzVR1xODpfMvL94VHPg7cHWgyP5r5qxZfOliM7 lMaQ== X-Gm-Message-State: ALoCoQmjZop6nusFpL5BUwZqgIbZFdgqxGHZHbGXnsFoy35kaIJq5//ABXnTqCRcOTWfaDZEAozi X-Received: by 10.224.152.2 with SMTP id e2mr31713833qaw.23.1423518004664; Mon, 09 Feb 2015 13:40:04 -0800 (PST) Received: from adriatix.rutgers.edu ([198.151.130.142]) by mx.google.com with ESMTPSA id w21sm11294264qgd.15.2015.02.09.13.40.04 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 09 Feb 2015 13:40:04 -0800 (PST) From: Jan Vesely To: dri-devel@lists.freedesktop.org Subject: [libdrm][PATCH 1/2] random: Use unsigned long for seed Date: Mon, 9 Feb 2015 16:39:54 -0500 Message-Id: <1423517995-28251-1-git-send-email-jan.vesely@rutgers.edu> X-Mailer: git-send-email 2.1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This is more consistent with the rest, and avoids potential undefined behavior (signed overflow) in drmRandom() Signed-off-by: Jan Vesely --- 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; 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"); drmRandomDestroy(state); printf("Checking periods...\n");