summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby Bingham <koorogi@koorogi.info>2014-07-31 21:08:39 -0500
committerBobby Bingham <koorogi@koorogi.info>2014-07-31 21:08:39 -0500
commita3d41b3ca19c8dfc306ac16f0b659961f1bd6255 (patch)
tree232424ac032133e0a0f8dac68f82b66342532c39
parentab27fe8c3edda02a2bd6088b07aa8c13a561a24d (diff)
Optimize swap for larger swaps
-rw-r--r--common.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/common.h b/common.h
index d7e1e47..c1bfb6c 100644
--- a/common.h
+++ b/common.h
@@ -7,6 +7,20 @@ size_t binary_search(const char *needle, char *haystack, size_t nmel, size_t wid
static void swap(char *a, char *b, size_t width)
{
+#ifdef __GNUC__
+ typedef uint32_t __attribute__((__may_alias__)) u32;
+
+ if ((uintptr_t)a % 4 == 0 && (uintptr_t)b % 4 == 0) {
+ for (; width >= 4; width -= 4) {
+ uint32_t tmp = *((u32*)a);
+ *((u32*)a) = *((u32*)b);
+ *((u32*)b) = tmp;
+ a += 4;
+ b += 4;
+ }
+ }
+#endif
+
while (width--) {
char tmp = *a;
*a++ = *b;