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