[base] Use rounding in CORDIC iterations. * src/base/fttrigon.c (ft_trig_pseudo_rotate, ft_trig_pseudo_polarize): Improve accuracy by rounding.
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 90 91 92 93 94 95 96 97 98 99 100 101
diff --git a/ChangeLog b/ChangeLog
index cd0cca4..e4a5daf 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
2013-01-02 Alexei Podtelezhnikov <apodtele@gmail.com>
+ [base] Use rounding in CORDIC iterations.
+
+ * src/base/fttrigon.c (ft_trig_pseudo_rotate,
+ ft_trig_pseudo_polarize): Improve accuracy by rounding.
+
+2013-01-02 Alexei Podtelezhnikov <apodtele@gmail.com>
+
[base] Reduce trigonometric algorithms.
After we get within 45 degrees by means of true 90-degree rotations,
diff --git a/src/base/fttrigon.c b/src/base/fttrigon.c
index 4bcc7ab..1ffbb48 100644
--- a/src/base/fttrigon.c
+++ b/src/base/fttrigon.c
@@ -192,7 +192,7 @@
FT_Angle theta )
{
FT_Int i;
- FT_Fixed x, y, xtemp;
+ FT_Fixed x, y, xtemp, b;
const FT_Fixed *arctanptr;
@@ -219,24 +219,23 @@
arctanptr = ft_trig_arctan_table;
/* Pseudorotations, with right shifts */
- i = 1;
- do
+ for ( i = 1, b = 1; i < FT_TRIG_MAX_ITERS; b <<= 1, i++ )
{
if ( theta < 0 )
{
- xtemp = x + ( y >> i );
- y = y - ( x >> i );
+ xtemp = x + ( ( y + b ) >> i );
+ y = y - ( ( x + b ) >> i );
x = xtemp;
theta += *arctanptr++;
}
else
{
- xtemp = x - ( y >> i );
- y = y + ( x >> i );
+ xtemp = x - ( ( y + b ) >> i );
+ y = y + ( ( x + b ) >> i );
x = xtemp;
theta -= *arctanptr++;
}
- } while ( ++i < FT_TRIG_MAX_ITERS );
+ }
vec->x = x;
vec->y = y;
@@ -248,7 +247,7 @@
{
FT_Angle theta;
FT_Int i;
- FT_Fixed x, y, xtemp;
+ FT_Fixed x, y, xtemp, b;
const FT_Fixed *arctanptr;
@@ -290,24 +289,23 @@
arctanptr = ft_trig_arctan_table;
/* Pseudorotations, with right shifts */
- i = 1;
- do
+ for ( i = 1, b = 1; i < FT_TRIG_MAX_ITERS; b <<= 1, i++ )
{
if ( y > 0 )
{
- xtemp = x + ( y >> i );
- y = y - ( x >> i );
+ xtemp = x + ( ( y + b ) >> i );
+ y = y - ( ( x + b ) >> i );
x = xtemp;
theta += *arctanptr++;
}
else
{
- xtemp = x - ( y >> i );
- y = y + ( x >> i );
+ xtemp = x - ( ( y + b ) >> i );
+ y = y + ( ( x + b ) >> i );
x = xtemp;
theta -= *arctanptr++;
}
- } while ( ++i < FT_TRIG_MAX_ITERS );
+ }
/* round theta */
if ( theta >= 0 )