github.com/icexin/eggos@v0.4.2-0.20220216025428-78b167e4f349/kernel/simd.go (about)

     1  package kernel
     2  
     3  const (
     4  	_CPUID_ECX_XSAVE = 1 << 26
     5  	_CPUID_ECX_AVX   = 1 << 28
     6  	_CPUID_EBX_AVX2  = 1 << 5
     7  
     8  	_CPUID_FN_STD = 0x00000001
     9  	_CPUID_FN_EXT = 0x80000001
    10  )
    11  
    12  //go:nosplit
    13  func sseInit()
    14  
    15  //go:nosplit
    16  func avxInit()
    17  
    18  //go:nosplit
    19  func cpuid(fn, cx uint32) (eax, ebx, ecx, edx uint32)
    20  
    21  //go:nosplit
    22  func simdInit() {
    23  	sseInit()
    24  
    25  	// init for avx
    26  	// first check avx function
    27  	_, _, ecx, _ := cpuid(_CPUID_FN_STD, 0)
    28  	if ecx&_CPUID_ECX_XSAVE == 0 {
    29  		return
    30  	}
    31  	if ecx&_CPUID_ECX_AVX == 0 {
    32  		return
    33  	}
    34  	_, ebx, _, _ := cpuid(0x0007, 0)
    35  	if ebx&_CPUID_EBX_AVX2 == 0 {
    36  		return
    37  	}
    38  	// all check passed, init avx
    39  	avxInit()
    40  }