jdk/src/share/classes/java/io/Bits.java
author martin
Wed, 30 Jun 2010 16:11:32 -0700
changeset 5993 47d52d67b5b2
parent 5506 202f599c92aa
child 7668 d4a77089c587
permissions -rw-r--r--
6935172: Optimize bit-twiddling in Bits.java Summary: Transformations to reduce size of bytecode Reviewed-by: sherman Contributed-by: Based on an idea by Ulf Zibis <ulf.zibis@gmx.de>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Utility methods for packing/unpacking primitive values in/out of byte arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * using big-endian byte ordering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
class Bits {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
     * Methods for unpacking primitive values from byte arrays starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
     * given offsets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static boolean getBoolean(byte[] b, int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        return b[off] != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static char getChar(byte[] b, int off) {
5993
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
    44
        return (char) ((b[off + 1] & 0xFF) +
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
    45
                       (b[off] << 8));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static short getShort(byte[] b, int off) {
5993
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
    49
        return (short) ((b[off + 1] & 0xFF) +
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
    50
                        (b[off] << 8));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static int getInt(byte[] b, int off) {
5993
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
    54
        return ((b[off + 3] & 0xFF)      ) +
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
    55
               ((b[off + 2] & 0xFF) <<  8) +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
               ((b[off + 1] & 0xFF) << 16) +
5993
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
    57
               ((b[off    ]       ) << 24);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static float getFloat(byte[] b, int off) {
5993
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
    61
        return Float.intBitsToFloat(getInt(b, off));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static long getLong(byte[] b, int off) {
5993
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
    65
        return ((b[off + 7] & 0xFFL)      ) +
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
    66
               ((b[off + 6] & 0xFFL) <<  8) +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
               ((b[off + 5] & 0xFFL) << 16) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
               ((b[off + 4] & 0xFFL) << 24) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
               ((b[off + 3] & 0xFFL) << 32) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
               ((b[off + 2] & 0xFFL) << 40) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
               ((b[off + 1] & 0xFFL) << 48) +
5993
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
    72
               (((long) b[off])      << 56);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static double getDouble(byte[] b, int off) {
5993
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
    76
        return Double.longBitsToDouble(getLong(b, off));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Methods for packing primitive values into byte arrays starting at given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * offsets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static void putBoolean(byte[] b, int off, boolean val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        b[off] = (byte) (val ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    static void putChar(byte[] b, int off, char val) {
5993
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
    89
        b[off + 1] = (byte) (val      );
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
    90
        b[off    ] = (byte) (val >>> 8);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    static void putShort(byte[] b, int off, short val) {
5993
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
    94
        b[off + 1] = (byte) (val      );
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
    95
        b[off    ] = (byte) (val >>> 8);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    static void putInt(byte[] b, int off, int val) {
5993
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
    99
        b[off + 3] = (byte) (val       );
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
   100
        b[off + 2] = (byte) (val >>>  8);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        b[off + 1] = (byte) (val >>> 16);
5993
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
   102
        b[off    ] = (byte) (val >>> 24);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    static void putFloat(byte[] b, int off, float val) {
5993
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
   106
        putInt(b, off,  Float.floatToIntBits(val));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    static void putLong(byte[] b, int off, long val) {
5993
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
   110
        b[off + 7] = (byte) (val       );
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
   111
        b[off + 6] = (byte) (val >>>  8);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        b[off + 5] = (byte) (val >>> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        b[off + 4] = (byte) (val >>> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        b[off + 3] = (byte) (val >>> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        b[off + 2] = (byte) (val >>> 40);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        b[off + 1] = (byte) (val >>> 48);
5993
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
   117
        b[off    ] = (byte) (val >>> 56);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    static void putDouble(byte[] b, int off, double val) {
5993
47d52d67b5b2 6935172: Optimize bit-twiddling in Bits.java
martin
parents: 5506
diff changeset
   121
        putLong(b, off, Double.doubleToLongBits(val));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
}