Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsigned 128-bit division on 64-bit machine

I have a 128-bit number stored as 2 64-bit numbers ("Hi" and "Lo"). I need only to divide it by a 32-bit number. How could I do it, using the native 64-bit operations from CPU?

(Please, note that I DO NOT need an arbitrary precision library. Just need to know how to make this simple division using native operations. Thank you).

like image 395
rookie Avatar asked Dec 08 '09 22:12

rookie


1 Answers

If you are storing the value (128-bits) using the largest possible native representation your architecture can handle (64-bits) you will have problems handling the intermediate results of the division (as you already found :) ).

But you always can use a SMALLER representation. What about FOUR numbers of 32-bits? This way you could use the native 64-bits operations without overflow problems.

A simple implementation (in Delphi) can be found here.

like image 64
F.D.Castel Avatar answered Sep 27 '22 17:09

F.D.Castel