lshift & rshift

I

Ian Collins

No. I know when I programm if * 2 will be used 10 times or 10000....
I know from the beginning where are the important and no important
part. I don't need a software to say me these kind of things, moreover
there are more dumbs than me (a computer beat myself, ha ha ha).
So you can guess better than the computer can measure? Performing
accurate and repeatable measurements is an ideal task for an entity with
an IQ of zero, such as a computer.
 
K

karthikbalaguru

No you're wrong. A saw that it change a * 2 by a + a. (in reality I
was looking at a * 7) and I saw something like that :
b = a + a
c = b + b
c + b + a
So after I try a * 2 and get
a + a
But it didn't use lshift.

Can you post the snapshot of the internal of a*2 and a+a that you
got ?

Karthik Balaguru
 
R

robertwessel2

No. I know when I programm if * 2 will be used 10 times or 10000....
I know from the beginning where are the important and no important
part. I don't need a software to say me these kind of things, moreover
there are more dumbs than me (a computer beat myself, ha ha ha).


It's been demonstrated repeatedly over the last few decades that
programmers are absolutely *terrible* at identifying hotspots in their
code. Sure, some easy cases are easy to spot, but others are not, and
it's very, very easy to spend a lot of time optimizing a piece of code
(and making it vastly less maintainable in the process) that isn't
actually a hotspot. Profiling will tell you the truth. Nor are all
these possible optimizations consistently beneficial, and often depend
on the implementation of the processor, so you must always measure
your optimizations.
 
I

iesvs

Can you post the snapshot of the internal of a*2 and a+a that you

I can't, it was in past. But I'm sure. I asked a friend, and he
explain me that on some architecture (like the HP adding machine)
there wasn't a multiplication in the CPU, so the multiplication was a
software and it was better to use + than *. And it was not a lshift, I
swear it.
 
I

iesvs

It's been demonstrated repeatedly over the last few decades that
programmers are absolutely *terrible* at identifying hotspots in their
code.

Like they're terrible to produce a correct code.
 
J

Joachim Schmitz

I can't, it was in past. But I'm sure. I asked a friend, and he
explain me that on some architecture (like the HP adding machine)
there wasn't a multiplication in the CPU, so the multiplication was a
software and it was better to use + than *. And it was not a lshift, I
swear it.
Look at the machine/assembly code that get's generated _now_ on _your
platform_ and compare the code it generated for

a+a
a*2
a<<1

Repeat the comparision with the various optimization levels your compiler
provides.
Then lookup the the processors assembly manual and count CPU cycles.

Bye, Jojo
 
I

iesvs

I can't, it was in past. But I'm sure. I asked a friend, and he
explain me that on some architecture (like the HP adding machine)
there wasn't a multiplication in the CPU, so the multiplication was a
software and it was better to use + than *. And it was not a lshift, I
swear it.

Finnaly I can, here you are :
I made gcc -S mult.c to get the assembly code.

****mult.c****
**************
int mult_by_7 (int a)
{
return a * 7;
}

int mult_by_2 (int a)
{
return a * 2;
}

****mult.s****
**************
.file "mult.c"
.text
..globl mult_by_7
.type mult_by_7, @function
mult_by_7:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %edx
movl %edx, %eax
sall $3, %eax
subl %edx, %eax
popl %ebp
ret
.size mult_by_7, .-mult_by_7
..globl mult_by_2
.type mult_by_2, @function
mult_by_2:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
addl %eax, %eax
popl %ebp
ret
.size mult_by_2, .-mult_by_2
.ident "GCC: (GNU) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)"
.section .note.GNU-stack,"",@progbits

I don't understand the first (assembly language is a little bit new
for me).
Bu for mult_by_2 you can see the addl %eax,%eax
so it changed a * 2 by a + A.

Or perhaps I don't understand what does this assembly code.
 
R

Richard Tobin

Then you shouldn't be fiddling with micro-optimisations.
[/QUOTE]
What ? I used to profile with my hands (like I debug with my hands).
And when a micro operation is used many times it becomes a macro
optimisations.

You are misunderstanding the term. Micro-optimisation doesn't mean
optimisation that has a very small effect, it means optimisation of
small operations, as opposed to optimisation of the algorithm.

Micro-optimisation can have have very large effects, but compilers are
generally better at it than humans.

-- Richard
 
J

Joachim Schmitz

Finnaly I can, here you are :
I made gcc -S mult.c to get the assembly code.

****mult.c****
**************
int mult_by_7 (int a)
{
return a * 7;
}

