github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/klauspost/crc32/crc32_amd64.go (about)

     1  // Copyright 2011 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  
     5  // +build !appengine,!gccgo
     6  
     7  package crc32
     8  
     9  // This file contains the code to call the SSE 4.2 version of the Castagnoli
    10  // and IEEE CRC.
    11  
    12  // haveSSE41/haveSSE42/haveCLMUL are defined in crc_amd64.s and use
    13  // CPUID to test for SSE 4.1, 4.2 and CLMUL support.
    14  func haveSSE41() bool
    15  func haveSSE42() bool
    16  func haveCLMUL() bool
    17  
    18  // castagnoliSSE42 is defined in crc_amd64.s and uses the SSE4.2 CRC32
    19  // instruction.
    20  //go:noescape
    21  func castagnoliSSE42(crc uint32, p []byte) uint32
    22  
    23  // ieeeCLMUL is defined in crc_amd64.s and uses the PCLMULQDQ
    24  // instruction as well as SSE 4.1.
    25  //go:noescape
    26  func ieeeCLMUL(crc uint32, p []byte) uint32
    27  
    28  var sse42 = haveSSE42()
    29  var useFastIEEE = haveCLMUL() && haveSSE41()
    30  
    31  func updateCastagnoli(crc uint32, p []byte) uint32 {
    32  	if sse42 {
    33  		return castagnoliSSE42(crc, p)
    34  	}
    35  	// only use slicing-by-8 when input is >= 16 Bytes
    36  	if len(p) >= 16 {
    37  		return updateSlicingBy8(crc, castagnoliTable8, p)
    38  	}
    39  	return update(crc, castagnoliTable, p)
    40  }
    41  
    42  func updateIEEE(crc uint32, p []byte) uint32 {
    43  	if useFastIEEE && len(p) >= 64 {
    44  		left := len(p) & 15
    45  		do := len(p) - left
    46  		crc = ^ieeeCLMUL(^crc, p[:do])
    47  		if left > 0 {
    48  			crc = update(crc, IEEETable, p[do:])
    49  		}
    50  		return crc
    51  	}
    52  
    53  	// only use slicing-by-8 when input is >= 16 Bytes
    54  	if len(p) >= 16 {
    55  		ieeeTable8Once.Do(func() {
    56  			ieeeTable8 = makeTable8(IEEE)
    57  		})
    58  		return updateSlicingBy8(crc, ieeeTable8, p)
    59  	}
    60  
    61  	return update(crc, IEEETable, p)
    62  }