Repost the addition of signed numbers

Source: Internet
Author: User
Tags ranges

To sum up, perform the signed operation step 1. extend each input to the same width as the output. operation 3. the last few bit widths of the calculation result. The bit width is the size of the output bit width. To prevent overflow, determine the size of the output width in advance.

Abstract
If you want to implement the originally used simulated algorithm using a hardware environment, there will be two basic problems: one is how to handle data? How does one handle overflow? Although it is very basic, once there is a problem, it is very difficult to debug.

Introduction
Environment: NC-OpenGL 5.4 + Debussy 5.4 V9

Generally, in the Development algorithm segment, we will use C/C ++ to launch these high-profile speeches, c/C ++ is convenient and straightforward to handle the problem of overflow, mainly because Int Is 4 bytes (32 bit) it's not easy to use overflow because it's too big. If we use hardware to implement it, there will be two basic questions right away, how should the hardware handle data? How to deal with overflow?

When we declare Reg and wire, we can naturally use +-*/to synthesize the corresponding sub-devices, multiplier and sub-devices, but these are all unsigned integers) computing, that is, computing can only be larger than or equal to zero integer addition and Division operations, but cannot handle computing operations. In addition, unlike C/C ++, the int type is 32 bit. In order to save the hardware cost, we will handle the value field and carefully declare the bit numbers of Reg and wire, for example, if it is only 4-bit or 8-bit, after the calculation, it may be in a boundary Test Pattern and overflow accidentally.

In the (original) unlimited data and dynamic data, the integrated computing (IC design) (OpenGL) (OS) (Linux) and (original) how to design a route? I have already discussed this issue in (SOC) (sparse core). This time I plan to discuss it again, in addition, we will take the issue of overflow into consideration.

In this article, we will first discuss the addition operation part, and the multiplication part will be discussed in another article?

Run the code in the kernel language
The formula provided by Tilde is "unsigned" and "Signed:

  • Unsigned: Not including signed bit
    • In 4 bits, the value ranges from 0000 ~ 1111, that is, 0 ~ 15
  • Signed: Contains signed bit (MSB is signed bit, 1 is positive, 0 is positive, and the number of digits is expressed as 2 bytes)
    • In 4 bits, the value ranges from 1000 ~ 0111, that is,-8 ~ + 7

Binary Signed addition Calculation
Before we start to execute the signed addition operation using the kernel, let's take a look at how the binary singed addition works?

Normal condition (No overflow)
(+ 6) + (-3) = (+ 3)

To save resources, we intentionally add 4-bit + 6 to 3-bit. If we directly add the two signed values, the answer is-7, although the answer is incorrect?

Because 4 bits and 3 bits are added, the result may be 5 bits. The correct method is to add 4 bits and 6 to signed extension to 5 bits, and the-3 of 3 bits must also be signed extension to 5 bits before they are added. If the last bit reaches 6 bits, the value of 6 bits will not be taken?

 

How can I add singed extension here? Simply put, when multiple bits are used to show the value of the signed signature, how does one reset the signed bit signature?

It means that if the value of a 3-Bit Signed value is to be expressed as 5 bits, it must be written to the signed bit to be expressed as 5 bits, so 101 must be changed to 11101?

Boundary Condition (positive overflow)
(+ 7) + (+ 3) = (+ 10)

To save resources, we intentionally add 4-bit + 7 to 3-bit + 3. If we directly add the two signed values, the answer is-6, although the answer is incorrect.

In the root user's example, + 7 and + 3 must be signed extension before they can be added. In this way, the correct answer + 10?

But now it's a problem. The value of + 10 must be 5 bits for display. If the value of + 10 is 4 bits, it can only be-8 ~ + 7, + 10 is probably already working on overflow?

If it can only be expressed in 4 bits, because it is positive, MSB must be 0 (sum [3] = 0), So If MSB is 1, it indicates that it comes from the forward, that is, it is just overflow (sum [3] In this example is 1, so it is already overflow). In addition, because the current calculation result is 5 bits and it is positive, therefore, sum [5] Must be 0.

That is to say,If sum [5] = 0 and sum [4] = 1So 01010 is positive overflow for 4 bits.

Boundary Condition (condition overflow)
(-5) + (-4) = (-9)

In the same way, we intentionally add 4-bit-5 to 3-bit-4. If we directly add the two signed values, the answer is-1, the answer is not correct?

In the previous two examples of root partitions, as in-5 and-4, a signed extension must be performed before addition. In this way, a positive answer is obtained-9? When the first digit reaches 6 bits, the answer is 10111?

If the value range of-9 is 4 bits, the value can only be-8 ~ + 7,-9 is it very difficult to say that it is already running overflow?

If the value can only be 4 bits, MSB must be 1 (sum [3] = 1) because it is backward. Therefore, if MSB is 0, it indicates that it is backward, that is, merge overflow (sum [3] In this example is 0, so there is already too overflow). In addition, because the current calculation result is 5 bits and it is complete, so sum [5] must be 1?

That is to say,If sum [5] is 1 and sum [4] is 0,So 10111 for 4 bits, is dynamic overflow?

Binary Signed addition calculation Summary

