aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "code/compressedStream.hpp" aoqi@0: #include "utilities/ostream.hpp" aoqi@0: aoqi@0: // 32-bit one-to-one sign encoding taken from Pack200 aoqi@0: // converts leading sign bits into leading zeroes with trailing sign bit aoqi@0: inline juint CompressedStream::encode_sign(jint value) { aoqi@0: return (value << 1) ^ (value >> 31); aoqi@0: } aoqi@0: inline jint CompressedStream::decode_sign(juint value) { aoqi@0: return (value >> 1) ^ -(jint)(value & 1); aoqi@0: } aoqi@0: aoqi@0: // 32-bit self-inverse encoding of float bits aoqi@0: // converts trailing zeroes (common in floats) to leading zeroes aoqi@0: inline juint CompressedStream::reverse_int(juint i) { aoqi@0: // Hacker's Delight, Figure 7-1 aoqi@0: i = (i & 0x55555555) << 1 | (i >> 1) & 0x55555555; aoqi@0: i = (i & 0x33333333) << 2 | (i >> 2) & 0x33333333; aoqi@0: i = (i & 0x0f0f0f0f) << 4 | (i >> 4) & 0x0f0f0f0f; aoqi@0: i = (i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | (i >> 24); aoqi@0: return i; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: jint CompressedReadStream::read_signed_int() { aoqi@0: return decode_sign(read_int()); aoqi@0: } aoqi@0: aoqi@0: // Compressing floats is simple, because the only common pattern aoqi@0: // is trailing zeroes. (Compare leading sign bits on ints.) aoqi@0: // Since floats are left-justified, as opposed to right-justified aoqi@0: // ints, we can bit-reverse them in order to take advantage of int aoqi@0: // compression. aoqi@0: aoqi@0: jfloat CompressedReadStream::read_float() { aoqi@0: int rf = read_int(); aoqi@0: int f = reverse_int(rf); aoqi@0: return jfloat_cast(f); aoqi@0: } aoqi@0: aoqi@0: jdouble CompressedReadStream::read_double() { aoqi@0: jint rh = read_int(); aoqi@0: jint rl = read_int(); aoqi@0: jint h = reverse_int(rh); aoqi@0: jint l = reverse_int(rl); aoqi@0: return jdouble_cast(jlong_from(h, l)); aoqi@0: } aoqi@0: aoqi@0: jlong CompressedReadStream::read_long() { aoqi@0: jint low = read_signed_int(); aoqi@0: jint high = read_signed_int(); aoqi@0: return jlong_from(high, low); aoqi@0: } aoqi@0: aoqi@0: CompressedWriteStream::CompressedWriteStream(int initial_size) : CompressedStream(NULL, 0) { aoqi@0: _buffer = NEW_RESOURCE_ARRAY(u_char, initial_size); aoqi@0: _size = initial_size; aoqi@0: _position = 0; aoqi@0: } aoqi@0: aoqi@0: void CompressedWriteStream::grow() { aoqi@0: u_char* _new_buffer = NEW_RESOURCE_ARRAY(u_char, _size * 2); aoqi@0: memcpy(_new_buffer, _buffer, _position); aoqi@0: _buffer = _new_buffer; aoqi@0: _size = _size * 2; aoqi@0: } aoqi@0: aoqi@0: void CompressedWriteStream::write_signed_int(jint value) { aoqi@0: // this encoding, called SIGNED5, is taken from Pack200 aoqi@0: write_int(encode_sign(value)); aoqi@0: } aoqi@0: aoqi@0: void CompressedWriteStream::write_float(jfloat value) { aoqi@0: juint f = jint_cast(value); aoqi@0: juint rf = reverse_int(f); aoqi@0: assert(f == reverse_int(rf), "can re-read same bits"); aoqi@0: write_int(rf); aoqi@0: } aoqi@0: aoqi@0: void CompressedWriteStream::write_double(jdouble value) { aoqi@0: juint h = high(jlong_cast(value)); aoqi@0: juint l = low( jlong_cast(value)); aoqi@0: juint rh = reverse_int(h); aoqi@0: juint rl = reverse_int(l); aoqi@0: assert(h == reverse_int(rh), "can re-read same bits"); aoqi@0: assert(l == reverse_int(rl), "can re-read same bits"); aoqi@0: write_int(rh); aoqi@0: write_int(rl); aoqi@0: } aoqi@0: aoqi@0: void CompressedWriteStream::write_long(jlong value) { aoqi@0: write_signed_int(low(value)); aoqi@0: write_signed_int(high(value)); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /// The remaining details aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: // set this to trigger unit test aoqi@0: void test_compressed_stream(int trace); aoqi@0: bool test_compressed_stream_enabled = false; aoqi@0: #endif aoqi@0: aoqi@0: // This encoding, called UNSIGNED5, is taken from J2SE Pack200. aoqi@0: // It assumes that most values have lots of leading zeroes. aoqi@0: // Very small values, in the range [0..191], code in one byte. aoqi@0: // Any 32-bit value (including negatives) can be coded, in aoqi@0: // up to five bytes. The grammar is: aoqi@0: // low_byte = [0..191] aoqi@0: // high_byte = [192..255] aoqi@0: // any_byte = low_byte | high_byte aoqi@0: // coding = low_byte aoqi@0: // | high_byte low_byte aoqi@0: // | high_byte high_byte low_byte aoqi@0: // | high_byte high_byte high_byte low_byte aoqi@0: // | high_byte high_byte high_byte high_byte any_byte aoqi@0: // Each high_byte contributes six bits of payload. aoqi@0: // The encoding is one-to-one (except for integer overflow) aoqi@0: // and easy to parse and unparse. aoqi@0: aoqi@0: jint CompressedReadStream::read_int_mb(jint b0) { aoqi@0: int pos = position() - 1; aoqi@0: u_char* buf = buffer() + pos; aoqi@0: assert(buf[0] == b0 && b0 >= L, "correctly called"); aoqi@0: jint sum = b0; aoqi@0: // must collect more bytes: b[1]...b[4] aoqi@0: int lg_H_i = lg_H; aoqi@0: for (int i = 0; ; ) { aoqi@0: jint b_i = buf[++i]; // b_i = read(); ++i; aoqi@0: sum += b_i << lg_H_i; // sum += b[i]*(64**i) aoqi@0: if (b_i < L || i == MAX_i) { aoqi@0: set_position(pos+i+1); aoqi@0: return sum; aoqi@0: } aoqi@0: lg_H_i += lg_H; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void CompressedWriteStream::write_int_mb(jint value) { aoqi@0: debug_only(int pos1 = position()); aoqi@0: juint sum = value; aoqi@0: for (int i = 0; ; ) { aoqi@0: if (sum < L || i == MAX_i) { aoqi@0: // remainder is either a "low code" or the 5th byte aoqi@0: assert(sum == (u_char)sum, "valid byte"); aoqi@0: write((u_char)sum); aoqi@0: break; aoqi@0: } aoqi@0: sum -= L; aoqi@0: int b_i = L + (sum % H); // this is a "high code" aoqi@0: sum >>= lg_H; // extracted 6 bits aoqi@0: write(b_i); ++i; aoqi@0: } aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: if (test_compressed_stream_enabled) { // hack to enable this stress test aoqi@0: test_compressed_stream_enabled = false; aoqi@0: test_compressed_stream(0); aoqi@0: } aoqi@0: #endif aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: /// a unit test (can be run by hand from a debugger) aoqi@0: aoqi@0: // Avoid a VS2005 compiler stack overflow w/ fastdebug build. aoqi@0: // The following pragma optimize turns off optimization ONLY aoqi@0: // for this block (a matching directive turns it back on later). aoqi@0: // These directives can be removed once the MS VS.NET 2005 aoqi@0: // compiler stack overflow is fixed. aoqi@0: #if defined(_MSC_VER) && _MSC_VER >=1400 && !defined(_WIN64) aoqi@0: #pragma optimize("", off) aoqi@0: #pragma warning(disable: 4748) aoqi@0: #endif aoqi@0: aoqi@0: // generator for an "interesting" set of critical values aoqi@0: enum { stretch_limit = (1<<16) * (64-16+1) }; aoqi@0: static jlong stretch(jint x, int bits) { aoqi@0: // put x[high 4] into place aoqi@0: jlong h = (jlong)((x >> (16-4))) << (bits - 4); aoqi@0: // put x[low 12] into place, sign extended aoqi@0: jlong l = ((jlong)x << (64-12)) >> (64-12); aoqi@0: // move l upwards, maybe aoqi@0: l <<= (x >> 16); aoqi@0: return h ^ l; aoqi@0: } aoqi@0: aoqi@0: PRAGMA_DIAG_PUSH aoqi@0: PRAGMA_FORMAT_IGNORED // Someone needs to deal with this. aoqi@0: void test_compressed_stream(int trace) { aoqi@0: CompressedWriteStream bytes(stretch_limit * 100); aoqi@0: jint n; aoqi@0: int step = 0, fails = 0; aoqi@0: #define CHECKXY(x, y, fmt) { \ aoqi@0: ++step; \ aoqi@0: int xlen = (pos = decode.position()) - lastpos; lastpos = pos; \ aoqi@0: if (trace > 0 && (step % trace) == 0) { \ aoqi@0: tty->print_cr("step %d, n=%08x: value=" fmt " (len=%d)", \ aoqi@0: step, n, x, xlen); } \ aoqi@0: if (x != y) { \ aoqi@0: tty->print_cr("step %d, n=%d: " fmt " != " fmt, step, n, x, y); \ aoqi@0: fails++; \ aoqi@0: } } aoqi@0: for (n = 0; n < (1<<8); n++) { aoqi@0: jbyte x = (jbyte)n; aoqi@0: bytes.write_byte(x); ++step; aoqi@0: } aoqi@0: for (n = 0; n < stretch_limit; n++) { aoqi@0: jint x = (jint)stretch(n, 32); aoqi@0: bytes.write_int(x); ++step; aoqi@0: bytes.write_signed_int(x); ++step; aoqi@0: bytes.write_float(jfloat_cast(x)); ++step; aoqi@0: } aoqi@0: for (n = 0; n < stretch_limit; n++) { aoqi@0: jlong x = stretch(n, 64); aoqi@0: bytes.write_long(x); ++step; aoqi@0: bytes.write_double(jdouble_cast(x)); ++step; aoqi@0: } aoqi@0: int length = bytes.position(); aoqi@0: if (trace != 0) aoqi@0: tty->print_cr("set up test of %d stream values, size %d", step, length); aoqi@0: step = 0; aoqi@0: // now decode it all aoqi@0: CompressedReadStream decode(bytes.buffer()); aoqi@0: int pos, lastpos = decode.position(); aoqi@0: for (n = 0; n < (1<<8); n++) { aoqi@0: jbyte x = (jbyte)n; aoqi@0: jbyte y = decode.read_byte(); aoqi@0: CHECKXY(x, y, "%db"); aoqi@0: } aoqi@0: for (n = 0; n < stretch_limit; n++) { aoqi@0: jint x = (jint)stretch(n, 32); aoqi@0: jint y1 = decode.read_int(); aoqi@0: CHECKXY(x, y1, "%du"); aoqi@0: jint y2 = decode.read_signed_int(); aoqi@0: CHECKXY(x, y2, "%di"); aoqi@0: jint y3 = jint_cast(decode.read_float()); aoqi@0: CHECKXY(x, y3, "%df"); aoqi@0: } aoqi@0: for (n = 0; n < stretch_limit; n++) { aoqi@0: jlong x = stretch(n, 64); aoqi@0: jlong y1 = decode.read_long(); aoqi@0: CHECKXY(x, y1, INT64_FORMAT "l"); aoqi@0: jlong y2 = jlong_cast(decode.read_double()); aoqi@0: CHECKXY(x, y2, INT64_FORMAT "d"); aoqi@0: } aoqi@0: int length2 = decode.position(); aoqi@0: if (trace != 0) aoqi@0: tty->print_cr("finished test of %d stream values, size %d", step, length2); aoqi@0: guarantee(length == length2, "bad length"); aoqi@0: guarantee(fails == 0, "test failures"); aoqi@0: } aoqi@0: PRAGMA_DIAG_POP aoqi@0: aoqi@0: #if defined(_MSC_VER) &&_MSC_VER >=1400 && !defined(_WIN64) aoqi@0: #pragma warning(default: 4748) aoqi@0: #pragma optimize("", on) aoqi@0: #endif aoqi@0: aoqi@0: #endif // PRODUCT