Integer promotions and performance penalty

S

Sune

Hi all,

there are several situations where integer promotion is performed in C
and I need to confirm my understanding of it. Let's contain the
discussion to workstation/server CPUs of 32/64 bits, I'm afraid 8/16
bits CPUs may add details I'm not interested in. Hopefully someone
here can help out:

- As long as I use function prototypes there will be no argument
integer promotions (unless they are variadic)
- All data types of smaller size than int, will be promoted to int
before any arithmetics can be performed.

Please confirm/shoot down the above statements.

Next part, will I pay a performance penalty for these integer
promotions? I've read somewhere, sometime ago that I would, but I'm
not able to find any convincing evidence that's the case.

Thanks in advance
/Olle
 
P

pete

Sune said:
Hi all,

there are several situations where integer promotion is performed in C
and I need to confirm my understanding of it. Let's contain the
discussion to workstation/server CPUs of 32/64 bits, I'm afraid 8/16
bits CPUs may add details I'm not interested in. Hopefully someone
here can help out:

- As long as I use function prototypes there will be no argument
integer promotions (unless they are variadic)
- All data types of smaller size than int, will be promoted to int
before any arithmetics can be performed.

Please confirm/shoot down the above statements.

They both seem correct to me.
Next part, will I pay a performance penalty for these integer
promotions?
Maybe.

I've read somewhere, sometime ago that I would, but I'm
not able to find any convincing evidence that's the case.

You could try writing two versions
of the same small simple program,
one with type char and the other with type int
and then take look at the disassembly.

And or you could write two versions
of a program with loops, and time them.
 
M

Martin Wells

Sune:
- All data types of smaller size than int, will be promoted to int
before any arithmetics can be performed.


if (INT_MAX >= USHRT_MAX)
{
unsigned short promotes to SIGNED int
}
else
{
unsigned short promotes to UNSIGNED int
}

Same goes for unsigned char with "INT_MAX >= UCHAR_MAX".

All the signed integer types smaller than int will promote to SIGNED
int.

Next part, will I pay a performance penalty for these integer
promotions? I've read somewhere, sometime ago that I would, but I'm
not able to find any convincing evidence that's the case.


As far as I know, the idea behind integer promotion is to reflect the
actual way computers work. For instance, take the following:

char unsigned a = 7;

char unsigned b = 9;

char unsigned c = a + b;

On a machine that has 32-Bit int's, it doesn't add bytes at all, but
rather adds 32-bit int's whose upper bits are all zero. Building
promotion into the language just made things more realistic.

I don't see how it could possibly make things less efficient.

Even if there's a system that has a CPU instruction for adding bytes,
it could use that instruction behind your back if it sees that there's
no il-effect of bypassing the promotion.

Martin
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top