Based on the previous three examples, we have the following conclusions:

  • M bit + M bit => (m + 1) Bit
  • M bit + n bit => (m + 1) bit, where n <m
    • Both M bit and n bit must be signed extension to (m + 1) bit before they can be added.
    • If the result is (m + 2) bit, the result is (m + 1) bit.
  • If sum [M + 1] ^ sum [m] is 1, overflow exists.
    • If sum [M + 1] is 0 and sum [m] is 1, it is normal overflow.
    • If sum [M + 1] is 1 and sum [m] is 0, then merge Overflow

 

Verify with the use of OpenGL
Signed_add.v/OpenGL

1 /*
2 (c) oomusou 2009 http://oomusou.cnblogs.com
3
4 filename: signed_add.v
5 simulator: NC-OpenGL 5.4 + Debussy 5.4 V9
6 Description: signed add & Overflow
7 release: Oct/24/2009 1.0
8 */
9
10 module signed_add (
11 CLK,
12 rst_n,
13 a_ I,
14 B _ I,
15 sum_o
16 );
17
18 input CLK;
19 input rst_n;
20 input [3: 0] a_ I;
21 Input [2: 0] B _ I;
22 output [3: 0] sum_o;
23
24 Reg [4: 0] sum_t;
25 [email protected] (posedge CLK or negedge rst_n)
26 if (~ Rst_n)
27 sum_t <= 5'h0;
28 else
29 sum_t <= {a_ I [3], a_ I} + {2 {B _ I [2]}, B _ I };
30
31 assign sum_o = (~ Sum_t [4] & sum_t [3])? 4 'b0111: // + Overflow
32 (sum_t [4] & ~ Sum_t [3])? 4 'b1000: //-Overflow
33 sum_t [3: 0];
34 endmodule


20 ~ 22 rows

Input [3: 0] a_ I;
Input [2: 0] B _ I;
Output [3: 0] sum_o;


If the input value is 3 bits, the input value is 4 bits, and the output value is 4 bits, which is the same as the previous example?

29 rows

Sum_t <= {a_ I [3], a_ I} + {2 {B _ I [2]}, B _ I };


4-bit a_ I is signed extension to 5 bit, and 3-bit B _ I is signed extension to 5 bit?

31 rows

(~ Sum_t [4] & sum_t [3])? 4 'b0111: // + Overflow


Judge whether the condition is positive overflow. If sum_t [4] is 0 and sum_t [3] is 1, is it positive overflow?

32 rows

(Sum_t [4] & ~ Sum_t [3])? 4 'b1000: //-Overflow


Determines whether a consumer is an external overflow. If sum_t [4] is 1 and sum_t [3] is 0, then it is an external overflow?

Testbench
Signed_add_tb.v/OpenGL

1 /*
2 (c) oomusou 2009 http://oomusou.cnblogs.com
3
4 filename: signed_add_tb.v
5 simulator: NC-OpenGL 5.4 + Debussy 5.4 V9
6 Description: signed add & overflow test.pdf
7 release: Oct/24/2009 1.0
8 */
9
10 'include "signed_add.v"
11
12 module signed_add_tb;
13
14 Reg CLK;
15 Reg rst_n;
16 Reg [3: 0] a_ I;
17 Reg [2: 0] B _ I;
18 wire [3: 0] sum_o;
19
20 // 4 Bit
21 //-8 ~ + 7
22 // 3 bit
23 //-4 ~ + 3
24 initial begin
25 // a_ I <= 4 'b0000;
26 // B _ I <= 3'b000;
27 a_ I <= 4'd0;
28 B _ I <= 3' D0;
29
30 // normal
31 // (+ 6) + (-3)
32 #10;
33 // a_ I <= 4 'b0110;
34 // B _ I <= 3 'b101;
35 a_ I <= 4'd6;
36 B _ I <=-3 'd3;
37
38 // Overflow
39 // 7 + 3 = 10
40 #20;
41 // a_ I <= 4 'b0111;
42 // B _ I <= 3 'b011;
43 a_ I <= 4'd7;
44 B _ I <= 3' D3;
45
46 // Underflow
47 // (-5) + (-4)
48 #20;
49 // a_ I <= 4 'b1011;
50 // B _ I <= 3'b100;
51 a_ I <=-4 'd5;
52 B _ I <=-3 'd4;
53
54 #20;
55 // a_ I <= 4 'b0000;
56 // B _ I <= 3'b000;
57 a_ I <= 4 'd0;
58 B _ I <= 3' D0;
59 end
60
61
62 initial CLK = 1' B0;
63 always #10 CLK = ~ CLK;
64
65 initial begin
66 rst_n = 1' B0;
67 #5;
68 rst_n = 1' B1;
69 end
70
71 initial begin
72 $ fsdbdumpfile ("signed_add.fsdb ");
73 $ fsdbdumpvars (0, signed_add_tb );
74 #100;
75 $ finish;
76 end
77
78 signed_add signed_add0 (
79. CLK (CLK ),
80. rst_n (rst_n ),
81. a_ I (a_ I ),
82. B _ I (B _ I ),
83. sum_o (sum_o)
84 );
85
86
87 endmodule


Result

 

7 + 3 = 10. Because there is a positive overflow, the 4-bit maximum value + 7 is used?
(-5) + (-4) = (-9). Because there is already an overflow, the minimum value of 4 bits is-8?

 

Download the complete program
Signed_add.7z

Conclusion
In this article, I will explain how to add a number to a number in a number or in a cell phone number or how to determine the problem of overflow, however, it is very important to use the hardware real-time algorithm. Next time I will discuss how to implement the multiplication of dynamic numbers in the digital path and the Tilde?

Repost the addition of signed numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.