[base] Update the overflow protection bit. The recent optimizations of CORDIC iterations drastically reduce the expansion factor. The vector components with MSB of 29 are now safe from overflow. * src/base/fttrigon.c (FT_TRIG_SAFE_MSB): New macro. (ft_trig_prenorm): Use it and remove dead code.
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
diff --git a/ChangeLog b/ChangeLog
index 6d2a672..0bc00b9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2013-01-10 Alexei Podtelezhnikov <apodtele@gmail.com>
+
+ [base] Update the overflow protection bit.
+
+ The recent optimizations of CORDIC iterations drastically reduce
+ the expansion factor. The vector components with MSB of 29 are now
+ safe from overflow.
+
+ * src/base/fttrigon.c (FT_TRIG_SAFE_MSB): New macro.
+ (ft_trig_prenorm): Use it and remove dead code.
+
2013-01-09 Alexei Podtelezhnikov <apodtele@gmail.com>
[base, pshinter] Use FT_ABS, FT_MIN, and FT_MAX for readability.
diff --git a/src/base/fttrigon.c b/src/base/fttrigon.c
index a7ad02d..ad6bc72 100644
--- a/src/base/fttrigon.c
+++ b/src/base/fttrigon.c
@@ -40,7 +40,11 @@
/* the Cordic shrink factor 0.858785336480436 * 2^32 */
-#define FT_TRIG_SCALE 0xDBD95B16UL
+#define FT_TRIG_SCALE 0xDBD95B16UL
+
+ /* the highest bit in overflow-safe vectror components,
+ MSB of 0.858785336480436 * sqrt(0.5) * 2^30 */
+#define FT_TRIG_SAFE_MSB 29
/* this table was generated for FT_PI = 180L << 16, i.e. degrees */
#define FT_TRIG_MAX_ITERS 23
@@ -124,7 +128,6 @@
z = FT_ABS( x ) | FT_ABS( y );
shift = 0;
-#if 1
/* determine msb bit index in `shift' */
if ( z >= ( 1L << 16 ) )
{
@@ -152,47 +155,20 @@
shift += 1;
}
- if ( shift <= 27 )
+ if ( shift <= FT_TRIG_SAFE_MSB )
{
- shift = 27 - shift;
+ shift = FT_TRIG_SAFE_MSB - shift;
vec->x = x << shift;
vec->y = y << shift;
}
else
{
- shift -= 27;
- vec->x = x >> shift;
- vec->y = y >> shift;
- shift = -shift;
- }
-
-#else /* 0 */
-
- if ( z < ( 1L << 27 ) )
- {
- do
- {
- shift++;
- z <<= 1;
- } while ( z < ( 1L << 27 ) );
- vec->x = x << shift;
- vec->y = y << shift;
- }
- else if ( z > ( 1L << 28 ) )
- {
- do
- {
- shift++;
- z >>= 1;
- } while ( z > ( 1L << 28 ) );
-
+ shift -= FT_TRIG_SAFE_MSB;
vec->x = x >> shift;
vec->y = y >> shift;
shift = -shift;
}
-#endif /* 0 */
-
return shift;
}