[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Saturated operations

jacob navia

4/16/2011 9:12:00 PM

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
6 Answers

ram

4/16/2011 9:44:00 PM

0

jacob navia <jacob@spamsink.net> writes:
>c = ((a+b) > 65535) ? 65535 : a+b; // What name should it have?

»Underspecified«?

jt

4/16/2011 11:01:00 PM

0

jacob navia <jacob@spamsink.net> wrote:
> 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
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://t...

pacman

4/16/2011 11:23:00 PM

0

In article <iod0nj$a5i$1@speranza.aioe.org>,
jacob navia <jacob@jspamsink.org> wrote:
>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.

--
Alan Curry

jacob navia

4/17/2011 6:53:00 AM

0

Le 17/04/11 01:23, Alan Curry a écrit :
> In article<iod0nj$a5i$1@speranza.aioe.org>,
> jacob navia<jacob@jspamsink.org> wrote:
>> 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.
>

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

jacob navia

4/17/2011 6:55:00 AM

0

Le 17/04/11 01:00, Jens Thoms Toerring a écrit :
> jacob navia<jacob@spamsink.net> wrote:
>> 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

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

jacob navia

4/17/2011 8:34:00 AM

0

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... :-)