bug-guile
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#21883: unnecessary bit shifting range limits


From: Stefan Israelsson Tampe
Subject: bug#21883: unnecessary bit shifting range limits
Date: Sun, 14 Oct 2018 11:46:12 +0200

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" <address@hidden>:
Zefram <address@hidden> 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 via email to

[Prev in Thread] Current Thread [Next in Thread]