[Top][All Lists]

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

Re: [bug-gnulib] Re: [bug-grep] length of dec. representation of a numbe

From: Stepan Kasal
Subject: Re: [bug-gnulib] Re: [bug-grep] length of dec. representation of a number
Date: Wed, 9 Mar 2005 12:42:47 +0100
User-agent: Mutt/1.4.1i

Hello Jim,
   thank you for the code.

> > and 2's-complement/1's-complement/sign+magnitude representation.

So, if I understand correctly, your code works for 2's-somplement and
1's complement, and you believe that sign+magnitude is not used in practice.
(I cannot see how TYPE_MINIMUM could work with sign+magnitude.)

But that means that signed TYPE_MINIMUM is always 100..00 in binary, so we
can use:
#define TYPE_MAXIMUM(t) ((t) (~ TYPE_MINIMUM (t)))

It took me some time to decipher the macros, perhaps we could document
them more:

/* The extra casts work around common compiler bugs.  */
#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))

/* - The outer cast is needed to work around a bug in Cray C
     It is necessary at least when t == time_t.
   - We cannot use (1 << ...), because << could mean signed shift.  */
#define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
                              ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))

#define TYPE_MAXIMUM(t) ((t) (~ TYPE_MINIMUM (t)))

/* Please note that the above macros work generally for two's complement
   as well as for one's complement.  And as they don't use binary + nor
   binary -, it doesn't matter whether these are checked for overflow.  */

Do you see any problem with these?

Stepan Kasal

reply via email to

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