[MLton-commit] r6435

spoons at mlton.org spoons at mlton.org
Mon Mar 3 06:52:32 PST 2008


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

Original commit 6122 by Matthew Fluet

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/branches/shared-heap-multicore/runtime/util/align.h

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

Modified: mlton/branches/shared-heap-multicore/runtime/util/align.h
===================================================================
--- mlton/branches/shared-heap-multicore/runtime/util/align.h	2008-03-03 14:52:23 UTC (rev 6434)
+++ mlton/branches/shared-heap-multicore/runtime/util/align.h	2008-03-03 14:52:32 UTC (rev 6435)
@@ -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