Thomas Hruska <[EMAIL PROTECTED]> wrote:
> While we are on the subject of wishing for things in the
> Standard, 'ror' and 'rol' operators should exist too.
> They can be faked using shift operators but I've never
> seen a compiler optimize the two shift instructions down
> to a ror/rol.

You've obviously never used gcc then... ;-)

  % type rol.c
  unsigned rol(unsigned x)
  {
    return (x << 1) | (x / (-1u/2+1));
  }
  
  unsigned ror(unsigned x)
  {
    return (x >> 1) | (x * (-1u/2+1));
  }
  
  % gcc -ansi -pedantic -O3 -S rol.c
    
  % type rol.s
          .file   "rol.c"
          .section .text
          .p2align 4,,15
  .globl _rol
  _rol:
          pushl   %ebp
          movl    %esp, %ebp
          movl    8(%ebp), %eax
          popl    %ebp
          roll    %eax
          ret
          .p2align 4,,15
  .globl _ror
  _ror:
          pushl   %ebp
          movl    %esp, %ebp
          movl    8(%ebp), %eax
          popl    %ebp
          roll    $31, %eax
          ret
          .ident  "GCC: (GNU) 4.2.1"
  
  % 

-- 
Peter

Reply via email to