title: Efficient floating-point division with constant integer divisors
body: <p>A recent <a href="https://stackoverflow.com/questions/35506226/will-the-compiler-optimize-division-into-multiplication">question</a>, whether compilers are allowed to replace floating-point division with floating-point multiplication, inspired me to ask this question.</p>
  
  <p>Under the stringent requirement, that the results after code transformation shall be bit-wise identical to the actual division operation,
  it is trivial to see that for binary IEEE-754 arithmetic, this is possible for divisors that are a power of two. As long as the reciprocal
  of the divisor is representable, multiplying by the reciprocal of the divisor delivers results identical to the division. For example, multiplication by <code>0.5</code> can replace division by <code>2.0</code>.</p>
  
  <p>One then wonders for what other divisors such replacements work, assuming we allow any short instruction sequence that replaces division but runs significantly faster, while delivering bit-identical results. In particular allow fused multiply-add operations in addition to plain multiplication.
  In comments I pointed to the following relevant paper:</p>
  
  <p><em>Nicolas Brisebarre, Jean-Michel Muller, and Saurabh Kumar Raina. Accelerating correctly rounded floating-point division when the divisor is known in advance. IEEE Transactions on Computers, Vol. 53, No. 8, August 2004, pp. 1069-1072.</em></p>
  
  <p>The technique advocated by the authors of the paper precomputes the reciprocal of the divisor <em>y</em> as a normalized head-tail pair <em>z<sub>h</sub>:z<sub>l</sub></em> as follows: <em>z<sub>h</sub> = 1 / y, z<sub>l</sub> = fma (-y, z<sub>h</sub>, 1) / y</em>. Later, the division <em>q = x / y</em> is then computed as <em>q = fma (z<sub>h</sub>, x, z<sub>l</sub> * x)</em>. The paper derives various conditions that divisor <em>y</em> must satisfy for this algorithm to work. As one readily observes, this algorithm has problems with infinities and zero when the signs of head and tail differ. More importantly, it will fail to deliver correct results for dividends <em>x</em> that are very small in magnitude, because computation of the quotient tail, <em>z<sub>l</sub> * x</em>, suffers from underflow.</p>
  
  <p>The paper also makes a passing reference to an alternative FMA-based division algorithm, pioneered by Peter Markstein when he was at IBM. The relevant reference is:</p>
  
  <p><em>P. W. Markstein. Computation of elementary functions on the IBM RISC System/6000 processor. IBM Journal of Research &amp; Development, Vol. 34, No. 1, January 1990, pp. 111-119</em></p>
  
  <p>In Markstein's algorithm, one first computes a reciprocal <em>rc</em>, from which an initial quotient <em>q = x * rc</em> is formed. Then, the remainder of the division is computed accurately with an FMA as <em>r = fma (-y, q, x)</em>, and an improved, more accurate quotient is finally computed as <em>q = fma (r, rc, q)</em>.</p>
  
  <p>This algorithm also has issues for <em>x</em> that are zeroes or infinities (easily worked around with appropriate conditional execution), but exhaustive testing using IEEE-754 single-precision <code>float</code> data shows that it delivers the correct quotient across all possibe dividends <em>x</em> for many divisors <em>y</em>, among these many small integers. This C code implements it:</p>
  
  <pre><code>/* precompute reciprocal */
  rc = 1.0f / y;
  
  /* compute quotient q=x/y */
  q = x * rc;
  if ((x != 0) &amp;&amp; (!isinf(x))) {
      r = fmaf (-y, q, x);
      q = fmaf (r, rc, q);
  }
  </code></pre>
  
  <p>On most processor architectures, this should translate into a branchless sequence of instructions, using either predication, conditional moves, or select-type instructions. To give a concrete example: For division by <code>3.0f</code>, the <code>nvcc</code> compiler of CUDA 7.5 generates the following machine code for a Kepler-class GPU:</p>
  
  <pre><code>    LDG.E R5, [R2];                        // load x
      FSETP.NEU.AND P0, PT, |R5|, +INF , PT; // pred0 = fabsf(x) != INF
      FMUL32I R2, R5, 0.3333333432674408;    // q = x * (1.0f/3.0f)
      FSETP.NEU.AND P0, PT, R5, RZ, P0;      // pred0 = (x != 0.0f) &amp;&amp; (fabsf(x) != INF)
      FMA R5, R2, -3, R5;                    // r = fmaf (q, -3.0f, x);
      MOV R4, R2                             // q
  @P0 FFMA R4, R5, c[0x2][0x0], R2;          // if (pred0) q = fmaf (r, (1.0f/3.0f), q)
      ST.E [R6], R4;                         // store q
  </code></pre>
  
  <p>For my experiments, I wrote the tiny C test program shown below that steps through integer divisors in increasing order and for each of them exhaustively tests the above code sequence against the proper division. It prints a list of the divisors that passed this exhaustive test. Partial output looks as follows:</p>
  
  <pre><code>PASS: 1, 2, 3, 4, 5, 7, 8, 9, 11, 13, 15, 16, 17, 19, 21, 23, 25, 27, 29, 31, 32, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 64, 65, 67, 69,
  </code></pre>
  
  <p>To incorporate the replacement algorithm into a compiler as an optimization, a whitelist of divisors to which the above code transformation can safely be applied is impractical. The output of the program so far (at a rate of about one result per minute) suggests that the fast code works correctly across all possible encodings of <code>x</code> for those divisors <code>y</code> that are odd integers or are powers of two. Anecdotal evidence, not a proof, of course.</p>
  
  <p><strong>What set of mathematical conditions can determine a-priori whether the transformation of division into the above code sequence is safe?</strong> Answers can assume that all the floating-point operations are performed in the default rounding mode of "round to nearest or even".</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;math.h&gt;
  
  int main (void)
  {
      float r, q, x, y, rc;
      volatile union {
          float f;
          unsigned int i;
      } arg, res, ref;
      int err;
  
      y = 1.0f;
      printf ("PASS: ");
      while (1) {
          /* precompute reciprocal */
          rc = 1.0f / y;
  
          arg.i = 0x80000000;
          err = 0;
          do {
              /* do the division, fast */
              x = arg.f;
              q = x * rc;
              if ((x != 0) &amp;&amp; (!isinf(x))) {
                  r = fmaf (-y, q, x);
                  q = fmaf (r, rc, q);
              }
              res.f = q;
              /* compute the reference, slowly */
              ref.f = x / y;
  
              if (res.i != ref.i) {
                  err = 1;
                  break;
              }
              arg.i--;
          } while (arg.i != 0x80000000);
  
          if (!err) printf ("%g, ", y);
          y += 1.0f;
      }
      return EXIT_SUCCESS;
  }
  </code></pre>