[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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
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;
}