| 1 |
/* |
| 2 |
* memmove test. |
| 3 |
* |
| 4 |
* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 |
* See https://llvm.org/LICENSE.txt for license information. |
| 6 |
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <stdint.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
#include "stringlib.h" |
| 14 |
|
| 15 |
static const struct fun |
| 16 |
{ |
| 17 |
const char *name; |
| 18 |
void *(*fun)(void *, const void *, size_t); |
| 19 |
} funtab[] = { |
| 20 |
#define F(x) {#x, x}, |
| 21 |
F(memmove) |
| 22 |
#if __aarch64__ |
| 23 |
F(__memmove_aarch64) |
| 24 |
# if __ARM_NEON |
| 25 |
F(__memmove_aarch64_simd) |
| 26 |
# endif |
| 27 |
#endif |
| 28 |
#undef F |
| 29 |
{0, 0} |
| 30 |
}; |
| 31 |
|
| 32 |
static int test_status; |
| 33 |
#define ERR(...) (test_status=1, printf(__VA_ARGS__)) |
| 34 |
|
| 35 |
#define A 32 |
| 36 |
#define LEN 250000 |
| 37 |
static unsigned char dbuf[LEN+2*A]; |
| 38 |
static unsigned char sbuf[LEN+2*A]; |
| 39 |
static unsigned char wbuf[LEN+2*A]; |
| 40 |
|
| 41 |
static void *alignup(void *p) |
| 42 |
{ |
| 43 |
return (void*)(((uintptr_t)p + A-1) & -A); |
| 44 |
} |
| 45 |
|
| 46 |
static void test(const struct fun *fun, int dalign, int salign, int len) |
| 47 |
{ |
| 48 |
unsigned char *src = alignup(sbuf); |
| 49 |
unsigned char *dst = alignup(dbuf); |
| 50 |
unsigned char *want = wbuf; |
| 51 |
unsigned char *s = src + salign; |
| 52 |
unsigned char *d = dst + dalign; |
| 53 |
unsigned char *w = want + dalign; |
| 54 |
void *p; |
| 55 |
int i; |
| 56 |
|
| 57 |
if (len > LEN || dalign >= A || salign >= A) |
| 58 |
abort(); |
| 59 |
for (i = 0; i < len+A; i++) { |
| 60 |
src[i] = '?'; |
| 61 |
want[i] = dst[i] = '*'; |
| 62 |
} |
| 63 |
for (i = 0; i < len; i++) |
| 64 |
s[i] = w[i] = 'a' + i%23; |
| 65 |
|
| 66 |
p = fun->fun(d, s, len); |
| 67 |
if (p != d) |
| 68 |
ERR("%s(%p,..) returned %p\n", fun->name, d, p); |
| 69 |
for (i = 0; i < len+A; i++) { |
| 70 |
if (dst[i] != want[i]) { |
| 71 |
ERR("%s(align %d, align %d, %d) failed\n", fun->name, dalign, salign, len); |
| 72 |
ERR("got : %.*s\n", dalign+len+1, dst); |
| 73 |
ERR("want: %.*s\n", dalign+len+1, want); |
| 74 |
break; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
static void test_overlap(const struct fun *fun, int dalign, int salign, int len) |
| 80 |
{ |
| 81 |
unsigned char *src = alignup(sbuf); |
| 82 |
unsigned char *dst = alignup(sbuf); |
| 83 |
unsigned char *want = wbuf; |
| 84 |
unsigned char *s = src + salign; |
| 85 |
unsigned char *d = dst + dalign; |
| 86 |
unsigned char *w = wbuf + dalign; |
| 87 |
void *p; |
| 88 |
|
| 89 |
if (len > LEN || dalign >= A || salign >= A) |
| 90 |
abort(); |
| 91 |
|
| 92 |
for (int i = 0; i < len+A; i++) |
| 93 |
src[i] = want[i] = '?'; |
| 94 |
|
| 95 |
for (int i = 0; i < len; i++) |
| 96 |
s[i] = w[i] = 'a' + i%23; |
| 97 |
|
| 98 |
/* Copy the potential overlap range. */ |
| 99 |
if (s < d) { |
| 100 |
for (int i = 0; i < (uintptr_t)d-(uintptr_t)s; i++) |
| 101 |
want[salign+i] = src[salign+i]; |
| 102 |
} else { |
| 103 |
for (int i = 0; i < (uintptr_t)s-(uintptr_t)d; i++) |
| 104 |
want[len + dalign + i] = src[len + dalign + i]; |
| 105 |
} |
| 106 |
|
| 107 |
p = fun->fun(d, s, len); |
| 108 |
if (p != d) |
| 109 |
ERR("%s(%p,..) returned %p\n", fun->name, d, p); |
| 110 |
for (int i = 0; i < len+A; i++) { |
| 111 |
if (dst[i] != want[i]) { |
| 112 |
ERR("%s(align %d, align %d, %d) failed\n", fun->name, dalign, salign, len); |
| 113 |
ERR("got : %.*s\n", dalign+len+1, dst); |
| 114 |
ERR("want: %.*s\n", dalign+len+1, want); |
| 115 |
abort(); |
| 116 |
break; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
int main() |
| 122 |
{ |
| 123 |
test_overlap(funtab+0, 2, 1, 1); |
| 124 |
|
| 125 |
int r = 0; |
| 126 |
for (int i=0; funtab[i].name; i++) { |
| 127 |
test_status = 0; |
| 128 |
for (int d = 0; d < A; d++) |
| 129 |
for (int s = 0; s < A; s++) { |
| 130 |
int n; |
| 131 |
for (n = 0; n < 100; n++) { |
| 132 |
test(funtab+i, d, s, n); |
| 133 |
test_overlap(funtab+i, d, s, n); |
| 134 |
} |
| 135 |
for (; n < LEN; n *= 2) { |
| 136 |
test(funtab+i, d, s, n); |
| 137 |
test_overlap(funtab+i, d, s, n); |
| 138 |
} |
| 139 |
} |
| 140 |
printf("%s %s\n", test_status ? "FAIL" : "PASS", funtab[i].name); |
| 141 |
if (test_status) |
| 142 |
r = -1; |
| 143 |
} |
| 144 |
return r; |
| 145 |
} |