Opened 16 years ago
Closed 16 years ago
#19 closed defect (fixed)
Modular arith bug 2
Reported by: | Raymond Toy | Owned by: | somebody |
---|---|---|---|
Priority: | major | Milestone: | |
Component: | Core | Version: | 19b |
Keywords: | Cc: |
Description
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.
Note: See
TracTickets for help on using
tickets.
This should be fixed in the 2008-09 snapshot by changing the modular arithmetic algorithm so that if the args to
logand
are known to be fixnums then regular fixnum arithmetic is done instead of trying to do modular arithmetic.