![]() Notice that the fractional portion of the structure is calculated as FULLSIZEINT- x, instead of using y as the size of the member. If FULLSIZEINT is longer than this maximum, then floating point needs to be used. This typedef is not portable, and is best put in a unique header file:įULLSIZEINT is either long, int, short, or char and either signed or unsigned and cannot be longer than the maximum length integer available, or the structure will not work. If this is not the case, reverse the declaration of the structure members integer and fraction in the structure. The structure assumes that the compiler assigns the bits in an integer from most significant to least significant. ![]() This typedef is a union with an embedded structure as follows. ![]() Typedefĭefinition of the fixed-point algorithm requires a typedef for each fixed-point variable type that will be used by the system. This consists of typedefs and macros, with the possibility that a function may need to be developed. The second step in the algorithm is to create the definitions for C that are required to implement the variables. The resultant columns show the actual ranges and granularity available after the bit requirements are complete. The results for our example requirements for all the initial parameters are shown in Table 1. If this sum is greater than the maximum integer size in your system, then floating-point numbers are required and will probably be more efficient. This sum will be referred to later as the FULLSIZEINT. Round this sum up to either 8, 16, or 32. 0625), and 7.įinally, the minimum number of bits required is x + y. For our example requirements, this is 7 (1/128 =. Next, calculate the number of bits y required to provide this granularity such that 1/2 y G 1/2 y -1. The example requirements define the granularity and no further calculation is needed. Then, determine the granularity G that is required. For our example requirements, x is 1, 7, and 11, respectively. If the number is to be signed, add 1 to x. Second, calculate the number of bits x required to store this number such that 2 x M 2 x -1. The method presented in this article is essentially the same as in assembly language and is described in the following steps. Unfortunately, C does not provide native support for fixed-point math. Through the simple use of integer operations, the math can be efficiently performed with very little loss of accuracy. Fixed-Point Arithmeticįixed-point math typically takes the form of a larger integer number, for instance 16 bits, where the most significant eight bits are the integer part and the least significant eight bits are the fractional part. This article will discuss a method to implement fixed-point math in C. The alternative, used by assembly language programmers for years, is to use fixed-point math that is executed using integer functions. However, if floating-point math is used on a microprocessor that supports only integer math, significant overhead can be incurred to emulate floating point, both in ROM size and in execution time. When used in a system that includes a hardware floating-point math unit, this can be a very powerful feature. This simplifies the task of programming and increases the accuracy of calculations. One advantage of using a high-level language is the native support of floating-point math. But floating-point values can be manipulated as integers, as a less expensive alternative. Floating-point arithmetic can be expensive if you’re using an integer-only processor.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |