Skip to content

Comment on 10 Million Concurrent Connections – The Kernel is the Problemparent

Comments

Strangely, I can't reproduce the effect with a divide by 16. With GCC 4.5.2 -O2, I see a "shrl $4, %eax" for unsigned, and "sarl $4, %eax" for signed. However, if I divide by 2, the results are different; the function:

  int div_by_2(int x) {return x/2;}
compiles to:
  div_by_2:
    movl	%edi, %eax
    shrl	$31, %eax
    addl	%edi, %eax
    sarl	%eax
    ret
Meanwhile, the signed variant:
  unsigned int div_by_2(unsigned int x) {return x/2;}
compiles to:
  div_by_2:
    movl	%edi, %eax
    shrl	%eax
    ret
My conclusion remains that for hot loops, you should check the compilers assembly to make sure the compiler is applying the strength reduction you expect.
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.