From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Mark H Weaver Newsgroups: gmane.lisp.guile.bugs Subject: bug#21883: unnecessary bit shifting range limits Date: Sun, 14 Oct 2018 04:18:13 -0400 Message-ID: <87tvlpszzu.fsf@netris.org> References: <20151112070725.GA875@fysh.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1539505028 8607 195.159.176.226 (14 Oct 2018 08:17:08 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 14 Oct 2018 08:17:08 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) Cc: 21883@debbugs.gnu.org To: Zefram Original-X-From: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Sun Oct 14 10:17:04 2018 Return-path: Envelope-to: guile-bugs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gBba9-00024j-L7 for guile-bugs@m.gmane.org; Sun, 14 Oct 2018 10:17:01 +0200 Original-Received: from localhost ([::1]:47549 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gBbcF-00066G-OO for guile-bugs@m.gmane.org; Sun, 14 Oct 2018 04:19:11 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:49866) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gBbcA-00066A-4a for bug-guile@gnu.org; Sun, 14 Oct 2018 04:19:06 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gBbc6-0007GA-Py for bug-guile@gnu.org; Sun, 14 Oct 2018 04:19:06 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:44500) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gBbc6-0007G2-MK for bug-guile@gnu.org; Sun, 14 Oct 2018 04:19:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1gBbc6-0005KO-8M for bug-guile@gnu.org; Sun, 14 Oct 2018 04:19:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Mark H Weaver Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Sun, 14 Oct 2018 08:19:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 21883 X-GNU-PR-Package: guile X-GNU-PR-Keywords: Original-Received: via spool by 21883-submit@debbugs.gnu.org id=B21883.153950512020450 (code B ref 21883); Sun, 14 Oct 2018 08:19:02 +0000 Original-Received: (at 21883) by debbugs.gnu.org; 14 Oct 2018 08:18:40 +0000 Original-Received: from localhost ([127.0.0.1]:48758 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gBbbk-0005Jm-7H for submit@debbugs.gnu.org; Sun, 14 Oct 2018 04:18:40 -0400 Original-Received: from world.peace.net ([64.112.178.59]:39288) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gBbbi-0005JV-9f for 21883@debbugs.gnu.org; Sun, 14 Oct 2018 04:18:38 -0400 Original-Received: from mhw by world.peace.net with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1gBbbb-0001vj-Jl; Sun, 14 Oct 2018 04:18:31 -0400 In-Reply-To: <20151112070725.GA875@fysh.org> (Zefram's message of "Thu, 12 Nov 2015 07:07:25 +0000") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Original-Sender: "bug-guile" Xref: news.gmane.org gmane.lisp.guile.bugs:9187 Archived-At: Zefram writes: > Not really outright bugs, but these responses are less than awesome: > > $ guile -c '(write (logbit? (ash 1 100) 123))' > ERROR: Value out of range 0 to 18446744073709551615: 1267650600228229401496703205376 > $ guile -c '(write (ash 0 (ash 1 100)))' > ERROR: Value out of range -9223372036854775808 to 9223372036854775807: 1267650600228229401496703205376 > $ guile -c '(write (ash 123 (ash -1 100)))' > ERROR: Value out of range -9223372036854775808 to 9223372036854775807: -1267650600228229401496703205376 > > In all three cases, the theoretically-correct result of the expression > is not only representable but easily computed. Commit 011aec7e240ef987931548d90c53e6692c85d01c on the stable-2.2 branch extends 'ash' and 'round-ash' to handle the easily computed cases of huge shifts. > The functions could be improved to avoid failing in these cases, by > adding logic amounting to: > > (define (better-logbit? b v) > (if (>= b (integer-length v)) (< v 0) (logbit? b v))) > > (define (better-ash v s) > (cond > ((= v 0) 0) > ((<= s (- (integer-length v))) (if (< v 0) -1 0)) > (else (ash v s)))) Unfortunately, simple implementations like the ones above slow down the common case with expensive checks that are rarely needed. The aforementioned commit takes pains to avoid slowing down the common case, but at the cost of extra code complexity. In theory we could do something similar with many other procedures that implement operations on bits and bit fields, but I wonder if it's worth the extra complexity. Mark