[base] Small optimization of the ancient code. * src/base/ftcalc.c (FT_MulDiv, FT_MulDiv_No_Round): Loosen up the condition for direct 32-bit calculations.
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
diff --git a/ChangeLog b/ChangeLog
index 6bc9b3c..30626ce 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2014-07-04 Alexei Podtelezhnikov <apodtele@gmail.com>
+
+ [base] Small optimization of the ancient code.
+
+ * src/base/ftcalc.c (FT_MulDiv, FT_MulDiv_No_Round): Loosen up the
+ condition for direct 32-bit calculations.
+
2014-06-27 Werner Lemberg <wl@gnu.org>
Fix Apple standard glyph names.
diff --git a/src/base/ftcalc.c b/src/base/ftcalc.c
index 6e65583..b2eba9b 100644
--- a/src/base/ftcalc.c
+++ b/src/base/ftcalc.c
@@ -363,15 +363,17 @@
/* */
/* we compute 'a*b+c/2', then divide it by 'c'. (positive values) */
/* */
- /* 46340 is FLOOR(SQRT(2^31-1)). */
+ /* A sufficient condition to avoid overflow is as follows. */
/* */
- /* if ( a <= 46340 && b <= 46340 ) then ( a*b <= 0x7FFEA810 ) */
+ /* a + b <= 2 * sqrt( X - c/2 ) */
/* */
- /* 0x7FFFFFFF - 0x7FFEA810 = 0x157F0 */
+ /* where X = 2^31 - 1. After Taylor expansion, we make it stronger */
/* */
- /* if ( c < 0x157F0*2 ) then ( a*b+c/2 <= 0x7FFFFFFF ) */
+ /* a + b <= 92681.9 - c / 92681.9 */
/* */
- /* and 2*0x157F0 = 176096 */
+ /* with explicit 2*sqrt(X) = 92681.9. What we actually use is this */
+ /* */
+ /* a + b <= 92681 - (c >> 16) */
/* */
FT_EXPORT_DEF( FT_Long )
@@ -390,7 +392,7 @@
s ^= b; b = FT_ABS( b );
s ^= c; c = FT_ABS( c );
- if ( a <= 46340L && b <= 46340L && c <= 176095L && c > 0 )
+ if ( (FT_ULong)a + (FT_ULong)b <= 92681UL - ( c >> 16 ) && c > 0 )
a = ( a * b + ( c >> 1 ) ) / c;
else if ( (FT_Int32)c > 0 )
@@ -427,7 +429,7 @@
s ^= b; b = FT_ABS( b );
s ^= c; c = FT_ABS( c );
- if ( a <= 46340L && b <= 46340L && c > 0 )
+ if ( (FT_ULong)a + (FT_ULong)b <= 92681UL && c > 0 )
a = a * b / c;
else if ( (FT_Int32)c > 0 )