Code using this module includes it into sim-main.h and then, as a
convention, defines macro's ALU*_END that records the result of any
- aritmetic performed. Ex:
+ arithmetic performed. Ex:
#include "sim-alu.h"
#define ALU32_END(RES) \
ALU*_RESULT: Generic ALU result output.
ALU*_HAD_OVERFLOW: Returns a nonzero value if signed overflow
- occured.
+ occurred.
ALU*_OVERFLOW_RESULT: If the macro ALU*_HAD_OVERFLOW is being
used this is the most efficient result available. Ex:
(RES) = ALU16_OVERFLOW_RESULT
ALU*_HAD_CARRY_BORROW: Returns a nonzero value if unsigned
- overflow or underflow (also refered to as carry and borrow)
- occured.
+ overflow or underflow (also referred to as carry and borrow)
+ occurred.
ALU*_CARRY_BORROW_RESULT: If the macro ALU*_HAD_CARRY_BORROW is being
used this is the most efficient result available. Ex:
-/* Twos complement aritmetic - addition/subtraction - carry/borrow
+/* Twos complement arithmetic - addition/subtraction - carry/borrow
(or you thought you knew the answer to 0-0)
UMAXn: The upper bound of an unsigned N bit value (the lower
bound is always zero).
- Un: UMAXn + 1. Unsigned arrithmetic is computed `modulo (Un)'.
+ Un: UMAXn + 1. Unsigned arithmetic is computed `modulo (Un)'.
- X[p]: Is bit P of X. X[0] denotes the least signifant bit.
+ X[p]: Is bit P of X. X[0] denotes the least significant bit.
~X[p]: Is the inversion of bit X[p]. Also equal to 1-X[p],
(1+X[p])mod(2).
Overflow/Overflow indicates an error in computation of signed
- arrithmetic. i.e. given X,Y in [MINn..MAXn]; overflow
+ arithmetic. i.e. given X,Y in [MINn..MAXn]; overflow
indicates that the result X+Y > MAXn or X+Y < MIN_INTx.
Hardware traditionally implements overflow by computing the XOR of
Addition - Overflow - method 1:
- Overflow occures when the sign (most significant bit) of the two N
+ Overflow occurs when the sign (most significant bit) of the two N
bit operands is identical but different to the sign of the result:
Rn = (Xn + Yn)
The two N bit operands are sign extended to M>N bits and then
- added. Overflow occures when SIGN_BIT<n> and SIGN_BIT<m> do not
+ added. Overflow occurs when SIGN_BIT<n> and SIGN_BIT<m> do not
match.
Rm = (SEXTn (Xn) + SEXTn (Yn))
The two N bit operands are sign extended to M>N bits and then
- added. Overflow occures when the result is outside of the sign
+ added. Overflow occurs when the result is outside of the sign
extended range [MINn .. MAXn].
Addition - Carry - Introduction:
- Carry (poorly named) indicates that an overflow occured for
+ Carry (poorly named) indicates that an overflow occurred for
unsigned N bit addition. i.e. given X, Y in [0..UMAXn] then
carry indicates X+Y > UMAXn or X+Y >= Un.
Given two signed N bit numbers, a carry can be detected by treating
the numbers as N bit unsigned and adding them using M>N unsigned
- arrithmetic. Carry is indicated by bit (1 << N) being set (result
+ arithmetic. Carry is indicated by bit (1 << N) being set (result
>= 2**N).
0 <= X < Un, 0 <= Y < Un
==> X + Y < 2 Un
- Consider Y when carry occures:
+ Consider Y when carry occurs:
X + Y >= Un, Y < Un
- ==> (Un - X) <= Y < Un # re-arange
+ ==> (Un - X) <= Y < Un # rearrange
==> Un <= X + Y < Un + X < 2 Un # add Xn
==> 0 <= (X + Y) mod Un < X mod Un
- or when carry as occured:
+ or when carry as occurred:
(X + Y) mod Un < X mod Un
- Consider Y when carry does not occure:
+ Consider Y when carry does not occur:
X + Y < Un
have X < Un, Y >= 0
==> X <= X + Y < Un
==> X mod Un <= (X + Y) mod Un
- or when carry has not occured:
+ or when carry has not occurred:
! ( (X + Y) mod Un < X mod Un)
- hence we get carry by computing in N bit unsigned arrithmetic.
+ hence we get carry by computing in N bit unsigned arithmetic.
carry <- (Xn + Yn) < Xn
==> X + ~Y + 1 # -Y = ~Y + 1
In addition to the result, the equation produces Carry-out. For
- succeeding extended prrcision calculations, the more general
+ succeeding extended precision calculations, the more general
equation can be used:
C[p]:R[p] = X[p] + ~Y[p] + C[p-1]
Treating Xn and Yn as unsigned values then a borrow (unsigned
- underflow) occures when:
+ underflow) occurs when:
B = Xn < Yn
==> C = Xn >= Yn
/* 32 bit target expressions:
- Since most hosts do not support 64 (> 32) bit arrithmetic, carry
+ Since most hosts do not support 64 (> 32) bit arithmetic, carry
method 4 and overflow method 4 are used. */
#define ALU32_BEGIN(VAL) \
/* 64 bit target expressions:
Even though the host typically doesn't support native 64 bit
- arrithmetic, it is still used. */
+ arithmetic, it is still used. */
#define ALU64_BEGIN(VAL) \
unsigned64 alu64_r = (VAL); \