// Saved Apple M5 Max assembly for the fixed-width public contenders.
// Machine: Apple M5 Max, Mac17,7
// Compiler: Apple clang 21.0.0 (clang-2100.1.1.101)
// Command:
//   clang -std=c23 -O2 -mcpu=native -S spinlock-contenders.c
//
// This is a reduced extract. The source uses _Atomic uint64_t counters and
// _Atomic uint32_t lock words.

_fetch_add_increment:
    mov     w8, #1
    ldadd   x8, x8, [x0]
    ret

_weak_cas_increment:
    ldr     x9, [x0]
    mov     x8, x9
Lweak_retry:
    add     x10, x9, #1
    cas     x8, x10, [x0]
    cmp     x8, x9
    mov     x9, x8
    b.ne    Lweak_retry
    ret

_strong_cas_increment:
    ldr     x9, [x0]
    mov     x8, x9
Lstrong_retry:
    add     x10, x9, #1
    cas     x8, x10, [x0]
    cmp     x8, x9
    mov     x9, x8
    b.ne    Lstrong_retry
    ret

// backed_off_cas_increment uses the same CAS attempt. A failed comparison
// calls nanosleep before the next attempt.

_naive_tas_lock:
    mov     w8, #1
Ltas_retry:
    swpa    w8, w9, [x0]
    cbnz    w9, Ltas_retry
    ret

// The eight read-only probes are unrolled by Apple Clang.
_sleeping_lock_probe:
    ldr     w8, [x19]
    cbnz    w8, Lnext_probe
    swpa    w20, w8, [x19]
    cbz     w8, Lacquired
Lnext_probe:
    // seven more load / conditional swpa pairs, then nanosleep

_sleeping_unlock:
    stlr    wzr, [x0]
    ret

_event_lock_wait:
    ldr     w10, [x0]
    cbz     w10, Levent_retry_exchange
    cmp     w9, #63
    b.hi    Levent_wfe
    yield
    b       _event_lock_wait
Levent_wfe:
    wfe
    b       _event_lock_wait
Levent_retry_exchange:
    swpa    w8, w10, [x0]

_event_unlock:
    stlr    wzr, [x0]
    sev
    ret
