Page 277 - ARM 64 Bit Assembly Language
P. 277

266 Chapter 8


               104  }
               105
               106  /* Print a signed fixed point number with the given number of
               107   bits in the fractional part.  NOTE: frac_bits must be between
               108   0 and 31 for this function to work properly.
               109  */
               110  #define MAX_DIGITS 8
               111  void printS( int num, int frac_bits )
               112  {
               113  unsigned int mask = (1 << frac_bits) - 1;
               114  unsigned int fracpart;
               115  int intpart;
               116  int count = 0;
               117  int frac_digits[MAX_DIGITS+1];
               118
               119  if(num < 0)
               120    {
               121      printf("-");
               122      num = -num;
               123    }
               124  else
               125    printf(" ");
               126  /* Remove the integer part and keep the fraction part */
               127  fracpart = num & mask;
               128  intpart = num>>frac_bits;
               129
               130  /* Print all of the digits in the fraction part into frac_digits.*/
               131  for(count=0;count<MAX_DIGITS+1;count++)
               132    {
               133      fracpart *= 10;
               134      frac_digits[count] = fracpart >> frac_bits;
               135      fracpart &= mask ;
               136    }
               137
               138  /* Round the fractional part */
               139  frac_digits[MAX_DIGITS] += 5;
               140  intpart += roundit(frac_digits,MAX_DIGITS);
               141  /* Print the integer part (with the sign, if it is negative) */
               142  printf("%d.",intpart);
               143  /* Print the fractional digits */
               144  for(count=0;count<MAX_DIGITS;count++)
               145    printf("%d",frac_digits[count]);
               146  }

                  These functions are not meant to be particularly efficient, but are given as examples of the
                  types of functions that would be needed for input and output of fixed point numbers. Another
                  way to approach fixed point I/O would be to provide only functions to convert fixed point val-
                  ues of various sizes to and from C strings, then use scanf and printf to read and write the
   272   273   274   275   276   277   278   279   280   281   282