Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cpcitor opened this issue May 16, 2018 · 2 comments
Open

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

cpcitor opened this issue May 16, 2018 · 2 comments

Comments

@cpcitor
Copy link
Owner

cpcitor commented May 16, 2018

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.

@lronaldo
Copy link
Contributor

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants