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

Reply via email to