From owner-svn-src-head@freebsd.org Mon May 30 16:19:02 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 413F5B54DF1 for ; Mon, 30 May 2016 16:19:02 +0000 (UTC) (envelope-from pfg@FreeBSD.org) Received: from nm36-vm2.bullet.mail.bf1.yahoo.com (nm36-vm2.bullet.mail.bf1.yahoo.com [72.30.238.138]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 019E21478 for ; Mon, 30 May 2016 16:19:01 +0000 (UTC) (envelope-from pfg@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1464624757; bh=T1ajSTfScZ6IBwC8XMQvMczWGK2xlF6o5dLS2NhSCrk=; h=Subject:To:References:Cc:From:Date:In-Reply-To:From:Subject; b=IsLvLPErN1K6wECduhCepGa85qrq/67lHlsxPZOX70W1qupy4elSitIiGBQ+KFvXlZBoBLH8njpNpg6djpv47oLAle6gH9mB4m0y0yBeO5O3aBa1drWEkg7BZ8M2vCYIqlXGmwY/oDcdP+OEt4qAtEKIS2VPKOyxyHdP0kxlSP7ZJnv2NVe7TyJz8hW+aG6bu5TnoCCEu06HueooW23zRu/5TyeVIp3RE30rHXz2FRA43XWBGVWv54XswHCuXlK3crYNOAzIt6lR/cQbB9byto3apavlxvJFsbJcWM0waGU3ZZ9htxQl/fQGLj6auzjg5b99SO7ZH5FSXfweh88keg== Received: from [66.196.81.172] by nm36.bullet.mail.bf1.yahoo.com with NNFMP; 30 May 2016 16:12:37 -0000 Received: from [98.139.211.161] by tm18.bullet.mail.bf1.yahoo.com with NNFMP; 30 May 2016 16:12:37 -0000 Received: from [127.0.0.1] by smtp218.mail.bf1.yahoo.com with NNFMP; 30 May 2016 16:12:37 -0000 X-Yahoo-Newman-Id: 595864.95835.bm@smtp218.mail.bf1.yahoo.com X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: dT5lFmQVM1nsVVQ3bhD7tufuKOQgXztEOBLZLiaiQEhnbMq lRqize_cn.k00KuNTziGG4rH7vluVyyW7O1_N5xxPnTKKdXvBwiGVlM05jgz n2gOoF9a6FAvoGWNlJImbgkMgG.m3sUtd4ko67UcjnpCaRdOTdzoV9syrMBo lAYEWoPFVHrZkCPCQnIBEJdpHdg5B_QEUybpZanZbdmRSFOhKR3tY_QCWDg4 KEY3hx4aYAAFm591kNbqVD7aNfM2JvA4Uf1noxv.UShIzJW.yLcXUp.SriY9 1meuuX.q7YZy9tDsAo2tlkkpCqzErE_lRvnGNX5nTKrtaMhh76lVf.DTzvhB cW9fExqfzG70wcxGnt8T5B0PoC3axlivm._IWgRnZr00kcYcGdxIdanqeMuR aAG1gIYjjmKlxNgZgR48DzDw_uE1nqCCyPyklr9R_67JxBLU4Xdei2z.B8iA wRnuqbl0NMU7RhBrTmnBBBe4ZsdbheNNXwautAzuwUWx2Cz98PZQQBr6kFdv bBRxhLl6TfcPlDKRcBdrrJZJg6HvXEYku X-Yahoo-SMTP: xcjD0guswBAZaPPIbxpWwLcp9Unf Subject: Re: svn commit: r300956 - head/lib/libc/stdlib To: Bruce Evans , "Andrey A. Chernov" References: <201605291357.u4TDv6No071840@repo.freebsd.org> <20160530110541.I924@besplex.bde.org> Cc: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org From: Pedro Giffuni Message-ID: Date: Mon, 30 May 2016 11:12:54 -0500 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:45.0) Gecko/20100101 Thunderbird/45.1.0 MIME-Version: 1.0 In-Reply-To: <20160530110541.I924@besplex.bde.org> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit 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: Mon, 30 May 2016 16:19:02 -0000 (Interesting discussion) On 05/29/16 21:17, Bruce Evans wrote: > On Sun, 29 May 2016, Andrey A. Chernov wrote: > >> Log: >> 1) Unifdef USE_WEAK_SEEDING since it is too obsolete to support and >> makes >> reading hard. > > Good. > >> 2) Instead of doing range transformation in each and every function >> here, >> do it single time directly in do_rand(). One "mod" operation overhead >> is not >> a big deal, but the code looks nicer and possible future functions >> additions >> or PRNG change do not miss range transformations neither have >> unneeded ones. > > The whole implementation is silly. It is manually optimized for 1980's > compilers. More below. > >> 3) Use POSIX argument types for visible functions (cosmetic). > > Not sure I like type changes. > >> Modified: head/lib/libc/stdlib/rand.c >> ============================================================================== >> >> --- head/lib/libc/stdlib/rand.c Sun May 29 12:21:54 2016 (r300955) >> +++ head/lib/libc/stdlib/rand.c Sun May 29 13:57:06 2016 (r300956) >> @@ -48,14 +48,6 @@ __FBSDID("$FreeBSD$"); >> static int >> do_rand(unsigned long *ctx) >> { >> -#ifdef USE_WEAK_SEEDING >> -/* >> - * Historic implementation compatibility. >> - * The random sequences do not vary much with the seed, >> - * even with overflowing. >> - */ >> - return ((*ctx = *ctx * 1103515245 + 12345) % ((u_long)RAND_MAX + >> 1)); > > This is a good implementation of a not very good LCG, made very bad by > botching RAND_MAX. The magic numbers except for RAND_MAX are copied > from the example in the C90 spec. I think they are good enough there. > The comment in at least the C99 spec says "// RAND_MAX assumed to be > 32767". This means that these magic numbers were chosen to work with > this value of RAND_MAX. (unsigned) longs are used to give a period > much longer than RAND_MAX and for technical reasons. Taking the modulo > to many fewer bits than the minimum of 32 for an unsigned long then > disguises the linearity. The BSD version almost completly breaks this > on arches with 32 bit longs by taking the modulo to 31 bits (mod 32 bits > would give complete breakage). Arches with 64-bit longs accidentally > work a bit better, by the coefficients are poorly chosen -- they should > be 64 bits and the arithmetic 128 bits. > FWIW, there are coefficients for a 64 bit LCG here: http://nuclear.llnl.gov/CNP/rng/rngman/node4.html It would be interesting to have a LCG optimized for modern platforms, even if we cannot produce more than 31 bits due to the standards. Pedro.