[vector] Questions on addSaturate and subSaturate

John Rose john.r.rose at oracle.com
Wed Mar 28 19:59:49 UTC 2018


On Mar 28, 2018, at 12:47 PM, Lupusoru, Razvan A <razvan.a.lupusoru at intel.com> wrote:
> 
> Hi everyone,
> 
> I am looking into implementation of addSaturate and subSaturate and I am trying to understand intended semantics.
> 
> From my expectation just from the method name on integral types, it seems that in case of overflow, it will max out at largest value. And in case of underflow, it will not wrap around past min value.

Yes.  And there is a subtle point that unsigned integral types underflow to zero
while signed types underflow to a value of the form 0x800…

> However, I have a couple of questions based on current implementation.
> 
> *         Should these methods be defined for FP types? If yes, what would be intended semantics for FP?

FP types naturally overflow and underflow to extremal (infinite) values.
Only integral types, which have wraparound on under/overflow, need a
special mode to replace that standard behavior by saturating.

> 
> *         Should these methods take care of both overflow and underflow? Currently it seems that they only take care of overflow in implementation.

They should probably handle both conditions by "pinning" to the extremal
value.

> *         For subword types, is the saturation the appropriate max/min values for that type? (Currently Integer.MAX_VALUE is used)

Byte.MIN_VALUE, etc.

> Any advice on intended semantics and whether these are valid uses cases for API would be valuable.

I think coders use saturating arithmetic when (a) they are measuring or counting
something, and (b) they don't want the cost of FP, and (c) they expect occasional
overflow and want predictable behavior when it happens.

I suppose edge detection might be an application:  You are measuring relations
between pixels, you are using limited precision (for speed) and you don't want
your algorithm to go haywire on overflows.  The wikipedia page on Sat. Arith.
mentions video decoders also.

— John


More information about the panama-dev mailing list