5.1.8Instruction Set Architecture (ISA)

RISC-V extensions (M, A, F, D, V, C)

3,525 words16 min readdifficulty · medium

The Six Standard Extensions

M Extension: Integer Multiplication and Division

Why it exists: Multiplication requires dedicated hardware (Wallace trees, Booth encoders). A minimal CPU saves die area by omitting it. But for crypto, DSP, or general compute, hardware multiply is essential.


A Extension: Atomic Memory Operations

Why it exists: Without atomics, implementing a lock requires disabling interrupts (impossible in user mode) or busy-waiting with race conditions. The A extension makes compare-and-swap a single hardware instruction.


F Extension: Single-Precision Floating-Point

Why it exists: Floating-point math requires specialized units (exponent alignment, normalization). A tiny microcontroller doesn't need this; a graphics chip does.


D Extension: Double-Precision Floating-Point

Why separate? Many embedded systems (audio codecs, sensors) only need single-precision. Doubling register width costs silicon area and power.


V Extension: Vector Operations

Why it exists: Modern workloads (image processing, ML, physics) perform the same operation on arrays. A scalar CPU processes one element per cycle; a vector CPU processes 4–64 elements per cycle.


C Extension: Compressed Instructions

Why it exists: Instruction memory is expensive (especially on embedded systems). If ADI x8, x8, 8 (used in every function epilogue) can fit in 16 bits instead of 32, you save billions of bytes in a typical program.


Extension Naming Convention


Recall Explain to a 12-Year-Old

Imagine you're building Lego robots. The base RISC-V kit (RV32I) has just enough pieces to make a robot that walks. But what if you want your robot to:

  • Do math homework fast → Add the M extension (multiplier gear).
  • High-five your friend's robot without bumping → Add A extension (atomic handshake).
  • Fly (needs to calculate air resistance) → Add F/D extensions (calculator for decimals).
  • Paint a picture (process lots of pixels at once) → Add V extension (many paintbrushes working together).
  • Fit in your pocket → Add C extension (fold the instructions smaller).

RISC-V lets you buy only the pieces you need. A tiny robot (smartwatch chip) skips the flying and painting parts. A huge robot (server CPU) has everything. This is cheaper and more efficient than forcing every robot to carry all possible attachments.



Connections


#flashcards/hardware

What does the M extension add to RISC-V? :: Integer multiply (MUL, MULH) and divide (DIV, REM) instructions. Without it, software must emulate via shift-add loops (100× slower).

Why are there three MULH variants (MULH, MULHU, MULHSU)?
Because32×32→64 multiplication produces a two-part result. MULH (signed×signed), MULHU (unsigned×unsigned), and MULHSU (signed×unsigned) handle different sign-extension requirements for the upper 32 bits.
What does LR.W do in the A extension?
Load-Reserved: loads a word and marks the address in a reservation register. If any other core writes to that cache line, the reservation is invalidated (enabling atomic compare-and-swap via SC.W).
How does SC.W (Store-Conditional) ensure atomicity?
It only succeeds if the reservation from LR.W is still valid. Returns 0 on success, 1 on failure. If another core wrote to the address, the reservation breaks and SC.W fails, forcing a retry.
What is the difference between F and D extensions?
F adds 32-bit (single-precision) floating-point; D adds 64-bit (double-precision). D requires wider register files (64-bit f0–f31) and is needed for scientific computing where 23-bit mantissa (F) has insufficient precision.
Why is the IEEE 754 exponent biased by 127 (single) or 1023 (double)?
To allow unsigned integer comparison of exponents. With bias, larger exponent → larger biased value, avoiding signed comparison logic. Bias = 2^(e-1) - 1 centers the range symmetrically.
What does vsetvli do in the V extension?
"Vector set length immediate": sets vl (vector length) to min(requested, max_elements). Returns the actual vl chosen by hardware. This enables strip-mining loops that handle arrays not evenly divisible by vector width.
Why is RISC-V's V extension variable-length instead of fixed like AVX-512?
Portability. Fixed-length forces all implementations to have the same register size (expensive for small chips). Variable-length (VLEN) lets a phone chip use 128-bit vectors and a server use 2048-bit vectors with the same software.
What is the space savings from the C extension?
~25% code size reduction. Common instructions (stack operations, small immediates, frequent registers) compress from 32 bits to 16 bits. Not all instructions compress, hence25% not 50%.
How does the C extension distinguish16-bit from 32-bit instructions?
Bits [1:0]. C instructions have [1:0] ≠ 11 (00, 01, 10). Standard 32-bit instructions always have [1:0] = 11. Hardware decodes by checking these bits.
What does "RV64GC" mean?
RV64 (64-bit base) + G (general = IMAFD = Integer + Multiply + Atomics + single-Float + Double) + C (Compressed). It's a common shorthand for a full-featured ISA.
Why can't C extension compress all instructions?
Encoding space. 16 bits can't hold three 5-bit register addresses (15 bits) plus opcode. Solution: compress only common patterns (sp-relative, a0–a7 arguments) or use 3-bit fields for x8–x15 (8 registers).
What is the purpose of AMOAD.W in the A extension?
Atomic read-modify-write in one instruction: reads mem[rs1], adds rs2, writes back, returns old value. Faster than LR/SC loop (4 instructions) for common atomic operations.
Why does the V extension need a "strip-mining" loop?
Because array sizes are not always multiples of vector width. Strip-mining processes min(vl, remaining_elements) per iteration, handling the "tail" naturally in the last iteration where vl < max_elements.
When should you use MULH vs MUL?
Use both for arbitrary-precision arithmetic. MUL gives lower32 bits, MULH gives upper 32 bits. Combined, they form the full 64-bit product needed for BigInt multiplication or overflow detection.

