Jump to content

  • Log In with Google      Sign In   
  • Create Account

is adding two same numbers faster than multiplication by two?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
16 replies to this topic

#1 JohnnyCode   Members   -  Reputation: 219

Like
0Likes
Like

Posted 25 July 2013 - 11:04 AM

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!



Sponsor:

#2 Paradigm Shifter   Crossbones+   -  Reputation: 5374

Like
5Likes
Like

Posted 25 July 2013 - 11:13 AM

Look at the disassembly and find out...

 

Note that the compiler will optimise multiplication by constants anyway so code wise it doesn't matter what you do (for integers with optimisation enabled anyway). This sort of thing doesn't matter anymore, it used to matter when hand coding in assembly (especially on processors which don't have a native multiply instruction).


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#3 Álvaro   Crossbones+   -  Reputation: 13326

Like
9Likes
Like

Posted 25 July 2013 - 11:20 AM

These days this particular case doesn't matter even if you are programming in assembly: Adding an integer to itself or shifting it one bit are similarly [and extremely] cheap operations.

 

If you are worried about performance, you should be thinking of cache misses (memory fragmentation), branch misprediction (conditional statements that don't follow a simple pattern) and a few expensive operations (logarithms, trigonometric functions...). You can even try to reduce your use of integer division. But addition and shifting are essentially free.

 

[EDIT: Of course, algorithmic improvements dwarf all the other considerations; but I assume you knew that.]


Edited by Álvaro, 25 July 2013 - 11:24 AM.


#4 Matias Goldberg   Crossbones+   -  Reputation: 3399

Like
6Likes
Like

Posted 25 July 2013 - 12:44 PM

Yep, what's been said.
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.
If you're working with signed integers, there's a higher chance the compiler won't change some arithmetic operations to bitshifting.
Wikipedia seems to have something about the subject.

Edited by Matias Goldberg, 25 July 2013 - 12:44 PM.


#5 Álvaro   Crossbones+   -  Reputation: 13326

Like
4Likes
Like

Posted 25 July 2013 - 12:49 PM


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



#6 Ravyne   GDNet+   -  Reputation: 7409

Like
4Likes
Like

Posted 25 July 2013 - 01:47 PM

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.



#7 frob   Moderators   -  Reputation: 21335

Like
5Likes
Like

Posted 25 July 2013 - 02:30 PM

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!

 

As others have touched on and Ravyne's link show, it is not as simple as it seems.

 

For the code b=b+b, the compiler will probably generate exactly the code you are expecting: add reg, reg

 

If it doesn't do that, it is because the compiler is smarter than you.  If that is the case, you don't need to worry about it.  That's why compiler optimizer programmers make the big money, so you don't have to.  :-)


Check out my personal indie blog at bryanwagstaff.com.

#8 SiCrane   Moderators   -  Reputation: 9598

Like
5Likes
Like

Posted 25 July 2013 - 04:31 PM

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

#9 Matias Goldberg   Crossbones+   -  Reputation: 3399

Like
3Likes
Like

Posted 25 July 2013 - 05:31 PM

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)

#10 mhagain   Crossbones+   -  Reputation: 7979

Like
4Likes
Like

Posted 25 July 2013 - 05:57 PM

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.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#11 Ravyne   GDNet+   -  Reputation: 7409

Like
1Likes
Like

Posted 25 July 2013 - 06:32 PM

 

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, 25 July 2013 - 07:07 PM.


#12 SiCrane   Moderators   -  Reputation: 9598

Like
1Likes
Like

Posted 25 July 2013 - 06:45 PM

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.



#13 Ravyne   GDNet+   -  Reputation: 7409

Like
1Likes
Like

Posted 25 July 2013 - 07:01 PM

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.



#14 Khatharr   Crossbones+   -  Reputation: 3003

Like
1Likes
Like

Posted 26 July 2013 - 12:52 AM


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, 26 July 2013 - 12:55 AM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#15 Álvaro   Crossbones+   -  Reputation: 13326

Like
2Likes
Like

Posted 26 July 2013 - 05:34 AM

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"]

#16 Matias Goldberg   Crossbones+   -  Reputation: 3399

Like
1Likes
Like

Posted 26 July 2013 - 06:00 PM

 

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.



#17 Khatharr   Crossbones+   -  Reputation: 3003

Like
0Likes
Like

Posted 27 July 2013 - 05:30 AM

 

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.


void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS