Cost of a typecast

K

kid joe

Hi all,

If I have a formula with lots of typecasts am I right in thinking that
these are usually not expensive operations?

For a simple example, if i is an int and I cast it to unsigned int in an
expression, AIUI the compiler doesnt need to actually convert i but it
will simply substitute assembly instructions relevant to unsinged instead
of singed (i.e. mul instead of imul, ja instead of jg etc)

What about structs, if a struct S2 is a struct S1 with some extra fields
and I downcast a struct S2 to a struct S1 will this also be a "free"
operation?

Cheers,
Joe


--

...................... o _______________ _,
` Good Evening! , /\_ _| | .-'_|
`................, _\__`[_______________| _| (_|
] [ \, ][ ][ (_|
 
G

Gene

Hi all,

If I have a formula with lots of typecasts am I right in thinking that
these are usually not expensive operations?

For a simple example, if i is an int and I cast it to unsigned int in an
expression, AIUI the compiler doesnt need to actually convert i but it
will simply substitute assembly instructions relevant to unsinged instead
of singed (i.e. mul instead of imul, ja instead of jg etc)

What about structs, if a struct S2 is a struct S1 with some extra fields
and I downcast a struct S2 to a struct S1 will this also be a "free"
operation?

Generally, you're right, but the answer is architecture and
implementation-specific. Pointer casts most likely have no cost
(though in the old 8086 segmentation scheme, e.g., they could). Same
for unsigned to signed integers of the same number of bits. However
float to double casts and back have a cost. So do casts from a
smaller integer type to a larger: the upper bits must be cleared or
sign-extended. I don't think you can cast structs, rather only
pointers to structs.
 
K

Keith Thompson

kid joe said:
If I have a formula with lots of typecasts am I right in thinking that
these are usually not expensive operations?

If you're using a lot of casts (that's the correct term, not
"typecasts"), you're probably doing something wrong. Most of the
conversions you need to do in C will be done implicitly. And
presumably an implicit conversion will have exactly the same cost as
the corresponding explicit conversion -- though the C standard says
nothing about the const of any operations.

If you can show us some sample code, we can probably help you simplify
it.
For a simple example, if i is an int and I cast it to unsigned int in an
expression, AIUI the compiler doesnt need to actually convert i but it
will simply substitute assembly instructions relevant to unsinged instead
of singed (i.e. mul instead of imul, ja instead of jg etc)

Maybe.

I think that on most implementations, conversions between integer
types of the same size and conversions involving pointers just
reinterpret the representation. Conversions involving floating-point
types typically have to do some actual work.

But signed/unsigned conversions on a system that doesn't use
2's-complement will also have to do some actual work, conversions
between integer types of different sizes might have to perform
truncation, sign-extension, or zero-extension, and pointer conversions
might have to do some work on exotic systems.
What about structs, if a struct S2 is a struct S1 with some extra fields
and I downcast a struct S2 to a struct S1 will this also be a "free"
operation?

You can't convert between one struct type and another.
Cheers,
Joe


--

...................... o _______________ _,
` Good Evening! , /\_ _| | .-'_|
`................, _\__`[_______________| _| (_|
] [ \, ][ ][ (_|

You have a blank line at the top of your .signature, and one or two
additional blnak lines at the bottom. Might I suggest deleting them?
 
B

Ben Bacarisse

Gene said:
Generally, you're right, but the answer is architecture and
implementation-specific. Pointer casts most likely have no cost
(though in the old 8086 segmentation scheme, e.g., they could).

On word-addressed machines (now getting to be more and more of a
historical anomaly) some pointer casts involve a shift or a mask.
Still unlikely to be expensive, though.
 
S

squeamz

kid joe said:
       ......................  o    _______________           _,
      ` Good Evening!  ,      /\_  _|             |        .-'_|
      `................,     _\__`[_______________|       _| (_|
                             ] [ \, ][         ][        (_|

You have a blank line at the top of your .signature, and one or two
additional blnak lines at the bottom.  Might I suggest deleting them?

Why does it matter?
 
K

Keith Thompson

kid joe said:
       ......................  o    _______________           _,
      ` Good Evening!  ,      /\_  _|             |        .-'_|
      `................,     _\__`[_______________|       _| (_|
                             ] [ \, ][         ][        (_|

You have a blank line at the top of your .signature, and one or two
additional blnak lines at the bottom.  Might I suggest deleting them?

Why does it matter?

Why do you need the blank lines?

We had a long discussion about your signature being too long. The
blank links just add to the length. It's not a huge deal, but it
would be polite to remove them.
 
I

Ian Collins

Richard said:
It would, but I can't help thinking it's a bit OTT to complain about
them. We're talking two, maybe three newline characters here. Given
the shortness of his fourth line, he's actually using fewer bytes
than 4 * normal_recommended_max_line_length. Given that the whole
point of the original netiquette convention was to play nice with
those on slow, expensive connections, he's actually placing nicer
than some regs.

Never mind the blank lines, he still gets the time of day wrong!
 
K

Kenny McCormack

kid joe said:
       ......................  o    _______________           _,
      ` Good Evening!  ,      /\_  _|             |        .-'_|
      `................,     _\__`[_______________|       _| (_|
                             ] [ \, ][         ][        (_|

You have a blank line at the top of your .signature, and one or two
additional blnak lines at the bottom.  Might I suggest deleting them?

Why does it matter?

It doesn't. Unless you are suffering from Aspergers.
 
R

Richard Tobin

If I have a formula with lots of typecasts am I right in thinking that
these are usually not expensive operations?

For a simple example, if i is an int and I cast it to unsigned int in an
expression, AIUI the compiler doesnt need to actually convert i but it
will simply substitute assembly instructions relevant to unsinged instead
of singed (i.e. mul instead of imul, ja instead of jg etc)

Yes, on normal architectures at least.

(I like the idea of "singed integers".)
What about structs, if a struct S2 is a struct S1 with some extra fields
and I downcast a struct S2 to a struct S1 will this also be a "free"
operation?

Assuming you mean *pointers* to structs, again, yes.

Some conversions are obviously not free, such as int<->floating, and
some may or may not be, such as between different sizes of int.

-- Richard
 
C

CBFalconer

Richard said:
(e-mail address removed) said:
Keith Thompson said:
--

...................... o _______________ _,
` Good Evening! , /\_ _| | .-'_|
`................, _\__`[_______________| _| (_|
] [ \, ][ ][ (_|

You have a blank line at the top of your .signature, and one or
two additional blnak lines at the bottom. Might I suggest
deleting them?

Why does it matter?

It doesn't, particularly. Somewhere or other, there's a guideline
that sig blocks should be limited to four lines, out of
consideration for low-bandwidth users. Two or three extra newline
characters in otherwise blank lines is not going to hurt anyone.

It's just one more petty annoyance, normally ignored. Much less
serious than the nuisance of posts by Han, McCormack, Richard the
nameless, and some others.
 
A

Amandil

I thought it was a weather forecast.
No, actually. It's an expression. Like on someone's face. Talking
about it is just barely topical, as it's something you can C.

Sorry about the pun. I know it isn't very good, I just couldn't
resist.

-- Marty Amandil
 
K

Keith Thompson

Malcolm McLean said:
Generally they are nops or single machine instructions. However there are
occasionally some horrors. Back in the 1990s we found a 20-instruction
conversion from float to char in one compiler.
Depends how you do it. If you convert an S1 * to an S2 * before passing it
to a function, this will be free.

Yes, because pointers to different struct types are required to have
the same representation.
This is the most common situation.
If you convert an S1 to an S2, then youa re creating a copy, which will
involve several copyin operations. But this is also true if you assign an S1
to another S1.

How exactly would you convert an S1 to an S2? There is no conversion
between struct types in C.
 
G

Guest

No, actually. It's an expression. Like on someone's face. Talking
about it is just barely topical, as it's something you can C.

do you mean "something you can't do in C"? Languages with
inheritance (like C++) can do downcasts.
 
A

Amandil

do you mean "something you can't do in C"? Languages with
inheritance (like C++) can do downcasts.

Puns that have to be explained lose their humor. You can C (see) the
expression on someone's face (with your eyes). If that person is very
glum, his expression may be called "downcast". (Topical also means a
medicine or cream applied to the skin, such as on one's face. An extra
pun, though I'll admit that...
-- Marty Amandil (Gandalf fought with a balrog, and died. But he's all
White now!)
 
J

JosephKK

On word-addressed machines (now getting to be more and more of a
historical anomaly) some pointer casts involve a shift or a mask.
Still unlikely to be expensive, though.

Maybe you could take another look at the alignment issues of X86_64 in
64 bit mode. It looks rather word addressed again. Not to mention
memory bus and I/O bus widths of 64 bits, and 128 bits and 256 bits
for memory.
 
B

Ben Bacarisse

JosephKK said:
Maybe you could take another look at the alignment issues of X86_64 in
64 bit mode. It looks rather word addressed again. Not to mention
memory bus and I/O bus widths of 64 bits, and 128 bits and 256 bits
for memory.

That's not really word addressed (unless I've missed your point). No
matter how slow the resulting pointer, the cast is still zero cost.
 

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,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top