cpc-dev-tool-chain icon indicating copy to clipboard operation
cpc-dev-tool-chain copied to clipboard

Add "division implemented by constant multiplication" hack.

Open cpcitor opened this issue 6 years ago • 2 comments

Based on https://github.com/stevenhoving/integer_division .

For example, include https://github.com/stevenhoving/integer_division/blob/master/integer_division_uint8.h and check if it's indeed faster than SDCC's division.

I see 3 cases:

  • divide by a constant known at compile-time. This is covered by Steven Hoving's code.
  • divide by a variable. Perhaps lookup parameters in a table and thus we are back to the previous problem.

Case 1: divide by a constant known at compile-time

Perhaps SDCC is smart enough in turn to transform the resulting multiplication-by-a-constant into a series of shift and add.

The net result, for example, is that we can transform a division by 10 into a simple, constant-time, series of shifts and add. This is nice on a simple platform like the Z80.

Case 2: divide by a variable

Nothing more to add: perhaps lookup parameters in a table and thus we are back to the previous problem.

cpcitor avatar May 16 '18 16:05 cpcitor

I can indeed confirm that SDCC is smart enough to do optimizations when multiplying/dividing by constants. I haven't test all possible combinations, but those I have tested are well translated by SDCC. When dividing by variables, SDCC introduces it's own dividing routines and calles them. I think math routines are actually defined in the folder /src/device/lib/z80/

lronaldo avatar May 16 '18 21:05 lronaldo

Please be aware that this code is not written by me, it was written by Alan Bowens. The git repository only archives the blogpost and its source code.

stevenhoving avatar Jun 08 '18 10:06 stevenhoving