Swap should be better done native?

Ulf Zibis Ulf.Zibis at gmx.de
Wed Mar 31 15:16:51 PDT 2010


Am 31.03.2010 21:06, schrieb Martin Buchholz:
> On Wed, Mar 31, 2010 at 12:01, Ulf Zibis<Ulf.Zibis at gmx.de>  wrote:
>    
>> What's about swap(short), swap(char) ?
>>      
> My understanding is that there are no "short" or "char" types at the
> bytecode level.
>
> So perhaps we can optimize by using integer-level swaps
>
> swap(x) { return (char) Integer.swapBytes(x<<  16); }
>
> Please try benchmarking/disassembling that.
>    

Original:
   0x00b8995b: mov    %edx,%ebx
   0x00b8995d: shl    $0x8,%ebx
   0x00b89960: shr    $0x8,%edx
   0x00b89963: or     %edx,%ebx
   0x00b89965: and    $0xffff,%ebx    // superfluous, as only 16 bits 
were saved later
   ...
   0x00b89975: mov    %bx,(%eax)         ;*invokevirtual putChar
                                         ; - 
java.nio.DirectByteBuffer::putChar at 30 (line 482)

swap(x) { return (char)Integer.reverseBytes(x << 16); }:
   0x00b9251d: shl    $0x10,%edx
   0x00b92520: bswap  %edx
   0x00b92522: and    $0xffff,%edx    // superfluous, as only 16 bits 
were saved later
   ...
   0x00b92532: mov    %dx,(%eax)         ;*invokevirtual putChar
                                         ; - 
java.nio.DirectByteBuffer::putChar at 30 (line 482)

swap(x) { return (char)(Integer.reverseBytes(x) >>> 16); }:
   0x00b8965d: bswap  %edx
   0x00b8965f: shr    $0x10,%edx
   ...
   0x00b8966c: mov    %dx,(%eax)         ;*invokevirtual putChar
                                         ; - 
java.nio.DirectByteBuffer::putChar at 30 (line 482)

... but best would be:
   0x00b89667: movbe    %dx,(%eax)         ;*invokevirtual putChar
                                         ; - 
java.nio.DirectByteBuffer::putChar at 30 (line 482)

swap(x) { return Character.reverseBytes(x); }:
   0x00b9365b: mov    %edx,%ebx
   0x00b9365d: shr    $0x8,%ebx
   0x00b93660: shl    $0x8,%edx
   0x00b93663: or     %edx,%ebx
   0x00b93665: and    $0xffff,%ebx   // superfluous, as only 16 bits 
were saved later
   ...
   0x00b93675: mov    %bx,(%eax)         ;*invokevirtual putChar
                                         ; - 
java.nio.DirectByteBuffer::putChar at 30 (line 482)

So can anybody file a bug to have intrinsic methods for 
Character.reverseBytes and Short.reverseBytes ?

-Ulf




More information about the nio-discuss mailing list