Lecture 16 – Signed Integers and Arithmetic Part II

Ryan Robucci

Table of Contents

References

Rounding Errors float to int

Takeway

To achieve common rounding when casting from float to int

Affecting Conversion Rounding Error with Numerator Bias

Assume unsigned int A,B;

Example:

Takeway

For positive integers, bias the numerator with half of the denominator to approximate division following by rounding

First go big or first go small: choices for order of operations affect result

Takeway

Impacts of Limited Precision in DSP Filters

Eliminating Overflow or Saturation with Input Prescaling

Increasing Effective Computation Precision with Input Prescaling

Log Scale

Avoid Unnecessary Operations

Implementing Saturation to large avoid modular arithmetic error

Soft Limiting

Instantaneous value mapping and affect on sinusoidal time series:

Quantization / Round-off Noise

Limit Cycles

Verilog: Working with signed and unsigned reg and wire

Verilog 2001 provides signed reg and wire vectors

Casting to and from signed may be implicit or may be explicit by using

Verilog: Signed/Unsigned Casting

Truncation and Extension

Truncation

Signed and Unsigned Extension

Verilog Rules for expression bit lengths

Verilog Contex-Determined Addition

Self-Determined Self-Determined Expression and Self-Determined Operands

Rules for expression bit lengths from IEEE Standards

5.4.1 Rules for expression bit lengths

The rules governing the expression bit lengths have been formulated so that most practical situations have a natural solution.

The number of bits of an expression (known as the size of the expression) shall be determined by the operands involved in the expression and the context in which the expression is given.

A self-determined expression is one where the bit length of the expression is solely determined by the expression itself—for example, an expression representing a delay value.

A context-determined expression is one where the bit length of the expression is determined by the bit length of the expression and by the fact that it is part of another expression. For example, the bit size of the right-hand expression of an assignment depends on itself and the size of the left-hand side.

Table 5-22 shows how the form of an expression shall determine the bit lengths of the results of the expression. In Table 5-22, i, j, and k represent expressions of an operand, and L(i) represents the bit length of the operand represented by i.

Multiplication may be performed without losing any overflow bits by assigning the result to something wide enough to hold it.

Reference from IEEE Standards

IEEE Standard for Verilog Hardware Description Language IEEE Std 1364-2005

Obtaining the IEEE Verilog Specification

At UMBC or if offsite use the UMBC single-sign-on option, goto http://ieeexplore.ieee.org/
Search for IEEE Std 1364-2005
You'll find

Addition and Mixed Sign

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;

Scaling by Powers of Two using Shift

Takeaway

Multiplication

1011 (M bits) x 10010 (Nbits):

Multiplication Overflow Check

Software Multiplication using Smaller Hardware Multipliers

Takeaway

Example: 8-bit multiplication using 4-bit multipliers

Assuming 4-bit multipliers:
20*30 = (0b000101000b00011110)(0b\red{0001}0100 * 0b\green{0001}1110)
= (116+4)×(116+14)(\red{{1 \cdot \underline{16}}} + 4) \times ( \green{1 \cdot \underline{16}} + 14)
= (1×1256)+(116×14)+(116×4)+(4×14)(\red{1} \times \green{1} \cdot \underline{256}) + ( \red{{1 \cdot \underline{16}}} \times 14) + ( \green{1 \cdot \underline{16}} \times 4) + ( 4 \times 14)
= (1×1)8+(1×14)4+(1×4)4+(4×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=600256 + 224 + 64 + 56 = 600

Karatsuba Algorithm

AB={AH,AL}{BH,BL}=(AH<<8)(BH<<8)+(AH<<8)(BL)+(AL)(BH<<8)+(ALBL)=((AHBH)<<16)+((AHBL+ALBH)<<8)+(ALBL)\begin{array}{c} A*B = \{AH, AL\} * \{BH, BL\} = \\ (AH << 8) * (BH << 8) +\\ (AH << 8) * (BL) + (AL) * (BH<<8) +\\ (AL * BL)\\ = ((AH*BH) << 16) +\\ ((AH *BL + AL* BH)<<8) +\\ (AL * BL)\\ \end{array}

(AHBH)<<16+(((AH+AL)(BH+BL)AHBHALBL)<<8)+ALBL\begin{array}{c} \textcolor{green}{(AH*BH)} <<16 + \\ (\textcolor{green}{(}\textcolor{red}{(AH+AL)*(BH+BL)}\textcolor{green}{-AH*BH-AL*BL)}<<8)+\\ \textcolor{green}{AL*BL} \end{array}

EMULATING LARGE MULTIPLIERS

Multiplication by Low-Density Constants

Takeaway

Rounded Integer Division

round(float(A)/float(B))=(A+B/2)/B|\rm round(float(A)/float(B))| = (|A|+|B/2|)/|B|

Takeaway

Rounded Division example with even denominator

 floating-point division: 7.0/float4=1.75 rounded result: round(7.0/float4)=2.0 cast result to int: int(7.0/float4)=1 bias float before cast: int((7.0/float4)0.5)=int((1.75)0.5)=int(2.25)=2 integer biasing: ((7+(4/2))/int4)=fix((7.0+(2))/float4)=fix(2.25)=2\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 && ) \red{- 0.5}) &&= \\ & && \rm int(( && \blue{-1.75} &&) \red{- 0.5}) &&= \\ & && \rm int( && && \blue{-2.25}) &&= -2 \\ &\text { integer biasing: } && \rm (( && 7\red{+(4/2)}) \underset{\rm int}/ -4 && ) &&= \\ & && \rm fix(( && 7.0\red{+(2)}) \underset{\rm float}/ -4 && ) &&= \\ & && \rm fix( && \blue{-2.25} && ) &&= -2 \\ \end{alignedat}

Rounded Division example with odd denominator

 floating-point division: 8.0/float3=2.666... rounded result: round(8.0/float3)=3.0 cast result to int: int(8.0/float3)=2 bias float before cast: int((8.0/float3)0.5)=int(2.666...0.5)=int(3.16...)=3 integer biasing: ((8+(3/2))/int3)=fix((8.0+(1))/float3)=fix(3.0)=3\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 && ) \red{- 0.5}) &&= \\ & && \rm int( && \blue{-2.666...} && \red{- 0.5} ) &&= \\ & && \rm int( && && \blue{-3.16...}) &&= -3 \\ &\text { integer biasing: } && \rm (( && 8\red{+(3/2)}) \underset{\rm int}/ -3 && ) &&= \\ & && \rm fix(( && 8.0\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-Dependant-Bias before Fix

To Properly Mimic Signed-Integer Divided by Power of Two with Shift, a Sign-dependant Pre-Shift Bias is Required

Takeaway

Divide xx by 2k2^k is defined to be ROUND TOWARDS ZERO --- mimicking this detail with arithmetic right-shift requires pre-biasing xx by adding 2k12^k-1: ( x + ((1<<k)-1) ) << k

Very Long and Arbitrary Precision Arithmetic Using Software

Fixed-Point Arithmetic

Later Topic will be Fixed-Point Arithmetic, which allows for computing with representation of fractional values using only integer arithmetic units

Additional Operations

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