Re: code optimization in embedded systems

Grant Edwards wrote:
On 2007-11-16, Wilco Dijkstra <Wilco_dot_Dijkstra@xxxxxxxxxxxx> wrote:
The code generated for i/4 goes like this (for 32-bit int):

res = (i + ((unsigned)(i >> 31) >> 30)) >> 2;

ASR cannot be used for division by powers of 2 as it rounds
towards minus infinity rather than zero, so an explicit round
step done to round the result up if it is negative.

Ah. I forgot about the rounding towards zero requirement --
that's fairly new isn't it?

Such a requirement is in the C99 standard, but was not in C90. The more
important problem is that 'z=4; i/z' should yield the same value as
'i/4', and if the processor's divide instruction rounds towards zero,
the ASR replacement code must emulate that.

IIRC, Python and Pascal do it the other way (with a "mod"
operation to match).

Not that I know of. At least, Turbo Pascal uses the same 'div'
instruction as C compilers of the same era.

Stefan

.

Relevant Pages

• Re: Functions
... > Rounds a number (away from zero) to the nearest even integer. ... > Excel and Word Function Lists ...
(microsoft.public.excel.misc)
• Re: Help with Course Work
... >Rounds a number (away from zero) to the nearest multiple ... >Rounds a number to a specified number of digits to the ...
(microsoft.public.excel.misc)
• Re: Help with Course Work
... Rounds a number to the nearest multiple of ... Rounds a number (away from zero) to the nearest even integer. ... Rounds a number down towards 0 to a specified number of digits to the ...
(microsoft.public.excel.misc)
• Re: Functions
... Rounds a number to the nearest multiple of ... Rounds a number (away from zero) to the nearest even integer. ... Excel and Word Function Lists ...
(microsoft.public.excel.misc)
• Report Design
... Rounds a number to a specified number of digits. ... Num_digits is the number of digits you want to round to. ... Rounds a number down, toward 0 (zero). ... or omitted rounds to the nearest integer. ...
(microsoft.public.access.reports)