This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Is it possible to catch overflow in long long multiply ?
- From: Victor STINNER <victor dot stinner at haypocalc dot com>
- To: gcc at gcc dot gnu dot org
- Date: Mon, 30 May 2005 08:57:31 +0200
- Subject: Is it possible to catch overflow in long long multiply ?
- Reply-to: victor dot stinner at haypocalc dot com
Hi,
I'm using gcc "long long" type for my calculator. I have to check
integer overflow. I'm using sign compare to check overflow, but it
doesn't work for 10^16 * 10^4 :
10000000000000000 * 10000
Here you have my code to check overflow :
long long produit = a * b; // a,b: long long
bool ok;
if (0 < a) {
if (0 < b)
ok = (produit > 0); // (+)*(+) -> (+) ?
else if (b == 0)
ok = true;
else
ok = (produit < 0); // (+)*(-) -> (-) ?
} else if (a == 0) {
ok = true;
} else {
if (0 < b)
ok = (produit < 0); // (-)*(+) -> (-) ?
else if (b == 0)
ok = true;
else
ok = (produit > 0); // (-)*(-) -> (+) ?
}
The problem is that "10000000000000000 * 10000" give
7766279631452241920. It's not the "expected result".
I know that GMP is the best solution, but I don't want to use it, I
prefer to just use long long.
I search in gcc code, but I didn't found the __multi3 function. I just
found #define __muldi3 __multi3 in gcc/libgcc2.h.
Any idea ?
Bye, Haypo