(Original) how to calculate floating point data? (SOC) (OpenGL)

Source: Internet
Author: User

Abstract
ShowAlgorithmWe often encounter Floating Point Data calculation. How to calculate floating point data is a frequently asked question of start-ups in the language of learning.

Introduction
Use environment: US us II 8.0

In de2_ TV of de2 and de2_70_ TV of DE2-70, there is a ycbcr2rgb. V which converts YCbCr into RGB. The formula is as follows:

R =   1.164 (Y - 16 ) +   1.596 (Cr - 128 )
G =   1.164 (Y - 16 ) -   0.391 (CB - 128 ) -   0.813 (Cr - 128 )
B =   1.164 (Y - 16 ) +   2.018 (CB - 128 )

Converting YCbCr into RGB involves Floating Point Data calculation. This is a challenge in the case of OpenGL. It is not as simple as C, due to ycbcr2rgb. V is more efficient than others, and is less suitable for example, whileIt may be the simplest floating point operation.

F = C *   1.8   +   32

C is the degree of attention, and F is the degree of attention. Let's take a look at how to implement it using tildes.

Cf2_bad.v/OpenGL 

1 Module C2f_bad (
2 Input Iclk,
3 Input Irst_n,
4 Input [ 7 : 0 ] IC,
5 Output [ 10 : 0 ]
6 );
7
8 Reg [ 7 : 0 ] C;
9 Reg [ 10 : 0 ] F;
10
11 Assign Of = F;
12
13 Always @( Posedge Iclk, Negedge Irst_n) Begin
14 If ( ! Irst_n) Begin
15 C <=   0 ;
16 F <=   0 ;
17 End
18 Else   Begin
19 F <= C *   1.8   +   32 ;
20 End
21 End
22
23 Endmodule

After us II was created, the following warning messages are displayed:

Error ( 10125 ): Maid error at c2f_bad.v ( 19 ): Must use only constant operands for operator " * "

It turns out that the floating point data cannot be directly used for the calculation in OpenGL.

C2f_good.v/OpenGL

1 /*  
2 (C) oomusou 2008 Http://oomusou.cnblogs.com
3
4 Filename: c2f_good.v
5 Compiler: Quartus II 8.0
6 Description: Demo how to calculate float;
7 Release: 10/11/2008 1.0
8 */
9
10 Module C2f_good (
11 Input Iclk,
12 Input Irst_n,
13 Input [ 7 : 0 ] IC,
14 Output [ 10 : 0 ]
15 );
16
17 Reg [ 7 : 0 ] C;
18 Reg [ 10 : 0 ] F;
19 Reg [ 10 : 0 ] Sum;
20
21 Assign Of = F;
22
23 Always @( Posedge Iclk, Negedge Irst_n) Begin
24 If ( ! Irst_n) Begin
25 C <=   0 ;
26 F <=   0 ;
27 Sum <=   0 ;
28 End
29 Else   Begin
30 C <= IC;
31 Sum <= C *   7   +   128 ;
32 F <= Sum >   2 ;
33 End
34 End
35
36 Endmodule

Result

Except for 2nd clock values because the sum value does not exist, 32 is unknown, and the values starting from 3rd clock values are correct.

Synthesis result

Fmax is 237.19 MHz

Currently, pipeline is 237.19 MHz if it is not used, and pipeline can be higher.

31 rows

Sum <= C *   7   +   128 ;
F <= Sum >   2 ;

Only these two lines are supported by the program. Why can this be used to calculate floating point data?

The original formula is as follows:

F = C *   1.8   +   32

Now, F is shifted to the left, so the right-hand side of each segment is multiplied by 2 ^ 2 = 4, 1.8*4 = 7.2, and the approximate value is 7, 32*4 = 128.

There is no way to enlarge the cursor bit. Due to the shift feature, only the Npower of 2 can be enlarged so that the floating point data can be enlarged, the number of records is close to the integer.

F <   2   = C *   7   +   128

32 rows

F <= Sum >   2 ;

Because it was previously magnified by 4 times, we moved back to the right two places.

Download the full program
C2f_good.7z

Conclusion
The speed is fast, the surface is small, and the hardware is easy to implement. The missing point is the accuracy deviation, which is limited to the number of digits, when necessary, it must be used together with the graphic phantoms.

See also
(Original license) How can I use OpenGL to convert YCbCr into RGB? (SOC) (OpenGL) (DE2-70)

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.