6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
authormartin
Wed, 30 Jun 2010 16:11:32 -0700
changeset 5994 267f4ce6a585
parent 5993 47d52d67b5b2
child 5995 0b76e67c2054
child 6869 7c6ddf135745
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs Reviewed-by: alanb, sherman Contributed-by: Ulf Zibis <ulf.zibis@gmx.de>
jdk/src/share/classes/java/nio/Bits.java
--- a/jdk/src/share/classes/java/nio/Bits.java	Wed Jun 30 16:11:32 2010 -0700
+++ b/jdk/src/share/classes/java/nio/Bits.java	Wed Jun 30 16:11:32 2010 -0700
@@ -41,25 +41,19 @@
     // -- Swapping --
 
     static short swap(short x) {
-        return (short)((x << 8) |
-                       ((char)x >>> 8));
+        return Short.reverseBytes(x);
     }
 
     static char swap(char x) {
-        return (char)((x << 8) |
-                      (x >>> 8));
+        return Character.reverseBytes(x);
     }
 
     static int swap(int x) {
-        return ((x << 24) |
-                ((x & 0x0000ff00) <<  8) |
-                ((x & 0x00ff0000) >>> 8) |
-                (x >>> 24));
+        return Integer.reverseBytes(x);
     }
 
     static long swap(long x) {
-        return (((long)swap((int)x) << 32) |
-                ((long)swap((int)(x >>> 32)) & 0xffffffffL));
+        return Long.reverseBytes(x);
     }
 
 
