unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: Stefan Israelsson Tampe <stefan.itampe@gmail.com>
To: Mark H Weaver <mhw@netris.org>
Cc: 21883@debbugs.gnu.org, Zefram <zefram@fysh.org>
Subject: bug#21883: unnecessary bit shifting range limits
Date: Sun, 14 Oct 2018 11:46:12 +0200	[thread overview]
Message-ID: <CAGua6m1gnynxOxWsTEjf=EAMirWVp+bOP2aTsiLs=d1Z0UmdLw@mail.gmail.com> (raw)
In-Reply-To: <CAGua6m1CrfW2jEmQcDn_-1szGQbNcboBa45M6inEE5As6FToXw@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1790 bytes --]

how would this slow down the code. just add the correction where you throw
the exception which should be in a branch outside the hot path.

Den 14 okt 2018 10:19 AM skrev "Mark H Weaver" <mhw@netris.org>:

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

[-- Attachment #2: Type: text/html, Size: 2518 bytes --]

  parent reply	other threads:[~2018-10-14  9:46 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
     [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 [this message]
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='CAGua6m1gnynxOxWsTEjf=EAMirWVp+bOP2aTsiLs=d1Z0UmdLw@mail.gmail.com' \
    --to=stefan.itampe@gmail.com \
    --cc=21883@debbugs.gnu.org \
    --cc=mhw@netris.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).