1 // Copyright 2025 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 atomic
6 7 import (
8 "internal/goarch"
9 "unsafe"
10 )
11 12 //go:nosplit
13 func goXchg8(addr *uint8, v uint8) uint8 {
14 // Align down to 4 bytes and use 32-bit CAS.
15 addr32 := (*uint32)(unsafe.Pointer(uintptr(unsafe.Pointer(addr)) &^ 3))
16 shift := (uintptr(unsafe.Pointer(addr)) & 3)
17 if goarch.BigEndian {
18 shift = shift ^ 3
19 }
20 shift = shift * 8
21 word := uint32(v) << shift
22 mask := uint32(0xFF) << shift
23 24 for {
25 old := *addr32 // Read the old 32-bit value
26 // Clear the old 8 bits then insert the new value
27 if Cas(addr32, old, (old&^mask)|word) {
28 // Return the old 8-bit value
29 return uint8((old & mask) >> shift)
30 }
31 }
32 }
33