flat assembler
Message board for the users of flat assembler.

Index > Heap > Assembly optimization is Compiler's job

Goto page Previous  1, 2

Do you agree?
Yes.
53%
 53%  [ 8 ]
No.
46%
 46%  [ 7 ]
Total Votes : 15

Author
Thread Post new topic Reply to topic
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
kohlrak wrote:

Still, the compiler still can't tell that using a loop and a store variable means left rotate or right rotate.

Care to explain what you mean?

Btw, there's no ROL/ROR operators in C/C++, but here's a horribly naïve piece of code, and what a decent compiler will come up with:

Code:
// horribly naïve and unsafe portability-wise, but could be extended with type traits and whatnot.
unsigned myrolx(unsigned val, unsigned amt)
{
       return (val << amt) | (val >> (32-amt));
}
extern int flaf;
int main()
{
  return myrolx(flaf, 16);
}
    


And the resulting assembly:
Code:
; version used when amt not known at compile-time
myrolx PROC
    _val$ = 8
   _amt$ = 12
  ; return (val << amt) | (val >> (32-amt));
      mov     eax, DWORD PTR _val$[esp-4]
 mov     ecx, DWORD PTR _amt$[esp-4]
 rol     eax, cl

 ret     0
myrolx ENDP

_main   PROC
        ;  flaf = myrolx(flaf, 16);
 mov     eax, [flaf]
 rol     eax, 16
     mov     [flaf]

  ; return myrolx(42, 10);
    mov     eax, 43008
  ret     0
_main      ENDP
    


...too bad it doesn't ROL directly on the memory addres, but hey... nobody's perfect Smile ...oh yeah, and there's probably some intrinsic _lrotl or similar on most systems, which might be better in case the optimizer doesn't recognize the code sequence as a ROL/ROR.
Post 01 Aug 2007, 01:53
View user's profile Send private message Visit poster's website Reply with quote
MichaelH



Joined: 03 May 2005
Posts: 402
MichaelH
kohlrak wrote:

but i don't have a usage for one outside of compiling C/C++ tutorials

Still haven't been able to get a Visual Studio comparable lib file... then again, i haven't been trying too hard.


So you are just a beginner Shocked ..... doesn't it make you feel all warm inside when you tell the truth Wink


Sorry about my little outburst yesterday, seems I still have to work on being able to let young teenagers tell me time and again that I'm a sinner if I don't follow their religion only to see that same person misrepresenting the truth all over the place ..... yes I know, it's a personality flaw of mine, perhaps one day lying cheating and misrepresenting the truth will be easier for me, you know, that day when I turn to your christian god and are able to throw ethics out the window ..... till that day, I hope you can accept my apology Smile
Post 01 Aug 2007, 05:11
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo
MichaelH wrote:
kohlrak wrote:

but i don't have a usage for one outside of compiling C/C++ tutorials

Still haven't been able to get a Visual Studio comparable lib file... then again, i haven't been trying too hard.


So you are just a beginner Shocked ..... doesn't it make you feel all warm inside when you tell the truth Wink


Get a room, you two. Laughing
Post 02 Aug 2007, 22:00
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.