func lowLevelCompressPairs()

in cpc/cpc_compressed_state.go [751:849]


func lowLevelCompressPairs(pairArray []int, numPairsToEncode, numBaseBits int, compressedWords []int) int {
	nextWordIndex := 0
	var bitBuf uint64 = 0
	bufBits := 0

	// Allocate the pointer array (used for writeUnary).
	ptrArr := make([]int64, 3)

	// golombLoMask = (1L << numBaseBits) - 1
	golombLoMask := (uint64(1) << uint(numBaseBits)) - 1

	predictedRowIndex := 0
	predictedColIndex := 0

	for pairIndex := 0; pairIndex < numPairsToEncode; pairIndex++ {
		rowCol := pairArray[pairIndex]
		// Extract row index (upper bits) and column index (lower 6 bits)
		rowIndex := rowCol >> 6
		colIndex := rowCol & 0x3F // 0x3F == 63

		if rowIndex != predictedRowIndex {
			predictedColIndex = 0
		}
		if rowIndex < predictedRowIndex || colIndex < predictedColIndex {
			panic(fmt.Sprintf("lowLevelCompressPairs: assertion failed: rowIndex=%d, predictedRowIndex=%d, colIndex=%d, predictedColIndex=%d",
				rowIndex, predictedRowIndex, colIndex, predictedColIndex))
		}

		// yDelta is the difference in row indices.
		yDelta := uint64(rowIndex - predictedRowIndex)
		// xDelta is the difference in column indices.
		xDelta := colIndex - predictedColIndex

		predictedRowIndex = rowIndex
		predictedColIndex = colIndex + 1

		// Retrieve the code information from the lookup table.
		codeInfo := uint64(lengthLimitedUnaryEncodingTable65[xDelta]) & 0xFFFF
		// Lower 12 bits are the code value.
		codeVal := codeInfo & 0xFFF
		// Upper bits (shifted right 12) are the code length.
		codeLen := int(codeInfo >> 12)

		// Append the code value into the bit buffer.
		bitBuf |= codeVal << uint(bufBits)
		bufBits += codeLen
		// Flush the bit buffer if we have 32 or more bits.
		if bufBits >= 32 {
			compressedWords[nextWordIndex] = int(bitBuf & 0xFFFFFFFF)
			nextWordIndex++
			bitBuf >>= 32
			bufBits -= 32
		}

		// Process Golomb coding for yDelta.
		golombLo := yDelta & golombLoMask
		golombHi := yDelta >> uint(numBaseBits)

		// Inline WriteUnary:
		ptrArr[NextWordIdx] = int64(nextWordIndex)
		ptrArr[BitBuf] = int64(bitBuf)
		ptrArr[BufBits] = int64(bufBits)
		// Call writeUnary to output unary code for golombHi.
		writeUnary(compressedWords, ptrArr, int(golombHi))
		// Retrieve updated values.
		nextWordIndex = int(ptrArr[NextWordIdx])
		bitBuf = uint64(ptrArr[BitBuf])
		bufBits = int(ptrArr[BufBits])

		// Append the lower bits of the Golomb code.
		bitBuf |= golombLo << uint(bufBits)
		bufBits += numBaseBits
		if bufBits >= 32 {
			compressedWords[nextWordIndex] = int(bitBuf & 0xFFFFFFFF)
			nextWordIndex++
			bitBuf >>= 32
			bufBits -= 32
		}
	}

	// Pad the bitstream so that the decompressor's 12-bit peek can't overrun its input.
	padding := 10 - numBaseBits
	if padding < 0 {
		padding = 0
	}
	bufBits += padding
	if bufBits >= 32 {
		compressedWords[nextWordIndex] = int(bitBuf & 0xFFFFFFFF)
		nextWordIndex++
		bitBuf >>= 32
		bufBits -= 32
	}
	if bufBits > 0 {
		// Flush any remaining bits.
		compressedWords[nextWordIndex] = int(bitBuf & 0xFFFFFFFF)
		nextWordIndex++
	}
	return nextWordIndex
}