"Go" FPGA internal decimal calculation

Source: Internet
Author: User

The position of the decimal point is fixed. We are going to use integers to represent fixed-point decimals, because the decimal point position is fixed, so there is no need to store it (if the location of the decimal point is stored, that is floating point). Since there is no place to store the decimal point, the computer certainly does not know the location of the decimal point, so the position of the decimal point is the person we write the program to remember.

First, take the 10 binary as an example. If we can calculate 12+34=46, then of course we can calculate 1.2+3.4 or 0.12+0.34. So the addition and subtraction of fixed-point decimals is the same as integers, and is independent of the position of the decimal point. The multiplication is different. 12*34=408, and 1.2*3.4=4.08. Here 1.2 of the decimal point is before the 1th digit, while the decimal point of 4.08 precedes the 2nd place, and the decimal point moves. So when you do multiplication, you need to adjust the position of the decimal point?! But since we are doing fixed-point decimal arithmetic, it is said that the position of the decimal point can not move!! How to solve this contradiction, that is to abandon the lowest bit. Just say 1.2*3.4=4.1, so we get the result of the correct fixed-point operation. So when you do a fixed-point decimal operation, you need to remember not only the location of the decimal point, but also the number of significant digits that represent the decimal point. In the above example, the number of valid digits is 2 and one after the decimal point.

Now enter binary. Our fixed-point decimal is expressed in 16-bit binary notation, the highest bit is the sign bit, then the effective bit is 15 bits. There can be 0-15 digits after the decimal point. We put the number of n after the decimal point called qn, for example, after the decimal point has 12 bits called the Q12 format of the fixed-point decimal, and Q0 is what we call an integer.

The maximum value for a positive number of Q12 is 0 111. 111111111111, the first 0 is the sign bit, after the number is 1, then this number is a decimal how much, very good operation, is 0x7fff/2^12 = 7.999755859375. The value of the expression for the fixed-point decimal in the QN format is divided by 2^n. In a computer or an integer, we think of it as the actual expressed value.

In turn, an actual value to be expressed by X is converted to the QN type of the fixed-point decimal time, is x*2^n. For example, 0.2 of the Q12 type fixed-point decimal is: 0.2*2^12 = 819.2, because this number to be stored in integers, so is 819 is 0x0333. Because the decimal part is discarded, 0x0333 is not an exact 0.2, in fact it is 819/2^12 = 0.199951171875.

Let's summarize with a mathematical expression:
x represents the actual number (* a floating-point), and Q represents its QN-type fixed-point decimal (An integer).
Q = (int) (x * 2^n)
x = (float) q/2^n

From the above formula we can quickly get a fixed-point decimal +-*/algorithm:
Assume that the values expressed by Q1,Q2,Q3 are x1,x2,x3, respectively
Q3 = q1 + Q2 If x3 = x1 + x2
Q3 = Q1-Q2 If x3 = x1-x2
Q3 = Q1 * q2/2^n If x3 = x1 * x2
Q3 = Q1 * 2^N/Q2 If x3 = x1/x2
We see that the addition and subtraction is the same as the general integer operation, while the multiplication method moves the value in order to make the decimal point of the result not move.
The multiplication of a fixed-point decimal with the C language is:
Short q1,q2,q3;
....
Q3= ((Long Q1) * (Long Q2)) >> N;

Because/2^n and * 2^n can be easily computed with a shift, a fixed-point decimal operation is much faster than a floating-point decimal. Let's use an example to verify the above formula:
Use Q12 to calculate 2.1 * 2.2, first convert 2.1 2.2 to Q12 fixed-point decimal:
2.1 * 2^12 = 8601.6 = 8602
2.2 * 2^12 = 9011.2 = 9011
(8602 * 9011) >> 12 = 18923
The actual value of 18923 is 18923/2^12 = 4.619873046875 and the actual result 4.62 of the difference of 0.000126953125, for the general calculation is accurate enough.

"Go" FPGA internal decimal calculation

Related Article

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.