Commit 09dbb059e1d5397ec468b1539d0fabaa6ecd066d

Alexei Podtelezhnikov 2013-01-08T23:29:44

[base] Clean up trigonometric core. * src/base/fttrigon.c: Document the algorithm in a large comment. (FT_TRIG_COSCALE): Remove macro. (FT_Tan: Use `FT_TRIG_SCALE' instead. (FT_Cos, FT_Vector_Unit): Ditto and round the return values.

diff --git a/ChangeLog b/ChangeLog
index e4a5daf..baab84f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2013-01-08  Alexei Podtelezhnikov  <apodtele@gmail.com>
+
+	[base] Clean up trigonometric core.
+
+	* src/base/fttrigon.c: Document the algorithm in a large comment.
+	(FT_TRIG_COSCALE): Remove macro.
+	(FT_Tan: Use `FT_TRIG_SCALE' instead.
+	(FT_Cos, FT_Vector_Unit): Ditto and round the return values.
+
 2013-01-02  Alexei Podtelezhnikov  <apodtele@gmail.com>
 
 	[base] Use rounding in CORDIC iterations.
diff --git a/src/base/fttrigon.c b/src/base/fttrigon.c
index 1ffbb48..48a0155 100644
--- a/src/base/fttrigon.c
+++ b/src/base/fttrigon.c
@@ -15,6 +15,19 @@
 /*                                                                         */
 /***************************************************************************/
 
+  /*************************************************************************/
+  /*                                                                       */
+  /* This is a fixed-point CORDIC implementation of trigonometric          */
+  /* functions as well as transformations between Cartesian and polar      */
+  /* coordinates. The angles are represented as a 16.16 fixed-point value  */
+  /* in degrees, i.e., the angular resolution is 2^-16 degrees. Note that   */
+  /* only vectors longer than 2^16*180/pi (or at least 22 bits) on a       */
+  /* discrete Cartesian greed can have the same or better angular          */
+  /* resolution. Therefore, to maintain this precision, some functions     */
+  /* require the interim upscaling of the vectors, whereas others operate  */
+  /* with 24-bit long vectors from the start.                              */
+  /*                                                                       */
+  /*************************************************************************/
 
 #include <ft2build.h>
 #include FT_INTERNAL_OBJECTS_H
@@ -29,9 +42,6 @@
   /* the Cordic shrink factor 0.858785336480436 * 2^32 */
 #define FT_TRIG_SCALE    0xDBD95B16UL
 
-  /* the following is 0.858785336480436 * 2^30 */
-#define FT_TRIG_COSCALE  0x36F656C6UL
-
   /* this table was generated for FT_PI = 180L << 16, i.e. degrees */
 #define FT_TRIG_MAX_ITERS  23
 
@@ -326,11 +336,11 @@
     FT_Vector  v;
 
 
-    v.x = FT_TRIG_COSCALE >> 2;
+    v.x = FT_TRIG_SCALE >> 8;
     v.y = 0;
     ft_trig_pseudo_rotate( &v, angle );
 
-    return v.x / ( 1 << 12 );
+    return ( v.x + 0x80L ) >> 8;
   }
 
 
@@ -351,7 +361,7 @@
     FT_Vector  v;
 
 
-    v.x = FT_TRIG_COSCALE >> 2;
+    v.x = FT_TRIG_SCALE >> 8;
     v.y = 0;
     ft_trig_pseudo_rotate( &v, angle );
 
@@ -386,11 +396,11 @@
   FT_Vector_Unit( FT_Vector*  vec,
                   FT_Angle    angle )
   {
-    vec->x = FT_TRIG_COSCALE >> 2;
+    vec->x = FT_TRIG_SCALE >> 8;
     vec->y = 0;
     ft_trig_pseudo_rotate( vec, angle );
-    vec->x >>= 12;
-    vec->y >>= 12;
+    vec->x = ( vec->x + 0x80L ) >> 8;
+    vec->y = ( vec->y + 0x80L ) >> 8;
   }