From cmucl-devel at common-lisp.net Tue Aug 12 16:10:38 2008 From: cmucl-devel at common-lisp.net (cmucl) Date: Tue, 12 Aug 2008 16:10:38 -0000 Subject: [cmucl-ticket] #18: Modular arith bug 1 Message-ID: <076.0333eefabbf6ebb585e9dad1b3f4c78e@common-lisp.net> #18: Modular arith bug 1 --------------------+------------------------------------------------------- Reporter: rtoy | Owner: somebody Type: defect | Status: new Priority: minor | Milestone: Component: Core | Version: 19d Keywords: | --------------------+------------------------------------------------------- Not really a bug, but a deficiency. {{{ (defun ref (a n) (declare (type (simple-array single-float (*)) a) (type (integer 0 15) n) (optimize (speed 3) (safety 0))) (aref a (logand #xf (+ n 15)))) }}} Relevant disassembly on sparc: {{{ 68: SRA %A1, 2, %NL0 ; %A1 = N ; No-arg-parsing entry point ; [:NON-LOCAL- ENTRY] 6C: ADD 15, %NL0 70: AND 15, %NL0 74: SLL %NL0, 2, %NL1 78: ADD %NL1, 1, %NL0 7C: LDF [%A0+%NL0], %F0 ; %A0 = A }}} So {{{N}}} is converted from a fixnum to an integer, the appropriate operations are done, and then converted back to a fixnum. All of this could have been done without the conversion like so: {{{ 40: ADD 60, %A1 ; %A1 = N ; No-arg-parsing entry point ; [:NON-LOCAL- ENTRY] 44: AND 60, %A1 48: ADD %A1, 1, %NL0 4C: LDF [%A0+%NL0], %F0 ; %A0 = A }}} -- Ticket URL: cmucl cmucl From cmucl-devel at common-lisp.net Tue Aug 12 16:20:47 2008 From: cmucl-devel at common-lisp.net (cmucl) Date: Tue, 12 Aug 2008 16:20:47 -0000 Subject: [cmucl-ticket] #19: Modular arith bug 2 Message-ID: <076.3d71255c270100cdd529f334dcb2479c@common-lisp.net> #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. -- Ticket URL: cmucl cmucl From cmucl-devel at common-lisp.net Tue Aug 12 17:08:04 2008 From: cmucl-devel at common-lisp.net (cmucl) Date: Tue, 12 Aug 2008 17:08:04 -0000 Subject: [cmucl-ticket] #20: Modular arith bug? Message-ID: <076.4184c85502b1faba8386ad54318421f9@common-lisp.net> #20: Modular arith bug? --------------------+------------------------------------------------------- Reporter: rtoy | Owner: somebody Type: defect | Status: new Priority: major | Milestone: Component: Core | Version: 19e Keywords: | --------------------+------------------------------------------------------- {{{ (defun mat3neg (tt v) (ldb (byte 32 0) (ash v (- tt)))) (defun mat3neg-a (tt v) (logand #xffffffff (ash v (- tt)))) (defun zot (z2) (declare (type (unsigned-byte 32) z2) (optimize speed (safety 0))) (mat3neg -28 z2)) (defun zot-a (z2) (declare (type (unsigned-byte 32) z2) (optimize speed (safety 0))) (mat3neg-a -28 z2)) }}} Compile {{{zot}}} and there are lots of compiler notes, from {{{mat3neg}}}. But compile {{{zot-a}}} and there's just one for boxing up the result. However, {{{zot}}} and {{{zot-a}}} are functionally identical, so {{{zot}}} and {{{zot-a}}} should produce the same code. But {{{zot}}} has to do a full call to {{{ash}}} and {{{two-arg-and}}}. The {{{ldb}}} must be confusing the compiler somehow. -- Ticket URL: cmucl cmucl From cmucl-devel at common-lisp.net Wed Aug 13 03:00:53 2008 From: cmucl-devel at common-lisp.net (cmucl) Date: Wed, 13 Aug 2008 03:00:53 -0000 Subject: [cmucl-ticket] Re: #20: Modular arith bug? In-Reply-To: <076.4184c85502b1faba8386ad54318421f9@common-lisp.net> References: <076.4184c85502b1faba8386ad54318421f9@common-lisp.net> Message-ID: <085.673164683f4524da661fd244422fdc17@common-lisp.net> #20: Modular arith bug? ---------------------+------------------------------------------------------ Reporter: rtoy | Owner: somebody Type: defect | Status: new Priority: major | Milestone: Component: Core | Version: 19e Resolution: | Keywords: ---------------------+------------------------------------------------------ Old description: > {{{ > (defun mat3neg (tt v) > (ldb (byte 32 0) (ash v (- tt)))) > > (defun mat3neg-a (tt v) > (logand #xffffffff (ash v (- tt)))) > > (defun zot (z2) > (declare (type (unsigned-byte 32) z2) > (optimize speed (safety 0))) > (mat3neg -28 z2)) > > (defun zot-a (z2) > (declare (type (unsigned-byte 32) z2) > (optimize speed (safety 0))) > (mat3neg-a -28 z2)) > }}} > > Compile {{{zot}}} and there are lots of compiler notes, from > {{{mat3neg}}}. But compile {{{zot-a}}} and there's just one for boxing > up the result. > > However, {{{zot}}} and {{{zot-a}}} are functionally identical, so > {{{zot}}} and {{{zot-a}}} should produce the same code. But {{{zot}}} > has to do a full call to {{{ash}}} and {{{two-arg-and}}}. > > The {{{ldb}}} must be confusing the compiler somehow. New description: {{{ (declaim (inline mat3neg mat3neg-a)) (defun mat3neg (tt v) (ldb (byte 32 0) (ash v (- tt)))) (defun mat3neg-a (tt v) (logand #xffffffff (ash v (- tt)))) (defun zot (z2) (declare (type (unsigned-byte 32) z2) (optimize speed (safety 0))) (mat3neg -28 z2)) (defun zot-a (z2) (declare (type (unsigned-byte 32) z2) (optimize speed (safety 0))) (mat3neg-a -28 z2)) }}} Compile {{{zot}}} and there are lots of compiler notes, from {{{mat3neg}}}. But compile {{{zot-a}}} and there's just one for boxing up the result. However, {{{zot}}} and {{{zot-a}}} are functionally identical, so {{{zot}}} and {{{zot-a}}} should produce the same code. But {{{zot}}} has to do a full call to {{{ash}}} and {{{two-arg-and}}}. The {{{ldb}}} must be confusing the compiler somehow. -- Ticket URL: cmucl cmucl From cmucl-devel at common-lisp.net Wed Aug 13 03:08:31 2008 From: cmucl-devel at common-lisp.net (cmucl) Date: Wed, 13 Aug 2008 03:08:31 -0000 Subject: [cmucl-ticket] Re: #20: Modular arith bug? In-Reply-To: <076.4184c85502b1faba8386ad54318421f9@common-lisp.net> References: <076.4184c85502b1faba8386ad54318421f9@common-lisp.net> Message-ID: <085.9f953b746313f3edb3751c8c8bdf416b@common-lisp.net> #20: Modular arith bug? ---------------------+------------------------------------------------------ Reporter: rtoy | Owner: somebody Type: defect | Status: new Priority: major | Milestone: Component: Core | Version: 19e Resolution: | Keywords: ---------------------+------------------------------------------------------ Comment (by rtoy): The issue is partly caused by ldb and, perhaps, partly by ash modular function optimizer. (ldb (byte size posn) x) is source-transformed to (%ldb size posn x), which has a deftransform that converts it to (logand (ash x (- posn)) (ash (1- (ash 1 vm:word-bits)) (- size vm:word-bits)). This expression must confuse logand-defopt-helper. One workaround is to change the %ldb to produce simpler expressions when posn and size are known constants. -- Ticket URL: cmucl cmucl From cmucl-devel at common-lisp.net Thu Aug 21 20:48:45 2008 From: cmucl-devel at common-lisp.net (cmucl) Date: Thu, 21 Aug 2008 20:48:45 -0000 Subject: [cmucl-ticket] #21: Modular arith bug 3 Message-ID: <076.9b423ea3f8e57eac1f44e23722d08d30@common-lisp.net> #21: Modular arith bug 3 --------------------+------------------------------------------------------- Reporter: rtoy | Owner: somebody Type: defect | Status: new Priority: major | Milestone: Component: Core | Version: 2008-08 Keywords: | --------------------+------------------------------------------------------- The following code doesn't use modular arithmetic for ash: {{{ (defun bug (v) (declare (type (unsigned-byte 32) v) (optimize (speed 3) (safety 0))) (logand #xffffffff (logxor v (ash v (- -16))))) }}} You get compiler warnings about {{{ASH}}}. But the equivalent code is ok: {{{ (defun bug-a (v) (declare (type (unsigned-byte 32) v) (optimize (speed 3) (safety 0))) (ldb (byte 32 0) (logxor v (ash v (- -16))))) }}} Also, if in {{{bug}}}, you change {{{(- -16)}}} to the obvious {{{16}}}, the compiler notes are gone, and the generated code is as expected. What appears to be happening is that when {{{logand-defopt-helper}}} is run, the compiler doesn't know the type of the shift for {{{ASH}}}. Hence, it can't do modular arithmetic stuff. However, when the code is finally generated, you can see that the shift is now a known constant value of 16. I don't know why. -- Ticket URL: cmucl cmucl From cmucl-devel at common-lisp.net Thu Aug 21 20:49:22 2008 From: cmucl-devel at common-lisp.net (cmucl) Date: Thu, 21 Aug 2008 20:49:22 -0000 Subject: [cmucl-ticket] Re: #21: Modular arith bug 3 In-Reply-To: <076.9b423ea3f8e57eac1f44e23722d08d30@common-lisp.net> References: <076.9b423ea3f8e57eac1f44e23722d08d30@common-lisp.net> Message-ID: <085.763ac6d5ef7c152e034e3a2dd31945b6@common-lisp.net> #21: Modular arith bug 3 ---------------------+------------------------------------------------------ Reporter: rtoy | Owner: somebody Type: defect | Status: new Priority: minor | Milestone: Component: Core | Version: 2008-08 Resolution: | Keywords: ---------------------+------------------------------------------------------ Changes (by rtoy): * priority: major => minor -- Ticket URL: cmucl cmucl From cmucl-devel at common-lisp.net Fri Aug 22 12:36:10 2008 From: cmucl-devel at common-lisp.net (cmucl) Date: Fri, 22 Aug 2008 12:36:10 -0000 Subject: [cmucl-ticket] Re: #18: Modular arith bug 1 In-Reply-To: <076.0333eefabbf6ebb585e9dad1b3f4c78e@common-lisp.net> References: <076.0333eefabbf6ebb585e9dad1b3f4c78e@common-lisp.net> Message-ID: <085.1a8f3040fa14c9077678ccbef2ee2d76@common-lisp.net> #18: Modular arith bug 1 ---------------------+------------------------------------------------------ Reporter: rtoy | Owner: somebody Type: defect | Status: closed Priority: minor | Milestone: Component: Core | Version: 19d Resolution: fixed | Keywords: ---------------------+------------------------------------------------------ Changes (by rtoy): * resolution: => fixed * status: new => closed Comment: This should be fixed in the 2008-09 snapshot. The cost for various VOPs were tweaked slightly to allow the fixnum VOPs a chance to run. -- Ticket URL: cmucl cmucl From cmucl-devel at common-lisp.net Fri Aug 22 12:37:50 2008 From: cmucl-devel at common-lisp.net (cmucl) Date: Fri, 22 Aug 2008 12:37:50 -0000 Subject: [cmucl-ticket] Re: #19: Modular arith bug 2 In-Reply-To: <076.3d71255c270100cdd529f334dcb2479c@common-lisp.net> References: <076.3d71255c270100cdd529f334dcb2479c@common-lisp.net> Message-ID: <085.64fe2ede146fbe36ce5286d12f318ce6@common-lisp.net> #19: Modular arith bug 2 ---------------------+------------------------------------------------------ Reporter: rtoy | Owner: somebody Type: defect | Status: closed Priority: major | Milestone: Component: Core | Version: 19b Resolution: fixed | Keywords: ---------------------+------------------------------------------------------ Changes (by rtoy): * resolution: => fixed * status: new => closed Comment: 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. -- Ticket URL: cmucl cmucl From cmucl-devel at common-lisp.net Fri Aug 22 12:40:15 2008 From: cmucl-devel at common-lisp.net (cmucl) Date: Fri, 22 Aug 2008 12:40:15 -0000 Subject: [cmucl-ticket] Re: #20: Modular arith bug? In-Reply-To: <076.4184c85502b1faba8386ad54318421f9@common-lisp.net> References: <076.4184c85502b1faba8386ad54318421f9@common-lisp.net> Message-ID: <085.952a5410cc18db0674cef78ee45020bd@common-lisp.net> #20: Modular arith bug? ---------------------+------------------------------------------------------ Reporter: rtoy | Owner: somebody Type: defect | Status: closed Priority: major | Milestone: Component: Core | Version: 19e Resolution: fixed | Keywords: ---------------------+------------------------------------------------------ Changes (by rtoy): * resolution: => fixed * status: new => closed Comment: This particular issue is fixed and should work as expected in the 2008-09 snapshot. The workaround was to change %ldb to optimize the expression at compile-time as much as possible. Closing this particular issue. -- Ticket URL: cmucl cmucl