|
1 | 1 | package volatile |
2 | 2 |
|
3 | | -// This file defines Register{8,16,32} types, which are convenience types for |
| 3 | +// This file defines Register{8,16,32,64} types, which are convenience types for |
4 | 4 | // volatile register accesses. |
5 | 5 |
|
6 | 6 | // Special types that causes loads/stores to be volatile (necessary for |
@@ -190,3 +190,65 @@ func (r *Register32) HasBits(value uint32) bool { |
190 | 190 | func (r *Register32) ReplaceBits(value uint32, mask uint32, pos uint8) { |
191 | 191 | StoreUint32(&r.Reg, LoadUint32(&r.Reg)&^(mask<<pos)|value<<pos) |
192 | 192 | } |
| 193 | + |
| 194 | +type Register64 struct { |
| 195 | + Reg uint64 |
| 196 | +} |
| 197 | + |
| 198 | +// Get returns the value in the register. It is the volatile equivalent of: |
| 199 | +// |
| 200 | +// *r.Reg |
| 201 | +// |
| 202 | +//go:inline |
| 203 | +func (r *Register64) Get() uint64 { |
| 204 | + return LoadUint64(&r.Reg) |
| 205 | +} |
| 206 | + |
| 207 | +// Set updates the register value. It is the volatile equivalent of: |
| 208 | +// |
| 209 | +// *r.Reg = value |
| 210 | +// |
| 211 | +//go:inline |
| 212 | +func (r *Register64) Set(value uint64) { |
| 213 | + StoreUint64(&r.Reg, value) |
| 214 | +} |
| 215 | + |
| 216 | +// SetBits reads the register, sets the given bits, and writes it back. It is |
| 217 | +// the volatile equivalent of: |
| 218 | +// |
| 219 | +// r.Reg |= value |
| 220 | +// |
| 221 | +//go:inline |
| 222 | +func (r *Register64) SetBits(value uint64) { |
| 223 | + StoreUint64(&r.Reg, LoadUint64(&r.Reg)|value) |
| 224 | +} |
| 225 | + |
| 226 | +// ClearBits reads the register, clears the given bits, and writes it back. It |
| 227 | +// is the volatile equivalent of: |
| 228 | +// |
| 229 | +// r.Reg &^= value |
| 230 | +// |
| 231 | +//go:inline |
| 232 | +func (r *Register64) ClearBits(value uint64) { |
| 233 | + StoreUint64(&r.Reg, LoadUint64(&r.Reg)&^value) |
| 234 | +} |
| 235 | + |
| 236 | +// HasBits reads the register and then checks to see if the passed bits are set. It |
| 237 | +// is the volatile equivalent of: |
| 238 | +// |
| 239 | +// (*r.Reg & value) > 0 |
| 240 | +// |
| 241 | +//go:inline |
| 242 | +func (r *Register64) HasBits(value uint64) bool { |
| 243 | + return (r.Get() & value) > 0 |
| 244 | +} |
| 245 | + |
| 246 | +// ReplaceBits is a helper to simplify setting multiple bits high and/or low at |
| 247 | +// once. It is the volatile equivalent of: |
| 248 | +// |
| 249 | +// r.Reg = (r.Reg & ^(mask << pos)) | value << pos |
| 250 | +// |
| 251 | +// go:inline |
| 252 | +func (r *Register64) ReplaceBits(value uint64, mask uint64, pos uint8) { |
| 253 | + StoreUint64(&r.Reg, LoadUint64(&r.Reg)&^(mask<<pos)|value<<pos) |
| 254 | +} |
0 commit comments