github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/queues/lscq/util.go (about)

     1  package lscq
     2  
     3  import (
     4  	"unsafe"
     5  
     6  	"github.com/songzhibin97/go-baseutils/sys/cpu"
     7  )
     8  
     9  const (
    10  	scqsize           = 1 << 16
    11  	lscqcacheLineSize = unsafe.Sizeof(cpu.CacheLinePad{})
    12  )
    13  
    14  func uint64Get63(value uint64) uint64 {
    15  	return value & ((1 << 63) - 1)
    16  }
    17  
    18  func uint64Get1(value uint64) bool {
    19  	return (value & (1 << 63)) == (1 << 63)
    20  }
    21  
    22  func uint64GetAll(value uint64) (bool, uint64) {
    23  	return (value & (1 << 63)) == (1 << 63), value & ((1 << 63) - 1)
    24  }
    25  
    26  func loadSCQFlags(flags uint64) (isSafe bool, isEmpty bool, cycle uint64) {
    27  	isSafe = (flags & (1 << 63)) == (1 << 63)
    28  	isEmpty = (flags & (1 << 62)) == (1 << 62)
    29  	cycle = flags & ((1 << 62) - 1)
    30  	return isSafe, isEmpty, cycle
    31  }
    32  
    33  func newSCQFlags(isSafe bool, isEmpty bool, cycle uint64) uint64 {
    34  	v := cycle & ((1 << 62) - 1)
    35  	if isSafe {
    36  		v += 1 << 63
    37  	}
    38  	if isEmpty {
    39  		v += 1 << 62
    40  	}
    41  	return v
    42  }
    43  
    44  func cacheRemap16Byte(index uint64) uint64 {
    45  	const cacheLineSize = lscqcacheLineSize / 2
    46  	rawIndex := index & uint64(scqsize-1)
    47  	cacheLineNum := (rawIndex) % (scqsize / uint64(cacheLineSize))
    48  	cacheLineIdx := rawIndex / (scqsize / uint64(cacheLineSize))
    49  	return cacheLineNum*uint64(cacheLineSize) + cacheLineIdx
    50  }