Architecture / ISA
Intermediate
RISC-V RV32IM / GCC
RISC-V M 확장(RV32M) 정수 곱셈 및 나눗셈 하드웨어 명령어 구조RISC-V M Extension (RV32M) Integer Multiplication and Division Instructions
기본 RV32I에 정수 곱셈(mul, mulh, mulhu, mulhsu)과 나눗셈·나머지(div, divu, rem, remu) 하드웨어 명령어를 추가하는 RV32M 표준 확장을 설명합니다.Reference for the RV32M standard extension, which adds hardware integer multiplication (mul, mulh, mulhu, mulhsu) and division/remainder (div, divu, rem, remu) instructions to RV32I.
#RISC-V #RV32M #Arithmetic #Hardware #ISA
핵심 개념기본 RV32I 명령어 세트에는 소형 코어의 실리콘 면적을 줄이기 위해 곱셈·나눗셈 명령어가 포함되어 있지 않습니다. M 확장(Standard Extension for Integer Multiplication and Division)을 활성화하면 32비트 × 32비트 = 64비트 곱셈의 상위 32비트(
mulh)와 하위 32비트(mul), 부호/비부호 나눗셈(div/divu) 및 나머지(rem/remu)를 하드웨어 명령어로 처리할 수 있습니다.Key ConceptThe base RV32I instruction set omits hardware multiply and divide instructions to keep minimal microcontroller cores small. Enabling the M Extension (Standard Extension for Integer Multiplication and Division) provides dedicated instructions for the upper (
mulh) and lower (mul) 32 bits of 32×32-bit products, as well as signed and unsigned division (div/divu) and remainder (rem/remu).1. RV32M 명령어 세트 요약1. RV32M Instruction Set Summary
좌우로 밀어서 표 전체 내용을 확인하세요
| 명령어 | 형식 | 연산 내용 | 용도 |
|---|---|---|---|
| mul rd, rs1, rs2 | R-type | rd = (rs1 * rs2)[31:0] | 32비트 곱셈 결과의 하위 32비트 |
| mulh rd, rs1, rs2 | R-type | rd = (rs1 * rs2)[63:32] | 부호 있는(signed × signed) 64비트 곱셈 상위 32비트 |
| mulhu rd, rs1, rs2 | R-type | rd = (rs1 * rs2)[63:32] | 부호 없는(unsigned × unsigned) 상위 32비트 |
| mulhsu rd, rs1, rs2 | R-type | rd = (rs1 * rs2)[63:32] | 부호 있는 rs1 × 부호 없는 rs2 상위 32비트 |
| div rd, rs1, rs2 | R-type | rd = rs1 / rs2 | 부호 있는 32비트 정수 나눗셈 |
| divu rd, rs1, rs2 | R-type | rd = rs1 / rs2 | 부호 없는 32비트 정수 나눗셈 |
| rem rd, rs1, rs2 | R-type | rd = rs1 % rs2 | 부호 있는 나머지 연산 |
| remu rd, rs1, rs2 | R-type | rd = rs1 % rs2 | 부호 없는 나머지 연산 |
Swipe horizontally to view the full table
| Instruction | Format | Operation | Purpose |
|---|---|---|---|
| mul rd, rs1, rs2 | R-type | rd = (rs1 * rs2)[31:0] | Lower 32 bits of 32-bit product |
| mulh rd, rs1, rs2 | R-type | rd = (rs1 * rs2)[63:32] | Upper 32 bits of signed × signed 64-bit product |
| mulhu rd, rs1, rs2 | R-type | rd = (rs1 * rs2)[63:32] | Upper 32 bits of unsigned × unsigned product |
| mulhsu rd, rs1, rs2 | R-type | rd = (rs1 * rs2)[63:32] | Upper 32 bits of signed rs1 × unsigned rs2 product |
| div rd, rs1, rs2 | R-type | rd = rs1 / rs2 | Signed 32-bit integer division |
| divu rd, rs1, rs2 | R-type | rd = rs1 / rs2 | Unsigned 32-bit integer division |
| rem rd, rs1, rs2 | R-type | rd = rs1 % rs2 | Signed remainder operation |
| remu rd, rs1, rs2 | R-type | rd = rs1 % rs2 | Unsigned remainder operation |
2. 64비트 전체 곱셈 결과 얻기 예제2. Computing a Full 64-Bit Product Example
# 32비트 정수 a(t0)와 b(t1)의 64비트 전체 곱을 (a1:a0)에 저장
# 권장 순서: mulh 계열을 먼저 실행한 뒤 mul을 실행 (매크로-옵 퓨전 지원)
mulh a1, t0, t1 # 상위 32비트 먼저 계산 (t0, t1 보존 필요)
mul a0, t0, t1 # 하위 32비트 계산
# 0으로 나누기(Divide by Zero) 확인:
# RISC-V는 0으로 나누어도 하드웨어 예외를 발생시키지 않고 규격상 -1(0xFFFFFFFF)을 반환합니다.
beqz t1, div_by_zero_handler
div a2, t0, t1
# Store the full 64-bit product of 32-bit integers a(t0) and b(t1) in (a1:a0)
# Recommended sequence: execute mulh first, followed immediately by mul (enables macro-op fusion)
mulh a1, t0, t1 # Compute upper 32 bits first (do not overwrite t0 or t1)
mul a0, t0, t1 # Compute lower 32 bits
# Checking for Divide by Zero:
# RISC-V does not trap on division by zero; it returns -1 (0xFFFFFFFF) by specification.
beqz t1, div_by_zero_handler
div a2, t0, t1
3. 실무 적용 및 주의사항3. Practical Considerations and Caveats
- 64비트 곱셈 시
mulh선행 배치 권장: RISC-V 스펙은 동일한 소스 레지스터 쌍(rs1,rs2)에 대해mulh를 먼저 실행하고 바로 이어서mul을 배치하면 하드웨어가 두 명령어를 단일 64비트 곱셈 연산으로 병합(Macro-op Fusion)할 수 있도록 권장합니다. 이때 첫 번째mulh의 목적지 레지스터(rd)가rs1이나rs2를 덮어쓰지 않도록 주의해야 합니다. - 0으로 나누기(Divide by Zero) 예외 미발생: x86이나 MIPS와 달리 RISC-V의
div/divu는 제수가 0일 때 하드웨어 트랩을 일으키지 않고 모든 비트가 1인 값(-1또는0xFFFFFFFF)을 반환하며,rem/remu는 피제수(rs1)를 그대로 반환합니다. 예외 처리가 필요한 언어 런타임에서는 나눗셈 전후에beqz분기 검사를 삽입해야 합니다. - 부호 있는 나눗셈 오버플로우 규칙: 최소 음수(
-2^31,0x80000000)를-1로 나누는 경우 몫이 32비트 부호 범위를 초과하므로, 규격에 따라div는0x80000000을,rem은0을 반환합니다.
- Recommended
mulh+mulOrdering for Macro-Op Fusion: The RISC-V specification recommends placingmulhimmediately beforemulwith identical source registers (rs1,rs2) so microarchitectures can fuse the pair into a single 64-bit multiplication. Ensure the destinationrdofmulhdoes not clobberrs1orrs2. - No Hardware Trap on Division by Zero: Unlike x86, RISC-V
div/divudo not raise an exception when dividing by zero; instead, they return all ones (-1/0xFFFFFFFF), andrem/remureturn the dividend (rs1). Insert an explicitbeqzcheck if your runtime requires a trap. - Signed Division Overflow Behavior: Dividing the minimum signed integer (
-2^31,0x80000000) by-1overflows 32-bit two's complement representation; per specification,divreturns0x80000000andremreturns0.
RV32M 확장은 별도의 HI/LO 특수 레지스터를 두지 않고 범용 레지스터 파일만으로 곱셈과 나눗셈을 수행하도록 설계되어, 파이프라인 설계와 컴파일러 레지스터 할당을 단순화합니다.
By writing products and quotients directly to general-purpose registers rather than dedicated HI/LO accumulators, RV32M simplifies both pipeline forwarding and compiler register allocation.