in Education by
Is there any efficient and portable way to check when multiplication operations with int64_t or uint64_t operands overflow in C? For instance, for addition of uint64_t I can do: if (UINT64_MAX - a < b) overflow_detected(); else sum = a + b; But I can not get to a similar simple expression for multiplication. All that occurs to me is breaking the operands into high and low uint32_t parts and performing the multiplication of those parts while checking for overflow, something really ugly and probably inefficient too. Update 1: Some benchmark code implementing several approaches added Update 2: Jens Gustedt method added benchmarking program: #include #include #include #define N 100000000 int d = 2; #define POW_2_64 ((double)(1 << 31) * (double)(1 << 31) * 4) #define calc_b (a + c) // #define calc_b (a + d) int main(int argc, char *argv[]) { uint64_t a; uint64_t c = 0; int o = 0; int opt; if (argc != 2) exit(1); opt = atoi(argv[1]); switch (opt) { case 1: /* faked check, just for timing */ for (a = 0; a < N; a++) { uint64_t b = a + c; if (c > a) o++; c += b * a; } break; case 2: /* using division */ for (a = 0; a < N; a++) { uint64_t b = a + c; if (b && (a > UINT64_MAX / b)) o++; c += b * a; } break; case 3: /* using floating point, unreliable */ for (a = 0; a < N; a++) { uint64_t b = a + c; if ((double)UINT64_MAX < (double)a * (double)b) o++; c += b * a; } break; case 4: /* using floating point and division for difficult cases */ for (a = 0; a < N; a++) { uint64_t b = a + c; double m = (double)a * (double)b; if ( ((double)(~(uint64_t)(0xffffffff)) < m ) && ( (POW_2_64 < m) || ( b && (a > UINT64_MAX / b) ) ) ) o++; c += b * a; } break; case 5: /* Jens Gustedt method */ for (a = 0; a < N; a++) { uint64_t b = a + c; uint64_t a1, b1; if (a > b) { a1 = a; b1 = b; } else { a1 = b; b1 = a; } if (b1 > 0xffffffff) o++; else { uint64_t a1l = (a1 & 0xffffffff) * b1; uint64_t a1h = (a1 >> 32) * b1 + (a1l >> 32); if (a1h >> 32) o++; } c += b1 * a1; } break; default: exit(2); } printf("c: %lu, o: %u\n", c, o); } So far, case 4 that uses floating point to filter most cases is the fastest when it is assumed that overflows are very unusual, at least on my computer where it is only two times slower than the do-nothing case. Case 5, is 30% slower than 4, but it always performs the same, there isn't any special case numbers that require slower processing as happens with 4. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Actually, the same principle can be used for multiplication: uint64_t a; uint64_t b; ... if (b != 0 && a > UINT64_MAX / b) { // if you multiply by b, you get: a * b > UINT64_MAX < error > } uint64_t c = a * b; For signed integers similar can be done, you'd probably need a case for each combination of signs.

Related questions

0 votes
    Is there any efficient and portable way to check when multiplication operations with int64_t or uint64_t operands ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    How many primary ways are there for detecting buffer-overflow? (a) 6 (b) 3 (c) 2 (d) 5 I have been ... bank, Cyber Security questions and answers pdf, mcq on Cyber Security pdf,...
asked Nov 4, 2021 in Education by JackTerrance
0 votes
    write a C program to Read a number.Find sum of digit at even position in a number and multiplication of digit ... position in a number. Select the correct answer from above options...
asked Dec 13, 2021 in Education by JackTerrance
0 votes
    Why apps developed in languages like C, C++ is prone to Buffer-overflow? (a) No string boundary checks in ... Security questions and answers pdf, mcq on Cyber Security pdf,...
asked Nov 4, 2021 in Education by JackTerrance
0 votes
    I am trying to generate 10,000 unique random integers in the range of 1 to 20,000 to store in a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I am trying to generate 10,000 unique random integers in the range of 1 to 20,000 to store in a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 10, 2022 in Education by JackTerrance
0 votes
    I would like to find out safe ways of implementing three dimensional arrays of integers in C++, using ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 31, 2022 in Education by JackTerrance
0 votes
    I would like to find out safe ways of implementing three dimensional arrays of integers in C++, using ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 30, 2022 in Education by JackTerrance
0 votes
    I would like to find out safe ways of implementing three dimensional arrays of integers in C++, using ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 29, 2022 in Education by JackTerrance
0 votes
    The transaction can no longer continue with its normal execution because of some internal condition, such ... topic in chapter Recovery System of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    I have to write in MIPS a program that multiplies two number using the add and shift method. After ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 17, 2022 in Education by JackTerrance
0 votes
    I want to multiply these two matrices using pure python. Input (M1 is 3*3 and Mt is a 3*2) M1 = ... But technically this should work. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Perform Binary Multiplication: 101010*1011 * O. 111001100 O 101001101 O 111001110 Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    Perform Binary Multiplication: 101010*1011 * 111001100 0 101001101 O 111001110 Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    Perform Binary Multiplication: 101010*1011 * 111001100 O 101001101 O 111001110 Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
...