[AD] fixdiv/fixmul

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Neither of my fixmul patches seems to have made it to the list. No problem, 
this one supercedes them both. It should improve speed of both fixmul and 
fixdiv (courtesy of Thomas Harte) on systems that can do 64 bit 
arithemetic.
That makes credit for three people on this small section of code. Amazing!

Evert
Index: include/allegro/inline/fmaths.inl
===================================================================
RCS file: /cvsroot/alleg/allegro/include/allegro/inline/fmaths.inl,v
retrieving revision 1.7
diff -u -r1.7 fmaths.inl
--- include/allegro/inline/fmaths.inl	1 Feb 2005 13:12:04 -0000	1.7
+++ include/allegro/inline/fmaths.inl	16 May 2005 16:10:51 -0000
@@ -103,7 +103,30 @@
 
 AL_INLINE(fixed, fixmul, (fixed x, fixed y),
 {
+#if 0
    return ftofix(fixtof(x) * fixtof(y));
+#elif defined LONG_LONG
+   LONG_LONG lx = x;
+   LONG_LONG ly = y;
+   LONG_LONG lres = (lx*ly)>>16;
+   int res = lres;
+   return res;
+#else
+   fixed sign = (x^y) & 0x80000000;
+   int mask_x = x >> 31;
+   int mask_y = y >> 31;
+   int mask_result = sign >> 31;
+   fixed result;
+
+   x = (x^mask_x) - mask_x;
+   y = (y^mask_y) - mask_y;
+
+   result = ((y >> 8)*(x >> 8) +
+             (((y >> 8)*(x&0xff)) >> 8) +
+             (((x >> 8)*(y&0xff)) >> 8));
+
+   return (result^mask_result) - mask_result;
+#endif
 })
 
 
@@ -113,8 +136,17 @@
       *allegro_errno = ERANGE;
       return (x < 0) ? -0x7FFFFFFF : 0x7FFFFFFF;
    }
-   else
+   else {
+#if defined LONG_LONG
+      LONG_LONG lx = x;
+      LONG_LONG ly = y;
+      LONG_LONG lres = (lx << 16) / ly;
+      int res = lres;
+      return res;
+#else
       return ftofix(fixtof(x) / fixtof(y));
+#endif
+   }
 })
 
 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/