Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7956 Discussions

Technical question : what is the cpu time cost of type casting ?

VinceRev
Beginner
588 Views
Hello.

I have technical question about type casting.
Assume that we have three different implementations of the same code about type casting :

// Implementation 1 : no cast
unsigned long long int a
unsigned long long int b
unsigned long long int c

a=b*c

// Implementation 2 : explicit cast
unsigned long long int a
unsigned long long int b
unsigned short int c

a=b*((unsigned long long int)(c))

// Implentation 3 : implicit cast
unsigned long long int a
unsigned long long int b
unsigned short int c

a=b*c

My question is the following :
If we compute the operation billion times, will there be a difference in computing time ?

And if there is no difference in computing time, what is the best implementation (assuming that we want to reduce memory consumption) ?

Is the implementation 3 more "risky" than implementation 2 ? Will they produce the same assembly code ?

Thank you very much ;-)
0 Kudos
1 Reply
jimdempseyatthecove
Honored Contributor III
588 Views
The instruction set contains a MOVSX/MOVSXD for sign extensions and MOVZX (no MOVZXD)
MOVSX can move a word (short) to a quadword (long long) with sign extension.
To move unsigned short (word) to a quadword (unsigned long long) with zero extension, this will require 2 instructions a zero extended move of short to long (dword), followed by a zero extended move of dword to qword.

C/C++ will promote the unsigned short c (regardless of cast) prior to multiplication (which will be 64 bits x 64 bits). If you examine the dissassembly window you will see the two instructions.

If you know that c is always in the range of 0:32767 then you can use the signed format.

The performance issue will likely reside with how you get the values into a, b, c

Can you more detail as to what you will be doing?

Jim Dempsey
0 Kudos
Reply