Path: blob/master/src/hotspot/share/code/compressedStream.cpp
40931 views
/*1* Copyright (c) 1997, 2017, 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 self-inverse encoding of float bits29// converts trailing zeroes (common in floats) to leading zeroes30inline juint CompressedStream::reverse_int(juint i) {31// Hacker's Delight, Figure 7-132i = (i & 0x55555555) << 1 | ((i >> 1) & 0x55555555);33i = (i & 0x33333333) << 2 | ((i >> 2) & 0x33333333);34i = (i & 0x0f0f0f0f) << 4 | ((i >> 4) & 0x0f0f0f0f);35i = (i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | (i >> 24);36return i;37}3839jint CompressedReadStream::read_signed_int() {40return decode_sign(read_int());41}4243// Compressing floats is simple, because the only common pattern44// is trailing zeroes. (Compare leading sign bits on ints.)45// Since floats are left-justified, as opposed to right-justified46// ints, we can bit-reverse them in order to take advantage of int47// compression.4849jfloat CompressedReadStream::read_float() {50int rf = read_int();51int f = reverse_int(rf);52return jfloat_cast(f);53}5455jdouble CompressedReadStream::read_double() {56jint rh = read_int();57jint rl = read_int();58jint h = reverse_int(rh);59jint l = reverse_int(rl);60return jdouble_cast(jlong_from(h, l));61}6263jlong CompressedReadStream::read_long() {64jint low = read_signed_int();65jint high = read_signed_int();66return jlong_from(high, low);67}6869CompressedWriteStream::CompressedWriteStream(int initial_size) : CompressedStream(NULL, 0) {70_buffer = NEW_RESOURCE_ARRAY(u_char, initial_size);71_size = initial_size;72_position = 0;73}7475void CompressedWriteStream::grow() {76u_char* _new_buffer = NEW_RESOURCE_ARRAY(u_char, _size * 2);77memcpy(_new_buffer, _buffer, _position);78_buffer = _new_buffer;79_size = _size * 2;80}8182void CompressedWriteStream::write_float(jfloat value) {83juint f = jint_cast(value);84juint rf = reverse_int(f);85assert(f == reverse_int(rf), "can re-read same bits");86write_int(rf);87}8889void CompressedWriteStream::write_double(jdouble value) {90juint h = high(jlong_cast(value));91juint l = low( jlong_cast(value));92juint rh = reverse_int(h);93juint rl = reverse_int(l);94assert(h == reverse_int(rh), "can re-read same bits");95assert(l == reverse_int(rl), "can re-read same bits");96write_int(rh);97write_int(rl);98}99100void CompressedWriteStream::write_long(jlong value) {101write_signed_int(low(value));102write_signed_int(high(value));103}104105106