// Copyright 2016 Tom Thorogood. All rights reserved.
// Use of this source code is governed by a
// Modified BSD License license that can be found in
// the LICENSE file.

// +build amd64,!gccgo,!appengine

package hex

import 

// RawEncode encodes src into EncodedLen(len(src))
// bytes of dst.  As a convenience, it returns the number
// of bytes written to dst, but this value is always EncodedLen(len(src)).
// RawEncode implements hexadecimal encoding for a given alphabet.
func (, ,  []byte) int {
	if len() != 16 {
		panic("invalid alphabet")
	}

	if len() < len()*2 {
		panic("dst buffer is too small")
	}

	if len() == 0 {
		return 0
	}

	switch {
	case cpu.X86.HasAVX:
		encodeAVX(&[0], &[0], uint64(len()), &[0])
	case cpu.X86.HasSSE41:
		encodeSSE(&[0], &[0], uint64(len()), &[0])
	default:
		encodeGeneric(, , )
	}

	return len() * 2
}

// Decode decodes src into DecodedLen(len(src)) bytes, returning the actual
// number of bytes written to dst.
//
// If Decode encounters invalid input, it returns an error describing the failure.
func (,  []byte) (int, error) {
	if len()%2 != 0 {
		return 0, errLength
	}

	if len() < len()/2 {
		panic("dst buffer is too small")
	}

	if len() == 0 {
		return 0, nil
	}

	var (
		  uint64
		 bool
	)
	switch {
	case cpu.X86.HasAVX:
		,  = decodeAVX(&[0], &[0], uint64(len()))
	case cpu.X86.HasSSE41:
		,  = decodeSSE(&[0], &[0], uint64(len()))
	default:
		,  = decodeGeneric(, )
	}

	if ! {
		return 0, InvalidByteError([])
	}

	return len() / 2, nil
}

//go:generate go run asm_gen.go

// This function is implemented in hex_encode_amd64.s
//go:noescape
func ( *byte,  *byte,  uint64,  *byte)

// This function is implemented in hex_encode_amd64.s
//go:noescape
func ( *byte,  *byte,  uint64,  *byte)

// This function is implemented in hex_decode_amd64.s
//go:noescape
func ( *byte,  *byte,  uint64) ( uint64,  bool)

// This function is implemented in hex_decode_amd64.s
//go:noescape
func ( *byte,  *byte,  uint64) ( uint64,  bool)