GNU bug report logs -
#21883
unnecessary bit shifting range limits
Previous Next
Full log
View this message in rfc822 format
Zefram <zefram <at> 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
This bug report was last modified 6 years and 304 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.