Source file src/hash/crc32/crc32_riscv64.go

     1  // Copyright 2026 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  package crc32
     6  
     7  import "internal/cpu"
     8  
     9  //go:noescape
    10  func ieeeUpdateCLMUL(crc uint32, tab *Table, p []byte) uint32
    11  
    12  //go:noescape
    13  func castagnoliUpdateCLMUL(crc uint32, tab *Table, p []byte) uint32
    14  
    15  func archAvailableIEEE() bool {
    16  	return cpu.RISCV64.HasZbc
    17  }
    18  
    19  var archIeeeTable8 *slicing8Table
    20  
    21  func archInitIEEE() {
    22  	if !cpu.RISCV64.HasZbc {
    23  		panic("arch-specific zbc instruction for IEEE not available")
    24  	}
    25  	// The slicing-by-8 table is used for byte-at-a-time alignment (inside the
    26  	// CLMUL routine) and for the tail that does not form a complete 16-byte
    27  	// block. It is also the fallback for inputs shorter than 16 bytes.
    28  	archIeeeTable8 = slicingMakeTable(IEEE)
    29  }
    30  
    31  func archUpdateIEEE(crc uint32, p []byte) uint32 {
    32  	if !cpu.RISCV64.HasZbc {
    33  		panic("arch-specific zbc instruction for IEEE not available")
    34  	}
    35  
    36  	// The CLMUL routine byte-aligns the input to a 16-byte boundary before
    37  	// the fold loop, so it is safe on cores that trap on unaligned accesses.
    38  	// Give it all complete 16-byte blocks.
    39  	if len(p) >= 16 {
    40  		aligned := len(p) &^ 15
    41  		crc = ^ieeeUpdateCLMUL(^crc, &archIeeeTable8[0], p[:aligned])
    42  		p = p[aligned:]
    43  	}
    44  	if len(p) == 0 {
    45  		return crc
    46  	}
    47  	return slicingUpdate(crc, archIeeeTable8, p)
    48  }
    49  
    50  func archAvailableCastagnoli() bool {
    51  	return cpu.RISCV64.HasZbc
    52  }
    53  
    54  var archCastagnoliTable8 *slicing8Table
    55  
    56  func archInitCastagnoli() {
    57  	if !cpu.RISCV64.HasZbc {
    58  		panic("arch-specific zbc instruction for Castagnoli not available")
    59  	}
    60  	// The slicing-by-8 table is used for byte-at-a-time alignment (inside the
    61  	// CLMUL routine) and for the tail that does not form a complete 16-byte
    62  	// block. It is also the fallback for inputs shorter than 16 bytes.
    63  	archCastagnoliTable8 = slicingMakeTable(Castagnoli)
    64  }
    65  
    66  func archUpdateCastagnoli(crc uint32, p []byte) uint32 {
    67  	if !cpu.RISCV64.HasZbc {
    68  		panic("arch-specific zbc instruction for Castagnoli not available")
    69  	}
    70  
    71  	// Same as archUpdateIEEE.
    72  	if len(p) >= 16 {
    73  		aligned := len(p) &^ 15
    74  		crc = ^castagnoliUpdateCLMUL(^crc, &archCastagnoliTable8[0], p[:aligned])
    75  		p = p[aligned:]
    76  	}
    77  	if len(p) == 0 {
    78  		return crc
    79  	}
    80  	return slicingUpdate(crc, archCastagnoliTable8, p)
    81  }
    82  

View as plain text