From f901989e14c9f5e06a677e66302d21d0af066dce Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Sun, 9 Aug 2020 23:05:38 -0700 Subject: [PATCH] Implemented _writeRaw(long,int) --- core/src/com/riiablo/io/BitOutput.java | 33 +++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/core/src/com/riiablo/io/BitOutput.java b/core/src/com/riiablo/io/BitOutput.java index 44313802..bf005e5f 100644 --- a/core/src/com/riiablo/io/BitOutput.java +++ b/core/src/com/riiablo/io/BitOutput.java @@ -110,10 +110,37 @@ public class BitOutput { void writeSigned(long value, int bits) { assert bits > 0 : "bits(" + bits + ") <= " + 0; assert bits <= Long.SIZE : "bits(" + bits + ") > " + Long.SIZE; - // bits == Long.SIZE write raw? final int shift = Long.SIZE - bits; - assert shift > 0 : "shift(" + shift + ") <= " + 0; - value = value << shift >> shift; + value = (value << shift >> shift) & MASKS[bits]; + _writeRaw(value, bits); + } + + void _writeRaw(long value, int bits) { + assert bits > 0 : "bits(" + bits + ") < " + 1; + assert bits <= Long.SIZE : "bits(" + bits + ") > " + Long.SIZE; + assert bitsCached < Byte.SIZE : "bitsCached(" + bitsCached + ") > " + (Byte.SIZE - 1); + incrementBitsWritten(bits); + cache |= (value << bitsCached); + bitsCached += bits; + + final int overflowBits = bitsCached - Long.SIZE; + _writeCache(bits); + if (overflowBits > 0) { + final int overflowShift = Long.SIZE - overflowBits; + cache = (value >>> overflowShift) & MASKS[overflowBits]; + } + } + + private void _writeCache(int bits) { + assert bits > 0 : "bits(" + bits + ") < " + 1; + assert bits < Long.SIZE : "bits(" + bits + ") > " + (Long.SIZE - 1); + assert bitsCached >= bits : "bitsCached(" + bitsCached + ") < bits(" + bits + ")"; + assert bitsCached <= (Long.SIZE + Byte.SIZE - 1) : "bitsCached(" + bitsCached + ") > " + (Long.SIZE + Byte.SIZE - 1); + for (; bitsCached >= Byte.SIZE; bitsCached -= Byte.SIZE) { + final long octet = cache & 0xFF; + byteOutput._write8(octet); + cache >>>= Byte.SIZE; + } } public void writeRaw(long value, int bits) {