bug-gnulib
[Top][All Lists]
Advanced

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

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


From: Stepan Kasal
Subject: [bug-gnulib] Re: [bug-grep] Re: length of dec. representation of a number
Date: Thu, 10 Mar 2005 10:09:54 +0100
User-agent: Mutt/1.4.1i

Hello Paul,
  thank you for the improved version.  Let me make a summary of comments
submitted by Paul Jarc, and add one idea of mine.
Text which is not modified is left with the "> " tags.

----------
/* The extra casts in the following macros are either needed because of
   integer promotion, or to work around compiler bugs, e.g., in
   Cray C 5.0.3.0.  */

> /* True if the arithmetic type T is an integer type.  bool counts as
>    an integer.  */
> #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1)
> 
> /* True if negative values of the signed integer type T use twos
>    complement, ones complement, or signed magnitude representation,
>    respectively.  Much GNU code assumes twos complement, but some
>    people like to be portable to all possible C hosts.  */
> #define TYPE_TWOS_COMPLEMENT(t) ((t) ~ (t) 0 == (t) -1)
> #define TYPE_ONES_COMPLEMENT(t) ((t) ~ (t) 0 == 0)
> #define TYPE_SIGNED_MAGNITUDE(t) ((t) ~ (t) 0 < (t) -1)

/* True if the arithmetic type T is signed.  */
#define TYPE_SIGNED(t) ((t) 1 > (t) -1)

> /* The maximum and minimum values for the integer type T.  These
>    macros have undefined behavior if T is signed and has padding bits.
>    If this is a problem for you, please let us know how to fix it for
>    your host.  */
> #define TYPE_MINIMUM(t) \
>   ((t) (! TYPE_SIGNED (t) \
>       ? (t) 0 \
>       : TYPE_SIGNED_MAGNITUDE (t) \
>       ? ~ (t) 0 \
>       : ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1)))

/* Note that we cannot use (1 << ...) on the last line, because << can
   act as sign-preserving shift.  (In theory, << is undefined as soon as
   the leftmost bit of the left operand is set, no matter whether the
   type is signed or not, but in practice this code works.)  */

> #define TYPE_MAXIMUM(t) \
>   ((t) (! TYPE_SIGNED (t) \
>       ? (t) -1 \
>       : ~ (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))))

For unsigned types on signed magnitude hosts, ((t) -1) is wrong.

#define TYPE_MAXIMUM(t) \
  ((t) (! TYPE_SIGNED (t) \
        ? ~ (t) 0 \
        : ~ (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))))

> /* Bound on length of the string representing an integer value or type T.
>    Subtract 1 for the sign bit if t is signed; log10 (2.0) < 146/485;
>    add 1 for integer division truncation; add 1 more for a minus sign
>    if needed.  */

#define INT_STRLEN_BOUND(t) \
  ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 146 / 485 + 1 \
        + TYPE_SIGNED (t))

> /* Bound on buffer size needed to represent an integer value or type T,
>    including the terminating null.  */
> #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)

Paul, will you accept these changes?

Stepan




reply via email to

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