int mult_by_2 (int a)
{
return a * 2;
}

****mult.s****
**************
.file "mult.c"
.text
.globl mult_by_7
.type mult_by_7, @function
mult_by_7:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %edx
movl %edx, %eax
sall $3, %eax
subl %edx, %eax
popl %ebp
ret
.size mult_by_7, .-mult_by_7
.globl mult_by_2
.type mult_by_2, @function
mult_by_2:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
addl %eax, %eax
popl %ebp
ret
.size mult_by_2, .-mult_by_2
.ident "GCC: (GNU) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)"
.section .note.GNU-stack,"",@progbits

I don't understand the first (assembly language is a little bit new
for me).
Bu for mult_by_2 you can see the addl %eax,%eax
so it changed a * 2 by a + A.
Looks like it. The other one looks likes a "a<<3 - a" to me, but that's only
a guess...
Now compare with the code for a shift_by_one.
Then switch on compiler optimization and check again.

Bye, Jojo
 
J

Joachim Schmitz

I also try a << 1
and it converted it into a + a
Exactly our point here. So coding a*2 has no performance advantage over a<<1
(or a+a), but a*2 has the advantage of readability.

Bye, Jojo
 
R

robertwessel2

Finnaly I can, here you are :
I made gcc -S mult.c to get the assembly code.

****mult.c****
**************
int mult_by_7 (int a)
{
return a * 7;

}

int mult_by_2 (int a)
{
return a * 2;

}

****mult.s****
**************
.file "mult.c"
.text
.globl mult_by_7
.type mult_by_7, @function
mult_by_7:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %edx
movl %edx, %eax
sall $3, %eax
subl %edx, %eax
popl %ebp
ret
.size mult_by_7, .-mult_by_7
.globl mult_by_2
.type mult_by_2, @function
mult_by_2:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
addl %eax, %eax
popl %ebp
ret
.size mult_by_2, .-mult_by_2
.ident "GCC: (GNU) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)"
.section .note.GNU-stack,"",@progbits

I don't understand the first (assembly language is a little bit new
for me).
Bu for mult_by_2 you can see the addl %eax,%eax
so it changed a * 2 by a + A.

Or perhaps I don't understand what does this assembly code.


The first routine compiles the (a*7) to ((a*8-a), with the
multiplication by eight done via a shift. In any event it looks like
you have not, as suggested, turned up the optimization level for GCC,
so he's generated fairly lame code. Try it with -O2 and see if that
doesn't reduce to a mov/lea/sub/ret sequence.
 
A

Army1987

Hello guys,
every time a rode a doc or a book about the language C I saw that
operators << and >> exist. But each time they said that << translate
the digit to the left (and >> ...) but no one said if a << 1 mean
every time a * 2, because there is the problem with the way the
integer are stored. So I never use thoses operators because I fear
strange behaviour on different architecture. Can I use them and be
sure that a << 1 means a * 2 on every computer ? Is is in the ansi
standard of language C ?

If the first operand is positive, it is.
 
I

iesvs

Looks like it. The other one looks likes a "a<<3 - a" to me, but that's only
a guess...

No you're right. It's just I'm not able to read assembly code.
I can't understand the result of my test. Maybe you're right.
I don't know.
 
R

Richard Heathfield

pete said:
That example doesn't show a difference
between shifting and a doubling.

I agree. It does, however, show that shifting doesn't always double the
number. The fact that doubling doesn't always double the number is another
issue. :)
 
R

Richard Bos

No. I know when I programm if * 2 will be used 10 times or 10000....
I know from the beginning where are the important and no important
part. I don't need a software to say me these kind of things, moreover
there are more dumbs than me (a computer beat myself, ha ha ha).

You overestimate yourself. That's ok; nearly everybody does, until he
gets a taste of what a _real_ profiler and debugging suite can do. But
it isn't wise to refuse to learn better.

Richard
 
K

karthikbalaguru

I also try a << 1
and it converted it into a + a

What is the optimisation level you have ?
Does it treat a<<1 and a*2 as a+a for all the optimisation levels ?
What is the architecture you use ?

It is dependent on your architecture and compiler you use.

Karthik Balaguru
 
J

Joachim Schmitz

karthikbalaguru said:
What is the optimisation level you have ?
Does it treat a<<1 and a*2 as a+a for all the optimisation levels ?
What is the architecture you use ?
I've checked with gcc on a pentium running Linux. It generates identical
code for a*2 and a<<1 in all optimization levels and identical code for a+a
with -O1 and higher (and only slightly different code with -O0).

Bye, Jojo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top