github.com/linuxboot/fiano@v1.2.0/pkg/bytes/is_zero_filled_amd64.go (about)

     1  // Copyright 2019 the LinuxBoot 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  //go:build amd64
     6  // +build amd64
     7  
     8  package bytes
     9  
    10  import (
    11  	"reflect"
    12  	"unsafe"
    13  )
    14  
    15  // IsZeroFilled returns true if b consists of zeros only.
    16  func IsZeroFilled(b []byte) bool {
    17  	hdr := (*reflect.SliceHeader)((unsafe.Pointer)(&b))
    18  	data := unsafe.Pointer(hdr.Data)
    19  	length := hdr.Len
    20  	if length == 0 {
    21  		return true
    22  	}
    23  
    24  	if uintptr(data)&0x07 != 0 {
    25  		// the data is not aligned, fallback to a simple way
    26  		return isZeroFilledSimple(b)
    27  	}
    28  
    29  	dataEnd := uintptr(data) + uintptr(length)
    30  	dataWordsEnd := uintptr(dataEnd) & ^uintptr(0x07)
    31  	// example:
    32  	//
    33  	//     012345678901234567
    34  	//     wwwwwwwwWWWWWWWWtt : w -- word 0; W -- word 1; t -- tail
    35  	//                     ^
    36  	//                     |
    37  	//                     +-- dataWordsEnd
    38  	for ; uintptr(data) < dataWordsEnd; data = unsafe.Pointer(uintptr(data) + 8) {
    39  		if *(*uint64)(data) != 0 {
    40  			return false
    41  		}
    42  	}
    43  	for ; uintptr(data) < dataEnd; data = unsafe.Pointer(uintptr(data) + 1) {
    44  		if *(*uint8)(data) != 0 {
    45  			return false
    46  		}
    47  	}
    48  	return true
    49  }