aboutsummaryrefslogtreecommitdiff
path: root/internal/adler32/adler32_amd64.go
blob: 7dfab299b5023fd4eacb64ba1147d6818f7c66f9 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//go:build amd64 && !purego

package adler32

import (
	"encoding/binary"
	"errors"
	"hash"
	"hash/adler32"

	"golang.org/x/sys/cpu"
)

// Size of an Adler-32 checksum in bytes.
const Size = 4

var hasAVX2 = cpu.X86.HasAVX2

// digest represents the partial evaluation of a checksum.
// The low 16 bits are s1, the high 16 bits are s2.
type digest uint32

func (d *digest) Reset() { *d = 1 }

// New returns a new hash.Hash32 computing the Adler-32 checksum.
func New() hash.Hash32 {
	if !hasAVX2 {
		return adler32.New()
	}

	d := new(digest)
	d.Reset()

	return d
}

func (d *digest) MarshalBinary() ([]byte, error) {
	b := make([]byte, 0, marshaledSize)
	b = append(b, magic...)
	b = binary.BigEndian.AppendUint32(b, uint32(*d))

	return b, nil
}

func (d *digest) UnmarshalBinary(b []byte) error {
	if len(b) < len(magic) || string(b[:len(magic)]) != magic {
		return errors.New("hash/adler32: invalid hash state identifier")
	}

	if len(b) != marshaledSize {
		return errors.New("hash/adler32: invalid hash state size")
	}

	*d = digest(binary.BigEndian.Uint32(b[len(magic):]))

	return nil
}

func (d *digest) Size() int { return Size }

func (d *digest) BlockSize() int { return 4 }

func (d *digest) Write(data []byte) (nn int, err error) {
	if hasAVX2 && len(data) >= 64 {
		h := adler32_avx2(uint32(*d), data)
		*d = digest(h)
	} else {
		h := update(uint32(*d), data)
		*d = digest(h)
	}

	return len(data), nil
}

func (d *digest) Sum32() uint32 { return uint32(*d) }

func (d *digest) Sum(in []byte) []byte {
	return binary.BigEndian.AppendUint32(in, uint32(*d))
}

// Checksum returns the Adler-32 checksum of data.
func Checksum(data []byte) uint32 {
	if hasAVX2 && len(data) >= 64 {
		return adler32_avx2(1, data)
	}

	return adler32.Checksum(data)
}