@@ -71,52 +65,52 @@
 
     static char getCharL(ByteBuffer bb, int bi) {
         return makeChar(bb._get(bi + 1),
-                        bb._get(bi + 0));
+                        bb._get(bi    ));
     }
 
     static char getCharL(long a) {
         return makeChar(_get(a + 1),
-                        _get(a + 0));
+                        _get(a    ));
     }
 
     static char getCharB(ByteBuffer bb, int bi) {
-        return makeChar(bb._get(bi + 0),
+        return makeChar(bb._get(bi    ),
                         bb._get(bi + 1));
     }
 
     static char getCharB(long a) {
-        return makeChar(_get(a + 0),
+        return makeChar(_get(a    ),
                         _get(a + 1));
     }
 
     static char getChar(ByteBuffer bb, int bi, boolean bigEndian) {
-        return (bigEndian ? getCharB(bb, bi) : getCharL(bb, bi));
+        return bigEndian ? getCharB(bb, bi) : getCharL(bb, bi);
     }
 
     static char getChar(long a, boolean bigEndian) {
-        return (bigEndian ? getCharB(a) : getCharL(a));
+        return bigEndian ? getCharB(a) : getCharL(a);
     }
 
     private static byte char1(char x) { return (byte)(x >> 8); }
-    private static byte char0(char x) { return (byte)(x >> 0); }
+    private static byte char0(char x) { return (byte)(x     ); }
 
     static void putCharL(ByteBuffer bb, int bi, char x) {
-        bb._put(bi + 0, char0(x));
+        bb._put(bi    , char0(x));
         bb._put(bi + 1, char1(x));
     }
 
     static void putCharL(long a, char x) {
-        _put(a + 0, char0(x));
+        _put(a    , char0(x));
         _put(a + 1, char1(x));
     }
 
     static void putCharB(ByteBuffer bb, int bi, char x) {
-        bb._put(bi + 0, char1(x));
+        bb._put(bi    , char1(x));
         bb._put(bi + 1, char0(x));
     }
 
     static void putCharB(long a, char x) {
-        _put(a + 0, char1(x));
+        _put(a    , char1(x));
         _put(a + 1, char0(x));
     }
 
@@ -143,52 +137,52 @@
 
     static short getShortL(ByteBuffer bb, int bi) {
         return makeShort(bb._get(bi + 1),
-                         bb._get(bi + 0));
+                         bb._get(bi    ));
     }
 
     static short getShortL(long a) {
         return makeShort(_get(a + 1),
-                         _get(a));
+                         _get(a    ));
     }
 
     static short getShortB(ByteBuffer bb, int bi) {
-        return makeShort(bb._get(bi + 0),
+        return makeShort(bb._get(bi    ),
                          bb._get(bi + 1));
     }
 
     static short getShortB(long a) {
-        return makeShort(_get(a),
+        return makeShort(_get(a    ),
                          _get(a + 1));
     }
 
     static short getShort(ByteBuffer bb, int bi, boolean bigEndian) {
-        return (bigEndian ? getShortB(bb, bi) : getShortL(bb, bi));
+        return bigEndian ? getShortB(bb, bi) : getShortL(bb, bi);
     }
 
     static short getShort(long a, boolean bigEndian) {
-        return (bigEndian ? getShortB(a) : getShortL(a));
+        return bigEndian ? getShortB(a) : getShortL(a);
     }
 
     private static byte short1(short x) { return (byte)(x >> 8); }
-    private static byte short0(short x) { return (byte)(x >> 0); }
+    private static byte short0(short x) { return (byte)(x     ); }
 
     static void putShortL(ByteBuffer bb, int bi, short x) {
-        bb._put(bi + 0, short0(x));
+        bb._put(bi    , short0(x));
         bb._put(bi + 1, short1(x));
     }
 
     static void putShortL(long a, short x) {
-        _put(a, short0(x));
+        _put(a    , short0(x));
         _put(a + 1, short1(x));
     }
 
     static void putShortB(ByteBuffer bb, int bi, short x) {
-        bb._put(bi + 0, short1(x));
+        bb._put(bi    , short1(x));
         bb._put(bi + 1, short0(x));
     }
 
     static void putShortB(long a, short x) {
-        _put(a, short1(x));
+        _put(a    , short1(x));
         _put(a + 1, short0(x));
     }
 
@@ -210,76 +204,76 @@
     // -- get/put int --
 
     static private int makeInt(byte b3, byte b2, byte b1, byte b0) {
-        return (((b3 & 0xff) << 24) |
+        return (((b3       ) << 24) |
                 ((b2 & 0xff) << 16) |
                 ((b1 & 0xff) <<  8) |
-                ((b0 & 0xff) <<  0));
+                ((b0 & 0xff)      ));
     }
 
     static int getIntL(ByteBuffer bb, int bi) {
         return makeInt(bb._get(bi + 3),
                        bb._get(bi + 2),
                        bb._get(bi + 1),
-                       bb._get(bi + 0));
+                       bb._get(bi    ));
     }
 
     static int getIntL(long a) {
         return makeInt(_get(a + 3),
                        _get(a + 2),
                        _get(a + 1),
-                       _get(a + 0));
+                       _get(a    ));
     }
 
     static int getIntB(ByteBuffer bb, int bi) {
-        return makeInt(bb._get(bi + 0),
+        return makeInt(bb._get(bi    ),
                        bb._get(bi + 1),
                        bb._get(bi + 2),
                        bb._get(bi + 3));
     }
 
     static int getIntB(long a) {
-        return makeInt(_get(a + 0),
+        return makeInt(_get(a    ),
                        _get(a + 1),
                        _get(a + 2),
                        _get(a + 3));
     }
 
     static int getInt(ByteBuffer bb, int bi, boolean bigEndian) {
-        return (bigEndian ? getIntB(bb, bi) : getIntL(bb, bi));
+        return bigEndian ? getIntB(bb, bi) : getIntL(bb, bi) ;
     }
 
     static int getInt(long a, boolean bigEndian) {
-        return (bigEndian ? getIntB(a) : getIntL(a));
+        return bigEndian ? getIntB(a) : getIntL(a) ;
     }
 
     private static byte int3(int x) { return (byte)(x >> 24); }
     private static byte int2(int x) { return (byte)(x >> 16); }
     private static byte int1(int x) { return (byte)(x >>  8); }
-    private static byte int0(int x) { return (byte)(x >>  0); }
+    private static byte int0(int x) { return (byte)(x      ); }
 
     static void putIntL(ByteBuffer bb, int bi, int x) {
         bb._put(bi + 3, int3(x));
         bb._put(bi + 2, int2(x));
         bb._put(bi + 1, int1(x));
-        bb._put(bi + 0, int0(x));
+        bb._put(bi    , int0(x));
     }
 
     static void putIntL(long a, int x) {
         _put(a + 3, int3(x));
         _put(a + 2, int2(x));
         _put(a + 1, int1(x));
-        _put(a + 0, int0(x));
+        _put(a    , int0(x));
     }
 
     static void putIntB(ByteBuffer bb, int bi, int x) {
-        bb._put(bi + 0, int3(x));
+        bb._put(bi    , int3(x));
         bb._put(bi + 1, int2(x));
         bb._put(bi + 2, int1(x));
         bb._put(bi + 3, int0(x));
     }
 
     static void putIntB(long a, int x) {
-        _put(a + 0, int3(x));
+        _put(a    , int3(x));
         _put(a + 1, int2(x));
         _put(a + 2, int1(x));
         _put(a + 3, int0(x));
@@ -305,14 +299,14 @@
     static private long makeLong(byte b7, byte b6, byte b5, byte b4,
                                  byte b3, byte b2, byte b1, byte b0)
     {
-        return ((((long)b7 & 0xff) << 56) |
+        return ((((long)b7       ) << 56) |
                 (((long)b6 & 0xff) << 48) |
                 (((long)b5 & 0xff) << 40) |
                 (((long)b4 & 0xff) << 32) |
                 (((long)b3 & 0xff) << 24) |
                 (((long)b2 & 0xff) << 16) |
                 (((long)b1 & 0xff) <<  8) |
-                (((long)b0 & 0xff) <<  0));
+                (((long)b0 & 0xff)      ));
     }
 
     static long getLongL(ByteBuffer bb, int bi) {
@@ -323,7 +317,7 @@
                         bb._get(bi + 3),
                         bb._get(bi + 2),
                         bb._get(bi + 1),
-                        bb._get(bi + 0));
+                        bb._get(bi    ));
     }
 
     static long getLongL(long a) {
@@ -334,11 +328,11 @@
                         _get(a + 3),
                         _get(a + 2),
                         _get(a + 1),
-                        _get(a + 0));
+                        _get(a    ));
     }
 
     static long getLongB(ByteBuffer bb, int bi) {
-        return makeLong(bb._get(bi + 0),
+        return makeLong(bb._get(bi    ),
                         bb._get(bi + 1),
                         bb._get(bi + 2),
                         bb._get(bi + 3),
@@ -349,7 +343,7 @@
     }
 
     static long getLongB(long a) {
-        return makeLong(_get(a + 0),
+        return makeLong(_get(a    ),
                         _get(a + 1),
                         _get(a + 2),
                         _get(a + 3),
@@ -360,11 +354,11 @@
     }
 
     static long getLong(ByteBuffer bb, int bi, boolean bigEndian) {
-        return (bigEndian ? getLongB(bb, bi) : getLongL(bb, bi));
+        return bigEndian ? getLongB(bb, bi) : getLongL(bb, bi);
     }
 
     static long getLong(long a, boolean bigEndian) {
-        return (bigEndian ? getLongB(a) : getLongL(a));
+        return bigEndian ? getLongB(a) : getLongL(a);
     }
 
     private static byte long7(long x) { return (byte)(x >> 56); }
@@ -374,7 +368,7 @@
     private static byte long3(long x) { return (byte)(x >> 24); }
     private static byte long2(long x) { return (byte)(x >> 16); }
     private static byte long1(long x) { return (byte)(x >>  8); }
-    private static byte long0(long x) { return (byte)(x >>  0); }
+    private static byte long0(long x) { return (byte)(x      ); }
 
     static void putLongL(ByteBuffer bb, int bi, long x) {
         bb._put(bi + 7, long7(x));
@@ -384,7 +378,7 @@
         bb._put(bi + 3, long3(x));
         bb._put(bi + 2, long2(x));
         bb._put(bi + 1, long1(x));
-        bb._put(bi + 0, long0(x));
+        bb._put(bi    , long0(x));
     }
 
     static void putLongL(long a, long x) {
@@ -395,11 +389,11 @@
         _put(a + 3, long3(x));
         _put(a + 2, long2(x));
         _put(a + 1, long1(x));
-        _put(a + 0, long0(x));
+        _put(a    , long0(x));
     }
 
     static void putLongB(ByteBuffer bb, int bi, long x) {
-        bb._put(bi + 0, long7(x));
+        bb._put(bi    , long7(x));
         bb._put(bi + 1, long6(x));
         bb._put(bi + 2, long5(x));
         bb._put(bi + 3, long4(x));
@@ -410,7 +404,7 @@
     }
 
     static void putLongB(long a, long x) {
-        _put(a + 0, long7(x));
+        _put(a    , long7(x));
         _put(a + 1, long6(x));
         _put(a + 2, long5(x));
         _put(a + 3, long4(x));
@@ -454,11 +448,11 @@
     }
 
     static float getFloat(ByteBuffer bb, int bi, boolean bigEndian) {
-        return (bigEndian ? getFloatB(bb, bi) : getFloatL(bb, bi));
+        return bigEndian ? getFloatB(bb, bi) : getFloatL(bb, bi);
     }
 
     static float getFloat(long a, boolean bigEndian) {
-        return (bigEndian ? getFloatB(a) : getFloatL(a));
+        return bigEndian ? getFloatB(a) : getFloatL(a);
     }
 
     static void putFloatL(ByteBuffer bb, int bi, float x) {
@@ -511,11 +505,11 @@
     }
 
     static double getDouble(ByteBuffer bb, int bi, boolean bigEndian) {
-        return (bigEndian ? getDoubleB(bb, bi) : getDoubleL(bb, bi));
+        return bigEndian ? getDoubleB(bb, bi) : getDoubleL(bb, bi);
     }
 
     static double getDouble(long a, boolean bigEndian) {
-        return (bigEndian ? getDoubleB(a) : getDoubleL(a));
+        return bigEndian ? getDoubleB(a) : getDoubleL(a);
     }
 
     static void putDoubleL(ByteBuffer bb, int bi, double x) {
@@ -794,7 +788,7 @@
     static native void copyFromIntArray(Object src, long srcPos, long dstAddr,
                                         long length);
     static native void copyToIntArray(long srcAddr, Object dst, long dstPos,
-                                        long length);
+                                      long length);
 
     static native void copyFromLongArray(Object src, long srcPos, long dstAddr,
                                          long length);