unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: Mark H Weaver <mhw@netris.org>
To: Zefram <zefram@fysh.org>
Cc: 21883@debbugs.gnu.org
Subject: bug#21883: unnecessary bit shifting range limits
Date: Sun, 14 Oct 2018 04:18:13 -0400	[thread overview]
Message-ID: <87tvlpszzu.fsf@netris.org> (raw)
In-Reply-To: <20151112070725.GA875@fysh.org> (Zefram's message of "Thu, 12 Nov 2015 07:07:25 +0000")

Zefram <zefram@fysh.org> 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





  reply	other threads:[~2018-10-14  8:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-12  7:07 bug#21883: unnecessary bit shifting range limits Zefram
2018-10-14  8:18 ` Mark H Weaver [this message]
     [not found]   ` <CAGua6m1D3Ed-T3GJntfZr5QesTvR_Gf1-JbsiAetdoxBGkce_w@mail.gmail.com>
     [not found]     ` <CAGua6m1CrfW2jEmQcDn_-1szGQbNcboBa45M6inEE5As6FToXw@mail.gmail.com>
2018-10-14  9:46       ` Stefan Israelsson Tampe
2018-10-14 22:19         ` Mark H Weaver
2018-10-15  7:08           ` Stefan Israelsson Tampe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87tvlpszzu.fsf@netris.org \
    --to=mhw@netris.org \
    --cc=21883@debbugs.gnu.org \
    --cc=zefram@fysh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).