Source File
cpu.go
Belonging Package
internal/cpu
// Copyright 2017 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.// Package cpu implements processor feature detection// used by the Go standard library.package cpuimport _ // for linkname// CacheLinePad is used to pad structs to avoid false sharing.type CacheLinePad struct{ _ [CacheLinePadSize]byte }// CacheLineSize is the CPU's assumed cache line size.// There is currently no runtime detection of the real cache line size// so we use the constant per GOARCH CacheLinePadSize as an approximation.var CacheLineSize uintptr = CacheLinePadSize// The booleans in X86 contain the correspondingly named cpuid feature bit.// HasAVX and HasAVX2 are only set if the OS does support XMM and YMM registers// in addition to the cpuid feature bit being set.// The struct is padded to avoid false sharing.var X86 struct {_ CacheLinePadHasAES boolHasADX boolHasAVX boolHasAVXVNNI boolHasAVX2 boolHasAVX512 bool // Virtual feature: F+CD+BW+DQ+VLHasAVX512F boolHasAVX512CD boolHasAVX512BW boolHasAVX512DQ boolHasAVX512VL boolHasAVX512GFNI boolHasAVX512VAES boolHasAVX512VNNI boolHasAVX512VBMI boolHasAVX512VBMI2 boolHasAVX512BITALG boolHasAVX512VPOPCNTDQ boolHasAVX512VPCLMULQDQ boolHasBMI1 boolHasBMI2 boolHasERMS boolHasFSRM boolHasFMA boolHasGFNI boolHasOSXSAVE boolHasPCLMULQDQ boolHasPOPCNT boolHasRDTSCP boolHasSHA boolHasSSE3 boolHasSSSE3 boolHasSSE41 boolHasSSE42 boolHasVAES boolHasVPCLMULQDQ bool_ CacheLinePad}// The booleans in ARM contain the correspondingly named cpu feature bit.// The struct is padded to avoid false sharing.var ARM struct {_ CacheLinePadHasVFPv4 boolHasIDIVA boolHasV7Atomics bool_ CacheLinePad}// The booleans in ARM64 contain the correspondingly named cpu feature bit.// The struct is padded to avoid false sharing.var ARM64 struct {_ CacheLinePadHasAES boolHasPMULL boolHasSHA1 boolHasSHA2 boolHasSHA512 boolHasSHA3 boolHasCRC32 boolHasATOMICS boolHasCPUID boolHasDIT boolHasSB boolIsNeoverse bool_ CacheLinePad}// The booleans in Loong64 contain the correspondingly named cpu feature bit.// The struct is padded to avoid false sharing.var Loong64 struct {_ CacheLinePadHasLSX bool // support 128-bit vector extensionHasLASX bool // support 256-bit vector extensionHasCRC32 bool // support CRC instructionHasLAMCAS bool // support AMCAS[_DB].{B/H/W/D}HasLAM_BH bool // support AM{SWAP/ADD}[_DB].{B/H} instructionHasLLACQ_SCREL bool // support LLACQ.{W/D}, SCREL.{W/D} instructionHasSCQ bool // support SC.Q instructionHasDBAR_HINTS bool // supports finer-grained DBAR hints_ CacheLinePad}var MIPS64X struct {_ CacheLinePadHasMSA bool // MIPS SIMD architecture_ CacheLinePad}// For ppc64(le), it is safe to check only for ISA level starting on ISA v3.00,// since there are no optional categories. There are some exceptions that also// require kernel support to work (darn, scv), so there are feature bits for// those as well. The minimum processor requirement is POWER8 (ISA 2.07).// The struct is padded to avoid false sharing.var PPC64 struct {_ CacheLinePadHasDARN bool // Hardware random number generator (requires kernel enablement)HasSCV bool // Syscall vectored (requires kernel enablement)IsPOWER8 bool // ISA v2.07 (POWER8)IsPOWER9 bool // ISA v3.00 (POWER9)IsPOWER10 bool // ISA v3.1 (POWER10)_ CacheLinePad}var S390X struct {_ CacheLinePadHasZARCH bool // z architecture mode is active [mandatory]HasSTFLE bool // store facility list extended [mandatory]HasLDISP bool // long (20-bit) displacements [mandatory]HasEIMM bool // 32-bit immediates [mandatory]HasDFP bool // decimal floating pointHasETF3EH bool // ETF-3 enhancedHasMSA bool // message security assist (CPACF)HasAES bool // KM-AES{128,192,256} functionsHasAESCBC bool // KMC-AES{128,192,256} functionsHasAESCTR bool // KMCTR-AES{128,192,256} functionsHasAESGCM bool // KMA-GCM-AES{128,192,256} functionsHasGHASH bool // KIMD-GHASH functionHasSHA1 bool // K{I,L}MD-SHA-1 functionsHasSHA256 bool // K{I,L}MD-SHA-256 functionsHasSHA512 bool // K{I,L}MD-SHA-512 functionsHasSHA3 bool // K{I,L}MD-SHA3-{224,256,384,512} and K{I,L}MD-SHAKE-{128,256} functionsHasVX bool // vector facility. Note: the runtime sets this when it processes auxv records.HasVXE bool // vector-enhancements facility 1HasKDSA bool // elliptic curve functionsHasECDSA bool // NIST curvesHasEDDSA bool // Edwards curves_ CacheLinePad}// RISCV64 contains the supported CPU features and performance characteristics for riscv64// platforms. The booleans in RISCV64, with the exception of HasFastMisaligned, indicate// the presence of RISC-V extensions.// The struct is padded to avoid false sharing.var RISCV64 struct {_ CacheLinePadHasFastMisaligned bool // Fast misaligned accessesHasV bool // Vector extension compatible with RVV 1.0HasZbb bool // Basic bit-manipulation extensionHasZbc bool // Carryless multiplication extensionHasZvbb bool // Vector Basic Bit-manipulationHasZvbc bool // Vector Carryless MultiplicationHasZvkg bool // Vector GCM/GMACHasZvkned bool // NIST Suite: Vector AES Block CipherHasZvknha bool // NIST Suite: Vector SHA-2 Secure HashHasZvknhb bool // NIST Suite: Vector SHA-2 Secure HashHasZvksed bool // ShangMi Suite: SM4 Block CipherHasZvksh bool // ShangMi Suite: SM3 Secure HashHasZvkt bool // Vector Data-Independent Execution Latency_ CacheLinePad}// CPU feature variables are accessed by assembly code in various packages.//go:linkname X86//go:linkname ARM//go:linkname ARM64//go:linkname Loong64//go:linkname MIPS64X//go:linkname PPC64//go:linkname S390X//go:linkname RISCV64// doDerived, if non-nil, is called after processing GODEBUG to set "derived"// feature flags.var doDerived func()// Initialize examines the processor and sets the relevant variables above.// This is called by the runtime package early in program initialization,// before normal init functions are run. env is set by runtime if the OS supports// cpu feature options in GODEBUG.func ( string) {doinit()processOptions()if doDerived != nil {doDerived()}}// options contains the cpu debug options that can be used in GODEBUG.// Options are arch dependent and are added by the arch specific doinit functions.// Features that are mandatory for the specific GOARCH should not be added to options// (e.g. SSE2 on amd64).var options []option// Option names should be lower case. e.g. avx instead of AVX.type option struct {Name stringFeature *boolSpecified bool // whether feature value was specified in GODEBUGEnable bool // whether feature should be enabled}// processOptions enables or disables CPU feature values based on the parsed env string.// The env string is expected to be of the form cpu.feature1=value1,cpu.feature2=value2...// where feature names is one of the architecture specific list stored in the// cpu packages options variable and values are either 'on' or 'off'.// If env contains cpu.all=off then all cpu features referenced through the options// variable are disabled. Other feature names and values result in warning messages.func processOptions( string) {:for != "" {:= "":= indexByte(, ',')if < 0 {, = , ""} else {, = [:], [+1:]}if len() < 4 || [:4] != "cpu." {continue}= indexByte(, '=')if < 0 {print("GODEBUG: no value specified for \"", , "\"\n")continue}, := [4:], [+1:] // e.g. "SSE2", "on"var boolswitch {case "on":= truecase "off":= falsedefault:print("GODEBUG: value \"", , "\" not supported for cpu option \"", , "\"\n")continue}if == "all" {for := range options {options[].Specified = trueoptions[].Enable =}continue}for := range options {if options[].Name == {options[].Specified = trueoptions[].Enable =continue}}print("GODEBUG: unknown cpu feature \"", , "\"\n")}for , := range options {if !.Specified {continue}if .Enable && !*.Feature {print("GODEBUG: can not enable \"", .Name, "\", missing CPU support\n")continue}*.Feature = .Enable}}// indexByte returns the index of the first instance of c in s,// or -1 if c is not present in s.// indexByte is semantically the same as [strings.IndexByte].// We copy this function because "internal/cpu" should not have external dependencies.func indexByte( string, byte) int {for := 0; < len(); ++ {if [] == {return}}return -1}
![]() |
The pages are generated with Golds v0.8.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |