|Small values in large registers (char,short,...) email@example.com (Jakob Engblom) (1997-12-02)|
|Re: Small values in large registers (char,short,...) firstname.lastname@example.org (Henry Spencer) (1997-12-05)|
|Re: Small values in large registers (char,short,...) email@example.com (1997-12-07)|
|From:||Henry Spencer <firstname.lastname@example.org>|
|Date:||5 Dec 1997 00:56:23 -0500|
|Organization:||SP Systems, Toronto|
|Keywords:||C, code, architecture|
Jakob Engblom <email@example.com> wrote:
>I am presently investigating implementating a C compiler for a "pure"
>32-bit architecture. A problem which I have found is that when you
>store chars and shorts (8 and 16 bit values) in the 32-bit registers,
>you will have to look out for overflows in certain situations.
Bear in mind that there is no such thing, in C, as doing char or short
arithmetic. Values of those types are promoted to int when used in
expressions, so on a machine with 32-bit int, all arithmetic is 32
bits wide at least. The resulting values are narrowed only when they
are stored in char or short variables.
If you are trying to locate char and short variables in registers,
there is no problem with saying "the variable is the bottom 8 bits of
the register", and storing random trash in the upper 24 bits, but you
must make sure that the random trash does not become visible in
calculations. If c1 and c2 are 8-bit signed char variables, each
holding the value 125, the value of c1+c2 is 250, not -6 plus an
overflow (remember, the value is 32 bits wide).
The issue of doing narrow calculations arises only as an optimization,
when higher bits of the results are not visible at all... and it is
required to produce the same effects as doing calculations of the
normal width. So c1 = c1+c2 could be done using 8-bit arithmetic, but
only if its overflow behavior is the same as that of promoting the
values to 32 bits, doing 32-bit arithmetic, and assigning the result.
| Henry Spencer
Return to the
Search the comp.compilers archives again.