Generally C is well suited as target language for compilers.

I use C as target language for the Seed7 compiler. This

works good. There are just some things where the

behaviour of C is undefined:

The right shift operator (>>) of C may or may not sign

extend for signed negative numbers.

Since the right shift operation of Seed7 is defined as sign

extending shift, I have to check if the C compiler does

sign extend for signed negative numbers and to use the

following algorithm:

#ifdef RSHIFT_DOES_SIGN_EXTEND

(a >b)

#else

(a < 0 ? ~(~a >b) : a >b)

#endif

Does somebody know some algorithm which delivers more

performance when the C right shift does not sign extend than

(a < 0 ? ~(~a >b) : a >b)?

For the integer division I have a similar problem. The C

integer division may trunc towards 0 or towards minus infinite.

Seed7 has two integer divisions. One (the 'div' operator)

truncates towards 0 while the other (the 'mdiv' operator)

truncates towards minus infinite. This is explained with the

following example:

-4 div 3 = -1

-4 mdiv 3 = -2

Currently I assume that the C integer division rounds towards

zero and therefore I use the following algorithm for the

'a mdiv b' division (which truncates towards minus infinite):

if (a 0 && b < 0) {

return (a - 1) / b - 1;

} else if (a < 0 && b 0) {

return (a + 1) / b - 1;

} else {

return a / b;

} /* if */

Do you know some code which delivers more performance for

the 'a mdiv b' division (which truncates towards minus infinite)?

What is the fastest algorithm for the 'mdiv' division?

Before using this algorithm I used the following algorithm for

the 'a mdiv b' division:

long c=a / b;

if (((a 0 && b < 0) || (a < 0 && b 0)) && a % b != 0) {

return c - 1;

} else {

return c;

} /* if */

My benchmarks show that the first algorithm for 'mdiv' is

faster (it also uses only one division instead of two).

But maybe somebody can prove me wrong?

Greetings Thomas Mertes

Seed7 Homepage: http://seed7.sourceforge.net

Seed7 - The extensible programming language: User defined statements

and operators, abstract data types, templates without special

syntax, OO with interfaces and multiple dispatch, statically typed,

interpreted or compiled, portable, runs under linux/unix/windows.