saturation-arithmetic

C - Saturating Signed Integer Multiplication with Bitwise Operators

时光总嘲笑我的痴心妄想 提交于 2020-01-13 20:25:45
问题 Alright, so the assignment I have to do is to multiply a signed integer by 2 and return the value. If the value overflows then saturate it by returning Tmin or Tmax instead. The challenge is using only these logical operators (! ~ & ^ | + << >>) with no (if statements, loops, etc.) and only allowed a maximum of 20 logical operators. Now my thought process to tackle this problem was first to find the limits. So I divided Tmin/max by 2 to get the boundaries. Here's what I have: Positive This

Saturating subtract/add for unsigned bytes

一曲冷凌霜 提交于 2019-12-20 07:59:02
问题 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 254 and b = 250; x = 10; badd = b + x; // badd must be 255, not 4 The obvious way to do this includes branching: bsub = b - min(b, x); badd = b + min(255 - b, x); I just wonder if there are any better ways to do this, i.e. by some hacky bit

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

99封情书 提交于 2019-12-10 14:48:22
问题 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? 回答1: If you have SSE4.1 available, I don't think you can get better than using the pmaxud + psubd approach suggested by @harold. With AVX2, you can of course also use the corresponding 256bit variants. __m128i subs_epu32_sse4(__m128i a, __m128i b){ __m128i mx = _mm_max

Add saturate 32-bit signed ints intrinsics?

落爺英雄遲暮 提交于 2019-12-07 16:31:14
问题 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 32 bits. The other calls seem to wrap around. 回答1: A signed overflow will happen if (and only if): the signs of both inputs are the same, and the sign of the sum (when added with wrap-around) is different from the input Using C-Operators: overflow =

Bitwise saturated addition in C (HW)

我的未来我决定 提交于 2019-12-07 01:02:43
问题 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 with some solutions involving casting and conditional statements, but those aren't allowed in the solution. Only the operators ~ ! ^ + << >> & and | . 回答1: For addition of signed numbers, overflow has happened if you add two numbers of the same sign

Add saturate 32-bit signed ints intrinsics?

隐身守侯 提交于 2019-12-06 03:05:45
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 32 bits. The other calls seem to wrap around. A signed overflow will happen if (and only if): the signs of both inputs are the same, and the sign of the sum (when added with wrap-around) is different from the input Using C-Operators: overflow = ~(a^b) & (a^(a+b)) . Also, if an overflow happens, the saturated result will have the same sign as either

Signed saturated add of 64-bit ints?

吃可爱长大的小学妹 提交于 2019-12-04 17:50:51
问题 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 = 0x7fffffffffffffffll; static const int64 kint64min = 0x8000000000000000ll; int64 signed_saturated_add(int64 x, int64 y) { bool x_is_negative = (x & kint64min) != 0; bool y_is_negative = (y & kint64min) != 0; int64 sum = x+y; bool sum_is_negative = (sum &

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

房东的猫 提交于 2019-12-04 07:41:10
问题 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 doing something like this. public void getHealed(){ if(health <= 85) health += 15; else if(health == 86) health += 14; else if(health == 87) health += 13; }// this would continue so that I would never go over 100 I understand my syntax about isn't

How to do unsigned saturating addition in C?

左心房为你撑大大i 提交于 2019-11-26 19:45:57
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 is x86 and ARM using gcc (4.1.2) and Visual Studio (for simulation only, so a fallback implementation is OK there). MSalters You probably want portable C code here, which your compiler will turn into proper ARM assembly. ARM has conditional moves, and these can be conditional on overflow. The algorithm then becomes add, and conditionally set the

How to do unsigned saturating addition in C?

岁酱吖の 提交于 2019-11-26 07:26:25
问题 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 is x86 and ARM using gcc (4.1.2) and Visual Studio (for simulation only, so a fallback implementation is OK there). 回答1: You probably want portable C code here, which your compiler will turn into proper ARM assembly. ARM has conditional moves, and