compressedStream.cpp revision 1472:c18cbe5936b8
1157387Srwatson/* 2157387Srwatson * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. 3222486Srwatson * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4157387Srwatson * 5157387Srwatson * This code is free software; you can redistribute it and/or modify it 6222486Srwatson * under the terms of the GNU General Public License version 2 only, as 7222486Srwatson * published by the Free Software Foundation. 8222486Srwatson * 9157387Srwatson * This code is distributed in the hope that it will be useful, but WITHOUT 10157387Srwatson * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11157387Srwatson * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12157387Srwatson * version 2 for more details (a copy is included in the LICENSE file that 13157387Srwatson * accompanied this code). 14157387Srwatson * 15157387Srwatson * You should have received a copy of the GNU General Public License version 16157387Srwatson * 2 along with this work; if not, write to the Free Software Foundation, 17157387Srwatson * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18157387Srwatson * 19157387Srwatson * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20157387Srwatson * or visit www.oracle.com if you need additional information or have any 21157387Srwatson * questions. 22157387Srwatson * 23157387Srwatson */ 24157387Srwatson 25157387Srwatson#include "incls/_precompiled.incl" 26157387Srwatson#include "incls/_compressedStream.cpp.incl" 27157387Srwatson 28157387Srwatson// 32-bit one-to-one sign encoding taken from Pack200 29157387Srwatson// converts leading sign bits into leading zeroes with trailing sign bit 30157387Srwatsoninline juint CompressedStream::encode_sign(jint value) { 31157387Srwatson return (value << 1) ^ (value >> 31); 32157387Srwatson} 33157387Srwatsoninline jint CompressedStream::decode_sign(juint value) { 34157387Srwatson return (value >> 1) ^ -(jint)(value & 1); 35157387Srwatson} 36157387Srwatson 37157387Srwatson// 32-bit self-inverse encoding of float bits 38157387Srwatson// converts trailing zeroes (common in floats) to leading zeroes 39157387Srwatsoninline juint CompressedStream::reverse_int(juint i) { 40157387Srwatson // Hacker's Delight, Figure 7-1 41157387Srwatson i = (i & 0x55555555) << 1 | (i >> 1) & 0x55555555; 42157387Srwatson i = (i & 0x33333333) << 2 | (i >> 2) & 0x33333333; 43157387Srwatson i = (i & 0x0f0f0f0f) << 4 | (i >> 4) & 0x0f0f0f0f; 44157387Srwatson i = (i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | (i >> 24); 45157387Srwatson return i; 46157387Srwatson} 47157387Srwatson 48222486Srwatson 49157387Srwatsonjint CompressedReadStream::read_signed_int() { 50157387Srwatson return decode_sign(read_int()); 51157387Srwatson} 52157387Srwatson 53157387Srwatson// Compressing floats is simple, because the only common pattern 54222486Srwatson// is trailing zeroes. (Compare leading sign bits on ints.) 55157387Srwatson// Since floats are left-justified, as opposed to right-justified 56222486Srwatson// ints, we can bit-reverse them in order to take advantage of int 57157387Srwatson// compression. 58157387Srwatson 59157387Srwatsonjfloat CompressedReadStream::read_float() { 60157387Srwatson int rf = read_int(); 61157402Srwatson int f = reverse_int(rf); 62157387Srwatson return jfloat_cast(f); 63157387Srwatson} 64157387Srwatson 65157387Srwatsonjdouble CompressedReadStream::read_double() { 66157387Srwatson jint rh = read_int(); 67157387Srwatson jint rl = read_int(); 68157387Srwatson jint h = reverse_int(rh); 69157387Srwatson jint l = reverse_int(rl); 70222486Srwatson return jdouble_cast(jlong_from(h, l)); 71157387Srwatson} 72157387Srwatson 73157387Srwatsonjlong CompressedReadStream::read_long() { 74157387Srwatson jint low = read_signed_int(); 75157387Srwatson jint high = read_signed_int(); 76157387Srwatson return jlong_from(high, low); 77157387Srwatson} 78157387Srwatson 79157387SrwatsonCompressedWriteStream::CompressedWriteStream(int initial_size) : CompressedStream(NULL, 0) { 80157402Srwatson _buffer = NEW_RESOURCE_ARRAY(u_char, initial_size); 81157387Srwatson _size = initial_size; 82157387Srwatson _position = 0; 83157387Srwatson} 84157387Srwatson 85157387Srwatsonvoid CompressedWriteStream::grow() { 86157387Srwatson u_char* _new_buffer = NEW_RESOURCE_ARRAY(u_char, _size * 2); 87157387Srwatson memcpy(_new_buffer, _buffer, _position); 88157387Srwatson _buffer = _new_buffer; 89222486Srwatson _size = _size * 2; 90157387Srwatson} 91157387Srwatson 92157387Srwatsonvoid CompressedWriteStream::write_signed_int(jint value) { 93157402Srwatson // this encoding, called SIGNED5, is taken from Pack200 94157387Srwatson write_int(encode_sign(value)); 95157387Srwatson} 96157387Srwatson 97157387Srwatsonvoid CompressedWriteStream::write_float(jfloat value) { 98157387Srwatson juint f = jint_cast(value); 99157387Srwatson juint rf = reverse_int(f); 100157402Srwatson assert(f == reverse_int(rf), "can re-read same bits"); 101157387Srwatson write_int(rf); 102157387Srwatson} 103157387Srwatson 104157387Srwatsonvoid CompressedWriteStream::write_double(jdouble value) { 105157387Srwatson juint h = high(jlong_cast(value)); 106157387Srwatson juint l = low( jlong_cast(value)); 107157387Srwatson juint rh = reverse_int(h); 108157387Srwatson juint rl = reverse_int(l); 109157387Srwatson assert(h == reverse_int(rh), "can re-read same bits"); 110157387Srwatson assert(l == reverse_int(rl), "can re-read same bits"); 111157387Srwatson write_int(rh); 112222486Srwatson write_int(rl); 113157387Srwatson} 114222486Srwatson 115222486Srwatsonvoid CompressedWriteStream::write_long(jlong value) { 116222486Srwatson write_signed_int(low(value)); 117157387Srwatson write_signed_int(high(value)); 118157387Srwatson} 119157387Srwatson 120157387Srwatson 121157387Srwatson/// The remaining details 122157387Srwatson 123157387Srwatson#ifndef PRODUCT 124157387Srwatson// set this to trigger unit test 125222486Srwatsonvoid test_compressed_stream(int trace); 126157387Srwatsonbool test_compressed_stream_enabled = false; 127222486Srwatson#endif 128222486Srwatson 129222486Srwatson// This encoding, called UNSIGNED5, is taken from J2SE Pack200. 130222486Srwatson// It assumes that most values have lots of leading zeroes. 131222486Srwatson// Very small values, in the range [0..191], code in one byte. 132222486Srwatson// Any 32-bit value (including negatives) can be coded, in 133222486Srwatson// up to five bytes. The grammar is: 134222486Srwatson// low_byte = [0..191] 135222486Srwatson// high_byte = [192..255] 136222486Srwatson// any_byte = low_byte | high_byte 137222486Srwatson// coding = low_byte 138222486Srwatson// | high_byte low_byte 139222486Srwatson// | high_byte high_byte low_byte 140222486Srwatson// | high_byte high_byte high_byte low_byte 141222486Srwatson// | high_byte high_byte high_byte high_byte any_byte 142222486Srwatson// Each high_byte contributes six bits of payload. 143222486Srwatson// The encoding is one-to-one (except for integer overflow) 144222486Srwatson// and easy to parse and unparse. 145222486Srwatson 146222486Srwatsonjint CompressedReadStream::read_int_mb(jint b0) { 147222486Srwatson int pos = position() - 1; 148222486Srwatson u_char* buf = buffer() + pos; 149222486Srwatson assert(buf[0] == b0 && b0 >= L, "correctly called"); 150222486Srwatson jint sum = b0; 151222486Srwatson // must collect more bytes: b[1]...b[4] 152222486Srwatson int lg_H_i = lg_H; 153222486Srwatson for (int i = 0; ; ) { 154222486Srwatson jint b_i = buf[++i]; // b_i = read(); ++i; 155222486Srwatson sum += b_i << lg_H_i; // sum += b[i]*(64**i) 156222486Srwatson if (b_i < L || i == MAX_i) { 157222486Srwatson set_position(pos+i+1); 158157387Srwatson return sum; 159157387Srwatson } 160157387Srwatson lg_H_i += lg_H; 161157387Srwatson } 162157387Srwatson} 163157387Srwatson 164222486Srwatsonvoid CompressedWriteStream::write_int_mb(jint value) { 165157402Srwatson debug_only(int pos1 = position()); 166157387Srwatson juint sum = value; 167222486Srwatson for (int i = 0; ; ) { 168157402Srwatson if (sum < L || i == MAX_i) { 169222486Srwatson // remainder is either a "low code" or the 5th byte 170157402Srwatson assert(sum == (u_char)sum, "valid byte"); 171157387Srwatson write((u_char)sum); 172222486Srwatson break; 173222486Srwatson } 174222486Srwatson sum -= L; 175222486Srwatson int b_i = L + (sum % H); // this is a "high code" 176222486Srwatson sum >>= lg_H; // extracted 6 bits 177222486Srwatson write(b_i); ++i; 178222486Srwatson } 179222486Srwatson 180222486Srwatson#ifndef PRODUCT 181222486Srwatson if (test_compressed_stream_enabled) { // hack to enable this stress test 182222486Srwatson test_compressed_stream_enabled = false; 183222486Srwatson test_compressed_stream(0); 184222486Srwatson } 185222486Srwatson#endif 186222486Srwatson} 187222486Srwatson 188222486Srwatson 189222486Srwatson#ifndef PRODUCT 190222486Srwatson/// a unit test (can be run by hand from a debugger) 191222486Srwatson 192222486Srwatson// Avoid a VS2005 compiler stack overflow w/ fastdebug build. 193222486Srwatson// The following pragma optimize turns off optimization ONLY 194222486Srwatson// for this block (a matching directive turns it back on later). 195222486Srwatson// These directives can be removed once the MS VS.NET 2005 196222486Srwatson// compiler stack overflow is fixed. 197222486Srwatson#if _MSC_VER >=1400 && !defined(_WIN64) 198222486Srwatson#pragma optimize("", off) 199222486Srwatson#endif 200222486Srwatson 201222486Srwatson// generator for an "interesting" set of critical values 202222486Srwatsonenum { stretch_limit = (1<<16) * (64-16+1) }; 203222486Srwatsonstatic jlong stretch(jint x, int bits) { 204222486Srwatson // put x[high 4] into place 205222486Srwatson jlong h = (jlong)((x >> (16-4))) << (bits - 4); 206157387Srwatson // put x[low 12] into place, sign extended 207157387Srwatson jlong l = ((jlong)x << (64-12)) >> (64-12); 208157387Srwatson // move l upwards, maybe 209157387Srwatson l <<= (x >> 16); 210157387Srwatson return h ^ l; 211157387Srwatson} 212222486Srwatson 213157387Srwatsonvoid test_compressed_stream(int trace) { 214222486Srwatson CompressedWriteStream bytes(stretch_limit * 100); 215157387Srwatson jint n; 216157387Srwatson int step = 0, fails = 0; 217157387Srwatson#define CHECKXY(x, y, fmt) { \ 218 ++step; \ 219 int xlen = (pos = decode.position()) - lastpos; lastpos = pos; \ 220 if (trace > 0 && (step % trace) == 0) { \ 221 tty->print_cr("step %d, n=%08x: value=" fmt " (len=%d)", \ 222 step, n, x, xlen); } \ 223 if (x != y) { \ 224 tty->print_cr("step %d, n=%d: " fmt " != " fmt, step, n, x, y); \ 225 fails++; \ 226 } } 227 for (n = 0; n < (1<<8); n++) { 228 jbyte x = (jbyte)n; 229 bytes.write_byte(x); ++step; 230 } 231 for (n = 0; n < stretch_limit; n++) { 232 jint x = (jint)stretch(n, 32); 233 bytes.write_int(x); ++step; 234 bytes.write_signed_int(x); ++step; 235 bytes.write_float(jfloat_cast(x)); ++step; 236 } 237 for (n = 0; n < stretch_limit; n++) { 238 jlong x = stretch(n, 64); 239 bytes.write_long(x); ++step; 240 bytes.write_double(jdouble_cast(x)); ++step; 241 } 242 int length = bytes.position(); 243 if (trace != 0) 244 tty->print_cr("set up test of %d stream values, size %d", step, length); 245 step = 0; 246 // now decode it all 247 CompressedReadStream decode(bytes.buffer()); 248 int pos, lastpos = decode.position(); 249 for (n = 0; n < (1<<8); n++) { 250 jbyte x = (jbyte)n; 251 jbyte y = decode.read_byte(); 252 CHECKXY(x, y, "%db"); 253 } 254 for (n = 0; n < stretch_limit; n++) { 255 jint x = (jint)stretch(n, 32); 256 jint y1 = decode.read_int(); 257 CHECKXY(x, y1, "%du"); 258 jint y2 = decode.read_signed_int(); 259 CHECKXY(x, y2, "%di"); 260 jint y3 = jint_cast(decode.read_float()); 261 CHECKXY(x, y3, "%df"); 262 } 263 for (n = 0; n < stretch_limit; n++) { 264 jlong x = stretch(n, 64); 265 jlong y1 = decode.read_long(); 266 CHECKXY(x, y1, INT64_FORMAT "l"); 267 jlong y2 = jlong_cast(decode.read_double()); 268 CHECKXY(x, y2, INT64_FORMAT "d"); 269 } 270 int length2 = decode.position(); 271 if (trace != 0) 272 tty->print_cr("finished test of %d stream values, size %d", step, length2); 273 guarantee(length == length2, "bad length"); 274 guarantee(fails == 0, "test failures"); 275} 276 277#if _MSC_VER >=1400 && !defined(_WIN64) 278#pragma optimize("", on) 279#endif 280 281#endif // PRODUCT 282