#19: Modular arith bug 2 --------------------+------------------------------------------------------- Reporter: rtoy | Owner: somebody Type: defect | Status: new Priority: major | Milestone: Component: Core | Version: 19b Keywords: | --------------------+------------------------------------------------------- This was reported by Martin Cracauer to cmucl-imp, 2005-07-20.
{{{ (defun fixnum-hasher (a) (declare (optimize (speed 3) (safety 0) #+nil(debug 1)) ;; BUG HERE, only in fast mode (type fixnum a)) ;; bug equally apears if you leave this out (logand (the fixnum ;; BUG HERE. Drop this declaration and the bug disappears (logxor (ash a -20) -482305527)) 1023)) }}}
Repeatedly calling {{{(fixnum-hasher 27)}}} returns random values! The problem is caused by modular arithmetic because if it is turned off (by {{{setf c::*enable-modular-arithmetic* nil)}}} the bug doesn't happen. The answer should be 521.
A related bug is
{{{ (defun foo (a b) (locally (declare (optimize (speed 3) (safety 0) (debug 1))) (the fixnum (logand (the fixnum (logxor (the fixnum a) (the fixnum b))) (the fixnum (1- (ash 1 18))))))) }}}
This bug is mentioned in the same thread, but is from sbcl-devel, 2005-04- 29.
{{{(foo -1234 1234)}}} returns random values. The correct answer is 262140.
In both cases, there is a call to vm::logxor-mod32 with fixnum args, returning an unsigned 32-bit result. This is not right.