Questions tagged [saturation-arithmetic]

Saturation arithmetic limits results of all operations to a fixed range between a minimum and maximum value; the results are "saturated" at the limits.

  • If a result is greater than the maximum, it is set ("clamped") to the maximum
  • If it is below the minimum, it is clamped to the minimum.

Properties like associativity and distributivity may fail in saturation arithmetic.

Examples (i.e. limits are -100..100)

  • 60 - 50 = 10 (no saturation)
  • 60 + 50 = 100 (saturated)
  • (60 + 50) - 50 = 50 (saturated)
  • 60 + (50 - 50) = 60 (no saturation)
  • 10 × 11 - 11 = 89 (saturated)
26 questions
89
votes
14 answers

How can I increment a variable without exceeding a maximum value?

I am working on a simple video game program for school and I have created a method where the player gets 15 health points if that method is called. I have to keep the health at a max of 100 and with my limited programming ability at this point I am…
user2053184
87
votes
11 answers

Saturating subtract/add for unsigned bytes

Imagine I have two unsigned bytes b and x. I need to calculate bsub as b - x and badd as b + x. However, I don't want underflow/overflow occur during these operations. For example (pseudo-code): b = 3; x = 5; bsub = b - x; // bsub must be 0, not…
ovk
  • 2,318
  • 1
  • 23
  • 30
51
votes
19 answers

How to do unsigned saturating addition in C?

What is the best (cleanest, most efficient) way to write saturating addition in C? The function or macro should add two unsigned inputs (need both 16- and 32-bit versions) and return all-bits-one (0xFFFF or 0xFFFFFFFF) if the sum overflows. Target…
12
votes
4 answers

Signed saturated add of 64-bit ints?

I'm looking for some C code for signed saturated 64-bit addition that compiles to efficient x86-64 code with the gcc optimizer. Portable code would be ideal, although an asm solution could be used if necessary. static const int64 kint64max =…
drwowe
  • 1,975
  • 1
  • 15
  • 17
7
votes
2 answers

Bitwise saturated addition in C (HW)

I'm working on an assignment and I can't figure out how to implement this. I have to make a function sadd(int x, int y) that returns the numbers added together unless it overflows (then just return the max possible int). I've been able to come up…
John C
  • 71
  • 1
  • 3
6
votes
4 answers

Multiply by 2 with signed saturation in 6 operations in C?

The problem for signed 2's complement 32-bit integers: satMul2 - multiplies by 2, saturating to Tmin or Tmax if overflow. Examples: satMul2(0x30000000) = 0x60000000            satMul2(0x40000000) = 0x7FFFFFFF (saturate to…
5
votes
2 answers

Cast type with range limit

Is there an elegant way to cast a bigger datatype to a smaller one without causing the result to overflow? E.g. casting 260 to uint8_t should result in 255 instead of 4. A possible solution would be: #include #include inline…
5
votes
1 answer

Is there a way to subtract packed unsigned doublewords, saturated, on x86, using MMX/SSE?

I've been looking at MMX/SSE and I am wondering. There are instructions for packed, saturated subtraction of unsigned bytes and words, but not doublewords. Is there a way of doing what I want, or if not, why is there none?
z0rberg's
  • 674
  • 5
  • 10
4
votes
1 answer

Saturating signed integer addition with only bitwise operators in C (HW)

For a homework assignment, I have to write a function in C that adds together two signed integers, but returns INT_MAX if there would be positive overflow and INT_MIN if there would be negative overflow. I have to follow very strict restrictions in…
Zach
  • 61
  • 1
  • 4
3
votes
2 answers

Add 32-bit words with saturation

Do you know any way to add with saturation 32-bit signed words using MMX/SSE assembler instructions? I can find 8/16 bits versions but no 32-bit ones.
LooPer
  • 1,459
  • 2
  • 15
  • 24
3
votes
3 answers

Add saturate 32-bit signed ints intrinsics?

Can someone recommend a fast way to add saturate 32-bit signed integers using Intel intrinsics (AVX, SSE4 ...) ? I looked at the intrinsics guide and found _mm256_adds_epi16 but this seems to only add 16-bit ints. I don't see anything similar for…
bitwise
  • 541
  • 6
  • 16
2
votes
2 answers

cuda SIMD instruction for per-byte multiplication with unsigned saturation

CUDA has a nice set of SIMD instructions for integers that allow efficient SIMD computations. Among those, there are some that compute addition and subtraction per byte or per half-word (like __vadd2 and __vadd4), however, I couldn't find a similar…
user2348209
  • 136
  • 11
2
votes
1 answer

What is the most efficient way to handle integer multiplication overflow with saturation with ARM Neon intrinsics?

I have the following multiplication between 2 16 bit vectors: int16x8_t dx; int16x8_t dy; int16x8_t dxdy = vmulq_s16(dx, dy); In case dx and dy are both large enough, the result will overflow. I would like to clamp the resulting multiplication…
Elad Maimoni
  • 3,703
  • 3
  • 20
  • 37
2
votes
1 answer

In CSS, is filter: saturate(100) any slower than saturate(2)?

For CSS filters, would there be any difference is the browser's speed to process saturate(2) as compared to any of the…
2
votes
1 answer

OpenGL handling float color saturation ("color overflow")?

I'm woking on a scientific visualisation using openGL where I try to basically create a "coloured fog" to illustrate a multidimensional field. I want the hue of the color to represent the direction of the field and the luminosity of the color to…
1
2