From owner-svn-src-all@freebsd.org Tue May 31 09:58:52 2016 Return-Path: Delivered-To: svn-src-all@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 88228B5463B; Tue, 31 May 2016 09:58:52 +0000 (UTC) (envelope-from brde@optusnet.com.au) Received: from mail110.syd.optusnet.com.au (mail110.syd.optusnet.com.au [211.29.132.97]) by mx1.freebsd.org (Postfix) with ESMTP id 28F271693; Tue, 31 May 2016 09:58:51 +0000 (UTC) (envelope-from brde@optusnet.com.au) Received: from c122-106-149-109.carlnfd1.nsw.optusnet.com.au (c122-106-149-109.carlnfd1.nsw.optusnet.com.au [122.106.149.109]) by mail110.syd.optusnet.com.au (Postfix) with ESMTPS id DD3517805FF; Tue, 31 May 2016 19:58:48 +1000 (AEST) Date: Tue, 31 May 2016 19:58:47 +1000 (EST) From: Bruce Evans X-X-Sender: bde@besplex.bde.org To: Andrey Chernov cc: Bruce Evans , pfg@freebsd.org, src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: Re: svn commit: r300956 - head/lib/libc/stdlib In-Reply-To: Message-ID: <20160531185907.Y2047@besplex.bde.org> References: <201605291357.u4TDv6No071840@repo.freebsd.org> <20160530110541.I924@besplex.bde.org> <20160531155327.I1534@besplex.bde.org> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Optus-CM-Score: 0 X-Optus-CM-Analysis: v=2.1 cv=c+ZWOkJl c=1 sm=1 tr=0 a=R/f3m204ZbWUO/0rwPSMPw==:117 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=kj9zAlcOel0A:10 a=lahBRLA8AAAA:8 a=i2mCBWboAAAA:8 a=YcMFh1NDAAAA:8 a=HTCe4BXcai_nrORYvSwA:9 a=CjuIK1q_8ugA:10 a=5cIw-q_vxBcA:10 a=hQ8eJWA_F3oA:10 a=vmqOMGAk8PMA:10 a=scM_vJrj1NO3ah21njXL:22 a=rcnv4SZFDOS18o934fMY:22 a=XzeFQDB4FLgTs7H1q4LV:22 X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 31 May 2016 09:58:52 -0000 On Tue, 31 May 2016, Andrey Chernov wrote: > On 31.05.2016 9:48, Bruce Evans wrote: >>> Perhaps you can find some ideas, answers and PRNG comparison in the >>> original paper: >>> http://www.firstpr.com.au/dsp/rand31/p1192-park.pdf >> >> The ones with Mersenne primes and tweaked Mersenne primes in the reference >> (lanl?) given by pfg@ seem OK. > > It should be again correctly implemented to not overflow 64bit (as any > other 64bit generator too). > > Moreover, it have visible patterns in the low order bits. This one from > the same site is better > http://nuclear.llnl.gov/CNP/rng/rngman/node7.html > but still have some pattern too and 61-bit. Next one does not suffer > from the pattern at all > http://nuclear.llnl.gov/CNP/rng/rngman/node8.html > but is 2^64-2^10+1. That's the tweaked Mersenne prime one. Surely there is a pattern in the bits for all of these, and the better ones just hide the pattern better from normal uses and tests? Isn't it well known that the lower bits have more patterns so shouldn't be used? This depends on whether the implementation exposes all its bits. Since the rand() specification and FreeBSD man pages don't say anything about this, it is a bug in the implementation to expose all its bits. The implementation is handicapped by using only 32 bits internally but wanting RAND_MAX to be almost 32 bits. > You can download SPRNG library implementing all of them here: > http://www.sprng.org/RNG/ > For me it is overcomplicated. The general case is certainly too complicated. It would let the user specify all the parameters for the LCG and generate optimal code for the choice on the fly. Maybe also change the parameters with the seed. But even most implementors don't know how to choose the parameters. That's just for the not so good LCG family of RNGs. >>> clang -O uses single "idivl" calculating both quotient and reminder for >>> current code, but for ldiv(3) case call/storage/additional calcs >>> overhead will be added. ldiv(3) does not reduce anything, see >>> stdlib/ldiv.c >> >> The extern functions give lots of overhead. Sometimes they get replaced >> automatically by builtins, so it is unclear when the extern functions are >> actually used. ldiv.c compiles to idivl for the division part but has >> lots of extras for the fixups. The fixups do nothing except waste time >> on most hardware/cstd combinations. IIRC, C99 requires direct division >> to have the same poor rounding rules as idiv(), so idiv() is not really >> needed in C99 and the fixups in it are negatively needed. The builtins >> know what is needed so they don't do the fixups in the usual case that >> the hardware follows the poor rounding rules. > > We don't have ldiv() builtin in any cae. Apparently integer division is too fundamental to be a builtin. strings -a `which clang` | grep builtin.*div on amd64 gives 47 builtins for division, but none seem to be for integer division. gcc-3.3 has just 4, all for SSE FP division. > For fixups, see full > explanation in the comment of stdlib/div.c That was what I was complaining about. div.c is for C90 (misspelled "ANSI"). div.c hasn't caught up with C99. C99 broke this by changing the rules for integer division to disallow correct rounding for and require consistently incorrect rounding. Correct rounding for a positive divisor is towards minus infinity so that the remainder is not negative. This is modulo arithmetic and has good algebraic properties. C99 requires rounding minus infinity, at least for positive divisors, under the extension "reliable integer division". In C90, the rounding is implementation- defined, so it may be correct, but it is "unreliable" since it can be anything. In C90, div() is specified as giving "reliable" division, and that is what the fixups implement. This now wastes time to change nothing. If the hardware does correct rounding, then the compiler already had to do the fixup and id should have produced good MD code for this. The C code then adds not so good MI code. Bruce