DevSolar / pdclib

The Public Domain C Library
https://pdclib.rootdirectory.de
Creative Commons Zero v1.0 Universal
228 stars 41 forks source link

Optimise the count of division/remainder operations #17

Closed SiarheiVolkau closed 3 years ago

SiarheiVolkau commented 3 years ago

Some compilers can't do the proper optimization of the code, which this PR modifies. At least GCC and Clang produces div_mod operation twice - for value / status->base and digit = value % status->base. In order to help compiler generate better code we need keep div and mod code sections close to each other.

Poor output example: https://godbolt.org/z/cEMx41 Better output example: https://godbolt.org/z/o3fc13

Of course we don't need to do the optimization instead of the compilers but maybe they thinks that status->base can be modified in between div and mod sections. Please take it into account.

DevSolar commented 3 years ago

Commited in SVN @ 988.

The heavy-handed macro magic in _PDCLIB_print.c has been bugging me for a while. I am thinking about replacing the whole thing with something else, possibly making use of lldiv().