func lowLevelUncompressPairs()

in cpc/cpc_compressed_state.go [851:931]


func lowLevelUncompressPairs(pairArray []int, numPairsToDecode, numBaseBits int, compressedWords []int, numCompressedWords int) error {
	// Output index for pairArray.
	pairIndex := 0
	ptrArr := make([]int64, 3)
	nextWordIndex := 0
	var bitBuf uint64 = 0
	bufBits := 0

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

	predictedRowIndex := 0
	predictedColIndex := 0

	// For each pair to decode:
	for pairIndex < numPairsToDecode {
		// Ensure we have at least 12 bits in bitBuf.
		if bufBits < 12 {
			if nextWordIndex >= len(compressedWords) {
				return fmt.Errorf("lowLevelUncompressPairs: insufficient compressedWords data")
			}
			bitBuf |= (uint64(compressedWords[nextWordIndex]) & 0xFFFFFFFF) << uint(bufBits)
			nextWordIndex++
			bufBits += 32
		}

		// Peek 12 bits.
		peek12 := int(bitBuf & 0xFFF) // 0xFFF is 12 bits.
		lookup := int(lengthLimitedUnaryDecodingTable65[peek12]) & 0xFFFF
		codeWordLength := lookup >> 8
		xDelta := lookup & 0xFF

		// Consume the xDelta bits.
		bitBuf >>= uint(codeWordLength)
		bufBits -= codeWordLength

		// Inline ReadUnary:
		ptrArr[NextWordIdx] = int64(nextWordIndex)
		ptrArr[BitBuf] = int64(bitBuf)
		ptrArr[BufBits] = int64(bufBits)
		golombHi := readUnary(compressedWords, ptrArr)
		// Retrieve updated values.
		nextWordIndex = int(ptrArr[NextWordIdx])
		bitBuf = uint64(ptrArr[BitBuf])
		bufBits = int(ptrArr[BufBits])

		// Ensure at least numBaseBits in bitBuf.
		if bufBits < numBaseBits {
			if nextWordIndex >= len(compressedWords) {
				return fmt.Errorf("lowLevelUncompressPairs: insufficient compressedWords data for golombLo")
			}
			bitBuf |= (uint64(compressedWords[nextWordIndex]) & 0xFFFFFFFF) << uint(bufBits)
			nextWordIndex++
			bufBits += 32
		}

		golombLo := bitBuf & golombLoMask
		bitBuf >>= uint(numBaseBits)
		bufBits -= numBaseBits

		// yDelta is the combination of the unary high and the base bits.
		yDelta := (uint64(golombHi) << uint(numBaseBits)) | golombLo
		// Now compute the pair's row and column.
		if yDelta > 0 {
			predictedColIndex = 0
		}
		rowIndex := predictedRowIndex + int(yDelta)
		colIndex := predictedColIndex + xDelta
		rowCol := (rowIndex << 6) | colIndex
		pairArray[pairIndex] = rowCol
		pairIndex++

		predictedRowIndex = rowIndex
		predictedColIndex = colIndex + 1
	}

	if nextWordIndex > numCompressedWords {
		return fmt.Errorf("lowLevelUncompressPairs: nextWordIndex %d exceeds numCompressedWords %d", nextWordIndex, numCompressedWords)
	}
	return nil
}