Chris Low-level systems developer
← Back to all posts

The Add That Returns the Old Value

ISA

The claim is about the amoadd.w instruction's semantics: rd receives the pre-add value while memory receives old plus rs2, as one atomic operation. The listings show each drop as a single amoadd.w against the naive lw, addiw, sw triple, and the QEMU run shows rd holding the old value on every call.

MICROARCH

No claim depends on how any core implements anything. No cache, pipeline, or timing behavior is asserted or measured; QEMU serves as a functional executor only, and the old-value contract is complete from the instruction semantics and the run.

FIRMWARE

No boot code, machine-mode service, or privilege handoff takes part. The experiment is a freestanding bare-metal binary with no runtime beyond a UART print, and the argument never enters firmware.

KERNEL

No kernel is involved at any step. The test binary runs on bare QEMU with no operating system, and no system interface appears in the argument.

ALGORITHM

The claim is one fixed instruction semantic applied as one fixed idiom, not a computation proved against an independent oracle. The old==1 rule is shown working across three drops and one acquire, but nothing here establishes a general algorithm.

A shared object keeps a reference count in one word of memory. Two harts finish with it at the same moment, and both run the drop: read the count, subtract one, write it back. Compiled, that is three separate instructions with the read and the write split apart: lw, addiw, sw. Between the load and the store the second hart can run its own load, and both write back the same decremented value. One reference is silently lost: the object is freed while someone still holds it, or it is never freed at all. [1]

One instruction closes the window. amoadd.w a0, a1, (a2) adds a1 into the word at the address in a2, and writes the value the word held before the add into a0. The read, the add, and the write happen as one indivisible operation: no hart can observe the middle. The return value is the whole trick. For a drop, pass -1: if the old value comes back as 1, this drop took the count to zero, and the caller holding that return value is the last holder. No lock, no retry loop. [1] [2]

The run confirms the contract. On the virt UART under QEMU 8.2.2, starting from a count of 3: the first drop printed old=3 now=2, the second old=2 now=1, the third old=1 now=0, and that third call, the one that saw the 1, is the one that frees. An acquire from zero printed old=0 now=1. The listing shows each drop as a single amoadd.w, against the naive lw, addiw, sw triple. [1]

The one instruction that does the drop:

unsigned int rc_drop(volatile unsigned int *p) {
    unsigned int old;
    __asm__ volatile("amoadd.w %0, %2, (%1)"
        : "=r"(old) : "r"(p), "r"(-1) : "memory");
    return old;  /* 1 here means this drop took the count to zero */
}
Blueprint-style diagram of amoadd.w: a memory cell holding 3, one instruction producing rd = 3 (the old value, highlighted) and memory = 2, then the drop chain 3 to 2 to 1 to 0 with the drop that saw old = 1 freeing the object
One instruction, two results. amoadd.w writes old + (-1) back to memory and hands the old value to the caller in rd; the drop that sees old = 1 knows it took the count to zero.
Diagram: hand-built by Chris Dillinger for this post

The habit this buys: design the decision around the old value, never a re-read. if (rc_drop(&refs) == 1) free(obj); A load after the atomic op is a second, separate observation; the old value in the destination register is the observation that belongs to your update. [1]

Sources

  1. 01
    lab/165-amoadd-refcount: the amoadd.w listings and QEMU runChris Dillinger

    The firsthand objdump listings from riscv64-unknown-elf-gcc 13.2.0 (-O2 -march=rv64imac_zicsr): each rc_drop/rc_acquire inlines to a single amoadd.w, the naive drop is lw, addiw, sw, and the UART log from QEMU 8.2.2 shows rd taking the old value on all four operations.

  2. 02
    RISC-V ISA Manual, unprivileged architectureRISC-V International

    The A extension defines each AMO as one atomic read-modify-write: the read, the operation, and the write happen with no observable intermediate state, which is what closes the lost-update window the naive sequence leaves open.

Stuck on a board or a crash? Fixed-price firmware work: crash triage $250/symptom, bare-metal bring-up from $500, C audit from $350. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app