| 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 self-inverse encoding of float bits |
| 30 | // converts trailing zeroes (common in floats) to leading zeroes |
| 31 | inline juint CompressedStream::reverse_int(juint i) { |
| 32 | // Hacker's Delight, Figure 7-1 |
| 33 | i = (i & 0x55555555) << 1 | ((i >> 1) & 0x55555555); |
| 34 | i = (i & 0x33333333) << 2 | ((i >> 2) & 0x33333333); |
| 35 | i = (i & 0x0f0f0f0f) << 4 | ((i >> 4) & 0x0f0f0f0f); |
| 36 | i = (i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | (i >> 24); |
| 37 | return i; |
| 38 | } |
| 39 | |
| 40 | jint CompressedReadStream::read_signed_int() { |
| 41 | return decode_sign(read_int()); |
| 42 | } |
| 43 | |
| 44 | // Compressing floats is simple, because the only common pattern |
| 45 | // is trailing zeroes. (Compare leading sign bits on ints.) |
| 46 | // Since floats are left-justified, as opposed to right-justified |
| 47 | // ints, we can bit-reverse them in order to take advantage of int |
| 48 | // compression. |
| 49 | |
| 50 | jfloat CompressedReadStream::read_float() { |
| 51 | int rf = read_int(); |
| 52 | int f = reverse_int(rf); |
| 53 | return jfloat_cast(f); |
| 54 | } |
| 55 | |
| 56 | jdouble CompressedReadStream::read_double() { |
| 57 | jint rh = read_int(); |
| 58 | jint rl = read_int(); |
| 59 | jint h = reverse_int(rh); |
| 60 | jint l = reverse_int(rl); |
| 61 | return jdouble_cast(jlong_from(h, l)); |
| 62 | } |
| 63 | |
| 64 | jlong CompressedReadStream::read_long() { |
| 65 | jint low = read_signed_int(); |
| 66 | jint high = read_signed_int(); |
| 67 | return jlong_from(high, low); |
| 68 | } |
| 69 | |
| 70 | CompressedWriteStream::CompressedWriteStream(int initial_size) : CompressedStream(NULL, 0) { |
| 71 | _buffer = NEW_RESOURCE_ARRAY(u_char, initial_size); |
| 72 | _size = initial_size; |
| 73 | _position = 0; |
| 74 | } |
| 75 | |
| 76 | void CompressedWriteStream::grow() { |
| 77 | u_char* _new_buffer = NEW_RESOURCE_ARRAY(u_char, _size * 2); |
| 78 | memcpy(_new_buffer, _buffer, _position); |
| 79 | _buffer = _new_buffer; |
| 80 | _size = _size * 2; |
| 81 | } |
| 82 | |
| 83 | void CompressedWriteStream::write_float(jfloat value) { |
| 84 | juint f = jint_cast(value); |
| 85 | juint rf = reverse_int(f); |
| 86 | assert(f == reverse_int(rf), "can re-read same bits" ); |
| 87 | write_int(rf); |
| 88 | } |
| 89 | |
| 90 | void CompressedWriteStream::write_double(jdouble value) { |
| 91 | juint h = high(jlong_cast(value)); |
| 92 | juint l = low( jlong_cast(value)); |
| 93 | juint rh = reverse_int(h); |
| 94 | juint rl = reverse_int(l); |
| 95 | assert(h == reverse_int(rh), "can re-read same bits" ); |
| 96 | assert(l == reverse_int(rl), "can re-read same bits" ); |
| 97 | write_int(rh); |
| 98 | write_int(rl); |
| 99 | } |
| 100 | |
| 101 | void CompressedWriteStream::write_long(jlong value) { |
| 102 | write_signed_int(low(value)); |
| 103 | write_signed_int(high(value)); |
| 104 | } |
| 105 | |