Concept Map

minimal, boots software

optional independently added

32x32 gives 64-bit

sign handling

needs hardware

read-modify-write

RV32I RV64I base ISA

Modular Extensions

Cost matches application needs

M Extension multiply divide

A Extension atomic ops

F Extension single-float

D Extension double-float

V Extension vector

C Extension compressed

MUL lower / MULH upper

MULH MULHU MULHSU

Wallace trees Booth encoders

LR SC AMO instructions

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho beta, RISC-V ka basic funda ye hai ki iska base instruction set — RV32I ya RV64I — jaan bujh ke bilkul chhota rakha gaya hai, sirf itna jitna zaroori hai ek system ko boot aur basic software chalane ke liye. Baaki saari extra functionality "extensions" ke through milti hai jo aap tabhi add karo jab zaroorat ho. Sochо jaise smartphone ka base model — camera, GPS, 5G alag modules hain, aap apni need ke hisaab se choose karte ho. Chhote embedded chips me aap mehenga hardware jaise multiplier ya floating-point unit skip kar sakte ho, aur servers me sab kuch daal sakte ho. Yahi modularity ka core intuition hai — hardware ka cost application ki zaroorat ke proportion me rehta hai.

Ab extensions ki baat karein toh, M extension integer multiply aur divide instructions deta hai — bina iske software ko shift-add loops se ye kaam karna padta hai jo lagbhag 100 guna slow hota hai. Yahan ek important detail hai: jab aap 32-bit ko 32-bit se multiply karte ho toh result 64-bit ka hota hai, isliye MUL lower half deta hai aur MULH upper half. Aur signed vs unsigned ka dhyan rakhna zaroori hai — MULH dono operands ko signed maanta hai, MULHU dono ko unsigned, aur MULHSU mixed case ke liye hai. Ye galti common hai ki log samajhte hain MULH hamesha positive dega, par signed multiplication me sign bits propagate hote hain, isliye sahi variant choose karna must hai.

A extension ka concept bhi bahut practical hai — ye atomic memory operations deta hai jo multicore systems me synchronization ke liye zaroori hain. Socho do cores ek hi variable x ko read karke increment karein aur wapas likhein — ek update lost ho sakta hai, ye race condition kehlata hai. LR/SC isko solve karta hai: LR address ko "reserve" kar leta hai, aur agar koi doosra core us location pe likhta hai toh reservation invalidate ho jaati hai, jisse SC fail ho jaata hai. Isse atomicity guarantee hoti hai. Ye why-it-matters isliye hai kyunki modern computing me multicore aur parallel processing har jagah hai, aur bina in atomic instructions ke reliable software likhna almost impossible ho jaata.

Go deeper — visual, from zero

Test yourself — Instruction Set Architecture (ISA)

Connections