www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - casting int to uint and vice versa - performance

reply Michal Minich <michal.minich gmail.com> writes:
What is performance overhead for casting value of type int to uint and in 
the opposite direction. What happen when I cast int to int or uint to uint 
(for the simplicity in generic code). I suppose there is no difference, 
and it is in fact no-op for processor - it depends only on 
interpretation. Only when making other operations compiler may generate 
different instruction when he considers some value of either of types. Am 
I right?

Thank you.
Dec 03 2010
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 03 Dec 2010 16:34:51 -0500, Michal Minich  
<michal.minich gmail.com> wrote:

 What is performance overhead for casting value of type int to uint and in
 the opposite direction. What happen when I cast int to int or uint to  
 uint
Nothing, there is no performance, type is strictly a compile-time concept.
 (for the simplicity in generic code). I suppose there is no difference,
 and it is in fact no-op for processor - it depends only on
 interpretation. Only when making other operations compiler may generate
 different instruction when he considers some value of either of types. Am
 I right?
Not sure what you mean by that last part... -Steve
Dec 03 2010
parent reply Michal Minich <michal.minich gmail.com> writes:
On Fri, 03 Dec 2010 16:44:42 -0500, Steven Schveighoffer wrote:

 On Fri, 03 Dec 2010 16:34:51 -0500, Michal Minich
 <michal.minich gmail.com> wrote:
 
 What is performance overhead for casting value of type int to uint and
 in the opposite direction. What happen when I cast int to int or uint
 to uint
Nothing, there is no performance, type is strictly a compile-time concept.
 (for the simplicity in generic code). I suppose there is no difference,
 and it is in fact no-op for processor - it depends only on
 interpretation. Only when making other operations compiler may generate
 different instruction when he considers some value of either of types.
 Am I right?
Not sure what you mean by that last part... -Steve
just that i + i generates different instructions than ui + ui where int i; uint ui; Not really important, because I'm currently interested in cast only. Thank you.
Dec 03 2010
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 03 Dec 2010 16:53:04 -0500, Michal Minich  
<michal.minich gmail.com> wrote:

 On Fri, 03 Dec 2010 16:44:42 -0500, Steven Schveighoffer wrote:

 On Fri, 03 Dec 2010 16:34:51 -0500, Michal Minich
 <michal.minich gmail.com> wrote:

 What is performance overhead for casting value of type int to uint and
 in the opposite direction. What happen when I cast int to int or uint
 to uint
Nothing, there is no performance, type is strictly a compile-time concept.
 (for the simplicity in generic code). I suppose there is no difference,
 and it is in fact no-op for processor - it depends only on
 interpretation. Only when making other operations compiler may generate
 different instruction when he considers some value of either of types.
 Am I right?
Not sure what you mean by that last part... -Steve
just that i + i generates different instructions than ui + ui where int i; uint ui; Not really important, because I'm currently interested in cast only. Thank you.
That is odd, I would think that i+i generates the same instructions as ui+ui. As far as I know, adding unsigned and signed is the same instruction, but unsigned add just ignores the carry bit. -Steve
Dec 03 2010
parent reply spir <denis.spir gmail.com> writes:
On Fri, 03 Dec 2010 17:49:47 -0500
"Steven Schveighoffer" <schveiguy yahoo.com> wrote:

 just that i + i generates different instructions than ui + ui where int
 i; uint ui;

 Not really important, because I'm currently interested in cast only.

 Thank you. =20
=20 That is odd, I would think that i+i generates the same instructions as =20 ui+ui. As far as I know, adding unsigned and signed is the same =20 instruction, but unsigned add just ignores the carry bit.
That used to be true (at least on some machines, a long time ago). Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Dec 03 2010
parent Don <nospam nospam.com> writes:
spir wrote:
 On Fri, 03 Dec 2010 17:49:47 -0500
 "Steven Schveighoffer" <schveiguy yahoo.com> wrote:
 
 just that i + i generates different instructions than ui + ui where int
 i; uint ui;

 Not really important, because I'm currently interested in cast only.

 Thank you.  
That is odd, I would think that i+i generates the same instructions as ui+ui. As far as I know, adding unsigned and signed is the same instruction, but unsigned add just ignores the carry bit.
That used to be true (at least on some machines, a long time ago). Denis -- -- -- -- -- -- -- vit esse estrany ☣ spir.wikidot.com
It's only multiply, divide, and right shift where the instructions are different.
Dec 03 2010