src/share/vm/code/compressedStream.cpp

Fri, 29 Jan 2010 08:33:24 -0800

author
twisti
date
Fri, 29 Jan 2010 08:33:24 -0800
changeset 1636
24128c2ffa87
parent 435
a61af66fc99e
child 1907
c18cbe5936b8
permissions
-rw-r--r--

6921339: backout 6917766
Reviewed-by: mr

duke@435 1 /*
duke@435 2 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 #include "incls/_precompiled.incl"
duke@435 26 #include "incls/_compressedStream.cpp.incl"
duke@435 27
duke@435 28 // 32-bit one-to-one sign encoding taken from Pack200
duke@435 29 // converts leading sign bits into leading zeroes with trailing sign bit
duke@435 30 inline juint CompressedStream::encode_sign(jint value) {
duke@435 31 return (value << 1) ^ (value >> 31);
duke@435 32 }
duke@435 33 inline jint CompressedStream::decode_sign(juint value) {
duke@435 34 return (value >> 1) ^ -(jint)(value & 1);
duke@435 35 }
duke@435 36
duke@435 37 // 32-bit self-inverse encoding of float bits
duke@435 38 // converts trailing zeroes (common in floats) to leading zeroes
duke@435 39 inline juint CompressedStream::reverse_int(juint i) {
duke@435 40 // Hacker's Delight, Figure 7-1
duke@435 41 i = (i & 0x55555555) << 1 | (i >> 1) & 0x55555555;
duke@435 42 i = (i & 0x33333333) << 2 | (i >> 2) & 0x33333333;
duke@435 43 i = (i & 0x0f0f0f0f) << 4 | (i >> 4) & 0x0f0f0f0f;
duke@435 44 i = (i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | (i >> 24);
duke@435 45 return i;
duke@435 46 }
duke@435 47
duke@435 48
duke@435 49 jint CompressedReadStream::read_signed_int() {
duke@435 50 return decode_sign(read_int());
duke@435 51 }
duke@435 52
duke@435 53 // Compressing floats is simple, because the only common pattern
duke@435 54 // is trailing zeroes. (Compare leading sign bits on ints.)
duke@435 55 // Since floats are left-justified, as opposed to right-justified
duke@435 56 // ints, we can bit-reverse them in order to take advantage of int
duke@435 57 // compression.
duke@435 58
duke@435 59 jfloat CompressedReadStream::read_float() {
duke@435 60 int rf = read_int();
duke@435 61 int f = reverse_int(rf);
duke@435 62 return jfloat_cast(f);
duke@435 63 }
duke@435 64
duke@435 65 jdouble CompressedReadStream::read_double() {
duke@435 66 jint rh = read_int();
duke@435 67 jint rl = read_int();
duke@435 68 jint h = reverse_int(rh);
duke@435 69 jint l = reverse_int(rl);
duke@435 70 return jdouble_cast(jlong_from(h, l));
duke@435 71 }
duke@435 72
duke@435 73 jlong CompressedReadStream::read_long() {
duke@435 74 jint low = read_signed_int();
duke@435 75 jint high = read_signed_int();
duke@435 76 return jlong_from(high, low);
duke@435 77 }
duke@435 78
duke@435 79 CompressedWriteStream::CompressedWriteStream(int initial_size) : CompressedStream(NULL, 0) {
duke@435 80 _buffer = NEW_RESOURCE_ARRAY(u_char, initial_size);
duke@435 81 _size = initial_size;
duke@435 82 _position = 0;
duke@435 83 }
duke@435 84
duke@435 85 void CompressedWriteStream::grow() {
duke@435 86 u_char* _new_buffer = NEW_RESOURCE_ARRAY(u_char, _size * 2);
duke@435 87 memcpy(_new_buffer, _buffer, _position);
duke@435 88 _buffer = _new_buffer;
duke@435 89 _size = _size * 2;
duke@435 90 }
duke@435 91
duke@435 92 void CompressedWriteStream::write_signed_int(jint value) {
duke@435 93 // this encoding, called SIGNED5, is taken from Pack200
duke@435 94 write_int(encode_sign(value));
duke@435 95 }
duke@435 96
duke@435 97 void CompressedWriteStream::write_float(jfloat value) {
duke@435 98 juint f = jint_cast(value);
duke@435 99 juint rf = reverse_int(f);
duke@435 100 assert(f == reverse_int(rf), "can re-read same bits");
duke@435 101 write_int(rf);
duke@435 102 }
duke@435 103
duke@435 104 void CompressedWriteStream::write_double(jdouble value) {
duke@435 105 juint h = high(jlong_cast(value));
duke@435 106 juint l = low( jlong_cast(value));
duke@435 107 juint rh = reverse_int(h);
duke@435 108 juint rl = reverse_int(l);
duke@435 109 assert(h == reverse_int(rh), "can re-read same bits");
duke@435 110 assert(l == reverse_int(rl), "can re-read same bits");
duke@435 111 write_int(rh);
duke@435 112 write_int(rl);
duke@435 113 }
duke@435 114
duke@435 115 void CompressedWriteStream::write_long(jlong value) {
duke@435 116 write_signed_int(low(value));
duke@435 117 write_signed_int(high(value));
duke@435 118 }
duke@435 119
duke@435 120
duke@435 121 /// The remaining details
duke@435 122
duke@435 123 #ifndef PRODUCT
duke@435 124 // set this to trigger unit test
duke@435 125 void test_compressed_stream(int trace);
duke@435 126 bool test_compressed_stream_enabled = false;
duke@435 127 #endif
duke@435 128
duke@435 129 // This encoding, called UNSIGNED5, is taken from J2SE Pack200.
duke@435 130 // It assumes that most values have lots of leading zeroes.
duke@435 131 // Very small values, in the range [0..191], code in one byte.
duke@435 132 // Any 32-bit value (including negatives) can be coded, in
duke@435 133 // up to five bytes. The grammar is:
duke@435 134 // low_byte = [0..191]
duke@435 135 // high_byte = [192..255]
duke@435 136 // any_byte = low_byte | high_byte
duke@435 137 // coding = low_byte
duke@435 138 // | high_byte low_byte
duke@435 139 // | high_byte high_byte low_byte
duke@435 140 // | high_byte high_byte high_byte low_byte
duke@435 141 // | high_byte high_byte high_byte high_byte any_byte
duke@435 142 // Each high_byte contributes six bits of payload.
duke@435 143 // The encoding is one-to-one (except for integer overflow)
duke@435 144 // and easy to parse and unparse.
duke@435 145
duke@435 146 jint CompressedReadStream::read_int_mb(jint b0) {
duke@435 147 int pos = position() - 1;
duke@435 148 u_char* buf = buffer() + pos;
duke@435 149 assert(buf[0] == b0 && b0 >= L, "correctly called");
duke@435 150 jint sum = b0;
duke@435 151 // must collect more bytes: b[1]...b[4]
duke@435 152 int lg_H_i = lg_H;
duke@435 153 for (int i = 0; ; ) {
duke@435 154 jint b_i = buf[++i]; // b_i = read(); ++i;
duke@435 155 sum += b_i << lg_H_i; // sum += b[i]*(64**i)
duke@435 156 if (b_i < L || i == MAX_i) {
duke@435 157 set_position(pos+i+1);
duke@435 158 return sum;
duke@435 159 }
duke@435 160 lg_H_i += lg_H;
duke@435 161 }
duke@435 162 }
duke@435 163
duke@435 164 void CompressedWriteStream::write_int_mb(jint value) {
duke@435 165 debug_only(int pos1 = position());
duke@435 166 juint sum = value;
duke@435 167 for (int i = 0; ; ) {
duke@435 168 if (sum < L || i == MAX_i) {
duke@435 169 // remainder is either a "low code" or the 5th byte
duke@435 170 assert(sum == (u_char)sum, "valid byte");
duke@435 171 write((u_char)sum);
duke@435 172 break;
duke@435 173 }
duke@435 174 sum -= L;
duke@435 175 int b_i = L + (sum % H); // this is a "high code"
duke@435 176 sum >>= lg_H; // extracted 6 bits
duke@435 177 write(b_i); ++i;
duke@435 178 }
duke@435 179
duke@435 180 #ifndef PRODUCT
duke@435 181 if (test_compressed_stream_enabled) { // hack to enable this stress test
duke@435 182 test_compressed_stream_enabled = false;
duke@435 183 test_compressed_stream(0);
duke@435 184 }
duke@435 185 #endif
duke@435 186 }
duke@435 187
duke@435 188
duke@435 189 #ifndef PRODUCT
duke@435 190 /// a unit test (can be run by hand from a debugger)
duke@435 191
duke@435 192 // Avoid a VS2005 compiler stack overflow w/ fastdebug build.
duke@435 193 // The following pragma optimize turns off optimization ONLY
duke@435 194 // for this block (a matching directive turns it back on later).
duke@435 195 // These directives can be removed once the MS VS.NET 2005
duke@435 196 // compiler stack overflow is fixed.
duke@435 197 #if _MSC_VER >=1400 && !defined(_WIN64)
duke@435 198 #pragma optimize("", off)
duke@435 199 #endif
duke@435 200
duke@435 201 // generator for an "interesting" set of critical values
duke@435 202 enum { stretch_limit = (1<<16) * (64-16+1) };
duke@435 203 static jlong stretch(jint x, int bits) {
duke@435 204 // put x[high 4] into place
duke@435 205 jlong h = (jlong)((x >> (16-4))) << (bits - 4);
duke@435 206 // put x[low 12] into place, sign extended
duke@435 207 jlong l = ((jlong)x << (64-12)) >> (64-12);
duke@435 208 // move l upwards, maybe
duke@435 209 l <<= (x >> 16);
duke@435 210 return h ^ l;
duke@435 211 }
duke@435 212
duke@435 213 void test_compressed_stream(int trace) {
duke@435 214 CompressedWriteStream bytes(stretch_limit * 100);
duke@435 215 jint n;
duke@435 216 int step = 0, fails = 0;
duke@435 217 #define CHECKXY(x, y, fmt) { \
duke@435 218 ++step; \
duke@435 219 int xlen = (pos = decode.position()) - lastpos; lastpos = pos; \
duke@435 220 if (trace > 0 && (step % trace) == 0) { \
duke@435 221 tty->print_cr("step %d, n=%08x: value=" fmt " (len=%d)", \
duke@435 222 step, n, x, xlen); } \
duke@435 223 if (x != y) { \
duke@435 224 tty->print_cr("step %d, n=%d: " fmt " != " fmt, step, n, x, y); \
duke@435 225 fails++; \
duke@435 226 } }
duke@435 227 for (n = 0; n < (1<<8); n++) {
duke@435 228 jbyte x = (jbyte)n;
duke@435 229 bytes.write_byte(x); ++step;
duke@435 230 }
duke@435 231 for (n = 0; n < stretch_limit; n++) {
duke@435 232 jint x = (jint)stretch(n, 32);
duke@435 233 bytes.write_int(x); ++step;
duke@435 234 bytes.write_signed_int(x); ++step;
duke@435 235 bytes.write_float(jfloat_cast(x)); ++step;
duke@435 236 }
duke@435 237 for (n = 0; n < stretch_limit; n++) {
duke@435 238 jlong x = stretch(n, 64);
duke@435 239 bytes.write_long(x); ++step;
duke@435 240 bytes.write_double(jdouble_cast(x)); ++step;
duke@435 241 }
duke@435 242 int length = bytes.position();
duke@435 243 if (trace != 0)
duke@435 244 tty->print_cr("set up test of %d stream values, size %d", step, length);
duke@435 245 step = 0;
duke@435 246 // now decode it all
duke@435 247 CompressedReadStream decode(bytes.buffer());
duke@435 248 int pos, lastpos = decode.position();
duke@435 249 for (n = 0; n < (1<<8); n++) {
duke@435 250 jbyte x = (jbyte)n;
duke@435 251 jbyte y = decode.read_byte();
duke@435 252 CHECKXY(x, y, "%db");
duke@435 253 }
duke@435 254 for (n = 0; n < stretch_limit; n++) {
duke@435 255 jint x = (jint)stretch(n, 32);
duke@435 256 jint y1 = decode.read_int();
duke@435 257 CHECKXY(x, y1, "%du");
duke@435 258 jint y2 = decode.read_signed_int();
duke@435 259 CHECKXY(x, y2, "%di");
duke@435 260 jint y3 = jint_cast(decode.read_float());
duke@435 261 CHECKXY(x, y3, "%df");
duke@435 262 }
duke@435 263 for (n = 0; n < stretch_limit; n++) {
duke@435 264 jlong x = stretch(n, 64);
duke@435 265 jlong y1 = decode.read_long();
duke@435 266 CHECKXY(x, y1, INT64_FORMAT "l");
duke@435 267 jlong y2 = jlong_cast(decode.read_double());
duke@435 268 CHECKXY(x, y2, INT64_FORMAT "d");
duke@435 269 }
duke@435 270 int length2 = decode.position();
duke@435 271 if (trace != 0)
duke@435 272 tty->print_cr("finished test of %d stream values, size %d", step, length2);
duke@435 273 guarantee(length == length2, "bad length");
duke@435 274 guarantee(fails == 0, "test failures");
duke@435 275 }
duke@435 276
duke@435 277 #if _MSC_VER >=1400 && !defined(_WIN64)
duke@435 278 #pragma optimize("", on)
duke@435 279 #endif
duke@435 280
duke@435 281 #endif // PRODUCT

mercurial