466 questions
3
votes
1
answer
123
views
How to map a value from one range to another accurately without using floating point? [closed]
I am writing firmware for a microcontroller. (In this specific case it is a Microchip PIC, but I am looking for an answer that is equally applicable to any brand of microcontroller.)
Arduino has a map ...
4
votes
3
answers
187
views
Overflow arithmetic in C programming language
My platform is x86_64, and assume there are 3 variables whose types are all uint16_t:
uint16_t a, b, c;
And for the following two code snippets:
(1)
uint16_t tmp = b - a;
uint16_t result1 = c - tmp;
...
0
votes
1
answer
53
views
Postgres fast wrapping arithmetic operations without overflow on bigint
Is there any way to do wrapping operations (addition, multiplication, SUM) on bigint columns?
By this I mean the standard method of wrapping addition in other programming languages on integer types of ...
2
votes
1
answer
127
views
Does C++ have dynamically declared types based on integer width arithmetics?
The title may seem odd, but I'm not sure what else to call it. If you have suggestions please let me know and I'll edit.
Consider this
uint8_t lo = 0x11; // given
uint8_t hi = 0x22; // given
uint16_t ...
4
votes
1
answer
130
views
Arithmetic shift-right integers with half rounding toward zero
I am looking for an efficient algorithm that computes arithmetic shift-right of integers that rounds to nearest integer with halfway rounding toward zero behavior. An answer can be a proper ...
0
votes
1
answer
144
views
A single byte that is from 0 to 255 to be rescaled from 0 to 7
A single 1 byte could be from 0 to 255. I intend to rescale it to be from 0 to 7. I have done this:
bit8 := uint8(136)
// To re-scale 1 byte from 0 to 2^3-1 i.e. 0 to 7
bit3 := bit8 / 0xff * 0b0111
...
1
vote
1
answer
108
views
What is wrong with my derivation of this carry flag result for SUB?
To be clear, I am certain that the derivation below is incorrect, but I am hoping someone can point out where in my "proof" I am wrong.
I am trying to prove that the x86 instruction setb ...
5
votes
4
answers
339
views
What is the fastest way to add 16 small numbers
I have two arrays, a and b. Each contain 16 bytes and I would like to add each b[i] to their corresponding a[i]. The arrays do not overlap and also I know that the resulting sums always fit in a byte ...
0
votes
0
answers
46
views
MIPS not transferring values properly between registers (adding 28 for no reason)
I'm writing a program in assembly that simulates multiplication via iterative addition. I have the algorithm done, and for my first test case, I set my multiplicand to 50. Upon transferring this value,...
4
votes
1
answer
96
views
Efficient 63-bit long computation of (a*b)/c in Java
I would like to compute (a*b)/c in Java in the most efficient way where:
a, b and c are unsigned long values (guaranteed to fit in 63 bits, i.e. with zero sign bit)
The result is also guaranteed to ...
-1
votes
1
answer
175
views
Subtract unsigned ints into signed long result. Never negative?
Found a bug in my code and noticed the following, when subtracting 2 numbers like
unsigned int a, b (with b > a)
signed long result = a - b
The result is always positive, even though b is larger. I ...
-3
votes
4
answers
271
views
How to divide a nonnegative variable integer by a constant fixed-point rational ≥1 without overflow (at all, if possible)?
How to compute ⌊𝑥÷𝑐⌋ for a variable integer 𝑥 and a fixed-point rational number 𝑐≥1 precisely? The input 𝑥 ∈ [0, INT_MAX] is stored in a variable of type int, 𝑐 ∈ [1, INT_MAX] is given as a ...
1
vote
0
answers
134
views
GMP Library: unsigned/signed arithmetics
I am trying to do some computations on big numbers, using the GMP library.
I am trying to understand how could I indicate GMP to use either signed or unsigned arithmetics.
Let's say that I have two &...
1
vote
2
answers
154
views
Emulate Javascript's 32bit signed integer arithmetic to C (or Perl) - some discrepancies
I am trying to translate simple JS code to C and/or Perl but I found a discrepancy in behaviour when doing arithmetic operations (+ - * / << >>) on integers and the result overflows. I ...
7
votes
3
answers
2k
views
Safely check if signed multiplication would overflow in C++17
I'm looking for a convenient idiom to check if a multiplication of two signed integers (longs or long longs) would overflow since signed arithmetic overflow is undefined behavior in C and C++. I'm ...