github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/compile/internal/gc/bitset.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  package gc
     6  
     7  type bitset8 uint8
     8  
     9  func (f *bitset8) set(mask uint8, b bool) {
    10  	if b {
    11  		*(*uint8)(f) |= mask
    12  	} else {
    13  		*(*uint8)(f) &^= mask
    14  	}
    15  }
    16  
    17  type bitset16 uint16
    18  
    19  func (f *bitset16) set(mask uint16, b bool) {
    20  	if b {
    21  		*(*uint16)(f) |= mask
    22  	} else {
    23  		*(*uint16)(f) &^= mask
    24  	}
    25  }
    26  
    27  type bitset32 uint32
    28  
    29  func (f *bitset32) set(mask uint32, b bool) {
    30  	if b {
    31  		*(*uint32)(f) |= mask
    32  	} else {
    33  		*(*uint32)(f) &^= mask
    34  	}
    35  }
    36  
    37  func (f bitset32) get2(shift uint8) uint8 {
    38  	return uint8(f>>shift) & 3
    39  }
    40  
    41  // set2 sets two bits in f using the bottom two bits of b.
    42  func (f *bitset32) set2(shift uint8, b uint8) {
    43  	// Clear old bits.
    44  	*(*uint32)(f) &^= 3 << shift
    45  	// Set new bits.
    46  	*(*uint32)(f) |= uint32(b&3) << shift
    47  }
    48  
    49  func (f bitset32) get3(shift uint8) uint8 {
    50  	return uint8(f>>shift) & 7
    51  }
    52  
    53  // set3 sets three bits in f using the bottom three bits of b.
    54  func (f *bitset32) set3(shift uint8, b uint8) {
    55  	// Clear old bits.
    56  	*(*uint32)(f) &^= 7 << shift
    57  	// Set new bits.
    58  	*(*uint32)(f) |= uint32(b&7) << shift
    59  }