Sign in to follow this  
JohnnyCode

is adding two same numbers faster than multiplication by two?

Recommended Posts

JohnnyCode    1046

Title quite says it. In other words, what will compiler compile if it recieves b=b+b;?

will it perform b=b*2, thus one bit shift, or it will add the two numbers? consider the numbers to be integers.

Thanks much!

Share this post


Link to post
Share on other sites
alvaro    21266


Note that some operations you'd think would be replaced by bit shifting doesn't yield the same result.
For example (-2 / 2) and (-2 >> 1) don't give the same result.

 

Shifting a signed integer to the right propagates the sign bit, which results in the correct result.

 

#include <iostream>

int main() {
  std::cout << (-2 / 2) << '\n';
  std::cout << (-2 >> 1) << '\n';
}

 

Output:

-1

-1

Share this post


Link to post
Share on other sites
Ravyne    14300

Any actual difference that might exist is going to stem from how the processor's particular micro-architecture handle the different options, and perhaps from reduced pressure on the instruction-cache if the instruction sizes are different. It's not something you can hope to control from code -- even assembly language -- so your best bet is to just do what reads most clearly. Moreso, even, because compilers themselves are very smart about this kind of thing and have more context than you; being overly clever in your arrangement of code can prevent the compiler from recognizing bits of code it could have optimized if they were just written in the straight-forward way.

 

Modern processors, too, are incredibly complex, and incredibly smart. Today, some common variations of certain instructions aren't even executed -- they cost literally zero processor cycles, and consume no back-end execution resources of the processor. Throughput of these kinds of instructions are limited by other pathways in the processor (i-cache bandwidth, register renaming, etc). I doubt if this particular case is handled in this way, but I mention it because it demonstrates clearly the amount of effort that processor designers expend to make even "dumb" code run fast -- If you do the simple, straight-forward, recommended thing, other people make careers of making that go fast. As a programmer, its really your job to pick the right algorithm and, for the most part, be done with it.

 

For an interesting read on the kinds of micro-architecture-level optimizations I mentioned, check out The Surprising Subtleties of Zeroing a Register.

Share this post


Link to post
Share on other sites
Matias Goldberg    9581

Note that some operations you'd think would be replaced by bit shifting doesn't yield the same result.
For example (-2 / 2) and (-2 >> 1) don't give the same result.

 
Shifting a signed integer to the right propagates the sign bit, which results in the correct result.
 
 
#include <iostream>

int main() {
  std::cout << (-2 / 2) << '\n';
  std::cout << (-2 >> 1) << '\n';
}
 
Output:
-1
-1

Ooops, that's not what I meant (though thanks SiCrane for pointing out the Standard thingy); I was more into the rounding issue, and my example was wrong because it doesn't truncate or round.
The correct example I meant to give is: (-3 / 2) != (-3 >> 1)

Share this post


Link to post
Share on other sites
mhagain    13430

In VS2010 I get the following:

		int b = 2;
00BCF613  mov         dword ptr [b],2  
		int c = b * 2;
00BCF61A  mov         edx,dword ptr [b]  
00BCF61D  shl         edx,1  
00BCF61F  mov         dword ptr [c],edx  
		int d = b + b;
00BCF622  mov         eax,dword ptr [b]  
00BCF625  add         eax,dword ptr [b]  
00BCF628  mov         dword ptr [d],eax  

Is that faster or not?  Depends on how your CPU implements the shl and add instrcutions, but in either case it's just 3 instructions (using a debug build, though).

 

The one thing I can guarantee is that unless you're doing this in a deep inner loop and at least millions of times, you are not going to notice one bit of difference in terms of overall performance.  This is just so much not worth worrying about.

Share this post


Link to post
Share on other sites
Ravyne    14300

 

Shifting a signed integer to the right propagates the sign bit, which results in the correct result.

Shifting a negative signed integer to the right may or may not propagate the sign bit, the results of right shifting a negative are implementation defined in C++.

 

 

... and that, by the way, encroaches on the dark corners of C++ that you best avoid.

 

Worse yet is undefined-behavior, in which the compiler might do with values it knows at compile-time is sometimes different than what the CPU would do if left to its own devices with run-time values. For example, If you shift a 32bit unsigned value of 1 left 32 positions under VS2012 for x86 target, and both the '1' and the shift amount are known, the compiler's optimizer folds this away and the apparant result is 0. However, if one of them is a runtime value, the operation must be executed by the CPU, and on x86 (and x64, it happens) the result is 1.

 

To plug an article I wrote for the Visual C++ team blog that talks about this kind of thing from a perspective of undefined/implementation-defined behaviors that might bite you when moving from x86/x64 to ARM, see Hello ARM: Exploring Undefined, Unspecified, and Implementation-defined Behavior in C++.

 

Plenty can go wrong when programmers play fast and loose with the standard, and end up being too clever by half smile.png

 

[Edit] Slight pivot to be more precise, as SiCrane pointed out below.

Edited by Ravyne

Share this post


Link to post
Share on other sites
SiCrane    11839

Actually, that one is undefined behavior rather than implementation defined behavior. Shifting an int by a value greater than or equal to the number of bits in the int is undefined.

