title: detecting multiplication of uint64_t integers overflow with C
body: <p>Is there any efficient and portable way to check when multiplication operations with int64_t or uint64_t operands overflow in C?</p>
  
  <p>For instance, for addition of uint64_t I can do:</p>
  
  <pre><code>if (UINT64_MAX - a &lt; b) overflow_detected();
  else sum = a + b;
  </code></pre>
  
  <p>But I can not get to a similar simple expression for multiplication.</p>
  
  <p>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.</p>
  
  <p><strong>Update 1</strong>: Some benchmark code implementing several approaches added</p>
  
  <p><strong>Update 2</strong>: Jens Gustedt method added</p>
  
  <p>benchmarking program:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;stdlib.h&gt;
  
  #define N 100000000
  
  int d = 2;
  
  #define POW_2_64 ((double)(1 &lt;&lt; 31) * (double)(1 &lt;&lt; 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 &lt; N; a++) {
              uint64_t b = a + c;
              if (c &gt; a) o++;
              c += b * a;
          }
          break;
  
      case 2: /* using division */
          for (a = 0; a &lt; N; a++) {
              uint64_t b = a + c;
              if (b &amp;&amp; (a &gt; UINT64_MAX / b)) o++;
              c += b * a;
          }
          break;
  
      case 3: /* using floating point, unreliable */
          for (a = 0; a &lt; N; a++) {
              uint64_t b = a + c;
              if ((double)UINT64_MAX &lt; (double)a * (double)b) o++;
              c += b * a;
          }
          break;
  
      case 4: /* using floating point and division for difficult cases */
          for (a = 0; a &lt; N; a++) {
              uint64_t b = a + c;
              double m = (double)a * (double)b;
              if ( ((double)(~(uint64_t)(0xffffffff)) &lt; m ) &amp;&amp;
                   ( (POW_2_64 &lt; m) ||
                     ( b &amp;&amp;
                       (a &gt; UINT64_MAX / b) ) ) ) o++;
              c += b * a;
          }
          break;
  
      case 5: /* Jens Gustedt method */
          for (a = 0; a &lt; N; a++) {
              uint64_t b = a + c;
              uint64_t a1, b1;
              if (a &gt; b) { a1 = a; b1 = b; }
              else       { a1 = b; b1 = a; }
              if (b1 &gt; 0xffffffff) o++;
              else {
                  uint64_t a1l = (a1 &amp; 0xffffffff) * b1;
                  uint64_t a1h = (a1 &gt;&gt; 32) * b1 + (a1l &gt;&gt; 32);
                  if (a1h &gt;&gt; 32) o++;
              }
              c += b1 * a1;
          }
          break;
  
      default:
          exit(2);
      }
      printf("c: %lu, o: %u\n", c, o);
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>