[Oberon] A C++ version of the Lola-2 compiler and an Oberon to C++compiler

Chris Burrows chris at cfbsoftware.com
Thu Apr 18 13:49:46 CEST 2019


> -----Original Message-----
> From: Oberon [mailto:oberon-bounces at lists.inf.ethz.ch] On Behalf Of
> rochus.keller at bluewin.ch
> Sent: Wednesday, 17 April 2019 7:35 PM
> To: oberon at lists.inf.ethz.ch
> Subject: [Oberon] A C++ version of the Lola-2 compiler and an Oberon
> to C++compiler
> 
> Thank you all again for your support.
> 
> In case you're interested I updated the Oberon to C++ compiler so DIV
> and MOD are now mapped to the corresponding functions which can be
> implemented in the _Root class (in _Global.h). The following default
> implementation is provided:
>         static int MOD(int a, int b) { return ( a % b + b ) % b; }
>         static int DIV(int a, int b) { return ( a - MOD(a, b) ) / b;
> }
> 

While this may well work it is quite inefficient. Depending on the target
processor, the % and / operations can be significantly more expensive than +
and -. MOD uses two of these and DIV uses three. 

Here is an alternative version which only requires one % or / operation for
both DIV and MOD. It also makes it clear that it is only in cases where the
dividend is negative that C++ differs from Oberon.

static int DIV(int a, int b)
{
    if (a < 0)
        return (a - b + 1) / b;
    else
        return a / b;
}

static int MOD(int a, int b)
{
    if (a < 0)
        return (b - 1) + ((a - b + 1)) % b;
    else
        return a % b;
}

It might be possible to simplify the formula for the negative dividend case
of MOD but I can't immediately see it.

Regards,
Chris Burrows
CFB Software
http://www.astrobe.com/RISC5




More information about the Oberon mailing list