Share this post


Link to post
Share on other sites
Ravyne    14300

You're right. I was being imprecise -- writing that article should have taught me better wink.png

 

  • Undefined: Anything can happen, even different things under different circumstances.
  • Implementation-defined: Whatever the compiler-vendor chooses to happen, happens. Its required to be documented and behave as documented, but might change in the future.
  • Unspecified: Whatever the compiler-vendor chooses to happen, happens, but its not documented.

 

Anyhow, the point that should be taken home here is that sometimes things that appear to do one thing on the surface might not, in fact, do that according to the standard, and in general the "straight forward" method of doing something usually subject to fewer such dangers. Another point in the column of not trying to out-clever the compiler.

Share this post


Link to post
Share on other sites
Khatharr    8812

The one thing I can guarantee is that unless you're doing this in a deep inner loop and at least millions of times, you are not going to notice one bit of difference in terms of overall performance.  This is just so much not worth worrying about.

 

^ This.

 

At this point, we've probably all spent more time on this thread than this kind of optimization will give back in the full life cycle of a program.

 

At best we're looking at a difference of one or two cycles, if there is a difference.

 

My crappy, outdated CPU has two cores that each run at 2.41 million cycles per second. I've spent a total of about 10 minutes poking around this thread from time to time. In terms of cycles on my crappy CPU we're looking at the neighborhood of 1,446,000,000.

 

How often does this code run?

Edited by Khatharr

Share this post


Link to post
Share on other sites
alvaro    21266

My crappy, outdated CPU has two cores that each run at 2.41 million cycles per second. I've spent a total of about 10 minutes poking around this thread from time to time. In terms of cycles on my crappy CPU we're looking at the neighborhood of 1,446,000,000.


2.41 million cycles per second? Are you running a Z80? smile.png

[It's actually "billion"]

Share this post


Link to post
Share on other sites
Matias Goldberg    9581

 

The one thing I can guarantee is that unless you're doing this in a deep inner loop and at least millions of times, you are not going to notice one bit of difference in terms of overall performance.  This is just so much not worth worrying about.

 
^ This.
 
At this point, we've probably all spent more time on this thread than this kind of optimization will give back in the full life cycle of a program.
 
At best we're looking at a difference of one or two cycles, if there is a difference.
 
My crappy, outdated CPU has two cores that each run at 2.41 million cycles per second. I've spent a total of about 10 minutes poking around this thread from time to time. In terms of cycles on my crappy CPU we're looking at the neighborhood of 1,446,000,000.
 
How often does this code run?

 

Something that is not being considered is that today's x86 instructions are pipelined.

 

If we take a look at my CPU's instruction latency list, most 'add' instructions have a latency of 0.33ns, but a throughput of 0.11ns

If we look instead at 'shl' & 'sar' instructions, they have a latency of 0.33ns but a throughput of 0.16ns.

This means in my CPU adding a register to itself is equally fast as bit shifting, and if there are many of those instructions to perform with no data dependencies (this is very important!), it's actually faster than bit shifting.

However other machines may have different latencies & throughput measurements, not to mention their out of order executions could be dumber/smarter, thus affecting the ability to reach throughput "ideal" performance

 

If we take a look at the mul & imul instructions, all of them are equal or above 1ns latency, and only a few have a throughput of 0.33ns while the rest are all above 1ns. In my machine it's clearly faster to use addition or bitshifting than multiplication.

Share this post


Link to post
Share on other sites
Khatharr    8812

 

My crappy, outdated CPU has two cores that each run at 2.41 million cycles per second. I've spent a total of about 10 minutes poking around this thread from time to time. In terms of cycles on my crappy CPU we're looking at the neighborhood of 1,446,000,000.


2.41 million cycles per second? Are you running a Z80? smile.png

[It's actually "billion"]

 

 
Ack. Billion, yes.
 
That makes it 1,446,000,000,000.
 
Meanwhile...
 
 

Something that is not being considered is that today's x86 instructions are pipelined.
 
If we take a look at my CPU's instruction latency list, most 'add' instructions have a latency of 0.33ns, but a throughput of 0.11ns
If we look instead at 'shl' & 'sar' instructions, they have a latency of 0.33ns but a throughput of 0.16ns.
This means in my CPU adding a register to itself is equally fast as bit shifting, and if there are many of those instructions to perform with no data dependencies (this is very important!), it's actually faster than bit shifting.
However other machines may have different latencies & throughput measurements, not to mention their out of order executions could be dumber/smarter, thus affecting the ability to reach throughput "ideal" performance
 
If we take a look at the mul & imul instructions, all of them are equal or above 1ns latency, and only a few have a throughput of 0.33ns while the rest are all above 1ns. In my machine it's clearly faster to use addition or bitshifting than multiplication.

 
There's not really a realistic scenario where you're going to end up with a mul or imul in this case. Even in debug mode VS optimizes *= 2 into a shl by 1. In release mode it registers the variable and does the add r32,r32, which is better by a difference of 0.05ns. 10 minutes is 600 billion ns, so you'd have to hit that instruction around 12 trillion times to get the 10 minutes back.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this