lshift & rshift

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • robertwessel2@yahoo.com

    #31
    Re: lshift & rshift

    On Sep 27, 5:23 am, "ie...@free .fr" <ie...@free.frw rote:
    On Sep 27, 12:15 pm, "ie...@free .fr" <ie...@free.frw rote:
    >
    Can you post the snapshot of the internal of a*2 and a+a that you
    got ?
    >
    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,"",@progb its
    >
    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.

    Comment

    • Army1987

      #32
      Re: lshift &amp; rshift

      On Thu, 27 Sep 2007 01:26:53 -0700, iesvs@free.fr wrote:
      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.

      --
      Army1987 (Replace "NOSPAM" with "email")
      A hamburger is better than nothing.
      Nothing is better than eternal happiness.
      Therefore, a hamburger is better than eternal happiness.

      Comment

      • iesvs@free.fr

        #33
        Re: lshift &amp; rshift

        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.

        Comment

        • robertwessel2@yahoo.com

          #34
          Re: lshift &amp; rshift

          On Sep 27, 5:16 am, "ie...@free .fr" <ie...@free.frw rote:
          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.

          Indeed. And please be sure to remember to include yourself in that
          category.

          Comment

          • Richard Heathfield

            #35
            Re: lshift &amp; rshift

            pete said:
            Richard Heathfield wrote:
            >>
            <snip>
            >ch <<= 1; /* ch is now 128 */
            >ch <<= 1; /* ch is now 0, even though 2 * 128 is 256. */
            >
            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. :-)

            --
            Richard Heathfield <http://www.cpax.org.uk >
            Email: -http://www. +rjh@
            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
            "Usenet is a strange place" - dmr 29 July 1999

            Comment

            • Richard Bos

              #36
              Re: lshift &amp; rshift

              "iesvs@free .fr" <iesvs@free.frw rote:
              Which is where a profiler comes in...
              >
              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

              Comment

              • karthikbalaguru

                #37
                Re: lshift &amp; rshift

                On Sep 27, 3:30 pm, "ie...@free .fr" <ie...@free.frw rote:
                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

                Comment

                • Joachim Schmitz

                  #38
                  Re: lshift &amp; rshift

                  "karthikbalagur u" <karthikbalagur u79@gmail.comsc hrieb im Newsbeitrag
                  news:1190897100 .262252.150400@ 19g2000hsx.goog legroups.com...
                  On Sep 27, 3:30 pm, "ie...@free .fr" <ie...@free.frw rote:
                  >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 ?
                  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


                  Comment

                  • Mark Bluemel

                    #39
                    Re: lshift &amp; rshift

                    iesvs@free.fr wrote:
                    I also try a << 1
                    and it converted it into a + a
                    >
                    Which presumably means that the designer of the compiler knew that it
                    was cheaper (fewer cycles) on the specific target architecture to add
                    "a" to itself than to use some other mechanism to achieve a 1-place left
                    shift.

                    If you don't want to trust the compiler writers to do a decent job of
                    optimisation, I suggest you move to writing assembler...

                    Comment

                    • iesvs@free.fr

                      #40
                      Re: lshift &amp; rshift

                      If you don't want to trust the compiler writers to do a decent job of
                      optimisation, I suggest you move to writing assembler...
                      No, no, I trust them. It's their work, not mine.
                      In beginning my question was theoretical.
                      The real meaning was, is << a left shift or something who shift the
                      numbers from the less important bit to the most important.
                      And people said it's the second, not the first.

                      Comment

                      • karthikbalaguru

                        #41
                        Re: lshift &amp; rshift

                        On Sep 27, 6:07 pm, "ie...@free .fr" <ie...@free.frw rote:
                        If you don't want to trust the compiler writers to do a decent job of
                        optimisation, I suggest you move to writing assembler...
                        >
                        No, no, I trust them. It's their work, not mine.
                        In beginning my question was theoretical.
                        The real meaning was, is << a left shift or something who shift the
                        numbers from the less important bit to the most important.
                        And people said it's the second, not the first.
                        Onceagain,
                        You have not replied to the below questions -
                        1) What is the optimisation level you have ?
                        2) Does it treat a<<1 and a*2 as a+a for all the optimisation
                        levels ?
                        3) What is the architecture you use ?

                        Karthik Balaguru

                        Comment

                        • iesvs@free.fr

                          #42
                          Re: lshift &amp; rshift

                          I just need a precision about an operator. I saw many code with the
                          operator << and didn't know if it was portable.

                          Today I don't care if "<< 1" is faster than "* 2" or "+".

                          Comment

                          • karthikbalaguru

                            #43
                            Re: lshift &amp; rshift

                            On Sep 27, 10:36 pm, "ie...@free .fr" <ie...@free.frw rote:
                            I just need a precision about an operator. I saw many code with the
                            operator << and didn't know if it was portable.
                            >
                            Today I don't care if "<< 1" is faster than "* 2" or "+".
                            You have not yest answered these questions which will clarify your
                            doubts .

                            1) What is the optimisation level you have ?
                            2) Does it treat a<<1 and a*2 as a+a for all the optimisation
                            levels ?
                            3) What is the architecture you use ?
                            4) Also tell about the compiler and programming language you use ?

                            Karthik Balaguru



                            Comment

                            • Joachim Schmitz

                              #44
                              Re: lshift &amp; rshift

                              "karthikbalagur u" <karthikbalagur u79@gmail.comsc hrieb im Newsbeitrag
                              news:1190915081 .349773.130800@ g4g2000hsf.goog legroups.com...
                              On Sep 27, 10:36 pm, "ie...@free .fr" <ie...@free.frw rote:
                              >I just need a precision about an operator. I saw many code with the
                              >operator << and didn't know if it was portable.
                              >>
                              >Today I don't care if "<< 1" is faster than "* 2" or "+".
                              >
                              You have not yest answered these questions which will clarify your
                              doubts .
                              >
                              1) What is the optimisation level you have ?
                              As he said earlier (implicitly): no optimization
                              2) Does it treat a<<1 and a*2 as a+a for all the optimisation
                              levels ?
                              Yes (I've checked)
                              3) What is the architecture you use ?
                              From the assembly he posted: Intel ix86
                              4) Also tell about the compiler and programming language you use ?
                              gcc and C

                              Bye, Jojo


                              Comment

                              • iesvs@free.fr

                                #45
                                Re: lshift &amp; rshift

                                1) What is the optimisation level you have ?
                                >
                                As he said earlier (implicitly): no optimization
                                No, I used to use -O3, but when I post my example I used no
                                optimization.
                                2) Does it treat a<<1 and a*2 as a+a for all the optimisation
                                levels ?
                                >
                                Yes (I've checked)
                                Ha, interesting.
                                3) What is the architecture you use ?
                                >
                                From the assembly he posted: Intel ix86
                                686 to be exact (P4 I think).
                                >
                                4) Also tell about the compiler and programming language you use ?
                                >
                                gcc and C
                                We're in a C forum.

                                Comment

                                Working...