Page 274 - ARM 64 Bit Assembly Language
P. 274

Non-integral mathematics 263


                                     Listing 8.4 Dividing x by −50 with 16 bits of precision.

                    1         // Assume that r0 already contains x, where -32769 < x < 32768
                    2         // and r1 is available to hold 1/-50 * 2^4
                    3         ldr    w1,=0xAE15       // Load 1/-50 * 2^4 into r1
                    4         mul    x1,w0,w1         // Perform multiply
                    5         asr    x1,x1,#20        // shift result right by 16+4 bits
                    6         add    x1,x1,x1,lsr #63 // add one if result is negative















                     Note that there are four “hidden” bits between the radix point and the sign. Since the recipro-
                     cal  1  is negative, we do not need to round by adding one to the number R. Therefore, we
                         −50
                     will use R = 1010111000010101 2 = AE15 16 in our multiply operation.
                     Since we are using 16-bit integer operations, the dividend, x, will be an S(15,0). The product
                     of an S(15,0) and an S(−5,16) will be an S(11,16). We will remove the 16 fractional bits by
                     shifting right. The four “hidden” bits indicate that the result must be shifted additional four
                     bits to the right, resulting in a total shift of 20 bits. Listing 8.4 shows how the 16-bit division
                     code would be implemented in ARM assembly.



                     8.5 Fixed point input and output

                     When working with fixed point, it is often convenient to define a few functions for conversion
                     between base ten numbers and their fixed point representation. Listing 8.5 provides an exam-
                     ple of converting from a base 10 number represented as a string of characters to a fixed point
                     binary number, and an example showing how to print out a fixed point number.

                        Listing 8.5 Functions to convert from a string to fixed point, and to print out a fixed
                                                        point number
                    1  /*************************************************************
                    2  Name: fixedfuncs.c
                    3  Author: Larry Pyeatt
                    4  Date: 2/22/2018
                    5  *************************************************************/
   269   270   271   272   273   274   275   276   277   278   279