From owner-freebsd-amd64@FreeBSD.ORG Tue Dec 21 01:47:54 2004 Return-Path: Delivered-To: freebsd-amd64@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 05C7516A4CE for ; Tue, 21 Dec 2004 01:47:54 +0000 (GMT) Received: from odin.ac.hmc.edu (Odin.AC.HMC.Edu [134.173.32.75]) by mx1.FreeBSD.org (Postfix) with ESMTP id CAB5843D1F for ; Tue, 21 Dec 2004 01:47:53 +0000 (GMT) (envelope-from brdavis@odin.ac.hmc.edu) Received: from odin.ac.hmc.edu (localhost.localdomain [127.0.0.1]) by odin.ac.hmc.edu (8.13.0/8.13.0) with ESMTP id iBL1lsib014404; Mon, 20 Dec 2004 17:47:54 -0800 Received: (from brdavis@localhost) by odin.ac.hmc.edu (8.13.0/8.13.0/Submit) id iBL1ls3V014403; Mon, 20 Dec 2004 17:47:54 -0800 Date: Mon, 20 Dec 2004 17:47:54 -0800 From: Brooks Davis To: "James R. Van Artsalen" Message-ID: <20041221014754.GA13070@odin.ac.hmc.edu> References: <41C778CD.1080602@jrv.org> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="opJtzjQTFsWo+cga" Content-Disposition: inline In-Reply-To: <41C778CD.1080602@jrv.org> User-Agent: Mutt/1.4.1i X-Virus-Scanned: by amavisd-new X-Spam-Status: No, hits=0.0 required=8.0 tests=none autolearn=no version=2.63 X-Spam-Checker-Version: SpamAssassin 2.63 (2004-01-11) on odin.ac.hmc.edu cc: freebsd-amd64@freebsd.org Subject: Re: /usr/games/random bug? X-BeenThere: freebsd-amd64@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Porting FreeBSD to the AMD64 platform List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 21 Dec 2004 01:47:54 -0000 --opJtzjQTFsWo+cga Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Dec 20, 2004 at 07:13:49PM -0600, James R. Van Artsalen wrote: > 5-stable "/usr/games/random -e 99" seems to always return 0, which makes= =20 > it very predictable. >=20 > 5.2.1 does this too. But i386 4.x seems to work as expected (an exit=20 > value between 0 and 98 inclusive). >=20 > The source code line in question is >=20 > return (int)((denom * random()) / LONG_MAX); >=20 > the assembler code is >=20 > .p2align 3 > .LC17: > .long 0 > .long 1006632960 >=20 > call random > cvtsi2sdq %rax, %xmm0 > mulsd 24(%rsp), %xmm0 > mulsd .LC17(%rip), %xmm0 > cvttsd2si %xmm0, %eax > jmp .L1 >=20 > I'm not familiar enough with IEEE fp rules or amd64 fp coding to say why= =20 > this doesn't work, nor if this source code is even reasonable. I believe the code is bogus. The code says, compute a long (denom*random) then divide it by the largest possiable value you can store in a long. Since integer math in C truncates, the value should always be 0 unless denom*random() happens to be LONG_MAX in which case it will be 1. It's intresting that this code ever worked. A quick check indicates that this doesn not work on alpha or sparc64. Apparently no one noticed that this code has been broken for over a decade (we got it from UCB). NetBSD switched from LONG_MAX to MAXRANDOM in 2000, but apparently didn't actually read the code since it also has the bug. -- Brooks --=20 Any statement of the form "X is the one, true Y" is FALSE. PGP fingerprint 655D 519C 26A7 82E7 2529 9BF0 5D8E 8BE9 F238 1AD4 --opJtzjQTFsWo+cga Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (GNU/Linux) iD8DBQFBx4DJXY6L6fI4GtQRAiT5AKDH1hBsa6xruWC4p4a0I8qNq4/wtwCghcTW OV5XfgZym8U0dIVJ3B6zbLQ= =1ANS -----END PGP SIGNATURE----- --opJtzjQTFsWo+cga--