From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Mark H Weaver Newsgroups: gmane.lisp.guile.devel Subject: Re: [PATCH] round-ash, a rounding arithmetic shift operator Date: Tue, 22 Feb 2011 12:54:46 -0500 Message-ID: <8739ngx8d5.fsf@netris.org> References: <87vd0l631d.fsf@netris.org> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1298397382 17555 80.91.229.12 (22 Feb 2011 17:56:22 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 22 Feb 2011 17:56:22 +0000 (UTC) Cc: guile-devel@gnu.org To: Andy Wingo Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Tue Feb 22 18:56:18 2011 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1PrwSp-000576-RX for guile-devel@m.gmane.org; Tue, 22 Feb 2011 18:56:17 +0100 Original-Received: from localhost ([127.0.0.1]:49190 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PrwSj-0008QA-1j for guile-devel@m.gmane.org; Tue, 22 Feb 2011 12:55:49 -0500 Original-Received: from [140.186.70.92] (port=42906 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PrwSC-00084x-Np for guile-devel@gnu.org; Tue, 22 Feb 2011 12:55:43 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PrwS2-0000xo-Dw for guile-devel@gnu.org; Tue, 22 Feb 2011 12:55:13 -0500 Original-Received: from world.peace.net ([216.204.32.208]:49763) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PrwS2-0000ts-84 for guile-devel@gnu.org; Tue, 22 Feb 2011 12:55:06 -0500 Original-Received: from ip68-9-118-38.ri.ri.cox.net ([68.9.118.38] helo=freedomincluded) by world.peace.net with esmtpa (Exim 4.69) (envelope-from ) id 1PrwRk-0004vO-GZ; Tue, 22 Feb 2011 12:54:48 -0500 Original-Received: from mhw by freedomincluded with local (Exim 4.69) (envelope-from ) id 1PrwRi-0001MW-4Z; Tue, 22 Feb 2011 12:54:46 -0500 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 216.204.32.208 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:11683 Archived-At: Andy Wingo writes: > On Tue 15 Feb 2011 10:49, Mark H Weaver writes: >> The first patch is trivial, but there for the sake of correctness. > > Please apply, thanks. Ludo applied this before the 2.0.0 release. >> The second patch adds round-ash, a rounding arithmetic shift operator. >> >> (round-ash n count) ==> (round (* n (expt 2 count))) >> >> but it's implemented much more efficiently than that, and requires that >> both n and count are exact integers. It cannot be implemented very >> efficiently in scheme, and I needed it to normalize floating-point >> significands using the default IEEE rounding mode. I think it probably >> has wider utility. It would be great to have it in 2.0. Any chance? > > I'm not sure I understand the name. There is no need to call "round" on > the result of calling "ash". Can you think of another name? The name is inspired by Taylor Campbell's names for the division operators. There is no need to call "round" on the result of calling "quotient", and yet we have adopted the name "round-quotient" for a rounded quotient. For consistency, I chose the name "round-ash" for a rounded arithmetic shift. For backward compatibility, we keep the name "quotient" which is short for "truncate-quotient", and similarly "ash" is short for "floor-ash". In theory, there could be an ash variant for every quotient operator, where the divisor is constrained to be a power of 2. However, the only variants needed to implement the IEEE 754 rounding modes for the usual sign-magnitude representation (where the significand is always non-negative) are floor-ash, ceiling-ash, and round-ash: (define floor-ash ash) (define (ceiling-ash n count) (- (ash (- n) count))) (use-modules (srfi srfi-60)) (define (round-ash n count) (let ((r (ash n count))) (if (and (negative? count) (bit-set? (- -1 count) n) (or (odd? r) (< (first-set-bit n) (- -1 count)))) (1+ r) r))) Given that round-ash is frequently called when using the default IEEE 754 rounding mode, it seems worth having a more efficient version than the code above. Having said all this, let's hold off on this patch for now. I'm modifying my bigfloat module to do strictly correct rounding, and that may require something slightly more powerful than ash and round-ash. In particular, I it may be necessary to have variants that return the remainder as well as the quotient, or maybe just some partial information about the remainder. Thanks, Mark