1

How to divide two signed 64 bit numbers on a 32 bit architecture?

I'm not asking for any code, just an algorithm I can implement. Let's assume that I have available instructions for unsigned and signed division (div and idiv).

I'm open to dividing by subtracting, if it can be done that way. It needs to work for signed numbers.

(editor's note: the first version of this question used the term "double precision", hence the comments asking for clarification on floating point vs. BigInteger).

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
sale
  • 41
  • 6
  • Divide by subtracting? How long will that take to divide 9223372036854775807 by 3? I would use a shift and compare/subtract, similar to the way you do it for decimal on paper, but binary (adjust negative values before and after). – Weather Vane May 12 '16 at 09:39
  • 1
    But... do you mean two **floating point** double Precision or two 64 bits integers? The former is trivial. – Margaret Bloom May 12 '16 at 09:42
  • I meant integers. Sorry if it is trivial, but kinda new in assembly. – sale May 12 '16 at 09:50
  • Maybe this may help? http://stackoverflow.com/questions/2884172/algorithm-for-dividing-very-large-numbers – Ped7g May 12 '16 at 09:53
  • I think this might help. Thanks! – sale May 12 '16 at 09:58
  • Look at the https://gmplib.org/ source code to see how they do it, if you're curious. Note that x86's 32bit operand-size `div` instruction is a 64b / 32b => 32b operation, but it faults if the quotient doesn't fit in 32bit. (Another interesting question recently: http://stackoverflow.com/questions/37085162/overcoming-the-x86-idiv-de-exception) – Peter Cordes May 12 '16 at 12:22
  • I will look into it. Thanks! – sale May 12 '16 at 14:07
  • See Knuth Volume 2 section 4.3.1 Algorithm D. – brian beuning Aug 28 '16 at 17:57

0 Answers0