Register Size Question

I

Immortal Nephi

You use data type to determine the size of char, short, and long. If
you choose to use either char or short to do calculation, C++ Compiler
converts from char or short to either maximum 32 bit register size or
64 register size to do calculation before they are converted back to
original size.

For example:

char a = 1;
char b = 2;
char s = 0;

short aa = 5;
short bb = 10;
short ss = 0;

s = a + b;
ss = aa + bb;

The C++ Compiler copies either a or aa from memory into 32 bit
register. CPU performs sum calculation. The result of sum in
register is converted back to either 8 bit or 16 bits back to the
memory. The performance can be improved to use 32 bit register than
16 bit register.

What happen if C++ Compiler may assign 32 bits to int on x86 and 64
bits to int on I64 or AMD machine? My code does not care to use
either 32 bits or 64 bits when calculation can be done in 16 bits. My
code should use minimum 32 bit register for best performance.

Like this here.

if (sizeof(int) == 4 ) // 32 bits machine
typedef long int register_t;
else // 64 bits machine
typedef long long int register_t

Please correct me if I am wrong.
 
A

Alf P. Steinbach

* Immortal Nephi:
You use data type to determine the size of char, short, and long. If
you choose to use either char or short to do calculation, C++ Compiler
converts from char or short to either maximum 32 bit register size or
64 register size to do calculation before they are converted back to
original size.

For example:

char a = 1;
char b = 2;
char s = 0;

short aa = 5;
short bb = 10;
short ss = 0;

s = a + b;
ss = aa + bb;

The C++ Compiler copies either a or aa from memory into 32 bit
register. CPU performs sum calculation. The result of sum in
register is converted back to either 8 bit or 16 bits back to the
memory. The performance can be improved to use 32 bit register than
16 bit register.

What happen if C++ Compiler may assign 32 bits to int on x86 and 64
bits to int on I64 or AMD machine? My code does not care to use
either 32 bits or 64 bits when calculation can be done in 16 bits. My
code should use minimum 32 bit register for best performance.

Like this here.

if (sizeof(int) == 4 ) // 32 bits machine
typedef long int register_t;
else // 64 bits machine
typedef long long int register_t

Please correct me if I am wrong.

Well, what you write above implies that C++ has some "maximum" size for the
built-in types.

It doesn't, but a given version of a given compiler with given options on a
given system with a given OS must choose a size for e.g. 'int' (in practice
though, for the case of running under an OS this choice is forced by the OS'
API, so it's usually only the OS that matters).

Some compilers supply a header [stdint.h] that has typedefs like the one you
indicated above, but using special names (not redefining built-in names, which
is impossible using typedef). If the compiler doesn't provide [stdint.h] --
which is not yet part of the C++ language! -- you may be able to make do with
corresponding Boost header. For example, for MSVC 7.x.

But focusing on micro-optimization is seldom a good idea, so I recommend just
using 'int' and be done with it.

After all, if the code works fine on a 32-bit system, then how much can any
little excess memory usage matter on a 64-bit system?


Cheers & hth.,

- Alf
 
J

James Kanze

You use data type to determine the size of char, short, and
long.

I'm not sure I understand that statement, but none of the basic
types have a "standard" size. The size always varies with the
implementation, The only guarantees are with regards to minimum
sizes (8 bits for the character types, 16 for short and int, and
32 for long).
If you choose to use either char or short to do calculation,

You can't. You can choose char or short for variables in
memory, but as soon as any arithmetic operators are involved,
they are promoted to int.
C++ Compiler converts from char or short to either maximum 32
bit register size or 64 register size to do calculation before
they are converted back to original size.

Are you talking about the language, or the generated code.
According to the language, char or short are promoted to int for
the calculation. And there's no such thing as a register. As
for the generated code, the compiler will use the most effective
set of machine instructions available (hopefully) to achieve the
required results---what actually gets generated varies
enormously from one compiler to the next. (Note that if the
results are only being used as a smaller type, the compiler
might forego the promotion in the machine code. All that counts
is that the "observable behavior" of the program be respected,.)
For example:
char a = 1;
char b = 2;
char s = 0;
short aa = 5;
short bb = 10;
short ss = 0;
s = a + b;
ss = aa + bb;
The C++ Compiler copies either a or aa from memory into 32 bit
register.

Maybe. Supposing the machine has 32 bit registers, of course.
CPU performs sum calculation. The result of sum in
register is converted back to either 8 bit or 16 bits back to
the memory. The performance can be improved to use 32 bit
register than 16 bit register.

Can it? Perhaps on some machines. Certainly not on others.
(The machine I learned C++ on didn't have 32 bit registers.)
What happen if C++ Compiler may assign 32 bits to int on x86
and 64 bits to int on I64 or AMD machine? My code does not
care to use either 32 bits or 64 bits when calculation can be
done in 16 bits. My code should use minimum 32 bit register
for best performance.

That's the compiler's job. It will do whatever it thinks best,
on the platform in question,
Like this here.
if (sizeof(int) == 4 ) // 32 bits machine
typedef long int register_t;
else // 64 bits machine
typedef long long int register_t
Please correct me if I am wrong.

Given that I don't really understand what you're trying to say,
it's difficult, but what little I do understand seems very
wrong. Basically, a given program has a set of possible
observable behaviors. The compiler generates code that will
correspond to one of those observable behaviors. The rules
defining integral promotion are part of the language, and affect
(at least in theory) the observable behavior. So the compiler
has to take them into account.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top