Commit 8fd87d4e69847e2897dfd5122e8d0a87952e6d45

Alexei Podtelezhnikov 2014-08-20T00:57:22

[base] Small optimization of `FT_MulFix'. * src/base/ftcalc.c (FT_MulFix): Loosen up the condition for direct 32-bit calculations.

diff --git a/ChangeLog b/ChangeLog
index cff13e3..f98c4b2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2014-08-20  Alexei Podtelezhnikov  <apodtele@gmail.com>
+
+	[base] Small optimization of `FT_MulFix'.
+
+	* src/base/ftcalc.c (FT_MulFix): Loosen up the condition for direct
+	32-bit calculations.
+
 2014-08-19  Alexei Podtelezhnikov  <apodtele@gmail.com>
 
 	[base] Use unsigned calculation in `FT_MulDiv'.
diff --git a/src/base/ftcalc.c b/src/base/ftcalc.c
index 9c72a76..5b17232 100644
--- a/src/base/ftcalc.c
+++ b/src/base/ftcalc.c
@@ -385,6 +385,15 @@
   /*                                                                     */
   /*  a + b <= 129895 - (c >> 17)                                        */
   /*                                                                     */
+  /*  FT_MulFix, on the other hand, is optimized for a small value of    */
+  /*  the first argument, when the second argument can be much larger.   */
+  /*  This can be achieved by scaling the second argument and the limit  */
+  /*  in the above inequalities. For example,                            */
+  /*                                                                     */
+  /*  a + (b >> 8) <= (131071 >> 4)                                      */
+  /*                                                                     */
+  /*  should work well to avoid the overflow.                            */
+  /*                                                                     */
 
   /* documentation is in freetype.h */
 
@@ -513,7 +522,7 @@
     ua = (FT_ULong)a;
     ub = (FT_ULong)b;
 
-    if ( ua <= 2048 && ub <= 1048576L )
+    if ( ua + ( ub >> 8 ) <= 8191UL )
       ua = ( ua * ub + 0x8000U ) >> 16;
     else
     {
@@ -544,7 +553,7 @@
     ua = (FT_ULong)a;
     ub = (FT_ULong)b;
 
-    if ( ua <= 2048 && ub <= 1048576L )
+    if ( ua + ( ub >> 8 ) <= 8191UL )
       ua = ( ua * ub + 0x8000UL ) >> 16;
     else
     {