Saturated operations

J

jacob navia

Hi

I am programming the "ValArray" for my containers library and I am
adding operations like "clamped add" or clamped subtract:

c = ((a+b) > 65535) ? 65535 : a+b; // What name should it have?

c = ((a-b) < 0) ? 0 : a-b; // Same question

What other operations would be nice to have besides those two?


Thanks in advance

jacob
 
J

Jens Thoms Toerring

jacob navia said:
I am programming the "ValArray" for my containers library and I am
adding operations like "clamped add" or clamped subtract:
c = ((a+b) > 65535) ? 65535 : a+b; // What name should it have?

It's not clear from this what it's about. 65535 obviously is
the upper limit for an unsigned 16-bit integer. On the other
hand, I can't see what the type of the result is. My guess is
that it's about avoiding overflow in unsigned additions. But
if that is meant to be 16-bit-clamping or if it is for a ma-
chine where an unsigned int has just 16 bits is unclear. That
difference could make a bit of a difference in what I would
consider to be a reasonable name.

If this is meant to clamp the result of an adition to the
range of an unsigned 16 bit value I guess it would make
sense to have that limit explicitely in the name, u16_add()
might do. And for the more general case (addition clamped
to the maximum value of an unsigned int on the machine) per-
haps u_add() would be suitable. For clamping to the maximum
(and also minium) of an unsigned long using lu_add() would
then might be the obvious choice.

Regards, Jens
 
A

Alan Curry

Hi

I am programming the "ValArray" for my containers library and I am
adding operations like "clamped add" or clamped subtract:

c = ((a+b) > 65535) ? 65535 : a+b; // What name should it have?

c = ((a-b) < 0) ? 0 : a-b; // Same question

"clamped" is not a bad name, but "saturated arithmetic" seems to be a little
more widespread (in MMX and Altivec documentation for example). The
operations would be called "add/subtract with saturation" or "saturated
addition/subtraction" in documentation, and in reducing those names to a C
identifier, you could use saturated_add() or sat_add() or sadd() depending on
how short you want it to be.
 
J

jacob navia

Le 17/04/11 01:23, Alan Curry a écrit :
"clamped" is not a bad name, but "saturated arithmetic" seems to be a little
more widespread (in MMX and Altivec documentation for example). The
operations would be called "add/subtract with saturation" or "saturated
addition/subtraction" in documentation, and in reducing those names to a C
identifier, you could use saturated_add() or sat_add() or sadd() depending on
how short you want it to be.

Saturated. OK.

Thanks for your input. After you wrote I have searched in Google
and there is even an article in Wikipedia. So that is the name
I was looking for

Thanks
 
J

jacob navia

Le 17/04/11 01:00, Jens Thoms Toerring a écrit :
It's not clear from this what it's about. 65535 obviously is
the upper limit for an unsigned 16-bit integer. On the other
hand, I can't see what the type of the result is. My guess is
that it's about avoiding overflow in unsigned additions. But
if that is meant to be 16-bit-clamping or if it is for a ma-
chine where an unsigned int has just 16 bits is unclear. That
difference could make a bit of a difference in what I would
consider to be a reasonable name.

If this is meant to clamp the result of an adition to the
range of an unsigned 16 bit value I guess it would make
sense to have that limit explicitely in the name, u16_add()
might do. And for the more general case (addition clamped
to the maximum value of an unsigned int on the machine) per-
haps u_add() would be suitable. For clamping to the maximum
(and also minium) of an unsigned long using lu_add() would
then might be the obvious choice.

Regards, Jens

Thanks for your input. Yes, I should have specified that
it is for shorts (16 bits)
 
J

jacob navia

Le 17/04/11 08:53, jacob navia a écrit :
Saturated. OK.

Thanks for your input. After you wrote I have searched in Google
and there is even an article in Wikipedia. So that is the name
I was looking for

Thanks

And I could have done that before writing that message.

I should stop working when my brain is out of order... :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top