cpcitor / cpc-dev-tool-chain

A development toolchain to compile your modern C or assembly projects on a modern OS and run on an Amstrad CPC or emulator.
45 stars 5 forks source link

Add "division implemented by constant multiplication" hack. #14

Open cpcitor opened 6 years ago

cpcitor commented 6 years ago

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:

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.

lronaldo commented 6 years ago

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/

stevenhoving commented 6 years ago

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.