bug-gnulib
[Top][All Lists]
Advanced

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

Re: xsize and flexmember


From: Bruno Haible
Subject: Re: xsize and flexmember
Date: Sat, 02 May 2020 01:13:48 +0200
User-agent: KMail/5.1.3 (Linux/4.4.0-177-generic; KDE/5.18.0; x86_64; ; )

Paul Eggert wrote:
> > I'm open to this. What prefix would you propose instead of 'x'?
> 
> The usual English-language name for this sort of thing is "saturation
> arithmetic", but SATURATED_ADD is kind of long. LLVM uses "sat" for saturated
> operations, perhaps the prefix "SAT" will do.

Fine with me.

> I could probably find uses for a new intprops.h macro INT_ADD_SAT (a, b) macro
> that would behave like INT_ADD_WRAPV (a, b, r) except it would return a
> saturated result instead of storing a wraparound-on-overflow result into *R 
> and
> returning an overflow indicator. This would be more general than a size_t-only
> approach.

That would be nice: it would allow to write more complex expressions in a more
natural way.

> It would be a little tricky, though, since A and B might not be the
> same type and then how does one define saturation? would it be relative to the
> type of A+B?

For unsigned types, saturation would be defined as "undefined value through
overflow". The addition of an overflown 'unsigned short' and an in-bounds
'unsigned int' would be an overflown 'unsigned int' - since you don't know
how many overflown 'unsigned short' have been summed into the first argument.
For subtraction, I would not define anything - I see no use for subtracting
possibly overflown values.

Would you plan to support the concept for signed types as well?

Bruno




reply via email to

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