summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJanne Grunau <j@jannau.net>2014-09-05 13:33:04 +0200
committerJanne Grunau <j@jannau.net>2014-10-09 23:22:34 +0200
commit36e75c3efec08b1e9bdb9c1f69a5b0018abd8ac7 (patch)
tree7deb8e00964e43ed5d5ac71a78540b20cafe25b7
parenteb5ce0ca4206ed4f74009c1b9a3a72407693448b (diff)
downloadgf-complete-36e75c3efec08b1e9bdb9c1f69a5b0018abd8ac7.tar.gz
use posix_memalign to align memory for SIMD region tests
Properly emulate aligned allocation if posix_memalign is not available.
-rw-r--r--configure.ac7
-rw-r--r--test/gf_unit.c48
-rw-r--r--tools/gf_time.c24
3 files changed, 66 insertions, 13 deletions
diff --git a/configure.ac b/configure.ac
index 31ab1fa..ad7bb83 100644
--- a/configure.ac
+++ b/configure.ac
@@ -22,6 +22,13 @@ AM_MAINTAINER_MODE([disable])
dnl Compiling with per-target flags requires AM_PROG_CC_C_O.
AC_PROG_CC
+# Check for functions to provide aligned memory
+#
+AC_CHECK_FUNCS([posix_memalign],
+ [found_memalign=yes; break])
+
+AS_IF([test "x$found_memalign" != "xyes"], [AC_MSG_WARN([No function for aligned memory allocation found])])
+
AX_EXT()
AC_ARG_ENABLE([neon],
diff --git a/test/gf_unit.c b/test/gf_unit.c
index 98ff98c..db26849 100644
--- a/test/gf_unit.c
+++ b/test/gf_unit.c
@@ -8,6 +8,14 @@
* Performs unit testing for gf arithmetic
*/
+#include "config.h"
+
+#ifdef HAVE_POSIX_MEMALIGN
+#ifndef _XOPEN_SOURCE
+#define _XOPEN_SOURCE 600
+#endif
+#endif
+
#include <stdio.h>
#include <getopt.h>
#include <stdint.h>
@@ -82,6 +90,9 @@ int main(int argc, char **argv)
uint32_t mask = 0;
char *ra, *rb, *rc, *rd, *target;
int align;
+#ifndef HAVE_POSIX_MEMALIGN
+ char *malloc_ra, *malloc_rb, *malloc_rc, *malloc_rd;
+#endif
if (argc < 4) usage(NULL);
@@ -116,18 +127,26 @@ int main(int argc, char **argv)
c = (gf_general_t *) malloc(sizeof(gf_general_t));
d = (gf_general_t *) malloc(sizeof(gf_general_t));
+#if HAVE_POSIX_MEMALIGN
+ if (posix_memalign((void **) &ra, 16, sizeof(char)*REGION_SIZE))
+ ra = NULL;
+ if (posix_memalign((void **) &rb, 16, sizeof(char)*REGION_SIZE))
+ rb = NULL;
+ if (posix_memalign((void **) &rc, 16, sizeof(char)*REGION_SIZE))
+ rc = NULL;
+ if (posix_memalign((void **) &rd, 16, sizeof(char)*REGION_SIZE))
+ rd = NULL;
+#else
//15 bytes extra to make sure it's 16byte aligned
- ra = (char *) malloc(sizeof(char)*REGION_SIZE+15);
- rb = (char *) malloc(sizeof(char)*REGION_SIZE+15);
- rc = (char *) malloc(sizeof(char)*REGION_SIZE+15);
- rd = (char *) malloc(sizeof(char)*REGION_SIZE+15);
-
- //this still assumes 8 byte aligned pointer from malloc
- //(which is usual on 32-bit machines)
- ra += (uint64_t)ra & 0xf;
- rb += (uint64_t)rb & 0xf;
- rc += (uint64_t)rc & 0xf;
- rd += (uint64_t)rd & 0xf;
+ malloc_ra = (char *) malloc(sizeof(char)*REGION_SIZE+15);
+ malloc_rb = (char *) malloc(sizeof(char)*REGION_SIZE+15);
+ malloc_rc = (char *) malloc(sizeof(char)*REGION_SIZE+15);
+ malloc_rd = (char *) malloc(sizeof(char)*REGION_SIZE+15);
+ ra = (uint8_t *) (((uintptr_t) malloc_ra + 15) & ~((uintptr_t) 0xf));
+ rb = (uint8_t *) (((uintptr_t) malloc_rb + 15) & ~((uintptr_t) 0xf));
+ rc = (uint8_t *) (((uintptr_t) malloc_rc + 15) & ~((uintptr_t) 0xf));
+ rd = (uint8_t *) (((uintptr_t) malloc_rd + 15) & ~((uintptr_t) 0xf));
+#endif
if (w <= 32) {
mask = 0;
@@ -423,10 +442,17 @@ int main(int argc, char **argv)
free(b);
free(c);
free(d);
+#ifdef HAVE_POSIX_MEMALIGN
free(ra);
free(rb);
free(rc);
free(rd);
+#else
+ free(malloc_ra);
+ free(malloc_rb);
+ free(malloc_rc);
+ free(malloc_rd);
+#endif
return 0;
}
diff --git a/tools/gf_time.c b/tools/gf_time.c
index d17a7c2..7402ab5 100644
--- a/tools/gf_time.c
+++ b/tools/gf_time.c
@@ -8,6 +8,14 @@
* Performs timing for gf arithmetic
*/
+#include "config.h"
+
+#ifdef HAVE_POSIX_MEMALIGN
+#ifndef _XOPEN_SOURCE
+#define _XOPEN_SOURCE 600
+#endif
+#endif
+
#include <stdio.h>
#include <getopt.h>
#include <stdint.h>
@@ -95,6 +103,9 @@ int main(int argc, char **argv)
time_t t0;
uint8_t *ra, *rb;
gf_general_t a;
+#ifndef HAVE_POSIX_MEMALIGN
+ uint8_t *malloc_ra, *malloc_rb;
+#endif
if (argc < 6) usage(NULL);
@@ -155,8 +166,17 @@ int main(int argc, char **argv)
printf("Seed: %ld\n", t0);
- ra = (uint8_t *) malloc(size);
- rb = (uint8_t *) malloc(size);
+#ifdef HAVE_POSIX_MEMALIGN
+ if (posix_memalign((void **) &ra, 16, size))
+ ra = NULL;
+ if (posix_memalign((void **) &rb, 16, size))
+ rb = NULL;
+#else
+ malloc_ra = (uint8_t *) malloc(size + 15);
+ malloc_rb = (uint8_t *) malloc(size + 15);
+ ra = (uint8_t *) (((uintptr_t) malloc_ra + 15) & ~((uintptr_t) 0xf));
+ rb = (uint8_t *) (((uintptr_t) malloc_rb + 15) & ~((uintptr_t) 0xf));
+#endif
if (ra == NULL || rb == NULL) { perror("malloc"); exit(1); }