|From:||Stefan Israelsson Tampe|
|Subject:||bug#21883: unnecessary bit shifting range limits|
|Date:||Sun, 14 Oct 2018 11:46:12 +0200|
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: 126765060022822940149670320537
> $ guile -c '(write (ash 0 (ash 1 100)))'
> ERROR: Value out of range -9223372036854775808 to 9223372036854775807: 126765060022822940149670320537
> $ guile -c '(write (ash 123 (ash -1 100)))'
> ERROR: Value out of range -9223372036854775808 to 9223372036854775807: -
> In all three cases, the theoretically-correct result of the _expression_
> is not only representable but easily computed.
692c85d01c on the stable-2.2 branch
extends 'ash' and 'round-ash' to handle the easily computed cases of
> 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)
> ((= 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.
|[Prev in Thread]||Current Thread||[Next in Thread]|