473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

a question on VC++.NET compiler

I have some code like this:
///////////////
void test(int* a)
{
a[0]+=((a[1]-a[2])<<3);
}
////////////////
after compilng with vc.net 2003, the asm code is:
///////////
PUBLIC ?test@@YIXPAH@Z ; test
; Function compile flags: /Ogty
; File c:\test.cpp
; COMDAT ?test@@YIXPAH@Z
_TEXT SEGMENT
?test@@YIXPAH@Z PROC NEAR ; test, COMDAT
; _a$ = eax
; 144 : void test(int* a)

mov ecx, DWORD PTR [eax+4]
sub ecx, DWORD PTR [eax+8]
add ecx, ecx
add ecx, ecx
add ecx, ecx
add DWORD PTR [eax], ecx
ret 0
?test@@YIXPAH@Z ENDP ; test
////////////////////////////////////////////
Question:
why using "add ecx,ecx" three times and not using "shl ecx,3" instead.

To my idea shl ecx,3 is faster.
Thanks for any answers.

Nov 17 '05 #1
4 854
mestupid wrote:
I have some code like this:
///////////////
void test(int* a)
{
a[0]+=((a[1]-a[2])<<3);
}
////////////////
after compilng with vc.net 2003, the asm code is:
///////////
PUBLIC ?test@@YIXPAH@Z ; test
; Function compile flags: /Ogty
; File c:\test.cpp
; COMDAT ?test@@YIXPAH@Z
_TEXT SEGMENT
?test@@YIXPAH@Z PROC NEAR ; test, COMDAT
; _a$ = eax
; 144 : void test(int* a)

mov ecx, DWORD PTR [eax+4]
sub ecx, DWORD PTR [eax+8]
add ecx, ecx
add ecx, ecx
add ecx, ecx
add DWORD PTR [eax], ecx
ret 0
?test@@YIXPAH@Z ENDP ; test
////////////////////////////////////////////
Question:
why using "add ecx,ecx" three times and not using "shl ecx,3" instead.

To my idea shl ecx,3 is faster.


I know very little about assembler, but a quick search on google seems
to show that you may be wrong, at least for P4:

http://www.intel.com/cd/ids/develope...4010.htm?prn=Y

It says: "Shifts, rotations Avoid if possible, schedule dependent
instructions as far away as possible; replace shl with additions". It
also says: "the shl µop has a latency of 4", whatever that means.

Tom
Nov 17 '05 #2
Tom Widmer wrote:
mestupid wrote:
why using "add ecx,ecx" three times and not using "shl ecx,3"
instead. To my idea shl ecx,3 is faster.


I know very little about assembler, but a quick search on google seems
to show that you may be wrong, at least for P4:

http://www.intel.com/cd/ids/develope...4010.htm?prn=Y

It says: "Shifts, rotations Avoid if possible, schedule dependent
instructions as far away as possible; replace shl with additions". It
also says: "the shl µop has a latency of 4", whatever that means.


IIUC, on a Pentium 4 class CPU, the adds will execute in 1/2 clock cycle
each in the execution core - three adds will be ~2X faster than the shift
would be.

-cd
Nov 17 '05 #3
Thanks -cd and Tom.
That's very helpful. and I read through IA32 Intel architecture
optimization. yeah shl has a long latency.

I have another question need to make it clear:
ADD has latency 0.5, Does that mean the cpu could handle two ADD per clock
cycle?

I guess ADD needs a total of 1.5 clock cycles. one for instruction and half
for latency. So three ADDs need 4.5 cycles

But shl has one instruction cycle and 4 for latency . so shl has totally 5
cycles.
Am I right?

Nov 17 '05 #4
mestupid wrote:
Thanks -cd and Tom.
That's very helpful. and I read through IA32 Intel architecture
optimization. yeah shl has a long latency.

I have another question need to make it clear:
ADD has latency 0.5, Does that mean the cpu could handle two ADD per
clock cycle?
That's correct.
I guess ADD needs a total of 1.5 clock cycles. one for instruction
and half for latency. So three ADDs need 4.5 cycles

But shl has one instruction cycle and 4 for latency . so shl has
totally 5 cycles.
Am I right?


You could be - I didn't check the actual instruction timings.

-cd
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mark | last post by:
The following code works with gnu compilers (ISO compliant declaration of the variable "i") void foo() { for (int i = 0; i < length; i++) { loop code.... } for (int i = 0; i < length; ...
3
by: debadeepti | last post by:
Hi All, I am porting an application from watcom to vc compiler. In watcom and other compiler (other then vc) memory allocation of __vfptr inside class object are different. In vc __vfptr is...
15
by: Ramaraj M Bijur | last post by:
Hi All, Could anyone help me to resolve following problem in C the IDE used is Microsoft VC++, Please let me know the which option in VC++ will do the needful The problem statement:...
5
by: Hari | last post by:
Guys please help me to solve this strange problem what Iam getting as follows.. Trying to instantiate a global instance of a template class as follows :- when i build this code with debug and...
2
by: Brian | last post by:
In particular, this question goes out to the Microsoft C++ development team. Back in June, Ronald Laeremans posted the following message. Has the optimizing compiler been included with the...
1
by: Aaron | last post by:
After taking 2 semesters of C++ at school, I've decided to get a compiler for my personal use when not at school, and one that can do Windows programming. I've pretty much come down to either vc++...
4
by: Alex | last post by:
Hi there I'm switching from VC++ 6.0 to VC++ .NET 2003. Since there is no stand-alone version of VC++ .NET 2003 Pro, I went and purchased the Standard version, which does not have an...
0
by: Ganapathy | last post by:
I have COM dll code written in VC 6.0. When i tried compiling this code in VC 7, The MIDL cmpiler gets called twice. i.e. it initially compiles fully & immediately a line - 64 bit processing'...
4
by: jimmy_sayavong | last post by:
I created a simple MFC program using VC++ 6. It generates EXE file and runs fine on one of my computer which has the C++ compiler installed. But when I copy the EXE program to the other...
17
by: Fabry | last post by:
Hi All, I'm new of this group and I do not know if this is the correct group for my question. I have a DLL with its export library (.lib) wrote in Borland C++ 6. In borland everything is OK and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.