unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#21883: unnecessary bit shifting range limits
@ 2015-11-12  7:07 Zefram
  2018-10-14  8:18 ` Mark H Weaver
  0 siblings, 1 reply; 5+ messages in thread
From: Zefram @ 2015-11-12  7:07 UTC (permalink / raw)
  To: 21883

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.  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))))

-zefram





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#21883: unnecessary bit shifting range limits
  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>
  0 siblings, 1 reply; 5+ messages in thread
From: Mark H Weaver @ 2018-10-14  8:18 UTC (permalink / raw)
  To: Zefram; +Cc: 21883

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





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#21883: unnecessary bit shifting range limits
       [not found]     ` <CAGua6m1CrfW2jEmQcDn_-1szGQbNcboBa45M6inEE5As6FToXw@mail.gmail.com>
@ 2018-10-14  9:46       ` Stefan Israelsson Tampe
  2018-10-14 22:19         ` Mark H Weaver
  0 siblings, 1 reply; 5+ messages in thread
From: Stefan Israelsson Tampe @ 2018-10-14  9:46 UTC (permalink / raw)
  To: Mark H Weaver; +Cc: 21883, Zefram

[-- 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 --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#21883: unnecessary bit shifting range limits
  2018-10-14  9:46       ` Stefan Israelsson Tampe
@ 2018-10-14 22:19         ` Mark H Weaver
  2018-10-15  7:08           ` Stefan Israelsson Tampe
  0 siblings, 1 reply; 5+ messages in thread
From: Mark H Weaver @ 2018-10-14 22:19 UTC (permalink / raw)
  To: Stefan Israelsson Tampe; +Cc: 21883, Zefram

Stefan Israelsson Tampe <stefan.itampe@gmail.com> writes:
> 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.

If you have a suggestion that's simpler than what I did in commits
011aec7e, 9448a078, and 1990aa91, and just as fast in the common cases,
feel free to propose a patch.  The words above are insufficient.

      Mark





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#21883: unnecessary bit shifting range limits
  2018-10-14 22:19         ` Mark H Weaver
@ 2018-10-15  7:08           ` Stefan Israelsson Tampe
  0 siblings, 0 replies; 5+ messages in thread
From: Stefan Israelsson Tampe @ 2018-10-15  7:08 UTC (permalink / raw)
  To: Mark H Weaver; +Cc: 21883, Zefram

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

i think you got it. sorry for the fuzz.


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

> Stefan Israelsson Tampe <stefan.itampe@gmail.com> writes:
> > 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.
>
> If you have a suggestion that's simpler than what I did in commits
> 011aec7e, 9448a078, and 1990aa91, and just as fast in the common cases,
> feel free to propose a patch.  The words above are insufficient.
>
>       Mark
>

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-10-15  7:08 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2018-10-14 22:19         ` Mark H Weaver
2018-10-15  7:08           ` Stefan Israelsson Tampe

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).