Allow cell overflow in block data (pre-1.16 style)

This commit is contained in:
Alpyne 2020-11-04 16:31:11 -08:00
parent b218552cc4
commit e7d18f7dff
1 changed files with 33 additions and 25 deletions

View File

@ -6,7 +6,8 @@
namespace Feather namespace Feather
{ {
// A container that maps values from a local palette to a // A container that maps values from a local palette to a global palette
// Implemented like Minecraft's BitStorage in 1.15.2 (with cell overflow)
template <typename T, uint MaxSize, uint MaxBits> template <typename T, uint MaxSize, uint MaxBits>
struct PalettedContainer struct PalettedContainer
{ {
@ -25,7 +26,6 @@ namespace Feather
uint size = MaxSize; uint size = MaxSize;
uint8 bits = MaxBits; uint8 bits = MaxBits;
uint64 mask = 0; uint64 mask = 0;
uint valuesPerCell = 0;
public: public:
PalettedContainer(uint size, uint8 bits) : size(size), data() { SetBits(bits); } PalettedContainer(uint size, uint8 bits) : size(size), data() { SetBits(bits); }
@ -33,43 +33,51 @@ namespace Feather
inline void SetBits(uint8 nbits) inline void SetBits(uint8 nbits)
{ {
bits = nbits; bits = nbits;
mask = (1 << bits) - 1; mask = (1UL << bits) - 1UL;
valuesPerCell = BITS_PER_CELL / bits;
Log::Debug("PalettedContainer::SetBits: {}, mask = {:#b}, valuesPerCell = {}", bits, mask, valuesPerCell);
} }
// TODO: optimize all math here // TODO: optimize all math here
inline int CellIndex(int index)
{
return index / valuesPerCell;
}
// Update value, returns previous value // Update value, returns previous value
// TODO: change from uint to const T& // TODO: change from uint to const T&
inline uint GetAndSet(int index, uint value) inline uint GetAndSet(int index, uint value)
{ {
// Note: Java >> is signed, while >>> is used for zero-fill shift.
// Usage of signed >> instead of >>> has been marked.
// Bit # we want to read from
int bitIndex = index * bits;
// Cell index
uint cell = bitIndex >> 6; // java signed >>
// Next cell, if bits overflow
uint next = ((index + 1) * bits - 1) >> 6; // java signed >>
uint shift = bitIndex ^ cell << 6;
auto [d, lock] = data.borrow(); auto [d, lock] = data.borrow();
// read previous value
uint64 cellValue = d[cell];
uint prev = (uint)(d[cell] >> shift & mask);
int id = value; // palette.GetID(value); d[cell] &= ~(mask << shift); // clear target bits
int cellIndex = CellIndex(index); d[cell] |= ((uint64)value & mask) << shift; // set target bits
uint64 shift = (index - cellIndex * valuesPerCell) * bits;//(index % valuesPerCell) * bits;
Log::Debug("PalettedContainer::GetAndSet: index {} -> cell {}, [{}:{}]", index, cellIndex, shift, shift + bits); // Bits overflow into next cell
if (next != cell)
{
uint shift1 = BITS_PER_CELL - shift;
uint shift2 = bits - shift1;
uint64 cell = d[cellIndex]; uint64 nextValue = d[next];
int prev = (cell >> shift) & mask;
//d[cellIndex] = ((cell & (mask << shift ^ 0xFFFFFFFFFFFFFFFFL)) | (uint64(id) & mask)) << shift; prev |= (uint)(d[next] << shift1 & mask);
// TODO: not 100% on the math here
d[next] = d[next] >> shift2 << shift2 | ((uint64)value & mask) >> shift1; // last shift is java signed >>
}
d[cellIndex] &= ~(mask << shift); // clear target bits return prev;
d[cellIndex] |= ((uint64)id & mask) << shift; // set target bits
//Log::Debug("Changed from {:#b} to {:#b}", cell, d[cellIndex]);
Log::Debug("Changed from {} (g: {}) to {} (g: {})", prev, palette.ByID(prev), value, palette.ByID(value));
return (uint)prev; // palette.ByID(prev);
} }
inline constexpr size_t GetNumLongs() { return NUM_LONGS; } inline constexpr size_t GetNumLongs() { return NUM_LONGS; }