[fpc-devel] Optimization for 'mod'

Anton Shepelev anton.txt at gmail.com
Fri Jan 5 13:57:10 CET 2018

J. Gareth Moreton:

> For example, code such as this:
>    Minutes := t div 60;
>    Seconds := t mod 60;
> ...is badly optimised,

Or not at all.

> because  the division is calculated twice (without
> the patch, 't div 60' uses a reciprocal  constant,
> while 't mod 60' just uses 'DIV'), whereas, ideal-
> ly, it should only be calculated  once  since  the
> numerator and denominator are the same...

How  about  a  compound DivMod() procedure that will
return both quotient and remainder:

procedure DivMod (* or QuotRem ? *)
(  const dividend, divisor:   integer;
   var   quotient, remainder: integer
   quotient  := dividend div divisor;
   remainder := dividend - quotient*divisor;

Similar  optimisation  is   already   available   in

Please, do not forward replies to my e-mail.

More information about the fpc-devel mailing list