Open cpcitor opened 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/
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.
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.