Cost of a typecast

Discussion in 'C Programming' started by kid joe, May 15, 2009.

  1. kid joe

    kid joe Guest

    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! , /\_ _| | .-'_|
    `................, _\__`[_______________| _| (_|
    ] [ \, ][ ][ (_|
     
    kid joe, May 15, 2009
    #1
    1. Advertising

  2. kid joe

    Gene Guest

    On May 15, 6:49 pm, kid joe <> wrote:
    > 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.
     
    Gene, May 16, 2009
    #2
    1. Advertising

  3. kid joe <> writes:
    > 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?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, May 16, 2009
    #3
  4. Gene <> writes:

    > On May 15, 6:49 pm, kid joe <> wrote:
    >> 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).


    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.

    --
    Ben.
     
    Ben Bacarisse, May 16, 2009
    #4
  5. kid joe

    Guest

    On May 15, 6:46 pm, Keith Thompson <> wrote:
    > kid joe <> writes:
    > > --

    >
    > >        ......................  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?
     
    , May 16, 2009
    #5
  6. writes:
    > On May 15, 6:46 pm, Keith Thompson <> wrote:
    >> kid joe <> writes:
    >> > --

    >>
    >> >        ......................  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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, May 16, 2009
    #6
  7. kid joe

    Ian Collins Guest

    Richard Heathfield wrote:
    >
    > 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!

    --
    Ian Collins
     
    Ian Collins, May 16, 2009
    #7
  8. In article <>,
    <> wrote:
    >On May 15, 6:46 pm, Keith Thompson <> wrote:
    >> kid joe <> writes:
    >> > --

    >>
    >> >        ......................  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.
     
    Kenny McCormack, May 16, 2009
    #8
  9. In article <gukrii$86o$>, kid joe <> wrote:

    >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
    --
    Please remember to mention me / in tapes you leave behind.
     
    Richard Tobin, May 16, 2009
    #9
  10. kid joe

    CBFalconer Guest

    Richard Heathfield wrote:
    > said:
    >> Keith Thompson <> wrote:
    >>> kid joe <> writes:
    >>>
    >>>> --
    >>>>
    >>>> ...................... 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.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, May 17, 2009
    #10
  11. kid joe

    Amandil Guest

    On May 16, 2:35 pm, Richard <> wrote:
    > Mark McIntyre <> writes:
    > > On 16/05/09 13:15, blargg wrote:

    >
    > >>> 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?

    >
    > >> What's a downcast?

    >
    > > an adjective....

    >
    > 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
     
    Amandil, May 17, 2009
    #11
  12. "Malcolm McLean" <> writes:
    > "kid joe" <> wrote in message news:
    >> If I have a formula with lots of typecasts am I right in thinking that
    >> these are usually not expensive operations?
    >>

    > 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.
    >>
    >> 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?
    >>

    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, May 17, 2009
    #12
  13. kid joe

    Guest

    On 17 May, 10:01, Amandil <> wrote:
    > On May 16, 2:35 pm, Richard <> wrote:> Mark McIntyre <> writes:
    > > > On 16/05/09 13:15, blargg wrote:


    > > >>> 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?

    >
    > > >> What's a downcast?

    >
    > > > an adjective....

    >
    > > 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.


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

    > Sorry about the pun. I know it isn't very good, I just couldn't
    > resist.
     
    , May 17, 2009
    #13
  14. kid joe

    Amandil Guest

    On May 17, 1:08 pm, wrote:
    > On 17 May, 10:01, Amandil <> wrote:
    >
    > > On May 16, 2:35 pm, Richard <> wrote:> Mark McIntyre <> writes:
    > > > > On 16/05/09 13:15, blargg wrote:
    > > > >>> 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?

    >
    > > > >> What's a downcast?

    >
    > > > > an adjective....

    >
    > > > 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.

    >
    > 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...
    > > ... I know it isn't very good, I just couldn't
    > > resist.


    -- Marty Amandil (Gandalf fought with a balrog, and died. But he's all
    White now!)
     
    Amandil, May 17, 2009
    #14
  15. kid joe

    JosephKK Guest

    On Sat, 16 May 2009 02:56:29 +0100, Ben Bacarisse
    <> wrote:

    >Gene <> writes:
    >
    >> On May 15, 6:49 pm, kid joe <> wrote:
    >>> 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).

    >
    >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.
     
    JosephKK, May 26, 2009
    #15
  16. "JosephKK"<> writes:

    > On Sat, 16 May 2009 02:56:29 +0100, Ben Bacarisse
    > <> wrote:

    <snip>
    >>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.


    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.

    --
    Ben.
     
    Ben Bacarisse, May 26, 2009
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Fitzpatrick

    Re: typecast error!

    Mark Fitzpatrick, Feb 1, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,685
    Lau Lei Cheong
    Feb 8, 2006
  2. Joe Van Meer

    Re: typecast error!

    Joe Van Meer, Feb 1, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    427
    Bob Lehmann
    Feb 2, 2006
  3. Otis Mukinfus

    Re: typecast error!

    Otis Mukinfus, Feb 3, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    379
    Terry Burns
    Feb 4, 2006
  4. Tomba

    generics typecast

    Tomba, Jan 9, 2006, in forum: Java
    Replies:
    8
    Views:
    1,340
  5. Venkat

    typecast int to string

    Venkat, Jan 7, 2004, in forum: C++
    Replies:
    9
    Views:
    17,867
    Martijn Lievaart
    Jan 8, 2004
Loading...

Share This Page