Commit 36e75c3efec08b1e9bdb9c1f69a5b0018abd8ac7

Authored by Janne Grunau
1 parent eb5ce0ca
Exists in master and in 3 other branches v2, v3, wip-18092

use posix_memalign to align memory for SIMD region tests

Properly emulate aligned allocation if posix_memalign is not available.
Showing 3 changed files with 66 additions and 13 deletions   Show diff stats
configure.ac
... ... @@ -22,6 +22,13 @@ AM_MAINTAINER_MODE([disable])
22 22 dnl Compiling with per-target flags requires AM_PROG_CC_C_O.
23 23 AC_PROG_CC
24 24  
  25 +# Check for functions to provide aligned memory
  26 +#
  27 +AC_CHECK_FUNCS([posix_memalign],
  28 + [found_memalign=yes; break])
  29 +
  30 +AS_IF([test "x$found_memalign" != "xyes"], [AC_MSG_WARN([No function for aligned memory allocation found])])
  31 +
25 32 AX_EXT()
26 33  
27 34 AC_ARG_ENABLE([neon],
... ...
test/gf_unit.c
... ... @@ -8,6 +8,14 @@
8 8 * Performs unit testing for gf arithmetic
9 9 */
10 10  
  11 +#include "config.h"
  12 +
  13 +#ifdef HAVE_POSIX_MEMALIGN
  14 +#ifndef _XOPEN_SOURCE
  15 +#define _XOPEN_SOURCE 600
  16 +#endif
  17 +#endif
  18 +
11 19 #include <stdio.h>
12 20 #include <getopt.h>
13 21 #include <stdint.h>
... ... @@ -82,6 +90,9 @@ int main(int argc, char **argv)
82 90 uint32_t mask = 0;
83 91 char *ra, *rb, *rc, *rd, *target;
84 92 int align;
  93 +#ifndef HAVE_POSIX_MEMALIGN
  94 + char *malloc_ra, *malloc_rb, *malloc_rc, *malloc_rd;
  95 +#endif
85 96  
86 97  
87 98 if (argc < 4) usage(NULL);
... ... @@ -116,18 +127,26 @@ int main(int argc, char **argv)
116 127 c = (gf_general_t *) malloc(sizeof(gf_general_t));
117 128 d = (gf_general_t *) malloc(sizeof(gf_general_t));
118 129  
  130 +#if HAVE_POSIX_MEMALIGN
  131 + if (posix_memalign((void **) &ra, 16, sizeof(char)*REGION_SIZE))
  132 + ra = NULL;
  133 + if (posix_memalign((void **) &rb, 16, sizeof(char)*REGION_SIZE))
  134 + rb = NULL;
  135 + if (posix_memalign((void **) &rc, 16, sizeof(char)*REGION_SIZE))
  136 + rc = NULL;
  137 + if (posix_memalign((void **) &rd, 16, sizeof(char)*REGION_SIZE))
  138 + rd = NULL;
  139 +#else
119 140 //15 bytes extra to make sure it's 16byte aligned
120   - ra = (char *) malloc(sizeof(char)*REGION_SIZE+15);
121   - rb = (char *) malloc(sizeof(char)*REGION_SIZE+15);
122   - rc = (char *) malloc(sizeof(char)*REGION_SIZE+15);
123   - rd = (char *) malloc(sizeof(char)*REGION_SIZE+15);
124   -
125   - //this still assumes 8 byte aligned pointer from malloc
126   - //(which is usual on 32-bit machines)
127   - ra += (uint64_t)ra & 0xf;
128   - rb += (uint64_t)rb & 0xf;
129   - rc += (uint64_t)rc & 0xf;
130   - rd += (uint64_t)rd & 0xf;
  141 + malloc_ra = (char *) malloc(sizeof(char)*REGION_SIZE+15);
  142 + malloc_rb = (char *) malloc(sizeof(char)*REGION_SIZE+15);
  143 + malloc_rc = (char *) malloc(sizeof(char)*REGION_SIZE+15);
  144 + malloc_rd = (char *) malloc(sizeof(char)*REGION_SIZE+15);
  145 + ra = (uint8_t *) (((uintptr_t) malloc_ra + 15) & ~((uintptr_t) 0xf));
  146 + rb = (uint8_t *) (((uintptr_t) malloc_rb + 15) & ~((uintptr_t) 0xf));
  147 + rc = (uint8_t *) (((uintptr_t) malloc_rc + 15) & ~((uintptr_t) 0xf));
  148 + rd = (uint8_t *) (((uintptr_t) malloc_rd + 15) & ~((uintptr_t) 0xf));
  149 +#endif
131 150  
132 151 if (w <= 32) {
133 152 mask = 0;
... ... @@ -423,10 +442,17 @@ int main(int argc, char **argv)
423 442 free(b);
424 443 free(c);
425 444 free(d);
  445 +#ifdef HAVE_POSIX_MEMALIGN
426 446 free(ra);
427 447 free(rb);
428 448 free(rc);
429 449 free(rd);
  450 +#else
  451 + free(malloc_ra);
  452 + free(malloc_rb);
  453 + free(malloc_rc);
  454 + free(malloc_rd);
  455 +#endif
430 456  
431 457 return 0;
432 458 }
... ...
tools/gf_time.c
... ... @@ -8,6 +8,14 @@
8 8 * Performs timing for gf arithmetic
9 9 */
10 10  
  11 +#include "config.h"
  12 +
  13 +#ifdef HAVE_POSIX_MEMALIGN
  14 +#ifndef _XOPEN_SOURCE
  15 +#define _XOPEN_SOURCE 600
  16 +#endif
  17 +#endif
  18 +
11 19 #include <stdio.h>
12 20 #include <getopt.h>
13 21 #include <stdint.h>
... ... @@ -95,6 +103,9 @@ int main(int argc, char **argv)
95 103 time_t t0;
96 104 uint8_t *ra, *rb;
97 105 gf_general_t a;
  106 +#ifndef HAVE_POSIX_MEMALIGN
  107 + uint8_t *malloc_ra, *malloc_rb;
  108 +#endif
98 109  
99 110  
100 111 if (argc < 6) usage(NULL);
... ... @@ -155,8 +166,17 @@ int main(int argc, char **argv)
155 166  
156 167 printf("Seed: %ld\n", t0);
157 168  
158   - ra = (uint8_t *) malloc(size);
159   - rb = (uint8_t *) malloc(size);
  169 +#ifdef HAVE_POSIX_MEMALIGN
  170 + if (posix_memalign((void **) &ra, 16, size))
  171 + ra = NULL;
  172 + if (posix_memalign((void **) &rb, 16, size))
  173 + rb = NULL;
  174 +#else
  175 + malloc_ra = (uint8_t *) malloc(size + 15);
  176 + malloc_rb = (uint8_t *) malloc(size + 15);
  177 + ra = (uint8_t *) (((uintptr_t) malloc_ra + 15) & ~((uintptr_t) 0xf));
  178 + rb = (uint8_t *) (((uintptr_t) malloc_rb + 15) & ~((uintptr_t) 0xf));
  179 +#endif
160 180  
161 181 if (ra == NULL || rb == NULL) { perror("malloc"); exit(1); }
162 182  
... ...