-/* $OpenBSD: unalign.c,v 1.1 2005/04/26 21:37:05 miod Exp $ */
+/* $OpenBSD: unalign.c,v 1.2 2008/07/26 10:25:04 miod Exp $ */
/* Written by Miod Vallat, 2004 AD -- this file is in the public domain */
/*
#include <stdio.h>
#include <sys/types.h>
+uint32_t array[5];
+
int
-main(int argc, char *argv[])
+unalign_read(uint64_t *addr)
{
-#if !defined(__LP64__)
- long array[4] = { 0x12345678, 0x13579ace, 0xffffabcd, 0x2468fedc };
- long long t;
- unsigned int i;
+ uint64_t t;
- t = *(long long *)(array + 1);
+ t = *addr;
#if BYTE_ORDER == BIG_ENDIAN
if (t != 0x13579aceffffabcdULL)
#else
#endif
return (1);
+ return (0);
+}
+
+void
+unalign_write(uint64_t *addr)
+{
+ uint64_t t;
+
t = 0xdeadbeaffadebabeULL;
- *(long long *)(array + 1) = t;
+ *addr = t;
+}
+
+int
+main(int argc, char *argv[])
+{
+#if !defined(__LP64__)
+ uint32_t *addr = array;
+
+ /* align on a 64 bit boundary */
+ if (((uint32_t)addr & 7) != 0)
+ addr++;
+
+ addr[0] = 0x12345678;
+ addr[1] = 0x13579ace;
+ addr[2] = 0xffffabcd;
+ addr[3] = 0x2468fedc;
+
+ if (unalign_read((uint64_t *)(addr + 1)))
+ return (1);
+
+ unalign_write((uint64_t *)(addr + 1));
#if BYTE_ORDER == BIG_ENDIAN
- if (array[1] != 0xdeadbeaf || array[2] != 0xfadebabe)
+ if (addr[1] != 0xdeadbeaf || addr[2] != 0xfadebabe)
#else
- if (array[1] != 0xfadebabe || array[2] != 0xdeadbeaf)
+ if (addr[1] != 0xfadebabe || addr[2] != 0xdeadbeaf)
#endif
return (1);
-
#endif /* __LP64__ */
+
return (0);
}