Integer Computation
Ryan Robucci
1. References
- \(^\dagger\) A few extended topics images from CD Rom for Mitra, Sanjit Kumar, and Yonghong Kuo. Digital signal processing: a computer-based approach. Vol. 2. New York: McGraw-Hill, 2006.
2. Integer Computers/Digital Circuits
- in RTL, a register is an ordered collection of flip-flops that store what we can call word
- among all digital circuit design, those ordered bits have no inherent universal meaning
- a register can store a collection without regard for the interpretation
- meaning can be assigned to each bit based on its placement
- a full collection of interpretations is what refer to as a type
- the type assigned to a variable in a computing language is largely to used to select the appropriate computing operation (function, instruction, or instruction variant)
- types don't exist in results of digital circuits themselves nor are they carried in the storage elements
- the underlying operations performed on the words can often be efficiently described for a model using integer operations, with the words being interpreted as integers
- it is important to separate the intuition of computing on various types from the rigorous implementation based on models comprising numerous integer operations
3. Signed Representations
- unsigned numbers are most commonly represented using the usual weighting by powers of two
- three common signed representations are
- sign magnitude (famous variant used in floating-point units)
- one's complement
- two's complement
| first bit | negation | sign test | repr. of zero | range-symmetry | |
| sign magnitude | encodes sign, remaining bits encode magnitude | "bit flip" msb | involves only left-most bit | redundant: -0,+0 | yes |
| one's complement | encodes sign, remaining bits encode magnitude | "bit flip" every bit | " | " | yes |
| two's complement | represents weight \(-2^{N-1}\) rather than \(2^{N-1}\), remaining bits have usual positive weight | "bit flip" every bit, add ONE |
" | all-zeros | no (extra negative value) |
Reading / Review / Reference Material  support
Signed and Unsigned Integers
- Consider \(n\) ordered bits representing a value \(X\)
- Unsigned Interpretation:
- \(\displaystyle\sum_{i=0}^{n-1}(2^i x_i)=\)
- \(\displaystyle \textcolor{red}{2^{n-1} x_{n-1} } + \sum_{i=0}^{n-2}(2^i x_i )\)
- Signed Interpretation (MSB used for representing Negative Numbers)
- \(\displaystyle \textcolor{red}{-2^{n-1}x_{n-1} } + \sum_{i=0}^{n-2}(2^i x_i )\)
- Unsigned Interpretation:
- Two's complement uses the msb to weight/contribute a large negative number instead of a large positive value
- Ex: 1001 is -8 + 1=-7
- 4-bit signed range is 0+4+2+1 =7 to -8+0+0+0 =-8
- 4-bit unsigned Range is 8+4+2+1=15 to 0+0+0+0 =0
- If MSB, \(x_{N-1}\), is 0 the interpretation is the same
- unsigned: \(X <= 2^{N-1}-1\)
- signed: X positive , \(X <= 2^{N-1}-1\)
- If MSB, \(x^N-1\) , is 1, the interpretation is different
- unsigned: \(X >= 2^{N-1}\)
- signed: \(X\) negative, \(X >= -1 \times 2^{N-1}\)
- When the MSB is 1, the adjustment from one interpretation to the other is an offset of 2 times the most significant weight, \(2 \times 2^{N-1}\), which is \(2^{N}\).
- Subtract \(2^N\) to go from unsigned to signed
- Add \(2^N\) to go from signed to unsigned
Ex: Receive bits from a UART `10000010` as unsigned this appears to be 130, but detecting that it is >=128 and then subtracting 256 gives results in -126
Negative Numbers in Two's Complement (\(2^N-X\))
- It is also useful to think about negative twos-complement values complement in this way:
- for a N-bit X, if the msb is 1 the value is the following difference: \(2^{N}\) - `$unsigned(X)`
- for a 16-bit bit-vector X, if the msb is 1, the value is the distance from \(2^{16}\) to the value from unsigned interpretation of X
- Ex:
- 16'b1111_1111_1111_11<span style="color:red">0</span>1 = -3 ; X+2+1 overflows to \(2^{16}\)
- 16'b1111_111<span style="color:red">0</span>_1111_11<span style="color:red">0</span>1 = -259 ; X+256+2+1 overflows to \(2^{16}\)
Two's Complement Wheel
Helps visualize
- bit interpretation
- modular arithmetic
- e.g. \((X+Y)\%2^4\)
- overflow
- adding (and subtracting) signed and unsigned numbers is no different at the bit/hardware level, represented as modular arithmetic
Conversion Arithmetic
- The following assumes word sizes are sufficient for the conversion arithmetic, e.g. working with 12-bit words in a 16-bit architecture
- Interpreting unsigned as signed:
- To reinterpret \(\textcolor{red}{(1)}2^{N-1}\) as \(\textcolor{red}{-(1)}2^{N-1}\), subtract \(2^N\)
- If reading bits from a data stream, the subtraction by be necessary in software
- Example: a 12-bit two's complement ADC value is read from a 16-bit I/O port as
12'b0000_1000_0000_0001. Using a default interpretation in C,printf("%d",X)displays the result as 2049. What is the actual value? Ans: -2047
- Interpreting signed as unsigned:
- To reinterpret \(\textcolor{red}{-(1)} 2^{N-1}\) as \((1) 2^{N-1}\), add \(2^N\)
- Ex: if we print -2047 as unsigned, we get 2049
Conversion for 4-bit Data
| Bits \(x_3x_2x_1x_0\) |
Unsigned | relation | Signed |
|---|---|---|---|
1111 |
15 | \(\xtofrom[+16]{-16}\) | \(\textcolor{red}{-1}=2^4-15\) |
1110 |
14 | \(\xtofrom[+16]{-16}\) | \(\textcolor{red}{-2}=2^4-14\) |
1101 |
13 | \(\xtofrom[+16]{-16}\) | \(\textcolor{red}{-3}=2^4-13\) |
1100 |
12 | \(\xtofrom[+16]{-16}\) | \(\textcolor{red}{-4}=2^4-12\) |
1011 |
11 | \(\xtofrom[+16]{-16}\) | \(\textcolor{red}{-5}=2^4-11\) |
1010 |
10 | \(\xtofrom[+16]{-16}\) | \(\textcolor{red}{-6}=2^4-10\) |
1001 |
9 | \(\xtofrom[+16]{-16}\) | \(\textcolor{red}{-7}=2^4-9\) |
1000 |
8 | \(\xtofrom[+16]{-16}\) | \(\textcolor{red}{-8}=2^4-8\) |
0111 |
\(\textcolor{blue}{7}\) | = | \(\textcolor{blue}{+7}\) |
0110 |
\(\textcolor{blue}{6}\) | = | \(\textcolor{blue}{+6}\) |
0101 |
\(\textcolor{blue}{5}\) | = | \(\textcolor{blue}{+5}\) |
0100 |
\(\textcolor{blue}{4}\) | = | \(\textcolor{blue}{+4}\) |
0011 |
\(\textcolor{blue}{3}\) | = | \(\textcolor{blue}{+3}\) |
0010 |
\(\textcolor{blue}{2}\) | = | \(\textcolor{blue}{+2}\) |
0001 |
\(\textcolor{blue}{1}\) | = | \(\textcolor{blue}{+1}\) |
0000 |
\(\textcolor{blue}{0}\) | = | \(\textcolor{blue}{+0}\) |
Visual Example: Sine Wave transmitted using 5-bit data:
Two's Complement as Complement-and-Increment
- A two-step process can represent negation, without requiring extending the number of bits for the numerical symbols.
Steps:
- Bit-Wise Complement
- Increment
- Let X and Y represent bits in hardware expressed as an unsigned interpretation, though they may store a two's complement representation
- First, recall that addition and subtraction with signed and unsigned numbers are exactly the same in hardware: (let \(\underset{N}\boxplus\) \(\underset{N}\boxminus\) be modular addition and subtraction)
- \(X \boxplus Y\) is \((X+Y) \% 2^N\) where \(X\) and \(Y\) are the vector of bits undergoing modular addition
- Negating in two’s complement:
- -X: (\(2^N \boxminus X\)) is \((2^N-X) \% 2^N = -X\)
- \(2^N\) cannot be represented with N bits, and we might like to represent operations using symbols in the same domain, that of N-bit numbers.
\(2^N-X\) can be represented in two operations
- Let \({\mathcal H}\) satisfy \[2^N = {\mathcal H}+1\]
so that \[{\mathcal H} = (2^N-1) = {\underbrace{{111....11_2}}_{N\rm\,bits}}\]
- Then define negation by the following function \[ \underset{N}\boxminus X: ({\mathcal H}\underset{N}\boxplus 1) \underset{N}\boxminus X = ({\mathcal H} \underset{N}\boxminus X) \underset{N}\boxplus 1\]
- Which in modular arithmetic is \[(({\mathcal H}-X) \% 2^N + 1) \% 2^N = -X\]
- Ex: 4-bit negation of 3
- \(x \rightarrow -x\) is \(x-2^4\), where the magnitude of the negative number is \(2^4-x\), but \(2^4\) has no 4-bit representation
- \(2^4 = 2^4 - 1 + 1 = 1111_2 + 0001_2\)
- \(3=0011_2\)
-3:
\begin{aligned} 2^4 - 3= &1111_2 & + 0001_2 &\textcolor{red}{- 0011_2}\\ =& 1111_2 & \textcolor{red}{ - 0011_2} &+ 0001_2\\ =& & \textcolor{red}{1100_2} &+ 0001_2\\ =& & \textcolor{red}{1101_2} \\ \end{aligned}
Ones Complement
- Ones complement representation represents negative numbers by just bit-complement
- Ones complement is inverting bits:
\(\underbrace{\mathcal H}_{\mathclap{``all\ ones"}} \underset{N}\boxminus X\) ,
can be arithmetically described as \[ \underbrace{(2^N-1)}_{\hspace{-3em}all\ ones\hspace{-3em}} \underset{N}\boxminus X \]
- in modular arithmetic is \(((2^N-1)-X )\% 2^N = -1-X\)
- Therefore, to generate a twos complement negative from a ones complement negation, just add 1 \((2^N-1)\underset{N}\boxminus X \underset{N}\boxplus1 = 2^N – X\) which as modular arithmetic is \(((2^N-1)-X +1)\%2^N = (2^N – X) \%2^N = -X\)
Sign-Magnitude
For Sign-Magnitude representation, bit N-1 is the sign and the remaining bits are the magnitude
- \(x_{N-1}x_{N-2}x_{N-3}\)
- both ones complement and sign-magnitude suffer from 0 have two representations
data streams with typically small values on either side of zero involve less bit flipping between which can be valuable for power or digital compression applied along the bit columns
+4: 00000100 +3: 00000011 +2: 00000010 +1: 00000001 +0: 00000000 -1: 10000001 -2: 10000010 -3: 10000011 -4: 10000100
vs+4: 00000100 +3: 00000011 +2: 00000010 +1: 00000001 +0: 00000000 -1: 11111111 -2: 11111110 -3: 11111101 -4: 11111100
Note in the sequence below that several column never change
+4: 00000100
+0: 00000000
+3: 00000011
+3: 00000011
+3: 00000011
+3: 00000011
-3: 10000011
+0: 00000000
+1: 00000001
+0: 00000000
+1: 00000001
-3: 10000011
+1: 00000001
+0: 00000000
-1: 10000001
-2: 10000010
-3: 10000011
-3: 10000011
+3: 00000011
+2: 00000010
+3: 00000011
+2: 00000010
+1: 00000001
-4: 10000100
+0: 00000000
+1: 00000001
+0: 00000000
+1: 00000001
+1: 00000001
+0: 00000000
+1: 00000001
+0: 00000000
-1: 10000001
-2: 10000010
-3: 10000011
-3: 10000011
-3: 10000011
-4: 10000100
-4: 10000100
4. Truncation and Extension for Integers (in C)
In C, if a mix of signed and unsigned operands of the same rank are provided to an operator, operands are first cast to be unsigned (dumb conversion (e.g. \(-1\rightarrow \text{big number}\))).
Signed/Unsigned Casting may be followed by length adjustment.
Unsigned integers undergo zero-padding/zero-extension for extension and bit are truncated (discarded) for shortening
Signed integers undergo sign-extension whereby the msb is repeated, while truncation is the same as that for unsigned:
Truncation
- Assignment to a shorter type is always just bit truncation
- no Python-like smart rounding such as unsigned(-1) → 0
- (Left) Truncation Errors:
- For unsigned, truncation is not a problem as long as all the truncated bits are 0
- ex: 000 0101 (5) , can truncate at most 3 bits
- For signed, truncation is no problem as long as all the bits truncated are the same AND they match the surviving msb
- ex: 1111 100 (-4) , can truncate at most 4 bits
- For unsigned, truncation is not a problem as long as all the truncated bits are 0
Signed vs Unsigned Integral Extension
Assignment to a longer type is done with either zero or sign extension depending on the source type:
- Unsigned types use zero extension
- Signed types use sign extension
5. Run-time Truncation Error checks
- Run-time checks can be performed in software, but hardware support may be available
- Usually try to ensure that errors are avoided by design
- For custom hardware, run-time checks can customized and included as-needed
6. Long/short unsigned/signed Conversion Rules
(C/C++) Combinations of (dest type) = (source type) to consider:
\begin{align*} \rm LHS &= RHS \\ \text{( unsigned long)} &= \text{(unsigned long)} \\ \text{( unsigned long)} &= \text{( signed long)} \\ \text{( signed long)} &= \text{(unsigned long)} \\ \text{( signed long)} &= \text{( signed long)} \\ \\ \text{(unsigned short)} &= \text{(unsigned long)}\\ \text{(unsigned short)} &= \text{( signed long)}\\ \text{( signed short)} &= \text{(unsigned long)}\\ \text{( signed short)} &= \text{( signed long)}\\ \\ \text{( unsigned long)} &= \text{(unsigned short)}\\ \text{( unsigned long)} &= \text{( signed short)}\\ \text{( signed long)} &= \text{(unsigned short)}\\ \text{( signed long)} &= \text{( signed short)}\\ \\ \text{(unsigned short)} &= \text{(unsigned short)}\\ \text{(unsigned short)} &= \text{( signed short)}\\ \text{( signed short)} &= \text{(unsigned short)}\\ \text{( signed short)} &= \text{( signed short)} \end{align*}- The C rules are simple and used in Verilog:
- extend or truncate source
- Going from longer to shorter, upper bits are truncated
- Going from shorter to longer, zero or sign extension is done depending on source type being unsigned or signed respectively
- bit copy
- To/From signed/unsigned is just bit copying, no other smart manipulation/conversion/rounding is done
- extend or truncate source
Long/short unsigned/signed Verilog Conversion Rule Demos  study
Verilog
module conversion_demo;
wire [7:0]
u8x = 8'b11111111;
wire signed [7:0]
s8x = 8'b11111111;
wire [15:0]
u16x = 16'b1111_1111_1111_1111;
wire signed [15:0]
s16x = 16'b1111_1111_1111_1111;
wire [15:0] u16y_ux = u8x;
wire [15:0] u16y_sx = s8x;
wire signed [15:0] s16y_ux = u8x;
wire signed [15:0] s16y_sx = s8x;
wire [7:0] u8y_ux = u16x;
wire [7:0] u8y_sx = s16x;
wire signed [7:0] s8y_ux = u16x;
wire signed [7:0] s8y_sx = s16x;
initial begin
#0;
$display("u16y_ux:%16b,%7d",u16y_ux,u16y_ux);
//u16y_ux: 0000000011111111, 255
$display("u16y_sx:%16b,%7d",u16y_sx,u16y_sx);
//u16y_sx: 1111111111111111, 65535
$display("s16y_ux:%16b,%7d",s16y_ux,s16y_ux);
//s16y_ux: 0000000011111111, 255
$display("s16y_sx:%16b,%7d",s16y_sx,s16y_sx);
//s16y_sx: 1111111111111111, -1
$display(" u8y_ux: %16b, %7d",u8y_ux,u8y_ux);
//u8y_ux: 11111111, 255
$display(" u8y_sx: %16b, %7d",u8y_sx,u8y_sx);
//u8y_sx: 11111111, 255
$display(" s8y_ux: %16b, %7d",s8y_ux,s8y_ux);
//s8y_ux: 11111111, -1
$display(" s8y_sx: %16b, %7d",s8y_sx,s8y_sx);
//s8y_sx: 11111111, -1
end
endmodule
C/C++
unsigned short u16x = 0xFFFF;
signed short s16x = 0xFFFF;
unsigned int u32x = 0xFFFFFFFF;
signed int s32x = 0xFFFFFFFF;
unsigned int u32y_u16x = u16x;
unsigned int u32y_s16x = s16x;
signed int s32y_u16x = u16x;
signed int s32y_s16x = s16x;
unsigned short u16y_u32x = u32x;
unsigned short u16y_s32x = s32x;
signed short s16y_u32x = u32x;
signed short s16y_s32x = s32x;
printf("sizeof(short) :%lu\n",sizeof(short));
printf("sizeof(int) :%lu\n",sizeof(int));
printf("---\n");
printf("u32y_u16x: %10u\n",u32y_u16x);
printf("u32y_s16x: %10u\n",u32y_s16x);
printf("s32y_u16x: %10d\n",s32y_u16x);
printf("s32y_s16x: %10d\n",s32y_s16x);
printf("u16y_u32x: %10u\n",u16y_u32x);
printf("u16y_s32x: %10u\n",u16y_s32x);
printf("s16y_u32x: %10d\n",s16y_u32x);
printf("s16y_s32x: %10d\n",s16y_s32x);
sizeof(short) :2
sizeof(int) :4
---
u32y_u16x: 65535
u32y_s16x: 4294967295
s32y_u16x: 65535
s32y_s16x: -1
u16y_u32x: 65535
u16y_s32x: 65535
s16y_u32x: -1
s16y_s32x: -1
7. Implicit Casting
- casting rules apply during implicit casting
- common instructions are not mixed type, but in custom hardware design it more common
- in C: for
signed_short_A + unsigned_B, the first argument establishes the type "B" extended and then its representation is interpreted as signed during the addition - best practice for documenting design intent is to perform explicit casting on arguments
- for Verilog, the decision for the casting target type is different, but the casting process for a given variable is the same
- more later
8. Review Points on Bit Length
- Twos-complement addition of two N-bit numbers can require up to N+1 bits to store a full result including the overflow
- If operands are not of equal length, take N to be the maximum of the two lengths
- Two’s complement addition can only overflow if signs of operands are the same (likewise for subtraction the signs must be different)
- Result of N-bit addition with overflow is dropping of MSBits’s: A+B = (A+B) mod (2^N)
- For multiplication, multiplying two N-bit numbers requires up to 2N bits to store the operand. Multiplying a N-bit with a M-bit requires up to N+M bits.
- in Verilog, it is more common to see a verity of customized bit lengths than in C.
9. Not All Adder Structures are Alike
Knowing the options for implementation of addition on a given platform in the context of algorithm implementation is important. There are tradeoffs for size and speed. An overview of adders is provided in a later lecture.
10. Verilog: Sign Extension along with Addition
Review
- Sign Extension (extention of signed values) involves repeating the msb
- Truncation is safe as long as alls truncated bit are the same as the surviving msb
- Verilog Extention/Truncation Might be performed implicitly or explicitly
wire signed [7:0] x,y;
wire signed [15:0] s1,s2;
assign s1 = {8{x[7]},x} +
{8{y[7]},y} // explicit sign
// extension
assign s2 = x + y; // implicit sign extension
11. Run-time Overflow Detection for Addition
- Two's complement ADDITION of two numbers where the longest is N-bit can require up to N+1 bits for the result
Two's complement ADDITION can only overflow if the signs of the operands are the same
- overflow a+b
- positive overflow (ideal result>127) is blue
- negative overflow (ideal result<-128) is red
- Overflow check using result:
- input sign bits are same and do not match result sign bit
- Overflow check using inputs-only:
- In C: ` overflow=((a>0) && (b>(INT_MAX-a))) | ((a<0) && (b<(INT_MIN+a)));`
Overflow Run-Time Check Examples:
Verilog
overflow=(a[N-1]==b[N-1]) && (a[N-1]!=y[N-1]);wire signed [7:0] x,y,s; wire flagOverflow; assign s = x+y; //context determined // 8-bit addition //overflow case is when the sign of the // input operands are the same and // sign of result does not match assign flagOverflow = (x[7] == y[7]) && (y[7] ~= s[7])
12. Run-time Overflow Detection for Subtraction
- Two's complement subtraction can only under/overflow if the signs of the operands are different, otherwise the magnitude of the result must be smaller than the maximum magnitude of the two operands.
a-b, overflow is colored
- Overflow check: sign bits of the input operands are different and resulting sign bit does not match the first operand
- Otherwise stated: sign bit of the second operand and the result are the same and not equal to that of the first
- Otherwise stated: invert the sign bit of the second operand and perform the bit test for addition overflow or negate (two's complement) second operand and perform the addition overflow test for operarands
- Verilog
- `overflow=(a[N-1]!=b[N-1]) && (a[N-1]!=y[N-1]);`
- C
- `overflow=((a>=0) && (b<0) && (y<0)) | ((a<0) && (b>=0) && (y>=0));`
- `overflow=((a> 0) && (b<0) && (y<0)) | ((a<0) && (b> 0) && (y>=0));`
- Like addition, might use hardware overflow flag if available
- Subtraction Overflow Test is equivalent to
EITHER
- Invert the sign bit of the second operand and use the bit test for addition overflow OR
- negate (two's complement) second operand and perform addition with addition overflow test
13. Rounding Errors: float to int
- In C, Conversion from float to int is defined as truncation of fractional digits, effectively rounding towards zero
- \(5.7 \rightarrow 5\)
- \(-5.7 \rightarrow -5\)
- modeled using "fix" function on floating point numbers:
fix(-5.7)=-5.0
- Nearest Integer Rounding using Floating Point Biasing:
- Need a functional equivalent to inspecting the fractional part magnitude to see if it is >= 0.5
- Add +/- .5 before truncation depending on sign
- Positive Numbers:
- \(5.7+.5 = 6.2 \rightarrow 6\)
- \(5.4+.5 = 5.9 \rightarrow 5\)
- \(-5.7+.5 = -5.2 \rightarrow -5\) doesn’t work
- For negative numbers, need to subtract
- \(-5.7-.5 = -6.2 \rightarrow -6\)
- \(-5.4-.5 = -5.9 \rightarrow -5\)
Ignoring edge cases, to achieve common rounding when casting from float to int
- For positive floats add 0.5 before casting
- For negative floats subtract 0.5 before casting
14. Affecting Conversion Rounding Error with Numerator Bias
Assume unsigned int A,B;
- The remainder is discarded at the end of integer division
- A/B produces floor(A/B) rather than round(A/B)
- need a substitute for inspecting the final remainder to see if is magnitude is at least half of the divisor
- bias the numerator to a larger magnitude by half of the divisor magnitude
- So (A+B/2)/B is how we get rounding with integer-only arithmetic
- If B is odd, round B/2 up or down based on application preference
Example:
- Want to set serial baud rate using clock divider
i.e. BAUD_RATE=CLK_RATE/CLK_DIV
Option 1: C:
#define CLK_DIV (CLK_RATE/BAUD_RATE)Verilog:
parameter CLK_DIV (CLK_RATE/BAUD_RATE)Option 2: BETTER CHOICE WITH INTEGER BIASING
#define CLK_DIV (CLK_RATE+(BAUD_RATE/2))/BAUD_RATEVerilog:
parameter CLK_DIV= (CLK_RATE+(BAUD_RATE/2))/BAUD_RATE
To generalize to negative numerators with positive denominators, rule is that the bias should increase the magnitude.
For integer division with positive denominators, bias the numerator to increase its magnitude by half of the denominator
15. Rounding Integer Division
- INTEGER_DIVISION (intX,intY) := fix(float(intX)/float(intY))
- is mathematically similar to floating point division followed by int conversion ROUND TOWARDS ZERO,
- INTEGER DIVISION (intX/intY) != round(float(x)/float(y))
- require effective bias of result magnitude (result \(\rightarrow +\infty\) for positive result and result \(\rightarrow -\infty\) for negative result
requires a sign-dependent bias on the numerator such as the following:
- \(\rm (x+\textcolor{red}{sign(x)\times |y/2|})/y\) or
- \(\rm \Big(x+\textcolor{red}{\big((sign(x) == sign(y))?1:-1\big) \times y/2}\Big)/y\)
Longer Explanation  support
- Rounded-result division of integers A,B may be accomplished by adding a bias to increase the magnitude result, such as by adding an offset(bias) to A that is half the magnitude of the divisor B (integer division half) and matches the sign of the numerator
- desired magnitude \[|\rm round(float(A)/float(B))| = (|A|+|B/2|)/|B|\]
- Why?: Because integer division is round towards 0, but if \(|A\%B|\) is at least half of \(|B|\) then we need to round away from 0, which can accomplished by effectively adding 0.5 to the magnitude of the result before rounding
A+(|B|*sign(A))/2 where sign(A) is 1 or -1 according to the sign of A
- Exercise: make code to divide a integer (signed), S, by 256 with a rounded result
result=((S>=0)?(S+128):(S-128))/256;
- Exercise: make code to divide a integer (signed), S, by 5 with a rounded result
result=((S>=0)?(S+2):(S-1))/5;
- A hardware (RTL to netlist) synthesizer may only support division by powers of two and possibly only division by constants
- float to int conversion is floating-point
fix()operator (subtract fractional part) with type conversion discarding fractional part
Examples and plots follow
- Rounded Division example with even denominator
\begin{alignedat}{5} &\text { floating-point division: } && \rm && 7.0 \underset{float}/ -4 && &&= -1.75 \\ &\text { rounded result: } && \rm round(&& 7.0 \underset{float}/ -4 && ) &&= -2.0 \\ &\text { cast result to int: } && \rm int( && 7.0 \underset{float}/ -4 && ) &&= -1 \\ &\text { bias float before cast: } && \rm int(( && 7.0 \underset{float}/ -4 && ) \textcolor{red}{- 0.5}) &&= \\ & && \rm int(( && \blue{-1.75} &&) \textcolor{red}{- 0.5}) &&= \\ & && \rm int( && && \blue{-2.25}) &&= -2 \\ &\text { integer biasing: } && \rm (( && 7\textcolor{red}{+(4/2)}) \underset{\rm int}/ -4 && ) &&= \\ & && \rm fix(( && 7.0\textcolor{red}{+(2)}) \underset{\rm float}/ -4 && ) &&= \\ & && \rm fix( && \blue{-2.25} && ) &&= -2 \\ \end{alignedat} - Rounded Division example with odd denominator
\begin{alignedat}{5} &\text { floating-point division: } && \rm && 8.0 \underset{float}/ -3 && &&= -2.666... \\ &\text { rounded result: } && \rm round(&& 8.0 \underset{float}/ -3 && ) &&= -3.0 \\ &\text { cast result to int: } && \rm int( && 8.0 \underset{float}/ -3 && ) &&= -2 \\ &\text { bias float before cast: } && \rm int(( && 8.0 \underset{float}/ -3 && ) \textcolor{red}{- 0.5}) &&= \\ & && \rm int( && \blue{-2.666...} && \textcolor{red}{- 0.5} ) &&= \\ & && \rm int( && && \blue{-3.16...}) &&= -3 \\ &\text { integer biasing: } && \rm (( && 8\textcolor{red}{+(3/2)}) \underset{\rm int}/ -3 && ) &&= \\ & && \rm fix(( && 8.0\textcolor{red}{+(1)}) \underset{\rm float}/ -3 && ) &&= \\ & && \rm fix( && \blue{-3.0} && ) &&= -3 \\ \end{alignedat} - Floor vs Fix vs Round for Singed Values
- Positive-Biased Value Before Fix
- Negative-Biased Value Before Fix
- Sign-Dependent-Bias before Fix
16. First go big or first go small? choices for order of operations affect result
- In general, if intermediate result will fit in the allowed integer word size, apply integer multiplications before integer divisions to avoid loss of precision (e.g. go big first unless overflow, in which case first go small )
- Need to consider loss of MSB or LSB digits with intermediate terms and make choices base on application or expected ranges for input values
- Example 1:
int i = 600, x = 54, y = 23;- want i/x*y : true answer is 255.555…
i/x*yresult is 253 : goodi*y/xresult is 255 : better(i*y+x/2)/xresult is 256 : best
- Example 2:
- Assume
- an architecture with 16-bit int,
unsigned int c = 7000, x=10,y=2;- want
c*x/ywhich is truly35000
c*=x;overflows c since(c*x)>65535resulting inx = 4465c/=y;get2232here from dividing 4465 by 2c/=y; c*=x;result is3500*10=35000
- Assume
- In general, if intermediate result will fit in the allowed integer word size, apply integer multiplications before integer divisions to avoid loss of precision
17. Impacts of Limited Precision in DSP Filters
- Limited-precision representation of coefficients limits realizable system parameters
Images from Digital Signal Processing with Student CD ROM 4th Edition by Sanjit Mitra
Direct-form II: very poor for high-pass below pass filters requiring pole near the real axis
Manipulation of the order of operations, or alternative numerical formulations can yield different possible system realizations Here is a equivalent system if infinite precision is used, but yields different results if parameters and calculations are of limited precision
18. Input Prescaling (and output post-scaling)
- scaling can be used to reduce inputs to increase effective input range and avoid saturation in intermediate or final result registers
- example: convert meters to kilometers to support larger distances
- scaling can be used to increase inputs to improve effective resolution
- example: convert meters to millimeters to improve precision
- post-scale (OFTEN OPTIONAL)
- if prescaling was used only to improve precision/range for intermediate calculations and units of outputs need to match the input, post-scaling can undo the scaling
- applications often do not require postscaling and the values at the new scale can be used directly
- scaling by powers of 2 is efficient
Input attenuation to avoiding output or internal overflow/saturation
- when final/intermediate results fall outside the ranges defined by the bitlengths of registers and arithmetic circuits, input scaling (attenuation) is an alternative to simply increasing the register and combinational circuit wordlengths
- BIBO (mathematical details are beyond scope of this discussion)
- in DSP theory, Bounded-Input Bounded-Output relationships can be determined to predict range of any intermediate register when a bound on the input is known
- the peak magnitude of the transfer function can be used to predict required register sizes
- this can be used to determine required register sizes and/or need/effectiveness of scaling
- in DSP theory, Bounded-Input Bounded-Output relationships can be determined to predict range of any intermediate register when a bound on the input is known
a less rigorous but intuitive approach is to attenuate the input and resimulate until no overflow/saturation occurs
- it is useful to be aware that overflow/saturation is often internal
Increasing Effective Computation Precision with Input Prescaling
- In previous graph, can apply multiplication in pre-scaling and division for post-scaling if orginal-scale result is required (many times it isn't)
- By performing internal computations with larger values, numerical loss of accuracy at each step is mitigated
- An analogy would be performing math with millimeters rather than meters
- Ex:
- Code:
(a/b) > (c/5)
- Given
a=21b=4c=26
- Integer result is same
- Prescale, conceptually:
- \(1000 \times (a/b) \gt 1000 \times (c/5)\)
- New C code:
( (1000*a) / b) > ( (1000* c)/5)
- Result
( (21000) / 4) > ( (26000)/5)5250 > 5200
- Code:
19. Log Scale / Distort
- Log Scales maintain relative accuracy and represent a useful domain change
- effectively compresses scale large values
- log10(100)=2,log10(1000)=3
- effectively expands scale for small values
- log2(1/2)=-1,log2(1/4)=-2
- Common to use "log-likihood" in probability computations when there are many possibilities each with a small fractional probability and an infinitesimal joint probability
- Multiplication becomes addition, division becomes subtraction
- effectively compresses scale large values
- Other monotonic functions can be used appropriate to store data with its square-root or its square
- can use inverse functions for pre- and post-distortion
e.g. input log , output exponentiation
20. Avoid Unnecessary Operations (with monotonic functions)
- You might improve computational throughput and accuracy by looking for and avoiding unnecessary operations
- Example: 2-D Euclidean Distance Comparison
- dist(a,b) >= dist(u,v)
- \(\sqrt{(a_x-b_x)^2+(a_y-b_y)^2} >= \sqrt{(u_x-v_x)^2+(u_y-v_y)^2}\)
sqrt(pow(a_x-b_x,2)+pow(a_y-b_y,2)) > sqrt(pow(u_x-v+x,2)+pow(u_y-v_y,2))
- Note that
sqrtis a monotonic function, therefore forf(x)=sqrt(x)- \((x>=y) \rightarrow f(x)>=f(y)\)
- Simplification:
pow(a_x-b_x,2)+pow(a_y-b_y,2) > pow(u_x-v_x,2)+pow(u_y-v_y,2)
Illustration: log or other monotonic distortion function
21. Implementing Saturation to large avoid modular arithmetic error
- Sometimes overflow is usual or practically unavoidable, and handing with detection followed by replacing result with limit is more appropriate than modulo arithmetic where small overflow results in a very large discrepancy
- Detect overflow condition – override output to avoid LARGE roll-over error from modulus arithmetic
- If result exceeds \(2^{N-2}-1\) replace with \(2^{N-2}-1\)
- If result exceeds \(-2^{N-2}\) replace with \(2^{N-2}\)
Schematic depiction (of required software or hardware)
- Example:
- heat controller where heating element unsigned 8-bit input value 250 should increase by 10
- should detect and set result to 255 (error of -5), rather than compute 5 (error of -250)
- Balanced Range to mitigate accumulated error
- many signal processing algorithms and control systems employ summation of values in a series
- accumulation of error is important consideration
- physical processes can also be affected by accumulated error
- i.e. accumulation effect is in physical domain
- Using limits \(2^{N-2}-1\) and \(-(2^{N-1})\) under some conditions may cause bias (non-zero average error) , might be better to limit to \(2^{m-2}-1\) and \(-(2^{N-1}-1)\)
- this creates balanced error accumulation
- many signal processing algorithms and control systems employ summation of values in a series
22. Soft Limiting
- Choosing Smoother Limiting Functions will smooth the estimated wave by "compressing" past a soft limit. The saturation error is smoothed and high-frequency distortion is mitigated.
- A linearized approach can be implemented cheaply using a scale for signal components exceeding a soft limit (may involve scale by const or scale by power of two)
Ex: \(0<\alpha<1\) : such as \(\alpha=.25\) so that upper range can be compressed using can compress upper range using shift by two TH=128
if (A>TH) A=TH+(A-TH)*ALPHA
- Smoother sigmoidal functions mimicking analog circuits can be used as well
Instantaneous value mapping and affect on sinusoidal time series:
23. Quantization / Round-off Noise  enrichment
- When error is small compared to a signal, quantization error is model as noise
- in fact it is nearly ideal to model the error as additive white noise when the error very small compared to the signal (high SNR) because the error appears to be uncorrelated with the input whereas the signal can appear to be related to the signal otherwise (see provided graphic)
- A computation stage can be numerically modeled using an ideal calculation followed by additive error
- Limited-precision effects are modeled as quantization error and analyzed as if noise is inserted into the system at the quantization/round-off step
- Good SNR (signal to noise ratio) demands that the signal's actual range in an application be large compared to the round-off error
24. Limit Cycles
- Limit Cycles are a special case of quantization (limited precision) error propagation of concern for systems with iterative feedback for which errors seem to propagated indefinitely over iterations
- Feedback in computations, e.g. DSP IIR systems, can exhibit limit cycles, which are indefinite oscillatory responses from either round-off or overflow errors. This is possible from the feedback of the Limited-precision error, and repetitive rounding.
- Errors may appear as constant outputs or +/- oscillations, even after the input becomes 0.
Digital Signal Processing, A Computer-Based Approach Sanjit K. Mitra McGraw Hill
- In these images \(\mathcal Q\) refers to a location where precision must be limited, e.g. limit precision (#bits).
- In DSP, FIR filters do not exhibit this since they have no feedback (no memory)
- Techniques to avoid unwanted limited cycle behavior or bound it usually involve increasing bit length of intermediate calculations, using increased bit length use feedback of error and feedback, introducing random quantization to mitigate perpetuating effects
- Reference for Additional Examples:
- Discrete-Time Signal Processing (3rd Edition) (Prentice-Hall Signal Processing Series) 3rd Edition by Alan V. Oppenheim (Author), Ronald W. Schafer
- Ex 6.15 steps through an example of this shows round-off error where the output oscillates
- Ex 6.16 steps through an example of this shows overflow error- based oscillation which can be more sever
- Discrete-Time Signal Processing (3rd Edition) (Prentice-Hall Signal Processing Series) 3rd Edition by Alan V. Oppenheim (Author), Ronald W. Schafer
25. Verilog: Working with signed and unsigned reg and wire
- Verilog 2001 provides signed reg and wire vectors, supporting implicit and explicit casting
- explicit casting provided via tasks
$unsigned()Ex:reg_s=$unsigned(reg_u);$signed()Ex:reg_u=$unsigned(reg_s);- these do not affect the bits, they affect subsequent operations
SystemVerilog provides newer casting syntax
type'()
remember, casting is often just reinterpretation of bitslogic unsigned [7:0] uX = 8'hFF; // 255 logic signed [7:0] sX; sX = signed'(uX); // -1logic signed [7:0] sX = -1; // 8'sb1111_1111 logic unsigned [7:0] uX; uX = unsigned'(sX); // 255logic signed [7:0] s8X = -1; int signed s32X; int signed s32Y; s32X = signed'(s8X); // sign extention s32Y = unsigned'(s8X); // zero-padding
26. Verilog: Signed/Unsigned Casting
- Implicit or Explicit Casting is always a dumb conversion (same as C), they never change bits, just the interpretation of the bits (e.g. -1 is not round to 0 upon conversion to unsigned, just reinterpreted as the largest unsigned value) for subsequent interpretation of operations by the compiler/synthesizer
- If a mix of signed and unsigned operands are provided to an operator, operands are first cast to be unsigned (like C)
- Signed/Unsigned Casting may be followed by length adjustment.
27. Truncation and Extension
- Truncation and Extension may be implied by context in Verilog.
- Some review of Truncation and Extension for Arithmetic Operators Discussion provided herein
28. Truncation
- Assignment to a shorter type is always just bit truncation
- no Python-like smart rounding such as \(unsigned(-1) \rightarrow 0\)
- Error Checking:
- For unsigned, truncation is not a problem as long as all the truncated bits are 0
- ex: 000**0101** (5), can truncate at most 3 bits
- For signed, truncation is not a problem as long as all the bits truncated are the same AND they match the surviving msb
- ex: 1111**100** (-4), can truncate at most 4 bits
- For unsigned, truncation is not a problem as long as all the truncated bits are 0
29. Signed and Unsigned Extension
- Assignment to a longer type is done with either zero or sign extension depending on the type:
- Unsigned types use zero extension
- Signed types use sign extension
30. Verilog Context-Determined Bitlengths
- Verilog is different than C in this regard
- in C,
Y16b = A8b + B8b, is 8-bit addition followed by result extension to 16bit - in Verilog,
Y16b = A8b + B8b, is an 16-bit addition (in the context of an assignment to 16 bit), which is preceded by operand extensions to 16bit - to remove the addition from a context that determines bit length a trivial concatenation operator can be used
- in Verilog,
Y16b = {A8b + B8b}, is an 8-bit addition (in the context of concatenation), with extension of result to 16 bit
- in C,
31. Mixed Sign and Gotchas
- generally, mixing signed and unsigned results in casting operands to unsigned
- simple conservative approaches is to never mixing signed and unsigned in expressions and use explicit casting/converting
classic gotcha
unsignedA =5; ... if (unsignedA -6 >=0) //always true
Reading  support
Addition and mixed sign
- Addition in the context with of assignment will cause extension of operands to the size of the result (the synthesizer may later remove useless hardware).
- However, the extension is performed according to the type of addition, which is determined by the operands. Therefore,
- signed extension is only performed if BOTH operands are signed regardless of the assignment.
module test();
reg signed [7:0] s;
reg [7:0] u;
reg signed [7:0] neg_two;
reg [15:0] x1,x2;
reg signed [15:0] y1,y2;
initial begin
neg_two = -2;
s = 1; u = 1;
x1 = u + neg_two; x2 = s + neg_two;
y1 = u + neg_two; y2 = s + neg_two;
$display("%b",x1); $display("%b",x2);
$display("%b",y1); $display("%b",y2); end
endmodule
Result:
0000000011111111
1111111111111111
0000000011111111
1111111111111111
Signed/Unsigned Pitfall Example
. . .
reg [3:0] bottleStock = 10; //**unsigned**
always @ (posedige clk, negedge rst_)
if (rst_==0)
bottleStock<=10;
else if (bottleStock >= 0) //always TRUE!!!
bottleStock <= bottleStock-1;
Signed/Unsigned Pitfall Example
. . .
input wire [2:0] remove;
signed reg [3:0] remainingStock = 10;//**signed**
always @ (posedige clk, negedge rst_)
if (rst_==0)
remainingStock<=10;
else if ((remainingStock-remove) >= 0) //always TRUE!!!
remainingStock <= remainingStock-remove;
32. Scaling by Powers of Two using Shift
- Multiplying by a positive integer power of two may be performed with the logical left-shift shift operator
- Multiplying by \(2^k\) is left shift by \(k\):
a<<k- Typically synthesizers require k to be a constant;
- k bits on left are discarded
- Detecting Overflow: no overflow if all discarded bits are the same and the new msb matches the truncated bits
- Logical Shift is usually inexpensive in hardware– just “rewiring”.
- Multiplying \(a\) power of two, \(2^k\), can be performed with a left shift by \(k\),
a<<kwhich in hardware is simply rewiring
33. Multiplication
In general to hold the result of multiplication M+N bits is needed, where M and N are the length of the operands
wire [N-1:0] a; wire [M-1:0] b; wire [M+N-1:0] y; y=a*b;- The multiplication in the context with the assignment will cause extension of operands to the size of the result (the synthesizer may later remove useless hardware). However, extension type performed according to input operands (signed extension performed only if BOTH operands signed)
- Multiplication by two variables can be expensive, with a size on the order of MxN (full 1-bit adders and
ANDgates as 1-bit mult) - Delay is proportional to M+N
1011 (M bits) x 10010 (Nbits):
Multiplication Overflow Check
- can compute full result and check truncation
- can can partial products (bit AND, shift) and check for overflow that word occur in shifting partial products and check after each addition
- can compute full result then divide and compare to input
(Non-Optimized) Software Multiplication using Smaller Hardware Multipliers
- 8-bit input multipliers can be used to compute 16-bit input multiplication,
- Many smaller partial products are computed and weighted or added into appropriately weighted output word
- mathematically shown: Y = {AH,AL} x {BH,BL} = AH*BH<<16+AH*BL<<8+AL*BH<<8+AL*BL.
- Here {AH,AL} is used to denote bit concatenation (not a C operator)
Compute 16-bit results:
- \(\textcolor{red} {AL \times BL}\)
- \(\textcolor{green} {AL \times BH}\)
- \(\textcolor{blue} {AH \times BL}\)
- \(\textcolor{orange} {AH \times BH}\)
Shift, Extend, Add \(\boxed{AH}\boxed{AL} \times \boxed{BH}\boxed{BL}=\)
\({\rm byte3}\) \({\rm byte2}\) \({\rm byte1}\) \({\rm byte0}\) sign ext \(\leftarrow\) \(\textcolor{red}{AL \times BL}\) + sign ext \(\leftarrow\) \(\textcolor{blue}{AH \times BL}\) \(0\) + sign ext \(\leftarrow\) \(\textcolor{green}{AL \times BH}\) \(0\) + \(\textcolor{orange}{AH \times BH}\) \(0\) \(0\) = \(Y_{\rm byte3}\) \(Y_{\rm byte2}\) \(Y_{\rm byte1}\) \(Y_{\rm byte0}\)
- See Application note AVR201: Using the AVR Hardware Multiplier: http://ww1.microchip.com/downloads/en/Appnotes/Atmel-1631-Using-the-AVR-Hardware-Multiplier_ApplicationNote_AVR201.pdf
- Can generalize to larger word-sizes, maintaining separate words of varying weights
- Can use Karatsuba Algorithm for fast multiplication of large numbers
- Long multiplications can be mapped to hardware-supported multiplications by breaking the TWO input operands into supported input word-sizes, performing all associated pair-wise multiplications between the TWO inputs (like [FIOL method](https://en.wikipedia.org/wiki/FOIL_method) ), and then adding the results with appropriate shifting to implement wights and sign extensions
Assuming 4-bit multipliers:
underscores denote constants
20 × 30 = \((0b\textcolor{red}{0001}0100 * 0b\textcolor{green}{0001}1110)\)
= \((\textcolor{red}{{1 \cdot \underline{16}}} + 4) \times ( \textcolor{green}{1 \cdot \underline{16}} + 14)\)
= \((\textcolor{red}{1} \times \textcolor{green}{1} \cdot \underline{256}) + ( \textcolor{red}{{1 \cdot \underline{16}}} \times 14) + ( \textcolor{green}{1 \cdot \underline{16}} \times 4) + ( 4 \times 14)\)
= \((1 \times 1) \ll \underline{8} + ( 1 \times 14) \ll \underline{4} + ( 1 \times 4) \ll \underline{4} + ( 4 \times 14)\)
= \(256 + 224 + 64 + 56 = 600\)
34. Karatsuba Algorithm
- 16-bit multiplication using FOUR 8-bit input multipliers:
- 16-bit multiplication using THREE 8-bit multipliers:
- To avoid the larger multiplier in \((\textcolor{green}{(}\textcolor{red}{(AH+AL)*(BH+BL)}\textcolor{green}{-AH*BH-AL*BL)})<<8\)
- Start with
\(-((AH-AL)*(BH-BL)\textcolor{green}{-AH*BH-AL*BL)}) <<8)\) - \((AH-AL)*(BH-BL)\) may be negative requiring an extra bit of output so,
- Compute the sign and absolute value of (AH-AL) as well as (BH-BL)
- Compute \(|(AH-AL)\times(BH-BL)|\) using the standard size multiplication and use the sign of the result to add or subtract the result from the sum
- Start with
BUILDING LARGE HARDWARE MULTIPLIERS FROM SMALLER HARDWARE MULTIPLIERS
- FPGA may have banks of “hard” multipliers (e.g. 8x8 multipliers, sometime in what is called DSP slices) so as to avoid using large portions of the programmable fabric. Ex. 8-bit multipliers can be used for 16-bit mult, mathematically shown:
Y = {AH,AL} x {BH,BL} = AH*BH<<16+AH*BL<<8+AL*BH<<8+AL*BL.
The synthesizer should recognize the size of the multiplication block construct the mapping to available multipliers for you. - Karatsuba Algorithm may be used
35. Multiplication by Low-Density Constants
- Multiplication by low-density constants (few non-zero bits) can be inexpensive:
\[u\times24 = u \times (16+ 8) = u<<4 + u<<3\]
- Using example from previous slide: if the second operand is a constant, the synthesizer reduces the multiplication to shift and one adder:
- Multiplying by low-density constants implemented with a few shift-add operations
36. To Properly Mimic Signed-Integer Divided by Power of Two using Arithmetic Shift, a Sign-dependant Pre-Shift Bias is Required
- The context here is that you are are given C code for an algorithm that has a signed-integer divide by power of two
- You want to implement the algorithm in hardware using a shift to achieve match results from software
- Divide \(x\) by \(2^k\) is defined to be ROUND TOWARDS ZERO — mimicking this detail with arithmetic right-shift requires pre-biasing \(x\) by adding \(2^k-1\):
( x + ((1<<k)-1) ) << k - C:
x/pow(2,k)== Verilog:( x + ((1<<k)-1) ) << k
- The context here is that you are are given C code for an algorithm that has a signed-integer divide by power of two
- You want to implement the algorithm in hardware using a shift to achieve matching results
- Divide x by \(2^k\) is almost the same as an arithmetic right shift
- discard k bits on the right and replicate sign bit k times on the left. Must use the arithmetic shift to perform sign extension;
x>>>k;same as{k{x[msbindex]},x[msbindex:k]} - However, “integer division” is defined by truncation of the fractional bits of the result, also known as “round towards zero” To mimic this behavior more is needed:
- For a positive x, integer division corresponds to
floor(x/n)- ex: 5/2 = 2
- If x is positive you can just use logical shifting:
x>>k;
- For a negative x, integer division corresponds to
ceil(x/n)- ex: -5/2= -2
- Note that arithmetic right-shift does not mimic this behavior
- For a positive x, integer division corresponds to
- If x is negative, we want ceil(x/n) which may be computed by applying a bias that is 1 lsb less than the divisor … this is just enough bias that with any fractional result the value becomes ''bigger'' than the next ''bigger'' negative integer so that:
- \({\rm floor} ((x+(2^k-1))/(2^k))\)
(x +((1<<k)-1)) >> k(note that here >> represents an signed arithmetic shift)- Example: (-5 + (2-1))/2=(-5+1)/2=-4/2=-2
(-5 +((1<<1)-1)) >> 1(-5 + 1 ) >> 1
37. Very Long and Arbitrary Precision Arithmetic Using Software
- Arbitrary precision arithmetic libraries are software libraries that support arithmetic on word-lengths of arbitrary precision, outside the native length provided by hardware.
- Embedded systems can implement larger word-length operations, which is a reasonable alternative to using a more complex processor if arbitrary word-lenth computation needs are not timing-critical
- For 16 bit calculations, an 8-bit architecture may support double-register arithmetic (e.g. use two registers to hold output of a 8X8 multiplication)
- For even longer numbers results can be calculated a piece at a time and overflow bits (add/sum) or overflow registers (multiply) can be used to compute larger results. The built-in C variable types are usually automatically handled by the compiler. If even longer types are needed, find an arbitrary precision arithmetic software library.
- So, don't say that you need a 128-bit processor to perform 128-bit arithmetic.
- Its just that implementing 128-bit arithmetic with a 8-bit processor via could be slow.
- Take for example performing two 128-bit operations a second when using an 8-bit processor.
- That shouldn't be a problem (even at only 8 MHz) and is not cause to change to a different hardware platform.
38. Previous Discussions
- Numerator Bias before casting and division
- Effect of order of operations
39. Fixed-Point Arithmetic
Later Topic will be Fixed-Point Arithmetic, which allows for computing with representation of fractional values using only integer arithmetic units
40. Additional Operations
- Equality
- Magnitude Comparison
Equality Comparison
wire signed [7:0] x,y;
wire flagEq;
flagEq = (x==y);
Example may be implemented by eight xnor2 followed by and8;
Magnitude Comparator
- Can also use subtraction and check for zero and sign bits
- Comparison chain (just x>y) representative circuit:
explanatory pseudo code:
if x[msb]&~y[msb] //if x=1???????,y=0???????
flag_x_gt_y=True; // return True
else if not ~x[msb]&y[msb] //elif not x=0???????,y=1???????
if x[msb-1]&~y[msb-1] // check remaining bits
flag_x_gt_y=True;
else if not ~x[msb-1]&y[msb-1]
if x[msb-2]&~y[msb-2]
flag_x_gt_y=True;
else if not ~x[msb-2]&y[msb-2]
.
.
.
if x[0]&~y[0]
flag_x_gt_y=True;
return False
- 4-Bit "Full" Magnitude Comparator Sharing Intermediate Terms
Ref: http://www.ti.com/lit/ds/symlink/sn74ls85.pdf
41. Modeling Error Propagation
Error introduced by division:
- Let \(q\) be ideal, errorless \(q=i/x\)
- Let computed value from integer division, \(i\underset{\rm int}{\boxed{/}}x\), be \(\hat{q}\) and the associated error be \(\Delta q\), such that
- \(\hat{q}= q + \Delta q\),
- Relative Error: \(\Delta q \over q\), relative magnitude of error of computed to ideal is related to input operands and precision of computation
- Often relative error is more pertinent than absolute error
- Example 5/2
- \(5\underbrace{/}_{int}2\) provides estimate \(\hat{q}\) <br>\(q_{\rm ideal}=\underbrace{2}_{\hat{q}}-\underbrace{(-0.5)}_{\Delta q}\)
- Relative Error: -.25 (i.e. -25%)
Error introduced by multiplication:
- Let \(m\) be ideal, errorless \(m= i \times x\)
- Let computed value from integer multiplication be, \(i\underset{\rm int}{\boxed{\times}}x\), be \(\hat{m}\) and the associated error be \(\Delta m\), such that
\(\hat{m}= m + \Delta m\)
- error could be caused by overflow or saturation
- error may be zero if no overflow can occur
Error propagation with multiplication:
- A following multiplication operation \(\times y\) multiplies the prior error:
\(\hat{q} \times y = (q + \Delta q)\times y = \underbrace{q \times y}_{\rm ideal} + \underbrace{\Delta q \times y}_{\rm multiplied\ error}\)
- Example 5/2*7
- 1st-step
- \(5\underbrace{/}_{int}2=2=\hat{q}\)
- note \(5.0/2.0 = 5\underbrace{/}_{int}2-{\texttt {error}}=\underbrace{2}_{\hat{q}}-\underbrace{(-0.5)}_{\Delta q}\)
- 2nd-step \(q\times7=2\times7 - (-0.5\times 7)=\underbrace{14}_{\hat{q}\times 7}-\underbrace{(-3.5)}_{error}\)
- 1st-step
- Example 5/2*7
Error propagated with division:
- A division, \(/ y\), scales the prior error:
- \(\hat{m} / y = (m + \Delta m) / y = \underbrace{m / y}_{\rm ideal} + \underbrace{\Delta m / y}_{\rm scaled\ error}\)
- Though division scales the absolute error which may appear favorable, division doesn't propagate reduced relative error
- relative error=\(\frac{\overbrace{\Delta m / y}^{\rm scaled\ error}}{\underbrace{m / y}_{\rm ideal}}=\frac{\Delta m }{m }\)
42. Overall Error
Common Error Metrics:
- the worse case error
- same penalty for rare error events as any common event
- average magnitude of error
- rare events of large error don't matter as much
- root-mean-square error ( a.k.a RMS error, or standard deviation \(\sigma\))
- like previous, but larger errors are penalized proportionally more than small errors
Example
- addition of numbers in the range -5 to 5 with uniform distribution, using 4 bit signed addition with balanced saturation +/- 7
- in this problem, worst-case error can be found directly: 5+5=7 has error of -3
analytical solutions studied for various important common operations, but numerical simulation and analysis can complement those, ex:
import numpy as np #input values a_ = np.arange(-5.0,6.0) b_ = np.arange(-5.0,6.0) #input probabilities pa_ = np.ones_like(a_)/len(a_) pb_ = np.ones_like(b_)/len(b_) #ideal and actual implementation models def ideal_op(a,b): return a+b def impl_op(a,b): return min(7,max(a+b,-7)) #numerical analysis of error for each input combination accounting for probability worst_err_mag = 0.0 expected_err_mag = 0.0 expected_err_sq = 0.0 for i in range(len(a_)): for j in range(len(b_)): e = impl_op(a_[i],b_[j]) - ideal_op(a_[i],b_[j]) p = pa_[i]*pb_[j] worst_err_mag = max(worst_err_mag,np.abs(e)) expected_err_mag += p*np.abs(e) expected_err_sq += p*e*e #report print(f"{'worst_err_mag':20} {worst_err_mag:6.3f}") print(f"{'expected_err_mag':20} {expected_err_mag:6.3f}") print(f"{'expected_err_sq':20} {expected_err_sq:6.3f}")worst_err_mag 3.000 expected_err_mag 0.025 expected_err_sq 0.074
Earlier examples shown to illustrate error generation and propagation with reordering of operatands
Keep the decision to go big or go small in mind as in each step of implementation in the next topic.
We will formally introduce an additional scale at each step and per-operand.
43. Fixed-point arithmetic
Floating Point Math and Fixed-Point Math
- If no floating point unit (FPU) is available, you can find a floating point software library. This will likely be slow.
- Another option is fixed-point math. You can write or use a library or just do it as needed inline….
Fixed-point arithmetic
- Problem: Want to use integer operations to represent adding
0011.1110and0001.1000 - Solution (by example with explanation to follow):
- Store
0011.1110as00111110 - Store
0001.1000as00011000 - Add
00011000+00111110=01010110 - Interpret
01010110as0101.0110
- Store
QM.N notation
- Can use common
QM.Nnotation: M+N bits, with M bits as whole part and N bits as fractional part. - To design the computation, must first determine number of bits to use for whole and fraction parts depending on range and precision needed. This determines the scale factors required to convert the faction to a whole number
- QM.N corresponds to \(S = 2^N\)
- Example
1101.0000 * 16 = 11010000 S=16, Q4.401.011000 * 64 = 01011000 S=64, Q2.6
- Signed Notation
- in some variants (ARM) Q8.8 implies that the sign bit is included in the integer part for a total of 16 bits
- lets use this
- in other variants (TI)
- Q7.8 implies an additional sign bit for a total of 16 bit
- UQ8.8 would be a 16bit value
- in some variants (ARM) Q8.8 implies that the sign bit is included in the integer part for a total of 16 bits
Addition and Subtraction with scale S (operands with same scale)
- \(A+B\) computed as \(A\times S \underbrace{+}_{\text{integer addition}} B\times S=C\times S\), so that the addition is an integer addition
- Interpret result by dividing output by S to obtain answer C
- using a power of two for S is efficient, though not required
- \(A-B\) computed as \(A\times S-B\times S=C\times S\)
- Divide result by S to interpret answer C
Example: Check if addition of 0.17 meters and 0.24 meters is greater than .9 meters
int Y_S100 = (.9 * 100); int A_S100 = (0.17 * 100); int B_S100 = (0.24 * 100); int C_S100 = A_S100+B_S100; //integer addition int flag = C_S100 > Y_S100; //integer subtraction/comparison
- To be human-understandable, an example presented with powers of 10 for scaling, but powers of 2 are usually appropriate for efficiency and predictability of errors and error propagation.
Multiplication with scale S
- \(A*B\) computed as \((A \times S)\underbrace{\times}_{\text{integer multiplication}} (B \times S)=C \times S \times S\)
- Divide result by \(S^2\) for final interpretation
- Or to maintain S-Scale representation for C for further computations, divide result by S
- Unfortunately, the intermediate result \(C \times S \times S\) required more storage than the scaled result \(C \times S\)
Example: Check if area of rectangle with sides of length 0.17 meters and 0.24 meters is greater than the area of a square with sides of length .2 meters
int AREASQ_S10000 = (.2 * 100) * (.2 * 100); int AREASQ_S100 = (.2 * .2 * 100); int A_S100 = (0.17 * 100); int B_S100 = (0.24 * 100); int ARECT_S10000 = A_S100*B_S100; //integer multiplication int flag0 = ARECT_S10000 > AREASQ_S10000; //integer subtraction/comparison int ARECT_S100 = ARECT_S10000/100; //convert to scale 100 int flag1 = ARECT_S100 > AREASQ_S100; //integer subtraction/comparison
- To be human understandable, an example presented with powers of 10 for scaling, but powers of 2 are usually appropriate for efficiency and predictability of errors and error propagation.
Division with scale S:
- A/B could be computed as \((A \times S)\underbrace{/}_{{\text{integer division}}}(B \times S)=C\)
- Scales cancel. Which is fine if you only wanted an integer answer
- Would need to multi by S to obtain scaled result \(C\times S\) for further math
- …but this is less accurate since the lower bits have already been lost
- For precision, better to prescale one of the operands \(((A\times S)\times S)/(B\times S)=C\times S\)
- Unfortunately, the intermediate term \(((A \times S)\times S)\) requires more storage and a larger computation
- For rounding, remember to apply numerator pre bias of \(\rm sign(\rm numerator) \times 1/2 \times |\rm denominator|\) when performing integer division
Example: Circumference to Radius Note that Python3 provides an explicit integer division:
//from math import * import numpy as np import pyforest tau = pi * 2 #tau C=tau+2.8 # some value for circumference #(note that adding was intentional here just to produce an interesting number 9.0831853072) #C C8=floor(C*8) #C*8,C8,C8/8 C256=floor(C*8*8) #C*8*8,C256,C256/(8*8) tau8=floor(tau * 8) #tau8,tau8/8 halftau8=floor(tau/2 * 8) //precomputed bias #halftau8,halftau8/8 print("C = %20.10f"%C) print("C/tau = %20.10f"%((C)/(tau))) print("C8/tau8 = %20.10f"%((C8)//(tau8))) print(" with pre-div bias : %20.10f"%((C8+halftau8)//(tau8))) print(" with prescale : %20.10f"%((C8*8)//(tau8)/8)) print(" with prescale and pre-div bias: %20.10f"%((C8*8+halftau8)//(tau8)/8)) print("C256/tau8 = %20.10f"%((C256)//(tau8)/8)) print(" with pre-div bias : %20.10f"%((C256+halftau8)//(tau8)/8)) print("---")
C = 9.0831853072
C/tau = 1.4456338407
C8/tau8 = 1.0000000000
with pre-div bias : 1.0000000000
with prescale : 1.3750000000
with prescale and pre-div bias: 1.5000000000
C256/tau8 = 1.3750000000
with pre-div bias : 1.5000000000
44. Bias before reducing precision
- Reducing precision is related to division, typically achieved using right-shift (or actually just dropping lsb bits)
- Therefore follow pre-biasing rule for division before dropping least significant bits
- to achieve rounding when removing lesser significant bits, first perform pre-shift biasing of 1/2 weight of surviving position according to the sign of the value
- if dividing by \(2^n\), before right shifting by n, add or subtract (1 << (n - 1))
- Examples converting positive value Q4.4 to Q6.2:
- Will need to loss bits in 1/8 and 1/16 position, with new lsb position of 1/4
- For positive numbers need to add 1/8th before truncation
- For negative numbers need to subtract 1/8th before truncation
- \(4 \frac{1}{8}\), 4.125, 0010.0010, stored as
00100010- Without prebias for correct rounding:
- right shift by 2 results in
00001000,lost ending bits10 - interpreted as 4.0
- right shift by 2 results in
- With prebias for correct rounding:
- \(4 \frac{1}{8} + \frac{1}{8} = 4 \frac{1}{4}\)
00100010+00000010=00100100
- right shift by 2:
00001001 - interpreted as 4.25 (4.125 rounded to the nearst \(\frac{1}{4}\))
- \(4 \frac{1}{8} + \frac{1}{8} = 4 \frac{1}{4}\)
- Without prebias for correct rounding:
- \(-1\frac{1}{16}\),
1110.1111stored as1110 1111- Without prebias:
- (Arithmetic) right shift by 2 results in
11111011,lost ending bits11 - interpreted as \(-1 \frac{1}{4}\)
- (Arithmetic) right shift by 2 results in
- With prebias:
- \(1 \frac{1}{16} + \frac{1}{8}\)
1110 1111+1111 1110=1111 0001
- right shift by 2:
1111 1100 - interpreted as -1 (\(-1\frac{1}{16}\) rounded to the nearst \(\frac{1}{4}\))
- \(1 \frac{1}{16} + \frac{1}{8}\)
- Without prebias:
45. Example Fixed-Point Arithmetic Library Code
- [Q (number format) - Wikipedia](https://en.wikipedia.org/wiki/Q_(number_format))
- reviewed code in-class, provided below
Code examples in the below block are copied and provided under the Creative Commons Attribution-ShareAlike License: https://creativecommons.org/licenses/by-sa/3.0/
Wikipedia contributors. (2021, November 24). Q (number format). In Wikipedia, The Free Encyclopedia. Retrieved 18:10, November 29, 2021, from https://en.wikipedia.org/w/index.php?title=Q_(number_format)&oldid=1056933643
int16_t q_add_sat(int16_t a, int16_t b)
{
int16_t result;
int32_t tmp;
tmp = (int32_t)a + (int32_t)b;
if (tmp > 0x7FFF)
tmp = 0x7FFF;
if (tmp < -1 * 0x8000)
tmp = -1 * 0x8000;
result = (int16_t)tmp;
return result;
}
// precomputed value:
#define K (1 << (Q - 1))
// saturate to range of int16_t
int16_t sat16(int32_t x)
{
if (x > 0x7FFF) return 0x7FFF;
else if (x < -0x8000) return -0x8000;
else return (int16_t)x;
}
int16_t q_mul(int16_t a, int16_t b)
{
int16_t result;
int32_t temp;
temp = (int32_t)a * (int32_t)b; // result type is operand's type
// Rounding; mid values are rounded up
temp += K;
// Correct by dividing by base and saturate result
result = sat16(temp >> Q);
return result;
}
int16_t q_div(int16_t a, int16_t b)
{
/* pre-multiply by the base (Upscale to Q16 so that the result will be in Q8 format) */
int32_t temp = (int32_t)a << Q;
/* Rounding: mid values are rounded up (down for negative values). */
/* OR compare most significant bits i.e. if (((temp >> 31) & 1) == ((b >> 15) & 1)) */
if ((temp >= 0 && b >= 0) || (temp < 0 && b < 0)) {
temp += b / 2; /* OR shift 1 bit i.e. temp += (b >> 1); */
} else {
temp -= b / 2; /* OR shift 1 bit i.e. temp -= (b >> 1); */
}
return (int16_t)(temp / b);
}
