Page 294 - ARM 64 Bit Assembly Language
P. 294

Non-integral mathematics 283


                                       Listing 8.9 Inefficient representation of a binimal.

                    1  typedef struct{
                    2   int sign;
                    3   int exponent;
                    4   int mantissa;
                    5  } poorfloat;



                     4. If battery life is a consideration, then a fixed point implementation can have an enormous
                         advantage.

                     Note also from the table that the assembly language version of the fixed-point sine function
                     beats the identical C version by a significant margin. It does take considerable effort to write
                     assembly code that provides higher performance than a good optimizing compiler, but in
                     some situations, the extra effort is warranted. Section 9.8 will demonstrate that a good as-
                     sembly language programmer who is familiar with the floating point hardware can beat the
                     compiler by an even wider performance margin.



                     8.7 Floating point numbers

                     Sometimes we need more range than we can easily get from fixed precision. One approach to
                     solving this problem is to create an aggregate data type that can represent a fractional num-
                     ber by having fields for an exponent, a sign bit, and an integer mantissa. For example, in C,
                     we could represent a fractional number using the data structure shown in Listing 8.9.That
                     data structure, along with some subroutines for addition, subtraction, multiplication and divi-
                     sion, would provide the capability to perform arithmetic without explicitly tracking the radix
                     point. The subroutines for the basic arithmetic operations could do that, thereby freeing the
                     programmer to work at a higher level.

                     The structure shown in Listing 8.9 is a rather inefficient way to represent a fractional number,
                     and may create different data structures on different machines. It would be better to find some
                     way to pack all three fields into 32 bits. The C language includes the notion of bit fields. This
                     allows the programmer to specify exactly how many bits are to be used for each field within
                     a struct, Listing 8.10 shows a C data structure that consumes 32 bits on all machines and
                     architectures. It provides the same fields as the structure in Listing 8.9, but specifies exactly
                     how many bits each field consumes. The compiler will compress this data structure into 32
                     bits, regardless of the natural word size of the machine.
                     The method of representing fractional numbers as a sign, exponent, and mantissa is very pow-
                     erful, and IEEE has set standards for various floating point formats. These formats can be
   289   290   291   292   293   294   295   296   297   298   299