Use any bit width in the C/C ++ Algorithm Design

Source: Internet
Author: User

Development point (fixed-point)AlgorithmIt is usually necessary to strike a balance between design functionality, Digital Precision Modeling, and verification (simulation) speed. Now, a new data class can simplify this process, so as to get a simpler and more precise modeling accuracy, better digital refinement, and faster verification cycle, ansi c/C ++ is the best language for developing this digital refinement algorithm.

An algorithm is inherently applicable to Operation integers or ideal real numbers (such as the coefficients of a Digital Filter). They may also use floating point or fixed point types. Generally, in the early stages of algorithm development, the float or double Floating Point types in C language are often used, because they provide a very large dynamic data range, it is applicable to most programs.

Use the built-in float Type of C to model an FIR Filter

Algorithms can refine the number so that fixed-point arithmetic can be used to reduce the complexity of hardware or software implementation. In terms of hardware, integer or fixed-point arithmetic is limited to the minimum bit width, which can meet the performance, space, and energy consumption needs in essence. If DSP processor is used in implementation, if you limit the algorithm to integer or fixed-point arithmetic, you can use the lowest possible processor for a specific program.

The modeling of fixed-point arithmetic can be completed through the built-in floating point or Integer type in the C language. In this case, it needs to be explicitly encoded and limited by the maximum number of floating point numbers and integers in C: A 64-bit integer or 53-bit ending number will impose more restrictions on the bit width of the operand. For example, if two 33-bit numbers are multiplied, the value exceeds the value range that can be expressed by a 64-bit C integer. Figure 2 demonstrates an example of a FIR filter, but the temp variable is limited to 15-Bit fixed-point precision, of which 10 are used for integer. In this implementation, the right part of the LSB is discarded (the truncation of the quantitative model), and the left part of the MSB is also discarded (the overflow model of the package, float (or double) models are limited in accuracy and cannot be re-synthesized (synthesis ). Similarly, since the strict bit precision definition of the integer model is preemptible, And the built-in floating point type is applied first, for Division operations, it is very difficult to implement.

Use float to model fixed-point Behavior

When many algorithms can be written based on the accuracy of the Local C data type, you will have great expectations for integers and fixed-point algorithms that support any length, and the hardware description language (HDL) for example, the path follows the same path in VHDL. As C/C ++ is increasingly used for advanced Synthesis and Verification tools (High-Level Synthesis and Verification tools ), it also proves that this language essentially has a data type library that meets the needs of current and future programs. The support of any length type also enables a uniform definition of data type behavior, while the unified semantics avoids some manual restrictions.

Algorithm C data type

The algorithm C data type is a class-based C ++ library, which implements integer and fixed point types of any length. These freely accessible types have a series of advantages, including unified and well-defined semantics, as well as the runtime speed comparable to the built-in data types in C/C ++. Compared with the corresponding types in EMC, the runtime speed exceeds 10 times. These data types can be used in any program that complies with the C ++ or EMC specifications and have highly synthetic semantics.

Semantics

Semantic uniformity and consistency are the key to avoiding functional errors in algorithms. The following examples also illustrate this:

As we all know, the range of the variable ActLength is 1 to 255. If the compiler synthesis does not know its range, it cannot be optimized accordingly, its declaration will change from int to more rigorous SC _uint <8> type. Although the synthesis will get better results, the simulation is incorrect. After some debugging, the source of the problem is found: In the comparison expression k> = ActLength, the two operands are converted into a signed int and an unsigned long (a 64-bit unsigned integer, which is the base type of the SC _uint type. This explanation is: the C/C ++ integer escalation rule specifies that the int operand is upgraded to an unsigned long before comparison. For example, if the value of k is-1, after being promoted to unsigned long, it will become 2 ^ 64-1.

Problems in semantics such as this are hard to perceive and related to Bit Width. For example, some people may want to expand the Bit Width of an existing algorithm, this problem may also be related to a specific platform, for example, to 1 <32 (both operands are of the int type and the result is of the int type) everyone expects to return 0, but on most platforms (or compilers), it will return 1 (no shift, only because the lower five digits of the second operand are calculated ); when the first operand is a 64-bit integer, the platform dependency is more obvious and frequent. The main problem is that the C/C ++ standard does not specify the shift value in the case of a 32-bit integer (the second operand) if the value exceeds the range 0 to 31, or if the value of the shift exceeds the value range 0 to 63 in the case of a 64-bit integer. Unfortunately, data types such as SC _int and SC _uint cannot avoid such platform dependencies for users.

Algorithm C data types are designed to provide unified and consistent semantics. Therefore, they are predictable. For example, mixing a symbolic number with an unsigned operand will still produce the expected results; the length of these types is not limited, so there is no so-called precision problem. All operations, including shift and division, have a complete and consistent definition. Mixing different types can also produce the expected results. For example, if x is a C built-in type, when y is an algorithm C type, the expressions x + y and y + x both return the same results.

Running time

The purpose is to use the built-in type (the Bit Width cannot exceed 64 bits) to support any length type and avoid the preceding semantic problems) run Time optimized by manual C/C ++ encoding. The C type of the algorithm is designed and implemented for fast execution and semantic synthesis. The Bit Width of all operations is determined by the C ++ compiler statically, which avoids dynamic memory allocation, this reduces the running time and makes semantics easier to synthesize. In addition, the implementation also optimizes the speed. Therefore, more dedicated and efficient code may be called, making full use of the optimization features of the current compiler.

 

Table 1: Comparison of running time with the specification of ac_fixed

Table 1 compares various runtime operations when fixed-point arithmetic is modeled using algorithm C fixed-point type ac_fixed. The implementation of float is shown in figure 2, SC _fixed_fast is the fixed-point data type with limited precision in EMC, and SC _fixed is the fixed-point data type with any precision. In practice, the FIR filter is called 10 ^ 8 times. TRN/WRAP runs for 6.5 seconds and RND/SAT is 29 seconds. Other types of running time can also be introduced from this table in sequence. For example, SC _fixed TRN/WRAP will take 6.5 s x 227 = 1476 s (nearly 25 minutes ). For reference, the algorithm in figure 1 (using float without fixed point modeling) takes 3.5 s (nearly twice as slow as ac_fixed with fixed point modeling ).

The preceding running time data is measured by GCC 4.1.1, and the data obtained in GCC or Visual C ++ 2005 of previous versions is roughly the same.

In addition, the running time can be further shortened by integer data type or bit operations. Table 2 is the result of a DCT algorithm. It is obtained by a shift operation that reads and writes two digits each time. The running time is SC _int with limited EMC precision and SC _bigint of any length.

Conclusion

Based on the general standard ansi c ++, this new integer and Fixed-Point Algorithm Type C allows the algorithm and system designers to specify any bit width, thus providing a simulation efficiency of 200 times higher than the traditional data type. These new data types can be a very valuable part of the C-to-RTL design chain and ensure the accuracy of any Bit Width throughout the implementation process.

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.