//go:noescape
func Xchg8(addr *uint8, v uint8) uint8
-//go:nosplit
-func goXchg8(addr *uint8, v uint8) uint8 {
- // Align down to 4 bytes and use 32-bit CAS.
- addr32 := (*uint32)(unsafe.Pointer(uintptr(unsafe.Pointer(addr)) &^ 3))
- shift := (uintptr(unsafe.Pointer(addr)) & 3) * 8 // little endian
- word := uint32(v) << shift
- mask := uint32(0xFF) << shift
-
- for {
- old := *addr32 // Read the old 32-bit value
- // Clear the old 8 bits then insert the new value
- if Cas(addr32, old, (old&^mask)|word) {
- // Return the old 8-bit value
- return uint8((old & mask) >> shift)
- }
- }
-}
-
//go:nosplit
func Xchguintptr(addr *uintptr, v uintptr) uintptr {
return uintptr(Xchg((*uint32)(unsafe.Pointer(addr)), uint32(v)))
//go:noescape
func Xchg(ptr *uint32, new uint32) uint32
+//go:nosplit
+func Xchg8(addr *uint8, v uint8) uint8 {
+ return goXchg8(addr, v)
+}
+
//go:noescape
func Xchg64(ptr *uint64, new uint64) uint64
return old
}
+//go:nosplit
+func Xchg8(addr *uint8, v uint8) uint8 {
+ return goXchg8(addr, v)
+}
+
//go:nosplit
//go:noinline
func Xchg64(ptr *uint64, new uint64) uint64 {
--- /dev/null
+// Copyright 2025 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package atomic
+
+import (
+ "internal/goarch"
+ "unsafe"
+)
+
+//go:nosplit
+func goXchg8(addr *uint8, v uint8) uint8 {
+ // Align down to 4 bytes and use 32-bit CAS.
+ addr32 := (*uint32)(unsafe.Pointer(uintptr(unsafe.Pointer(addr)) &^ 3))
+ shift := (uintptr(unsafe.Pointer(addr)) & 3)
+ if goarch.BigEndian {
+ shift = shift ^ 3
+ }
+ shift = shift * 8
+ word := uint32(v) << shift
+ mask := uint32(0xFF) << shift
+
+ for {
+ old := *addr32 // Read the old 32-bit value
+ // Clear the old 8 bits then insert the new value
+ if Cas(addr32, old, (old&^mask)|word) {
+ // Return the old 8-bit value
+ return uint8((old & mask) >> shift)
+ }
+ }
+}
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-//go:build 386 || amd64 || arm || arm64 || loong64 || mips || mipsle || mips64 || mips64le || ppc64 || ppc64le || riscv64
-
package atomic_test
import (