Abstract:
A method for a processor computing a first trigonometric function to use an alternative trigonometric function for certain ranges of the operand. A modulo function may be used to provide an operand with a reduced range, and the modulo function may subtract in multiple steps in a manner that preserves low-order bits.

Description:
BACKGROUND 
       [0001]    Microprocessors and microcontrollers often need to compute various math functions, for example trigonometric functions such as sine and cosine. One common method is to use a Taylor series in which a math function is approximated by a polynomial. In general, when a polynomial P n (x) is used to approximate a function f(x) there is some inherent error when the polynomial is truncated at x n , even for computation with infinite precision. Using processors with finite precision increases the error as low-order bits are lost during computation. The error can be reduced by increasing the number of bits used to represent each quantity. However, in general, increasing the number of bits increases gate count, increases power consumption, and may increase computation time. There is an ongoing need for increased precision in computation of math functions, particularly for low-gate-count ultra-low-power applications. 
     
    
     
       BRIEF DESCRIPTION OF THE DRAWINGS 
         [0002]      FIG. 1A  is a graph of a trigonometric function (cos(x)) illustrating error in a polynomial approximation of the trigonometric function. 
           [0003]      FIG. 1B  is a graph of a trigonometric function (sin(x)) illustrating error in a polynomial approximation of the trigonometric function. 
           [0004]      FIG. 2  is a flow chart of an example embodiment of a method of computing a trigonometric function. 
           [0005]      FIG. 3  is a block diagram schematic of an example embodiment of a processor system. 
       
    
    
     DETAILED DESCRIPTION 
       [0006]    The following equations are Taylor series approximations of cos(x) and sin(x), where x is in radians. 
         [0000]    
       
         
           
             
               
                 cos 
                  
                 
                   ( 
                   x 
                   ) 
                 
               
               ≈ 
               
                 
                   ∑ 
                   
                     n 
                     = 
                     0 
                   
                   ∞ 
                 
                  
                 
                     
                 
                  
                 
                   
                     
                       - 
                       
                         1 
                         n 
                       
                     
                     
                       
                         ( 
                         
                           2 
                            
                           n 
                         
                         ) 
                       
                       ! 
                     
                   
                    
                   
                     x 
                     
                       2 
                        
                       n 
                     
                   
                 
               
             
             = 
             
               1 
               - 
               
                 
                   x 
                   2 
                 
                 
                   2 
                   ! 
                 
               
               + 
               
                 
                   x 
                   4 
                 
                 
                   4 
                   ! 
                 
               
               - 
               … 
             
           
         
       
       
         
           
             
               
                 sin 
                  
                 
                   ( 
                   x 
                   ) 
                 
               
               ≈ 
               
                 
                   ∑ 
                   
                     n 
                     = 
                     0 
                   
                   ∞ 
                 
                  
                 
                     
                 
                  
                 
                   
                     
                       - 
                       
                         1 
                         n 
                       
                     
                     
                       
                         ( 
                         
                           
                             2 
                              
                             n 
                           
                           + 
                           1 
                         
                         ) 
                       
                       ! 
                     
                   
                    
                   
                     x 
                     
                       
                         2 
                          
                         n 
                       
                       + 
                       1 
                     
                   
                 
               
             
             = 
             
               x 
               - 
               
                 
                   x 
                   3 
                 
                 
                   3 
                   ! 
                 
               
               + 
               
                 
                   x 
                   5 
                 
                 
                   5 
                   ! 
                 
               
               - 
               … 
             
           
         
       
     
         [0007]    In  FIG. 1A , the solid line illustrates an exact curve for cos(x) (where x is in radians) and the dashed line illustrates a Taylor series approximation of cos(x) (with the error exaggerated to facilitate illustration).  FIG. 1B  illustrates an exact curve for sin(x) (solid line) and a Taylor series approximation of sin(x) (dashed line). In each of  FIGS. 1A and 1B , the Taylor series is accurate at x=0 and some inherent error occurs as x increases. In particular, the error for cos(x) peaks around x=π/2 radians where cos(x) approaches zero. 
         [0008]    Since the accuracy of a Taylor series is best when x=0, the accuracy of a Taylor series for cos(x) can be improved by shifting the origin of the series, depending on the value of x, and using the appropriate trigonometric function for the shifted origin. For example, cos(x)=sin (π/2−x), and for some ranges of x, the accuracy of a Taylor series for sin (π/2−x) may be better than the accuracy for a Taylor series for cos(x). In particular, the accuracy of a Taylor series for cos(x) can be improved by using the Taylor series for sin(π/2−x) when x is between π/4 and π/2. Likewise, the Taylor series accuracy for cos(x) can be improved by using the Taylor series for −sin(x−π/2) when x is between π/2 and π3π/4, and so forth. The table below details which trigonometric function (as implemented by a Taylor series approximation) has the least error for a range of x from x=0 to x=2π. 
         [0000]    
       
         
               
               
               
             
           
               
                   
                   
               
               
                   
                   
                 Taylor 
               
               
                   
                 x 
                 series 
               
               
                   
                   
               
             
             
               
                   
                 0 − π/4 
                 cos(x) 
               
               
                   
                 π/4 − π/2 
                 sin(π/2 − x) 
               
               
                   
                 π/2 − 3π/4 
                 −sin(x − π/2) 
               
               
                   
                 3π/4 − π 
                 −cos(π − x) 
               
               
                   
                 π − 5π/4 
                 −cos(x − π) 
               
               
                   
                 5π/4 − 3π/2 
                 −sin(3π/2 − x) 
               
               
                   
                 3π/2 − 7π/4 
                 sin(x − 3π/2) 
               
               
                   
                 7π/4 − 2π 
                 cos(x − 7π/4) 
               
               
                   
                   
               
             
          
         
       
     
         [0009]    Note that it is not necessary to switch algorithms at exact multiples of π/4. The Taylor series may be sufficiently accurate to use sin(x) beyond x=pi/4 and the boundaries and ranges in the table are just examples of convenient boundaries. 
         [0010]    In general, the value of x may be greater than 2 π. If x is greater than 2 π, then the operand may be brought within a range of zero to 2 π by using (x modulo 2 π) as the operand instead of x. Given an operand within the range of zero to 2 π, the proper choice of which trigonometric function to use may be determined by using the integer value of operand/(π/4) as an index for the above table. 
         [0011]    In conventional algorithms for the modulo function, (a mod n) is computed as, for example, (a−n*int(a/n). Conventional algorithms for a modulo function may be inaccurate when the dividend is large because low-order bits are lost due to shifting and rounding. A more accurate modulo algorithm is provided below. 
         [0012]    Assume that a processor needs to calculate (x mod 2 π): 
         [0013]    1. Let quotient=int(x/(2 π)) 
         [0014]    2. Divide the digits of π into multiple parts as follows: 
         [0000]    
       
         
               
               
               
               
               
             
           
               
                   
                   
               
             
             
               
                   
                 π = 
                 3.141592 
                 6535897  
                 9323846 
               
               
                   
                   
               
             
          
         
       
     
         [0015]    3. Let op1=3.141592 
         [0016]    4. Let op2=6.53589710 −7    
         [0017]    5. Let op3=9.323846*10 −14    
         [0018]    6. Compute (x mod 2 π)=((x/2−quotient*op1)−quotient*op2))−quotient*op3 
         [0019]    That is, the improved modulo function subtracts in multiple steps that retain the lower order bits of the remainder. In particular, each of op1, op2, op3 may be single-precision numbers and the improved modulo function enables a calculation with single-precision that is more accurate than a conventional algorithm using double-precision. 
         [0020]    The above discussion uses computation of cos(x) as an example, but the principle of using an alternate trigonometric function that is more accurate for a range of x is equally applicable to sin(x). For example, when x is between π/4 and π/2, a processor may compute cos(π/2−x) instead of sin(x). 
         [0021]      FIG. 2  is a flow chart illustrating an example embodiment of a method  200  of computing a trigonometric function for an operand x. At step  202 , a processor uses a first algorithm when x is within a first range. At step  204 , the processor uses a second algorithm when x is within a second range, where the first and second algorithms are different algorithms. 
         [0022]      FIG. 3  illustrates an example embodiment of a processor  300 . The processor  300  includes memory  302 , a control unit  304 , and arithmetic logic  306 . The processor  300  may be programmed to use the arithmetic logic  306  to compute a trigonometric function in accordance with the method of  FIG. 2 . 
         [0023]    In summary, by using alternative trigonometric functions depending on the value of the operand, and by using the improved modulo function, the methods described above can achieve better accuracy using single-precision computation than conventional methods using double-precision computation. As a result, a processor can have lower complexity and lower energy and computation of trigonometric functions may be faster.