3

From what I understand, the current method of doing integer division is calculating the inverse in hardware and then performing a multiplication.

I have some C# code where a ton of time is being spent in integer division, with values that are changed rarely enough that their values can be cached.

I want to perform the hardware algorithm in software, i.e. calculate the inverse of the divisors and turn all these divisions into multiplication. Does anyone know the algorithm to perform this transformation, or even better, is there something in the CLR that can perform this automatically?

Chuu
  • 4,301
  • 2
  • 28
  • 54
  • Can you not write this in a lower level language (say C) and interop with that instead? – Oded Jun 27 '12 at 20:33
  • You're saying that the CPU actually does floating-point math for an integer division?! (For example, x / 4 = x * 0.25). If that is so, how come integer arithmetic is generally assumed to be much faster than floating point arithmetic? – stakx - no longer contributing Jun 27 '12 at 20:35
  • @stakx Integer division in hardware is more complex than floating-point divison. I can't speak to its specific implementation however. – NominSim Jun 27 '12 at 20:38
  • @NominSim: This is clearly not true, given that floating-point division requires an integer division on the mantissas. – Oliver Charlesworth Jun 27 '12 at 20:39
  • @Chuu: But computer integer arithmetic is not on a finite field. – Oliver Charlesworth Jun 27 '12 at 20:42
  • @OliCharlesworth I should have said, requires more clock cycles, instead of "is more complex" – NominSim Jun 27 '12 at 20:42
  • @OliCharlesworth I delete the comment you are referred to because it's not completely correct (it was an assertion about the existence of inverses in finite fields), but integer arithmetic in hardware is a finite field thanks to overflow. – Chuu Jun 27 '12 at 20:44
  • @Chuu: No it's not. Not all values have [multiplicative inverses](http://en.wikipedia.org/wiki/Field_(mathematics)#Definition_and_illustration). – Oliver Charlesworth Jun 27 '12 at 20:48
  • @stakx: Integer division is so slow that some architectures (CRAY-1, Alpha) implement it by doing a floating point division. – Gabe Jun 27 '12 at 21:16
  • @Gabe: Really? As I said above, floating-point division necessarily involves integer division internally; why are these platform's standalone integer division implementations so slow? – Oliver Charlesworth Jun 27 '12 at 21:40
  • 1
    @OliCharlesworth: If you check out http://gmplib.org/~tege/divcnst-pldi94.pdf, you'll see that on page 2 that not only are there several architectures without integer divide hardware, but that cycle counts (for architectures that do have it) often *increase* in successive generations. – Gabe Jun 27 '12 at 22:04
  • @Gabe: That's interesting, thanks. It seems reasonable that cycle counts increase as processors get faster (need more pipelining to maintain throughput), but I assumed that the same would hold for the FPU. – Oliver Charlesworth Jun 27 '12 at 22:24

1 Answers1

5

There is an excellent blog post series on the topic of integer division by multiplication. It really contains everything that is needed to implement the transformation yourself.

usr
  • 168,620
  • 35
  • 240
  • 369
  • 5
    Link-only answers are generally frowned-upon at SO; could you give the gist of the procedure in your answer? – Oliver Charlesworth Jun 27 '12 at 20:50
  • 2
    I think this answer says everything there is to say to answer the question. The linked postings are of excellent quality. I haven't just copied the first Google hit. I took it from my browser history after remembering it. **Feel free to edit my post, though.** – usr Jun 27 '12 at 21:31
  • 3
    On problem with link only answers is that you have no control of the site. If it goes down in the future, this answer is useless to anyone who reads it. – Oliver Charlesworth Jun 27 '12 at 21:33