1 /* 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "code/compressedStream.hpp" 27 #include "utilities/ostream.hpp" 28 29 // 32-bit one-to-one sign encoding taken from Pack200 30 // converts leading sign bits into leading zeroes with trailing sign bit 31 inline juint CompressedStream::encode_sign(jint value) { 32 return (value << 1) ^ (value >> 31); 33 } 34 inline jint CompressedStream::decode_sign(juint value) { 35 return (value >> 1) ^ -(jint)(value & 1); 36 } 37 38 // 32-bit self-inverse encoding of float bits 39 // converts trailing zeroes (common in floats) to leading zeroes 40 inline juint CompressedStream::reverse_int(juint i) { 41 // Hacker's Delight, Figure 7-1 42 i = (i & 0x55555555) << 1 | (i >> 1) & 0x55555555; 43 i = (i & 0x33333333) << 2 | (i >> 2) & 0x33333333; 44 i = (i & 0x0f0f0f0f) << 4 | (i >> 4) & 0x0f0f0f0f; 45 i = (i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | (i >> 24); 46 return i; 47 } 48 49 50 jint CompressedReadStream::read_signed_int() { 51 return decode_sign(read_int()); 52 } 53 54 // Compressing floats is simple, because the only common pattern 55 // is trailing zeroes. (Compare leading sign bits on ints.) 56 // Since floats are left-justified, as opposed to right-justified 57 // ints, we can bit-reverse them in order to take advantage of int 58 // compression. 59 60 jfloat CompressedReadStream::read_float() { 61 int rf = read_int(); 62 int f = reverse_int(rf); 63 return jfloat_cast(f); 64 } | 1 /* 2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "code/compressedStream.hpp" 27 #include "utilities/ostream.hpp" 28 29 // 32-bit one-to-one sign encoding taken from Pack200 30 // converts leading sign bits into leading zeroes with trailing sign bit 31 inline juint CompressedStream::encode_sign(jint value) { 32 return (value << 1) ^ (value >> 31); 33 } 34 inline jint CompressedStream::decode_sign(juint value) { 35 return (value >> 1) ^ -(jint)(value & 1); 36 } 37 38 // 32-bit self-inverse encoding of float bits 39 // converts trailing zeroes (common in floats) to leading zeroes 40 inline juint CompressedStream::reverse_int(juint i) { 41 // Hacker's Delight, Figure 7-1 42 i = (i & 0x55555555) << 1 | ((i >> 1) & 0x55555555); 43 i = (i & 0x33333333) << 2 | ((i >> 2) & 0x33333333); 44 i = (i & 0x0f0f0f0f) << 4 | ((i >> 4) & 0x0f0f0f0f); 45 i = (i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | (i >> 24); 46 return i; 47 } 48 49 50 jint CompressedReadStream::read_signed_int() { 51 return decode_sign(read_int()); 52 } 53 54 // Compressing floats is simple, because the only common pattern 55 // is trailing zeroes. (Compare leading sign bits on ints.) 56 // Since floats are left-justified, as opposed to right-justified 57 // ints, we can bit-reverse them in order to take advantage of int 58 // compression. 59 60 jfloat CompressedReadStream::read_float() { 61 int rf = read_int(); 62 int f = reverse_int(rf); 63 return jfloat_cast(f); 64 } |