github.com/s1s1ty/go@v0.0.0-20180207192209-104445e3140f/src/internal/cpu/cpu_x86.go (about)

     1  // Copyright 2017 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 386 amd64 amd64p32
     6  
     7  package cpu
     8  
     9  const CacheLineSize = 64
    10  
    11  // cpuid is implemented in cpu_x86.s.
    12  func cpuid(eaxArg, ecxArg uint32) (eax, ebx, ecx, edx uint32)
    13  
    14  // xgetbv with ecx = 0 is implemented in cpu_x86.s.
    15  func xgetbv() (eax, edx uint32)
    16  
    17  func init() {
    18  	maxID, _, _, _ := cpuid(0, 0)
    19  
    20  	if maxID < 1 {
    21  		return
    22  	}
    23  
    24  	_, _, ecx1, edx1 := cpuid(1, 0)
    25  	X86.HasSSE2 = isSet(26, edx1)
    26  
    27  	X86.HasSSE3 = isSet(0, ecx1)
    28  	X86.HasPCLMULQDQ = isSet(1, ecx1)
    29  	X86.HasSSSE3 = isSet(9, ecx1)
    30  	X86.HasFMA = isSet(12, ecx1)
    31  	X86.HasSSE41 = isSet(19, ecx1)
    32  	X86.HasSSE42 = isSet(20, ecx1)
    33  	X86.HasPOPCNT = isSet(23, ecx1)
    34  	X86.HasAES = isSet(25, ecx1)
    35  	X86.HasOSXSAVE = isSet(27, ecx1)
    36  
    37  	osSupportsAVX := false
    38  	// For XGETBV, OSXSAVE bit is required and sufficient.
    39  	if X86.HasOSXSAVE {
    40  		eax, _ := xgetbv()
    41  		// Check if XMM and YMM registers have OS support.
    42  		osSupportsAVX = isSet(1, eax) && isSet(2, eax)
    43  	}
    44  
    45  	X86.HasAVX = isSet(28, ecx1) && osSupportsAVX
    46  
    47  	if maxID < 7 {
    48  		return
    49  	}
    50  
    51  	_, ebx7, _, _ := cpuid(7, 0)
    52  	X86.HasBMI1 = isSet(3, ebx7)
    53  	X86.HasAVX2 = isSet(5, ebx7) && osSupportsAVX
    54  	X86.HasBMI2 = isSet(8, ebx7)
    55  	X86.HasERMS = isSet(9, ebx7)
    56  	X86.HasADX = isSet(19, ebx7)
    57  }
    58  
    59  func isSet(bitpos uint, value uint32) bool {
    60  	return value&(1<<bitpos) != 0
    61  }