package guts

import (
	
	
)

// Each platform provides compressNode, which compresses a single node;
// compressChunkBlocks, which compresses the leading blocks of a chunk (the
// first of which starts the chunk), replacing cv with the result; and three
// kernels that compress many nodes at once:
//
//   - compressChunks stores the chaining values of the first n chunks of buf
//     in cvs.
//   - compressParents merges the first n pairs of cvs, storing the chaining
//     value of each pair's parent node in the first n elements of cvs.
//   - compressBlocks compresses MaxSIMD copies of a node with successive
//     counter values, storing the results in out.
//
// A kernel may process more nodes than requested, clobbering unused elements
// of cvs.

// Compress compresses a node, specified by its fields, into a 16-word output.
// It is equivalent to CompressNode, but avoids copying.
func ( *[16]uint32,  *[8]uint32,  *[16]uint32,  uint64,  uint32,  uint32) {
	compressNode(, , , , , )
}

// CompressNode compresses a node into a 16-word output.
func ( Node) ( [16]uint32) {
	compressNode(&, &.CV, &.Block, .Counter, .BlockLen, .Flags)
	return
}

// ChainingValue compresses n and returns the first 8 output words.
func ( Node) [8]uint32 {
	var  [16]uint32
	compressNode(&, &.CV, &.Block, .Counter, .BlockLen, .Flags)
	return [8]uint32([:8])
}

// PadBlock returns the words of b, which must be no longer than a block,
// padded with zeros.
func ( []byte) ( [16]uint32) {
	if littleEndian {
		copy((*[BlockSize]byte)(unsafe.Pointer(&))[:], )
		return
	}
	var  [BlockSize]byte
	copy([:], )
	return bytesToWordsSlow(&)
}

// CompressBuffer compresses up to MaxSIMD chunks in parallel and returns their
// root node.
func ( *[MaxSIMD * ChunkSize]byte,  int,  *[8]uint32,  uint64,  uint32) Node {
	if  <= ChunkSize {
		return CompressChunk([:], , , )
	}
	var  [MaxSIMD][8]uint32
	 := uint64( / ChunkSize)
	compressChunks(&, , int(), , , )
	if  :=  % ChunkSize;  != 0 {
		// the partial chunk must be compressed serially
		[] = ChainingValue(CompressChunk([-:], , +, ))
		++
	}
	return mergeSubtrees(&, , , )
}

// CompressChunk compresses a single chunk, returning its final (uncompressed)
// node.
func ( []byte,  *[8]uint32,  uint64,  uint32) Node {
	 := Node{
		CV:       *,
		Counter:  ,
		BlockLen: BlockSize,
		Flags:     | FlagChunkStart,
	}
	// compress every block but the last
	if  := (len() - 1) / BlockSize * BlockSize;  > 0 {
		compressChunkBlocks(&.CV, [:], , )
		 = [:]
		.Flags &^= FlagChunkStart
	}
	.Block = PadBlock()
	.BlockLen = uint32(len())
	.Flags |= FlagChunkEnd
	return 
}

// CompressBlocks compresses MaxSIMD copies of n with successive counter values,
// storing the results in out.
func ( *[MaxSIMD * BlockSize]byte,  Node) {
	compressBlocks(, )
}

// mergeSubtrees merges numCVs chaining values, returning their root node.
func mergeSubtrees( *[MaxSIMD][8]uint32,  uint64,  *[8]uint32,  uint32) Node {
	for  > 2 {
		// an odd CV is carried up to the next level
		 := [-1]
		compressParents(, int(/2), , )
		if %2 != 0 {
			[/2] = 
		}
		 = ( + 1) / 2
	}
	return ParentNode([0], [1], , )
}

// On little-endian platforms, bytes and words can be converted by
// reinterpreting memory.
var littleEndian = binary.NativeEndian.Uint16([]byte{1, 0}) == 1

// BytesToWords converts an array of 64 bytes to an array of 16 words.
func ( [64]byte) [16]uint32 {
	// NOTE: the slow path is a separate function so that this one can be
	// inlined.
	if littleEndian {
		return *(*[16]uint32)(unsafe.Pointer(&))
	}
	return bytesToWordsSlow(&)
}

func bytesToWordsSlow( *[64]byte) ( [16]uint32) {
	for  := range  {
		[] = binary.LittleEndian.Uint32([4*:])
	}
	return
}

// WordsToBytes converts an array of 16 words to an array of 64 bytes.
func ( [16]uint32) [64]byte {
	if littleEndian {
		return *(*[64]byte)(unsafe.Pointer(&))
	}
	return wordsToBytesSlow(&)
}

func wordsToBytesSlow( *[16]uint32) ( [64]byte) {
	for ,  := range  {
		binary.LittleEndian.PutUint32([4*:], )
	}
	return
}