1 Feb 2007 fzort   » (Journeyer)

fejj: the standard way to do that is to replace the division with a multiplication by the reciprocal, multiplied by some power of 2. Then divide the result by that power of 2, which can be done with a right shift. In the case of division by 7, you'd end up with the increasingly more accurate expressions:

q = ((a<<3) + a) >> 6

q = ((a<<6) + (a<<3) + a) >> 9

q = ((a<<9) + (a<<6) + (a<<3) + a) >> 12

q = ((a<<12) + (a<<9) + (a<<6) + (a<<3) + a) >> 15


and so on (in binary, 1/7 is .001001001...). Optionally, the result can be fixed with something like

r = a - q*7; while (r >= 7) { q++; r -= 7; }

The multiplication by 7 can be replaced with shifts and adds, of course.

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!