Share min_containing_bytes between CInputStream and COutputStream

Former-commit-id: 1c956d06f2
This commit is contained in:
Phillip Stephens 2022-10-11 20:32:26 -07:00
parent 637de45b40
commit 8d0b032e01
2 changed files with 5 additions and 9 deletions

View File

@ -2,6 +2,7 @@
#include "string.h"
#include "Kyoto/Streams/StreamSupport.hpp"
#include "Kyoto/Alloc/CMemory.hpp"
CInputStream::CInputStream(int len)
@ -97,8 +98,6 @@ uint CInputStream::ReadBytes(void* dest, unsigned long len) {
return curReadLen;
}
static inline uint BitsToBytes(uint bits) { return (bits / 8) + ((bits % 8) ? 1 : 0); }
uint CInputStream::ReadBits(uint bitCount) {
if (x20_bitOffset >= bitCount) {
uint mask = 0xffffffff;
@ -125,7 +124,7 @@ uint CInputStream::ReadBits(uint bitCount) {
ret = (mask & (x1c_bitWord >> bwShift)) << shiftAmt;
}
uint len = BitsToBytes(shiftAmt);
uint len = min_containing_bytes(shiftAmt);
x20_bitOffset = 0;
Get(&x1c_bitWord, len);

View File

@ -1,5 +1,7 @@
#include "Kyoto/Streams/COutputStream.hpp"
#include "Kyoto/Streams/StreamSupport.hpp"
#include "Kyoto/Alloc/CMemory.hpp"
#include <string.h>
@ -57,14 +59,9 @@ void COutputStream::DoFlush() {
}
}
static inline u32 min_containing_bytes(u32 v) {
v = 32 - v;
return (v / 8) + ((v % 8) != 0);
}
void COutputStream::FlushShiftRegister() {
if (mShiftRegisterOffset < 32) {
DoPut((void*)&mShiftRegister, min_containing_bytes(mShiftRegisterOffset));
DoPut((void*)&mShiftRegister, min_containing_bytes(32 - mShiftRegisterOffset));
mShiftRegister = 0;
mShiftRegisterOffset = 32;
}