Page 286 - ARM 64 Bit Assembly Language
P. 286

Non-integral mathematics 275

                           Listing 8.7 ARM assembly implementation of sinx and cosx using fixed point
                                                        calculations.
                    1         //*************************************************************
                    2         // Name: sincos.S
                    3         // Author: Larry Pyeatt
                    4         // Date: 2/22/2018
                    5         //*************************************************************
                    6         /*
                    7         This is a version of the sin/cos functions that uses symmetry
                    8         to enhance precision.  The actual sin and cos routines convert
                    9         the input to lie in the range 0 to pi/2, then pass it to the
                   10         worker routine that computes the result.  The result is then
                   11         converted back to correspond with the original input.
                   12
                   13         We calculate sin(x) using the first seven terms of the Taylor
                   14         Series:  sin(x) = x - x^3/3! + x^5/5! - x^7/7! + x^9/9! - ...
                   15         and we calculate cos(x) using the relationship:
                   16         cos(x) = sin(pi/2-x)
                   17
                   18         We start by defining a helper function, which we call sinq.
                   19         The sinq function calculates sin(x) for 0<=x<=pi/2.  The
                   20         input, x, must be an S(1,30) number. The factors of x that
                   21         sinq will use are: x, x^3, x^5, x^7, x^9, x^11, and x^13.
                   22
                   23         Dividing by (2n+1)! is changed to a multiply by a coefficient
                   24         as we compute each term, we will add it to the sum, stored as
                   25         an S(2,61). Therefore, we want the product of each power of x
                   26         and its coefficient to be converted to an S(2,61) for the add.
                   27         It turns out that this just requires a small shift.
                   28
                   29         We build a table to decide how much to shift each product
                   30         before adding it to the total.  x^2 will be stored as an
                   31         S(2,29), and x is given as an S(1,30).  After multiplying x by
                   32         x^2, we will shift left one bit, so the procedure is:
                   33         x   will be an  S(1,30) - multiply by x^2 and shift left
                   34         x^3 will be an  S(3,28) - multiply by x^2 and shift left
                   35         x^5 will be an  S(5,26) - multiply by x^2 and shift left
                   36         x^7 will be an  S(7,24) - multiply by x^2 and shift left
                   37         x^9 will be an  S(9,22) - multiply by x^2 and shift left
                   38         x^11 will be an S(11,20)- multiply by x^2 and shift left
                   39         x^13 will be an S(13,18)- multiply by x^2 and shift left
                   40         x^15 will be an S(15,16)- multiply by x^2 and shift left
                   41         x^17 will be an S(17,14)
                   42         */
                   43         /*
                   44         The following table shows the constant coefficients
                   45         needed for calculating each term.
                   46
   281   282   283   284   285   286   287   288   289   290   291