Commit 7f49111f81ac6082dbe5cee9e1147306bba4f27e

Alexei Podtelezhnikov 2014-09-25T22:54:38

[base] Avoid unnecessary long division. This applies to `FT_MulDiv' but not to `FT_DivFix', where overflows or lack thereof are predicted accurately. * src/base/ftcalc.c (ft_div64by32): Improve readability. (FT_MulDiv, FT_MulDiv_No_Round) [!FT_LONG64]: Use straight division when multiplication stayed within 32 bits.

diff --git a/ChangeLog b/ChangeLog
index 808ca3a..f5666bc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2014-09-25  Alexei Podtelezhnikov  <apodtele@gmail.com>
+
+	[base] Avoid unnecessary long division.
+
+	This applies to `FT_MulDiv' but not to `FT_DivFix', where overflows or
+	lack thereof are predicted accurately.
+
+	* src/base/ftcalc.c (ft_div64by32): Improve readability.
+	(FT_MulDiv, FT_MulDiv_No_Round) [!FT_LONG64]: Use straight division
+	when multiplication stayed within 32 bits.
+
 2014-09-24  Werner Lemberg  <wl@gnu.org>
 
 	[autofit] Minor clean-ups.
diff --git a/src/base/ftcalc.c b/src/base/ftcalc.c
index be65a7a..6a39477 100644
--- a/src/base/ftcalc.c
+++ b/src/base/ftcalc.c
@@ -303,16 +303,14 @@
     i = 32;
     do
     {
-      r <<= 1;
       q <<= 1;
-      r  |= lo >> 31;
+      r   = ( r << 1 ) | ( lo >> 31 ); lo <<= 1;  /* left 64-bit shift */
 
       if ( r >= y )
       {
         r -= y;
         q |= 1;
       }
-      lo <<= 1;
     } while ( --i );
 
     return q;
@@ -416,7 +414,10 @@
       temp2.hi = 0;
       temp2.lo = (FT_UInt32)(c >> 1);
       FT_Add64( &temp, &temp2, &temp );
-      a = ft_div64by32( temp.hi, temp.lo, (FT_Int32)c );
+
+      /* last attempt to ditch long division */
+      a = temp.hi == 0 ? temp.lo / c
+                       : ft_div64by32( temp.hi, temp.lo, (FT_Int32)c );
     }
 
     return ( s < 0 ? -a : a );
@@ -450,7 +451,10 @@
 
 
       ft_multo64( (FT_Int32)a, (FT_Int32)b, &temp );
-      a = ft_div64by32( temp.hi, temp.lo, (FT_Int32)c );
+
+      /* last attempt to ditch long division */
+      a = temp.hi == 0 ? temp.lo / c
+                       : ft_div64by32( temp.hi, temp.lo, (FT_Int32)c );
     }
 
     return ( s < 0 ? -a : a );