From owner-svn-src-head@freebsd.org Sun May 22 02:43:31 2016 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 5536CB43DC8 for ; Sun, 22 May 2016 02:43:31 +0000 (UTC) (envelope-from mailing-machine@vniz.net) Received: from mail-lf0-f65.google.com (mail-lf0-f65.google.com [209.85.215.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id F259112D3 for ; Sun, 22 May 2016 02:43:30 +0000 (UTC) (envelope-from mailing-machine@vniz.net) Received: by mail-lf0-f65.google.com with SMTP id p10so4590086lfb.2 for ; Sat, 21 May 2016 19:43:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:cc:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=CuZHZSSB4p1fh5P81EYerCM+1/6rCj2QECk3nZLMt0c=; b=gGCPywCHRwOox/UCoQehPLR3QAM23KQH9TroSX5WDXjPX6IAxNz9r2kL+P4eSHZV17 XRLyTRHPZ8/Veqjj9yuYolQ3s0q46iD9Zie2DcMCnRqpUH9vhLoRKUjQl5KkvWUrtkXO rZN/qWCIMJMrKuzY+x1J795INft8dT9pxjTuMkEIQuaIo2+ycXmIGkYqLFzFUdhj3Nys Dn4yQIE/oUsap/AA4vtcuI9Laz9fMHtqE4xEquhuHdDemhrvO6nqZ8TIQVOqGAbogHFa m6bEp8Ho0JDlT6vUXhqKa4z+jBPOC3pB3GOVicjFF4ClEggCt3dArsUbfOWatPqfPMrY CZgw== X-Gm-Message-State: ALyK8tIXIVx6eCM/xtQMZRTm32Shv4SnBfWBFeTk3uIPytZ4UVIDODWGrALfhDrTD7wBSQ== X-Received: by 10.25.77.195 with SMTP id a186mr2263990lfb.159.1463885003394; Sat, 21 May 2016 19:43:23 -0700 (PDT) Received: from [192.168.1.2] ([89.169.173.68]) by smtp.gmail.com with ESMTPSA id d63sm3069447lfb.48.2016.05.21.19.43.22 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 21 May 2016 19:43:22 -0700 (PDT) Subject: Re: svn commit: r300377 - head/sys/compat/ndis To: Bruce Evans , Pedro Giffuni References: <201605211752.u4LHqiHQ031457@repo.freebsd.org> <262938a6-50bd-b6f4-24c9-895b837a368e@FreeBSD.org> <20160522091455.A1014@besplex.bde.org> Cc: cem@freebsd.org, src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org From: Andrey Chernov Message-ID: <8bd5428f-6955-72c3-0294-72f7ef936e16@freebsd.org> Date: Sun, 22 May 2016 05:43:20 +0300 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.1.0 MIME-Version: 1.0 In-Reply-To: <20160522091455.A1014@besplex.bde.org> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 22 May 2016 02:43:31 -0000 On 22.05.2016 3:06, Bruce Evans wrote: > On Sat, 21 May 2016, Pedro Giffuni wrote: > >> On 05/21/16 16:55, Conrad Meyer wrote: >>> On Sat, May 21, 2016 at 1:40 PM, Pedro Giffuni wrote: >>>> >>>> >>>> On 05/21/16 14:05, Conrad Meyer wrote: >>>>> Won't this still return a negative integer in many cases? >>>>> >>>>> random(9) returns u_long, whereas this rand() routine returns 'int'. >>>>> >>>>> Even on architectures where long is the same size as ordinary >>>>> integers, the range of possible results of the 'random() / 2 + 1' >>>>> expression, before implicit cast to signed, is [1, 2^31] (inclusive). >>>> >>>> According to: >>>> sys/libkern/random.c >>>> >>>> The result is uniform on [0, 2^31 - 1]. >>> >>> Ah, I missed that. Sorry! In that case, I'm not sure why this is >>> needed — the result fits in a non-negative 2's complement signed >>> integer. >> >> Actually, I had missed it too. And I also had no idea we were working >> around the zero singularity. >> >> I will revert the change and will do an adjustment for the case where >> we use 0 as a seed (which in MS should be equivalent to 1). > > The libc version has complications related to this. The libkern > version has rotted by not being kept up to date with the libc version. > 4.4BSD-Lite has sort of the reverse bitrot -- in libc, it only has the > bad LCG that copied from an example in the 1990 C standard, while > it has the better LCG copied from 1988 Communications in the ACM in > libkern. > FreeBSD still has the ACM version in libkern, and has a fixed copy > of that in libc, with the bad old version under an ifdef. > > The libc version now adjusts the range from [0, 0x7fffffff] to > 0, 0x7ffffffd] and reduces RAND_MAX by 2 to match. The claimed uniformity > for the larger range is very wrong, since the ACM algorithm can only > produce numbers in the range [1(or is it 0?), 0x7ffffffe] starting from a > seed in the range [1, 0x7ffffffe(or is it 1 higher?)]. There are problems > at both extremities, and it isn't clear if the new or old adjustments to > avoid them preserve uniformity. It is clear that the range was at least > 1 too high, since the ACM algorithm does a modulo by 0x7fffffff. libc version does range adjustment for better uniformity only for rand(3), not for random(3). There is no RAND_MAX constant in the random(3) API. POSIX require that random(3) should stay in [0, 2^31-1].