Page 278 - ARM 64 Bit Assembly Language
P. 278
Non-integral mathematics 267
strings. For some applications it could also be useful to have functions for converting between
IEEE floating point (covered later in this chapter) and fixed point formats.
8.6 Computing sine and cosine
It has been said, and is commonly accepted, that “you can’t beat the compiler.” The mean-
ing of this statement is that using hand-coded assembly language is futile and/or worthless
because the compiler is “smarter” than a human. This statement is a myth, as will now be
demonstrated.
There are many mathematical functions that are useful in programming. Two of the most use-
ful functions are sinx and cosx. However, these functions are not always implemented in
hardware, particularly for fixed point representations. If these functions are required for fixed
point computation, then they must be written in software. These two functions have some nice
properties that can be exploited. In particular:
• If we have the sinx function, then we can calculate cosx using the relationship
π
cosx = sin . (8.1)
2 − x
Therefore, we only need to get the sine function working, and then we can implement
cosine with only a little extra effort.
• sinx is cyclical, so ...sin−2π = sin0 = sin2π .... This means that we can limit the do-
main of our function to the range [−π,π].
• sinx is symmetric, so that sin−x =−sinx. This means that we can further restrict the
domain to [0,π].
• After we restrict the domain to [0,π], we notice another symmetry, sinx = sin(π −
π
x), π ≤ x ≤ π and we can further restrict the domain to [0, ].
2 2
• the range of both functions, sinx and cosx, is in the range [−1,1].
If we exploit all of these properties, then we can write a single shared function to be used by
both sine and cosine. We will name this function sinq, and choose the following fixed point
formats:
• sinq will accept x as an S(1,30),and
• sinq will return an S(1,30).
These formats were chosen because S(1,30) is a good format for storing a signed number
between zero and π , and also the optimal format for storing a signed number between one and
2
negative one.