Professional Documents
Culture Documents
Edition
The Hardware/Software Interface
Chapter 3
Arithmetic for Computers
§3.1 Introduction
Arithmetic for Computers
n Operations on integers
n Addition and subtraction
n Multiplication and division
n Dealing with overflow
n Floating-point real numbers
n Representation and operations
Length of product is
the sum of operand
lengths
Initially 0
n Can be pipelined
n Several multiplication performed in parallel
Chapter 3 — Arithmetic for Computers — 9
RISC-V Multiplication
n Four multiply instructions:
n mul: multiply
n Gives the lower 64 bits of the product
n mulh: multiply high
n Gives the upper 64 bits of the product, assuming the
operands are signed
n mulhu: multiply high unsigned
n Gives the upper 64 bits of the product, assuming the
operands are unsigned
n mulhsu: multiply high signed/unsigned
n Gives the upper 64 bits of the product, assuming one
operand is signed and the other unsigned
n Use mulh result to check for 64-bit overflow
Chapter 3 — Arithmetic for Computers — 10
§3.4 Division
Division
n Check for 0 divisor
n Long division approach
quotient n If divisor ≤ dividend bits
dividend n 1 bit in quotient, subtract
1001 n Otherwise
1000 1001010 n 0 bit in quotient, bring down next
-1000 dividend bit
divisor
10 n Restoring division
101 n Do the subtract, and if remainder
1010 goes < 0, add divisor back
-1000 n Signed division
remainder 10
n Divide using absolute values
n Adjust sign of quotient and remainder
n-bit operands yield n-bit as required
quotient and remainder
Initially dividend
Step 1
Step 2
Step 3
Step 4
^ ^ ^ ^ ^
| | | | |
| | | | - sticky bit (s)
| | | - round bit (r)
| | - guard bit (g)
| - 23-bit mantissa from a representation
- hidden bit
Chapter 3 — Arithmetic for Computers — 41
Rounding
n When a mantissa is to be shifted in order to align radix
points, the bits that fall off the least significant end of the
mantissa go into these extra bits.
n The guard & round bits are just 2 extra bits of precision
used in calculations.
n The sticky bit is an indication of what is/could be in lesser
significant bits that are not kept:
n If a value of ‘1’ ever is shifted into the sticky bit