On Tue, 4 Jun 2019, Tejas Joshi wrote:

> Hello.
> 
> > NaN, and you should make sure it behaves accordingly.  (If it should never
> > be called for them, a gcc_assert would be appropriate.)
> 
> I can't find any documentation about how and when to use gcc_assert.
> But I used it looking at the comment at its definition and locations
> it is used, is this appropriate? Or is it supposed to be used before
> calling the function? :
> 
> +bool
> +is_even (REAL_VALUE_TYPE *r)
> +{
> +  /* The function is not supposed to use for Inf and NaN. */
> +  gcc_assert (r->cl != rvc_inf);
> +  gcc_assert (r->cl != rvc_nan);

I'd suggest making the comment above the function be clear about what 
classes of arguments are or are not valid, and then you don't need a 
comment on the assertions.

Is REAL_EXP meaningful for rvc_zero?  If not, you should check for 
rvc_zero and handle it appropriately before doing anything checking 
REAL_EXP.

> > So n is the bit position, and w is the word position, of the bit with
> > value 1; n-1 is the position of the bit with value 0.5.
> > If n is a multiple of HOST_BITS_PER_LONG (that is, the bits with values
> > 0.5 and 1 are in different words), this will incorrectly return false when
> > the 0.5 bit is set.
> 
> I did not understand this. What is the bit with value 1?

I don't understand your question.  The "sig" array contains 
SIGNIFICAND_BITS bits.  The most significant one has value 2^(REAL_EXP-1) 
and thus the least significant one has value 
2^(REAL_EXP-SIGNIFICAND_BITS).  The ones we care about for the present 
purposes are the bit with value 1 (to tell whether an integer part is even 
or odd), the bit with value 0.5 and all the bits lower than that (to tell 
whether the fractional part is exactly 0.5 or not).

> But when n is a multiple of HOST_BITS_PER_LONG, the function was
> computing value of w wrong (e.g. for number 2^63 + 0.5). At such time,
> would the following improvisation be acceptable in is_halfway_below?

That still seems wrong.

For testing for a halfway value you don't care about the bit with value 1.  
You do care about the bit with value 0.5, and you do care about the lower 
bits.

So you should probably set n = SIGNIFICAND_BITS - REAL_EXP (r) - 1 (under 
a conditional with < not <=; if REAL_EXP (r) == SIGNIFICAND_BITS, the 
least significant bit has value 1 and the number must be an integer).  
That way, n is the bit position of the bit with value 0.5.  Then you can 
compute w from n without special casing to get the word position of the 
bit with value 0.5.  For the words below w you check they are entirely 0.  
For word w you need to check both that bit n is 1 and the lower bits are 
0.

-- 
Joseph S. Myers
jos...@codesourcery.com

Reply via email to