473,398 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Integer promotions and performance penalty

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

Oct 2 '07 #1
2 1653
Sune wrote:
>
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.

--
pete
Oct 2 '07 #2
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

Oct 2 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: dave | last post by:
I am reading input from a form. I want to validate the input by making sure that the string is actually an integer. How would I do this? Do i need to convert it to a character array and break down...
7
by: Michael Andersson | last post by:
Hi! Does the use of exception handling induce a performance penalty during the execution of non exception handling code? Regards, /Michael
4
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is...
2
by: Peter Bär | last post by:
A Question to the C#/.Net Gods of this forum: are there performance penalties when i compile (C#, FW1.1, ASP.NET, Studio2003) a central baseclass in a different assembly than all the derived...
4
by: spibou | last post by:
On 6.3.1.1 of N1124 we read: If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. A few lines above that...
6
by: sarathy | last post by:
Hi, What is integer promotion? How is it different from arithmetic conversion? Regards, Sarathy
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
7
by: Spoon | last post by:
Hello everyone, In my code, I use uint16_t (exactly 16-bit-wide unsigned integer type) and I need to print their value in base 10. ...
30
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Let's say we had a simple function for returning the amount of days in a month: unsigned DaysInMonth(unsigned const month) { switch (month) { case 8: case 3: case 5:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.