Page 284 - ARM 64 Bit Assembly Language
P. 284

Non-integral mathematics 273

                     sine and cosine function can be implemented in ARM assembly using fixed point computa-
                     tion, and Listing 8.8 shows a main program which prints a table of values and their sine and
                     cosines.

                           Listing 8.6 C implementation of sinx and cosx using fixed point calculations.
                    1  #include <stdio.h>
                    2  //*************************************************************
                    3  // Name: sincos.c
                    4  // Author: Larry Pyeatt
                    5  // Date: 2/22/2018
                    6  //*************************************************************
                    7  // This file provides functions for calculating sine and
                    8  // cosine using fixed-point arithmetic. It uses the first
                    9  // nine terms in the Taylor series.
                   10
                   11  #define pi 0x3243F6A8  // pi as an S(3,28)
                   12  #define pi_2 0x1921FB54  // pi/2 as an S(3,28)
                   13  #define pi_x2 0x6487ED51 // 2*pi as an S(3,28)
                   14
                   15  // Define a structure that holds coefficient and shift.
                   16  struct tabentry{
                   17   int coeff;
                   18   int shift;
                   19  };
                   20
                   21  // create a table of coefficients and shift amounts.
                   22  #define TABSIZE 8
                   23  static struct tabentry sintab[]={
                   24   {0xAAAAAAAA,  0},  // term 2
                   25   {0x44444445,  2},  // term 3
                   26   {0x97F97F97,  6},  // term 4
                   27   {0x5C778E96,  10}, // term 5
                   28   {0x9466EA60,  15}, // term 6
                   29   {0x5849184F,  20}, // term 7
                   30   {0x94603063,  26}, // term 8
                   31   {0x654B1DC1,  32}  // term 9
                   32  };
                   33
                   34  //*************************************************************
                   35  // sinq is used internally by the sine and cosine functions
                   36  //  input: x as an S(1,30) s.t. 0 <= x <= pi/2
                   37  //  returns sin(x) as an S(3,28)
                   38  static int sinq(int x)
                   39  {
                   40   long long sum;
                   41   long long tmp;
                   42   long long curpower = x;
                   43   long long xsq;
   279   280   281   282   283   284   285   286   287   288   289