- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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

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

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

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 ;-)

Link Copied

1 Reply

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page