github.com/usbarmory/tamago@v0.0.0-20240508072735-8612bbe1e454/bits/bits32.go (about)

     1  // https://github.com/usbarmory/tamago
     2  //
     3  // Copyright (c) WithSecure Corporation
     4  // https://foundry.withsecure.com
     5  //
     6  // Use of this source code is governed by the license
     7  // that can be found in the LICENSE file.
     8  
     9  // Package bits provides primitives for bitwise operations on 32/64 bit
    10  // registers.
    11  package bits
    12  
    13  // Get returns the pointed value at a specific bit position and with a bitmask
    14  // applied.
    15  func Get(addr *uint32, pos int, mask int) uint32 {
    16  	return uint32((int(*addr) >> pos) & mask)
    17  }
    18  
    19  // Set modifies the pointed value by setting an individual bit at the position
    20  // argument.
    21  func Set(addr *uint32, pos int) {
    22  	*addr |= (1 << pos)
    23  }
    24  
    25  // Clear modifies the pointed value by clearing an individual bit at the
    26  // position argument.
    27  func Clear(addr *uint32, pos int) {
    28  	*addr &= ^(1 << pos)
    29  }
    30  
    31  // SetTo modifies the pointed value by setting an individual bit at the
    32  // position argument.
    33  func SetTo(addr *uint32, pos int, val bool) {
    34  	if val {
    35  		Set(addr, pos)
    36  	} else {
    37  		Clear(addr, pos)
    38  	}
    39  }
    40  
    41  // SetN modifies the pointed value by setting a value at a specific bit
    42  // position and with a bitmask applied.
    43  func SetN(addr *uint32, pos int, mask int, val uint32) {
    44  	*addr = (*addr & (^(uint32(mask) << pos))) | (val << pos)
    45  }