guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] round-ash, a rounding arithmetic shift operator


From: Mark H Weaver
Subject: Re: [PATCH] round-ash, a rounding arithmetic shift operator
Date: Tue, 22 Feb 2011 12:54:46 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)

Andy Wingo <address@hidden> writes:

> On Tue 15 Feb 2011 10:49, Mark H Weaver <address@hidden> 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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]