I have got a loop in a game that I am working on that does a cast from unsigned short to unsigned char.
Will this have a performance hit on the code, and if so, to what magnitude? Equivalent of a bit shift, an addition or multiplication or what??
Any ideas??
-- And that''s my $0.02 worth --
Hang on, where I come from, $0.02 is rounded down. Does that mean my opinion is worthless or priceless?
CHROM
Casting from one type to another.. performance hit?
-- That's my $0.02 worth --
Hang on, where I come from, $0.02 is rounded down. Does that mean my opinion is worthless or priceless?
CHROM
Hang on, where I come from, $0.02 is rounded down. Does that mean my opinion is worthless or priceless?
CHROM
Well, first these are worst case answers - I''m using VC5 debug mode instead of release mode -- because even with a cout displaying the variables at the end release mode optimized out all the variables and sent constants to cout! So in the real world it may be further optimized.
I''m no assembly guru - but thats what you want to look at to see whats going on.
--- C:\My Documents\Dev\temptest\main.cpp ---------------------------------------------------------------------------------
1:
2: int main()
3: {
00401010 push ebp
00401011 mov ebp,esp
00401013 sub esp,14h
4: unsigned short us1, us2;
5: unsigned char uc;
6: float f1;
7:
8: us1 = 50;
00401016 mov word ptr [us1],offset main(0x0040101a)+0Ah
9:
10: us2 = us1; // unsigned short to unsigned short (no cast)
0040101C mov ax,word ptr [us1]
00401020 mov word ptr [us2],ax
11:
12: uc = (unsigned char) us1; // here is the cast
00401024 mov cl,byte ptr [us1]
00401027 mov byte ptr [uc],cl
13:
14: // just for kicks lets look at the cost of casts to an from floats
15:
16: f1 = (float) us1; // unsigned short to float
0040102A mov edx,dword ptr [us1]
0040102D and edx,0FFFFh
00401033 mov dword ptr [ebp-14h],edx
00401036 fild dword ptr [ebp-14h]
00401039 fst dword ptr [f1]
17:
18: us2 = ( unsigned short ) f1; // float to usigned short
0040103C call __ftol(0x0040110c)
00401041 mov word ptr [us2],ax
19:
20: return 0;
00401045 xor eax,eax
21: }
00401047 mov esp,ebp
00401049 pop ebp
0040104A ret
As you can see there is no real hit casting from one int to another type of int -- its still 2 mov instructions. Floats on the other hand are a bit slower.
Jack
I''m no assembly guru - but thats what you want to look at to see whats going on.
--- C:\My Documents\Dev\temptest\main.cpp ---------------------------------------------------------------------------------
1:
2: int main()
3: {
00401010 push ebp
00401011 mov ebp,esp
00401013 sub esp,14h
4: unsigned short us1, us2;
5: unsigned char uc;
6: float f1;
7:
8: us1 = 50;
00401016 mov word ptr [us1],offset main(0x0040101a)+0Ah
9:
10: us2 = us1; // unsigned short to unsigned short (no cast)
0040101C mov ax,word ptr [us1]
00401020 mov word ptr [us2],ax
11:
12: uc = (unsigned char) us1; // here is the cast
00401024 mov cl,byte ptr [us1]
00401027 mov byte ptr [uc],cl
13:
14: // just for kicks lets look at the cost of casts to an from floats
15:
16: f1 = (float) us1; // unsigned short to float
0040102A mov edx,dword ptr [us1]
0040102D and edx,0FFFFh
00401033 mov dword ptr [ebp-14h],edx
00401036 fild dword ptr [ebp-14h]
00401039 fst dword ptr [f1]
17:
18: us2 = ( unsigned short ) f1; // float to usigned short
0040103C call __ftol(0x0040110c)
00401041 mov word ptr [us2],ax
19:
20: return 0;
00401045 xor eax,eax
21: }
00401047 mov esp,ebp
00401049 pop ebp
0040104A ret
As you can see there is no real hit casting from one int to another type of int -- its still 2 mov instructions. Floats on the other hand are a bit slower.
Jack
Cheers for that Jack...
Clears up a question I''ve thought of for a couple of days. I would assume that if VC5 is doing it okay then the latest eVC is doing it okay as well, but I''ll have a dig.
Interesting to note the float stuff. Must remember not to do that, lucky just moving ints around really.
-- And that''s my $0.02 worth --
Hang on, where I come from, $0.02 is rounded down. Does that mean my opinion is worthless or priceless?
CHROM
Clears up a question I''ve thought of for a couple of days. I would assume that if VC5 is doing it okay then the latest eVC is doing it okay as well, but I''ll have a dig.
Interesting to note the float stuff. Must remember not to do that, lucky just moving ints around really.
-- And that''s my $0.02 worth --
Hang on, where I come from, $0.02 is rounded down. Does that mean my opinion is worthless or priceless?
CHROM
-- That's my $0.02 worth --
Hang on, where I come from, $0.02 is rounded down. Does that mean my opinion is worthless or priceless?
CHROM
Hang on, where I come from, $0.02 is rounded down. Does that mean my opinion is worthless or priceless?
CHROM
The reason a float/int cast is slower is because the number must be truncated, but not in a bitwise manner. Floats are encoded based on the IEEE standard, and whenever you case from float to int, the cpu must determine just how big the decimal part is, and lob it off the number, so to speak.
But with pointer casts, or integer casts, or any casts of raw binary variables to/from types that are based on the raw binary number system all you''re doing is changing the way the compiler interprets the data at the memory location, not modifying the actual number at all.
I hope that made sense..
Good luck.
But with pointer casts, or integer casts, or any casts of raw binary variables to/from types that are based on the raw binary number system all you''re doing is changing the way the compiler interprets the data at the memory location, not modifying the actual number at all.
I hope that made sense..
Good luck.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement