[MLton-commit] r6122

Matthew Fluet fluet at mlton.org
Sat Nov 3 08:25:35 PST 2007


Optimize util/align.h replacing integer division/modulus with bit
mask, assuming the desired alignment is a power of two.

Original suggestion to optimize util/align.h from Florian Weimer.
Optimization patch from Vesa Karvonen.

Benchmark results:
http://mlton.org/pipermail/mlton/2007-November/030042.html


----------------------------------------------------------------------

U   mlton/trunk/runtime/util/align.h

----------------------------------------------------------------------

Modified: mlton/trunk/runtime/util/align.h
===================================================================
--- mlton/trunk/runtime/util/align.h	2007-11-03 11:39:42 UTC (rev 6121)
+++ mlton/trunk/runtime/util/align.h	2007-11-03 16:25:34 UTC (rev 6122)
@@ -15,31 +15,31 @@
 }
 
 static inline size_t alignDown (size_t a, size_t b) {
-  assert (b >= 1);
-  a -= a % b;
+  assert (b >= 1 && b == (b & -b));
+  a &= -b;
   assert (isAligned (a, b));
   return a;
 }
 
 static inline uintmax_t alignMaxDown (uintmax_t a, uintmax_t b) {
-  assert (b >= 1);
-  a -= a % b;
+  assert (b >= 1 && b == (b & -b));
+  a &= -b;
   assert (isAlignedMax (a, b));
   return a;
 }
 
 static inline size_t align (size_t a, size_t b) {
-  assert (b >= 1);
+  assert (b >= 1 && b == (b & -b));
   a += b - 1;
-  a -= a % b;
+  a &= -b;
   assert (isAligned (a, b));
   return a;       
 }
 
 static inline uintmax_t alignMax (uintmax_t a, uintmax_t b) {
-  assert (b >= 1);
+  assert (b >= 1 && b == (b & -b));
   a += b - 1;
-  a -= a % b;
+  a &= -b;
   assert (isAligned (a, b));
   return a;       
 }




More information about the MLton-commit mailing list