blob: 4e50b56c6f9597bb7176006728049e1e7cf37ec4 [file] [log] [blame]
Chris Zou5833d842016-04-18 19:30:17 -04001// Copyright 2016 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package crc32
6
Ruixin(Peter) Bao9a3f22b2020-04-27 15:23:37 -04007import "internal/cpu"
8
Chris Zou5833d842016-04-18 19:30:17 -04009const (
10 vxMinLen = 64
Michael Munday4b17b152016-08-20 21:09:53 -040011 vxAlignMask = 15 // align to 16 bytes
Chris Zou5833d842016-04-18 19:30:17 -040012)
13
Ruixin(Peter) Bao9a3f22b2020-04-27 15:23:37 -040014// hasVX reports whether the machine has the z/Architecture
Chris Zou5833d842016-04-18 19:30:17 -040015// vector facility installed and enabled.
Ruixin(Peter) Bao9a3f22b2020-04-27 15:23:37 -040016var hasVX = cpu.S390X.HasVX
Chris Zou5833d842016-04-18 19:30:17 -040017
18// vectorizedCastagnoli implements CRC32 using vector instructions.
19// It is defined in crc32_s390x.s.
Russ Cox98396682022-01-30 20:13:43 -050020//
Chris Zou5833d842016-04-18 19:30:17 -040021//go:noescape
22func vectorizedCastagnoli(crc uint32, p []byte) uint32
23
24// vectorizedIEEE implements CRC32 using vector instructions.
25// It is defined in crc32_s390x.s.
Russ Cox98396682022-01-30 20:13:43 -050026//
Chris Zou5833d842016-04-18 19:30:17 -040027//go:noescape
28func vectorizedIEEE(crc uint32, p []byte) uint32
29
Radu Berindebdde1012016-08-28 14:36:06 -040030func archAvailableCastagnoli() bool {
31 return hasVX
Radu Berinde90c3cf42016-08-27 13:17:30 -040032}
33
Radu Berindebdde1012016-08-28 14:36:06 -040034var archCastagnoliTable8 *slicing8Table
35
36func archInitCastagnoli() {
37 if !hasVX {
38 panic("not available")
Chris Zou5833d842016-04-18 19:30:17 -040039 }
Radu Berindebdde1012016-08-28 14:36:06 -040040 // We still use slicing-by-8 for small buffers.
41 archCastagnoliTable8 = slicingMakeTable(Castagnoli)
Chris Zou5833d842016-04-18 19:30:17 -040042}
43
Radu Berindebdde1012016-08-28 14:36:06 -040044// archUpdateCastagnoli calculates the checksum of p using
45// vectorizedCastagnoli.
46func archUpdateCastagnoli(crc uint32, p []byte) uint32 {
47 if !hasVX {
48 panic("not available")
Chris Zou5833d842016-04-18 19:30:17 -040049 }
Radu Berindebdde1012016-08-28 14:36:06 -040050 // Use vectorized function if data length is above threshold.
51 if len(p) >= vxMinLen {
Chris Zou5833d842016-04-18 19:30:17 -040052 aligned := len(p) & ^vxAlignMask
53 crc = vectorizedCastagnoli(crc, p[:aligned])
54 p = p[aligned:]
Radu Berindebdde1012016-08-28 14:36:06 -040055 }
56 if len(p) == 0 {
Chris Zou5833d842016-04-18 19:30:17 -040057 return crc
58 }
Radu Berindebdde1012016-08-28 14:36:06 -040059 return slicingUpdate(crc, archCastagnoliTable8, p)
Chris Zou5833d842016-04-18 19:30:17 -040060}
61
Radu Berindebdde1012016-08-28 14:36:06 -040062func archAvailableIEEE() bool {
63 return hasVX
64}
65
66var archIeeeTable8 *slicing8Table
67
68func archInitIEEE() {
69 if !hasVX {
70 panic("not available")
71 }
72 // We still use slicing-by-8 for small buffers.
73 archIeeeTable8 = slicingMakeTable(IEEE)
74}
75
76// archUpdateIEEE calculates the checksum of p using vectorizedIEEE.
77func archUpdateIEEE(crc uint32, p []byte) uint32 {
78 if !hasVX {
79 panic("not available")
80 }
81 // Use vectorized function if data length is above threshold.
82 if len(p) >= vxMinLen {
Chris Zou5833d842016-04-18 19:30:17 -040083 aligned := len(p) & ^vxAlignMask
84 crc = vectorizedIEEE(crc, p[:aligned])
85 p = p[aligned:]
Radu Berindebdde1012016-08-28 14:36:06 -040086 }
87 if len(p) == 0 {
Chris Zou5833d842016-04-18 19:30:17 -040088 return crc
89 }
Radu Berindebdde1012016-08-28 14:36:06 -040090 return slicingUpdate(crc, archIeeeTable8, p)
Chris Zou5833d842016-04-18 19:30:17 -040091}