Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/code/compressedStream.cpp
32285 views
/*1* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "code/compressedStream.hpp"26#include "utilities/ostream.hpp"2728// 32-bit one-to-one sign encoding taken from Pack20029// converts leading sign bits into leading zeroes with trailing sign bit30inline juint CompressedStream::encode_sign(jint value) {31return (value << 1) ^ (value >> 31);32}33inline jint CompressedStream::decode_sign(juint value) {34return (value >> 1) ^ -(jint)(value & 1);35}3637// 32-bit self-inverse encoding of float bits38// converts trailing zeroes (common in floats) to leading zeroes39inline juint CompressedStream::reverse_int(juint i) {40// Hacker's Delight, Figure 7-141i = (i & 0x55555555) << 1 | (i >> 1) & 0x55555555;42i = (i & 0x33333333) << 2 | (i >> 2) & 0x33333333;43i = (i & 0x0f0f0f0f) << 4 | (i >> 4) & 0x0f0f0f0f;44i = (i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | (i >> 24);45return i;46}474849jint CompressedReadStream::read_signed_int() {50return decode_sign(read_int());51}5253// Compressing floats is simple, because the only common pattern54// is trailing zeroes. (Compare leading sign bits on ints.)55// Since floats are left-justified, as opposed to right-justified56// ints, we can bit-reverse them in order to take advantage of int57// compression.5859jfloat CompressedReadStream::read_float() {60int rf = read_int();61int f = reverse_int(rf);62return jfloat_cast(f);63}6465jdouble CompressedReadStream::read_double() {66jint rh = read_int();67jint rl = read_int();68jint h = reverse_int(rh);69jint l = reverse_int(rl);70return jdouble_cast(jlong_from(h, l));71}7273jlong CompressedReadStream::read_long() {74jint low = read_signed_int();75jint high = read_signed_int();76return jlong_from(high, low);77}7879CompressedWriteStream::CompressedWriteStream(int initial_size) : CompressedStream(NULL, 0) {80_buffer = NEW_RESOURCE_ARRAY(u_char, initial_size);81_size = initial_size;82_position = 0;83}8485void CompressedWriteStream::grow() {86u_char* _new_buffer = NEW_RESOURCE_ARRAY(u_char, _size * 2);87memcpy(_new_buffer, _buffer, _position);88_buffer = _new_buffer;89_size = _size * 2;90}9192void CompressedWriteStream::write_signed_int(jint value) {93// this encoding, called SIGNED5, is taken from Pack20094write_int(encode_sign(value));95}9697void CompressedWriteStream::write_float(jfloat value) {98juint f = jint_cast(value);99juint rf = reverse_int(f);100assert(f == reverse_int(rf), "can re-read same bits");101write_int(rf);102}103104void CompressedWriteStream::write_double(jdouble value) {105juint h = high(jlong_cast(value));106juint l = low( jlong_cast(value));107juint rh = reverse_int(h);108juint rl = reverse_int(l);109assert(h == reverse_int(rh), "can re-read same bits");110assert(l == reverse_int(rl), "can re-read same bits");111write_int(rh);112write_int(rl);113}114115void CompressedWriteStream::write_long(jlong value) {116write_signed_int(low(value));117write_signed_int(high(value));118}119120121/// The remaining details122123#ifndef PRODUCT124// set this to trigger unit test125void test_compressed_stream(int trace);126bool test_compressed_stream_enabled = false;127#endif128129// This encoding, called UNSIGNED5, is taken from J2SE Pack200.130// It assumes that most values have lots of leading zeroes.131// Very small values, in the range [0..191], code in one byte.132// Any 32-bit value (including negatives) can be coded, in133// up to five bytes. The grammar is:134// low_byte = [0..191]135// high_byte = [192..255]136// any_byte = low_byte | high_byte137// coding = low_byte138// | high_byte low_byte139// | high_byte high_byte low_byte140// | high_byte high_byte high_byte low_byte141// | high_byte high_byte high_byte high_byte any_byte142// Each high_byte contributes six bits of payload.143// The encoding is one-to-one (except for integer overflow)144// and easy to parse and unparse.145146jint CompressedReadStream::read_int_mb(jint b0) {147int pos = position() - 1;148u_char* buf = buffer() + pos;149assert(buf[0] == b0 && b0 >= L, "correctly called");150jint sum = b0;151// must collect more bytes: b[1]...b[4]152int lg_H_i = lg_H;153for (int i = 0; ; ) {154jint b_i = buf[++i]; // b_i = read(); ++i;155sum += b_i << lg_H_i; // sum += b[i]*(64**i)156if (b_i < L || i == MAX_i) {157set_position(pos+i+1);158return sum;159}160lg_H_i += lg_H;161}162}163164void CompressedWriteStream::write_int_mb(jint value) {165debug_only(int pos1 = position());166juint sum = value;167for (int i = 0; ; ) {168if (sum < L || i == MAX_i) {169// remainder is either a "low code" or the 5th byte170assert(sum == (u_char)sum, "valid byte");171write((u_char)sum);172break;173}174sum -= L;175int b_i = L + (sum % H); // this is a "high code"176sum >>= lg_H; // extracted 6 bits177write(b_i); ++i;178}179180#ifndef PRODUCT181if (test_compressed_stream_enabled) { // hack to enable this stress test182test_compressed_stream_enabled = false;183test_compressed_stream(0);184}185#endif186}187188189#ifndef PRODUCT190/// a unit test (can be run by hand from a debugger)191192// Avoid a VS2005 compiler stack overflow w/ fastdebug build.193// The following pragma optimize turns off optimization ONLY194// for this block (a matching directive turns it back on later).195// These directives can be removed once the MS VS.NET 2005196// compiler stack overflow is fixed.197#if defined(_MSC_VER) && _MSC_VER >=1400 && !defined(_WIN64)198#pragma optimize("", off)199#pragma warning(disable: 4748)200#endif201202// generator for an "interesting" set of critical values203enum { stretch_limit = (1<<16) * (64-16+1) };204static jlong stretch(jint x, int bits) {205// put x[high 4] into place206jlong h = (jlong)((x >> (16-4))) << (bits - 4);207// put x[low 12] into place, sign extended208jlong l = ((jlong)x << (64-12)) >> (64-12);209// move l upwards, maybe210l <<= (x >> 16);211return h ^ l;212}213214PRAGMA_DIAG_PUSH215PRAGMA_FORMAT_IGNORED // Someone needs to deal with this.216void test_compressed_stream(int trace) {217CompressedWriteStream bytes(stretch_limit * 100);218jint n;219int step = 0, fails = 0;220#define CHECKXY(x, y, fmt) { \221++step; \222int xlen = (pos = decode.position()) - lastpos; lastpos = pos; \223if (trace > 0 && (step % trace) == 0) { \224tty->print_cr("step %d, n=%08x: value=" fmt " (len=%d)", \225step, n, x, xlen); } \226if (x != y) { \227tty->print_cr("step %d, n=%d: " fmt " != " fmt, step, n, x, y); \228fails++; \229} }230for (n = 0; n < (1<<8); n++) {231jbyte x = (jbyte)n;232bytes.write_byte(x); ++step;233}234for (n = 0; n < stretch_limit; n++) {235jint x = (jint)stretch(n, 32);236bytes.write_int(x); ++step;237bytes.write_signed_int(x); ++step;238bytes.write_float(jfloat_cast(x)); ++step;239}240for (n = 0; n < stretch_limit; n++) {241jlong x = stretch(n, 64);242bytes.write_long(x); ++step;243bytes.write_double(jdouble_cast(x)); ++step;244}245int length = bytes.position();246if (trace != 0)247tty->print_cr("set up test of %d stream values, size %d", step, length);248step = 0;249// now decode it all250CompressedReadStream decode(bytes.buffer());251int pos, lastpos = decode.position();252for (n = 0; n < (1<<8); n++) {253jbyte x = (jbyte)n;254jbyte y = decode.read_byte();255CHECKXY(x, y, "%db");256}257for (n = 0; n < stretch_limit; n++) {258jint x = (jint)stretch(n, 32);259jint y1 = decode.read_int();260CHECKXY(x, y1, "%du");261jint y2 = decode.read_signed_int();262CHECKXY(x, y2, "%di");263jint y3 = jint_cast(decode.read_float());264CHECKXY(x, y3, "%df");265}266for (n = 0; n < stretch_limit; n++) {267jlong x = stretch(n, 64);268jlong y1 = decode.read_long();269CHECKXY(x, y1, INT64_FORMAT "l");270jlong y2 = jlong_cast(decode.read_double());271CHECKXY(x, y2, INT64_FORMAT "d");272}273int length2 = decode.position();274if (trace != 0)275tty->print_cr("finished test of %d stream values, size %d", step, length2);276guarantee(length == length2, "bad length");277guarantee(fails == 0, "test failures");278}279PRAGMA_DIAG_POP280281#if defined(_MSC_VER) &&_MSC_VER >=1400 && !defined(_WIN64)282#pragma warning(default: 4748)283#pragma optimize("", on)284#endif285286#endif // PRODUCT287288289