OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Simple sanity test of memcpy, memmove, and memset intrinsics. |
| 3 * (fixed length buffers, variable length buffers, etc.) |
| 4 */ |
| 5 |
| 6 #include "mem_intrin.h" |
| 7 |
| 8 #include <stdint.h> /* cstdint requires -std=c++0x or higher */ |
| 9 #include <cstdlib> |
| 10 #include <cstring> |
| 11 |
| 12 typedef int elem_t; |
| 13 |
| 14 /* |
| 15 * Reset buf to the sequence of bytes: n, n+1, n+2 ... length - 1 |
| 16 */ |
| 17 static void __attribute__((noinline)) reset_buf(uint8_t *buf, |
| 18 uint8_t init, |
| 19 size_t length) { |
| 20 size_t i; |
| 21 size_t v = init; |
| 22 for (i = 0; i < length; ++i) |
| 23 buf[i] = v++; |
| 24 } |
| 25 |
| 26 static int __attribute__((noinline)) sum_buf(uint8_t *buf, |
| 27 size_t length) { |
| 28 size_t i; |
| 29 int sum = 0; |
| 30 for (i = 0; i < length; ++i) |
| 31 sum += buf[i]; |
| 32 return sum; |
| 33 } |
| 34 |
| 35 #define NWORDS 32 |
| 36 #define BYTE_LENGTH (NWORDS * sizeof(elem_t)) |
| 37 |
| 38 int memcpy_test_fixed_len(uint8_t init) { |
| 39 elem_t buf[NWORDS]; |
| 40 elem_t buf2[NWORDS]; |
| 41 reset_buf((uint8_t *)buf, init, BYTE_LENGTH); |
| 42 memcpy((void *)buf2, (void *)buf, BYTE_LENGTH); |
| 43 return sum_buf((uint8_t*)buf2, BYTE_LENGTH); |
| 44 } |
| 45 |
| 46 int memmove_test_fixed_len(uint8_t init) { |
| 47 elem_t buf[NWORDS]; |
| 48 reset_buf((uint8_t *)buf, init, BYTE_LENGTH); |
| 49 memmove((void *)(buf + 4), (void *)buf, BYTE_LENGTH - (4 * sizeof(elem_t))); |
| 50 return sum_buf((uint8_t*)buf + 4, BYTE_LENGTH - 4); |
| 51 } |
| 52 |
| 53 int memset_test_fixed_len(uint8_t init) { |
| 54 elem_t buf[NWORDS]; |
| 55 memset((void *)buf, init, BYTE_LENGTH); |
| 56 return sum_buf((uint8_t*)buf, BYTE_LENGTH); |
| 57 } |
| 58 |
| 59 int memcpy_test(uint8_t *buf, void *buf2, uint8_t init, size_t length) { |
| 60 reset_buf(buf, init, length); |
| 61 memcpy(buf2, (void *)buf, length); |
| 62 return sum_buf((uint8_t *)buf2, length); |
| 63 } |
| 64 |
| 65 int memmove_test(uint8_t *buf, void *buf2, uint8_t init, size_t length) { |
| 66 int sum1; |
| 67 int sum2; |
| 68 const int overlap_bytes = 4 * sizeof(elem_t); |
| 69 if (length <= overlap_bytes) |
| 70 return 0; |
| 71 uint8_t *overlap_buf = buf + overlap_bytes; |
| 72 size_t reduced_length = length - overlap_bytes; |
| 73 reset_buf(buf, init, length); |
| 74 |
| 75 /* Test w/ overlap. */ |
| 76 memmove((void *)overlap_buf, (void *)buf, reduced_length); |
| 77 sum1 = sum_buf(overlap_buf, reduced_length); |
| 78 /* Test w/out overlap. */ |
| 79 memmove(buf2, (void *)buf, length); |
| 80 sum2 = sum_buf((uint8_t *)buf2, length); |
| 81 return sum1 + sum2; |
| 82 } |
| 83 |
| 84 int memset_test(uint8_t *buf, void *buf2, uint8_t init, size_t length) { |
| 85 memset((void *)buf, init, length); |
| 86 memset(buf2, init + 4, length); |
| 87 return sum_buf(buf, length) + sum_buf((uint8_t *)buf2, length); |
| 88 } |
OLD | NEW |