src/share/vm/code/compressedStream.cpp

changeset 435
a61af66fc99e
child 1907
c18cbe5936b8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/code/compressedStream.cpp	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,281 @@
     1.4 +/*
     1.5 + * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "incls/_precompiled.incl"
    1.29 +#include "incls/_compressedStream.cpp.incl"
    1.30 +
    1.31 +// 32-bit one-to-one sign encoding taken from Pack200
    1.32 +// converts leading sign bits into leading zeroes with trailing sign bit
    1.33 +inline juint CompressedStream::encode_sign(jint  value) {
    1.34 +  return (value << 1) ^ (value >> 31);
    1.35 +}
    1.36 +inline jint  CompressedStream::decode_sign(juint value) {
    1.37 +  return (value >> 1) ^ -(jint)(value & 1);
    1.38 +}
    1.39 +
    1.40 +// 32-bit self-inverse encoding of float bits
    1.41 +// converts trailing zeroes (common in floats) to leading zeroes
    1.42 +inline juint CompressedStream::reverse_int(juint i) {
    1.43 +  // Hacker's Delight, Figure 7-1
    1.44 +  i = (i & 0x55555555) << 1 | (i >> 1) & 0x55555555;
    1.45 +  i = (i & 0x33333333) << 2 | (i >> 2) & 0x33333333;
    1.46 +  i = (i & 0x0f0f0f0f) << 4 | (i >> 4) & 0x0f0f0f0f;
    1.47 +  i = (i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | (i >> 24);
    1.48 +  return i;
    1.49 +}
    1.50 +
    1.51 +
    1.52 +jint CompressedReadStream::read_signed_int() {
    1.53 +  return decode_sign(read_int());
    1.54 +}
    1.55 +
    1.56 +// Compressing floats is simple, because the only common pattern
    1.57 +// is trailing zeroes.  (Compare leading sign bits on ints.)
    1.58 +// Since floats are left-justified, as opposed to right-justified
    1.59 +// ints, we can bit-reverse them in order to take advantage of int
    1.60 +// compression.
    1.61 +
    1.62 +jfloat CompressedReadStream::read_float() {
    1.63 +  int rf = read_int();
    1.64 +  int f  = reverse_int(rf);
    1.65 +  return jfloat_cast(f);
    1.66 +}
    1.67 +
    1.68 +jdouble CompressedReadStream::read_double() {
    1.69 +  jint rh = read_int();
    1.70 +  jint rl = read_int();
    1.71 +  jint h  = reverse_int(rh);
    1.72 +  jint l  = reverse_int(rl);
    1.73 +  return jdouble_cast(jlong_from(h, l));
    1.74 +}
    1.75 +
    1.76 +jlong CompressedReadStream::read_long() {
    1.77 +  jint low  = read_signed_int();
    1.78 +  jint high = read_signed_int();
    1.79 +  return jlong_from(high, low);
    1.80 +}
    1.81 +
    1.82 +CompressedWriteStream::CompressedWriteStream(int initial_size) : CompressedStream(NULL, 0) {
    1.83 +  _buffer   = NEW_RESOURCE_ARRAY(u_char, initial_size);
    1.84 +  _size     = initial_size;
    1.85 +  _position = 0;
    1.86 +}
    1.87 +
    1.88 +void CompressedWriteStream::grow() {
    1.89 +  u_char* _new_buffer = NEW_RESOURCE_ARRAY(u_char, _size * 2);
    1.90 +  memcpy(_new_buffer, _buffer, _position);
    1.91 +  _buffer = _new_buffer;
    1.92 +  _size   = _size * 2;
    1.93 +}
    1.94 +
    1.95 +void CompressedWriteStream::write_signed_int(jint value) {
    1.96 +  // this encoding, called SIGNED5, is taken from Pack200
    1.97 +  write_int(encode_sign(value));
    1.98 +}
    1.99 +
   1.100 +void CompressedWriteStream::write_float(jfloat value) {
   1.101 +  juint f = jint_cast(value);
   1.102 +  juint rf = reverse_int(f);
   1.103 +  assert(f == reverse_int(rf), "can re-read same bits");
   1.104 +  write_int(rf);
   1.105 +}
   1.106 +
   1.107 +void CompressedWriteStream::write_double(jdouble value) {
   1.108 +  juint h  = high(jlong_cast(value));
   1.109 +  juint l  = low( jlong_cast(value));
   1.110 +  juint rh = reverse_int(h);
   1.111 +  juint rl = reverse_int(l);
   1.112 +  assert(h == reverse_int(rh), "can re-read same bits");
   1.113 +  assert(l == reverse_int(rl), "can re-read same bits");
   1.114 +  write_int(rh);
   1.115 +  write_int(rl);
   1.116 +}
   1.117 +
   1.118 +void CompressedWriteStream::write_long(jlong value) {
   1.119 +  write_signed_int(low(value));
   1.120 +  write_signed_int(high(value));
   1.121 +}
   1.122 +
   1.123 +
   1.124 +/// The remaining details
   1.125 +
   1.126 +#ifndef PRODUCT
   1.127 +// set this to trigger unit test
   1.128 +void test_compressed_stream(int trace);
   1.129 +bool test_compressed_stream_enabled = false;
   1.130 +#endif
   1.131 +
   1.132 +// This encoding, called UNSIGNED5, is taken from J2SE Pack200.
   1.133 +// It assumes that most values have lots of leading zeroes.
   1.134 +// Very small values, in the range [0..191], code in one byte.
   1.135 +// Any 32-bit value (including negatives) can be coded, in
   1.136 +// up to five bytes.  The grammar is:
   1.137 +//    low_byte  = [0..191]
   1.138 +//    high_byte = [192..255]
   1.139 +//    any_byte  = low_byte | high_byte
   1.140 +//    coding = low_byte
   1.141 +//           | high_byte low_byte
   1.142 +//           | high_byte high_byte low_byte
   1.143 +//           | high_byte high_byte high_byte low_byte
   1.144 +//           | high_byte high_byte high_byte high_byte any_byte
   1.145 +// Each high_byte contributes six bits of payload.
   1.146 +// The encoding is one-to-one (except for integer overflow)
   1.147 +// and easy to parse and unparse.
   1.148 +
   1.149 +jint CompressedReadStream::read_int_mb(jint b0) {
   1.150 +  int     pos = position() - 1;
   1.151 +  u_char* buf = buffer() + pos;
   1.152 +  assert(buf[0] == b0 && b0 >= L, "correctly called");
   1.153 +  jint    sum = b0;
   1.154 +  // must collect more bytes:  b[1]...b[4]
   1.155 +  int lg_H_i = lg_H;
   1.156 +  for (int i = 0; ; ) {
   1.157 +    jint b_i = buf[++i]; // b_i = read(); ++i;
   1.158 +    sum += b_i << lg_H_i;  // sum += b[i]*(64**i)
   1.159 +    if (b_i < L || i == MAX_i) {
   1.160 +      set_position(pos+i+1);
   1.161 +      return sum;
   1.162 +    }
   1.163 +    lg_H_i += lg_H;
   1.164 +  }
   1.165 +}
   1.166 +
   1.167 +void CompressedWriteStream::write_int_mb(jint value) {
   1.168 +  debug_only(int pos1 = position());
   1.169 +  juint sum = value;
   1.170 +  for (int i = 0; ; ) {
   1.171 +    if (sum < L || i == MAX_i) {
   1.172 +      // remainder is either a "low code" or the 5th byte
   1.173 +      assert(sum == (u_char)sum, "valid byte");
   1.174 +      write((u_char)sum);
   1.175 +      break;
   1.176 +    }
   1.177 +    sum -= L;
   1.178 +    int b_i = L + (sum % H);  // this is a "high code"
   1.179 +    sum >>= lg_H;             // extracted 6 bits
   1.180 +    write(b_i); ++i;
   1.181 +  }
   1.182 +
   1.183 +#ifndef PRODUCT
   1.184 +  if (test_compressed_stream_enabled) {  // hack to enable this stress test
   1.185 +    test_compressed_stream_enabled = false;
   1.186 +    test_compressed_stream(0);
   1.187 +  }
   1.188 +#endif
   1.189 +}
   1.190 +
   1.191 +
   1.192 +#ifndef PRODUCT
   1.193 +/// a unit test (can be run by hand from a debugger)
   1.194 +
   1.195 +// Avoid a VS2005 compiler stack overflow w/ fastdebug build.
   1.196 +// The following pragma optimize turns off optimization ONLY
   1.197 +// for this block (a matching directive turns it back on later).
   1.198 +// These directives can be removed once the MS VS.NET 2005
   1.199 +// compiler stack overflow is fixed.
   1.200 +#if _MSC_VER >=1400 && !defined(_WIN64)
   1.201 +#pragma optimize("", off)
   1.202 +#endif
   1.203 +
   1.204 +// generator for an "interesting" set of critical values
   1.205 +enum { stretch_limit = (1<<16) * (64-16+1) };
   1.206 +static jlong stretch(jint x, int bits) {
   1.207 +  // put x[high 4] into place
   1.208 +  jlong h = (jlong)((x >> (16-4))) << (bits - 4);
   1.209 +  // put x[low 12] into place, sign extended
   1.210 +  jlong l = ((jlong)x << (64-12)) >> (64-12);
   1.211 +  // move l upwards, maybe
   1.212 +  l <<= (x >> 16);
   1.213 +  return h ^ l;
   1.214 +}
   1.215 +
   1.216 +void test_compressed_stream(int trace) {
   1.217 +  CompressedWriteStream bytes(stretch_limit * 100);
   1.218 +  jint n;
   1.219 +  int step = 0, fails = 0;
   1.220 +#define CHECKXY(x, y, fmt) { \
   1.221 +    ++step; \
   1.222 +    int xlen = (pos = decode.position()) - lastpos; lastpos = pos; \
   1.223 +    if (trace > 0 && (step % trace) == 0) { \
   1.224 +      tty->print_cr("step %d, n=%08x: value=" fmt " (len=%d)", \
   1.225 +                    step, n, x, xlen); } \
   1.226 +    if (x != y) {                                                     \
   1.227 +      tty->print_cr("step %d, n=%d: " fmt " != " fmt, step, n, x, y); \
   1.228 +      fails++; \
   1.229 +    } }
   1.230 +  for (n = 0; n < (1<<8); n++) {
   1.231 +    jbyte x = (jbyte)n;
   1.232 +    bytes.write_byte(x); ++step;
   1.233 +  }
   1.234 +  for (n = 0; n < stretch_limit; n++) {
   1.235 +    jint x = (jint)stretch(n, 32);
   1.236 +    bytes.write_int(x); ++step;
   1.237 +    bytes.write_signed_int(x); ++step;
   1.238 +    bytes.write_float(jfloat_cast(x)); ++step;
   1.239 +  }
   1.240 +  for (n = 0; n < stretch_limit; n++) {
   1.241 +    jlong x = stretch(n, 64);
   1.242 +    bytes.write_long(x); ++step;
   1.243 +    bytes.write_double(jdouble_cast(x)); ++step;
   1.244 +  }
   1.245 +  int length = bytes.position();
   1.246 +  if (trace != 0)
   1.247 +    tty->print_cr("set up test of %d stream values, size %d", step, length);
   1.248 +  step = 0;
   1.249 +  // now decode it all
   1.250 +  CompressedReadStream decode(bytes.buffer());
   1.251 +  int pos, lastpos = decode.position();
   1.252 +  for (n = 0; n < (1<<8); n++) {
   1.253 +    jbyte x = (jbyte)n;
   1.254 +    jbyte y = decode.read_byte();
   1.255 +    CHECKXY(x, y, "%db");
   1.256 +  }
   1.257 +  for (n = 0; n < stretch_limit; n++) {
   1.258 +    jint x = (jint)stretch(n, 32);
   1.259 +    jint y1 = decode.read_int();
   1.260 +    CHECKXY(x, y1, "%du");
   1.261 +    jint y2 = decode.read_signed_int();
   1.262 +    CHECKXY(x, y2, "%di");
   1.263 +    jint y3 = jint_cast(decode.read_float());
   1.264 +    CHECKXY(x, y3, "%df");
   1.265 +  }
   1.266 +  for (n = 0; n < stretch_limit; n++) {
   1.267 +    jlong x = stretch(n, 64);
   1.268 +    jlong y1 = decode.read_long();
   1.269 +    CHECKXY(x, y1, INT64_FORMAT "l");
   1.270 +    jlong y2 = jlong_cast(decode.read_double());
   1.271 +    CHECKXY(x, y2, INT64_FORMAT "d");
   1.272 +  }
   1.273 +  int length2 = decode.position();
   1.274 +  if (trace != 0)
   1.275 +    tty->print_cr("finished test of %d stream values, size %d", step, length2);
   1.276 +  guarantee(length == length2, "bad length");
   1.277 +  guarantee(fails == 0, "test failures");
   1.278 +}
   1.279 +
   1.280 +#if _MSC_VER >=1400 && !defined(_WIN64)
   1.281 +#pragma optimize("", on)
   1.282 +#endif
   1.283 +
   1.284 +#endif // PRODUCT

mercurial