| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Simple sanity test of memcpy, memmove, and memset intrinsics. | 2 * Simple sanity test of memcpy, memmove, and memset intrinsics. |
| 3 * (fixed length buffers, variable length buffers, etc.) | 3 * (fixed length buffers, variable length buffers, etc.) |
| 4 */ | 4 */ |
| 5 | 5 |
| 6 #include <stdint.h> /* cstdint requires -std=c++0x or higher */ | 6 #include <stdint.h> /* cstdint requires -std=c++0x or higher */ |
| 7 #include <cstdlib> | 7 #include <cstdlib> |
| 8 #include <cstring> | 8 #include <cstring> |
| 9 | 9 |
| 10 #include "mem_intrin.h" | 10 #include "mem_intrin.h" |
| 11 #include "xdefs.h" |
| 11 | 12 |
| 12 typedef int elem_t; | 13 typedef int elem_t; |
| 13 | 14 |
| 14 /* | 15 /* |
| 15 * Reset buf to the sequence of bytes: n, n+1, n+2 ... length - 1 | 16 * Reset buf to the sequence of bytes: n, n+1, n+2 ... length - 1 |
| 16 */ | 17 */ |
| 17 static void __attribute__((noinline)) | 18 static void __attribute__((noinline)) |
| 18 reset_buf(uint8_t *buf, uint8_t init, size_t length) { | 19 reset_buf(uint8_t *buf, uint8_t init, SizeT length) { |
| 19 size_t i; | 20 SizeT i; |
| 20 size_t v = init; | 21 SizeT v = init; |
| 21 for (i = 0; i < length; ++i) | 22 for (i = 0; i < length; ++i) |
| 22 buf[i] = v++; | 23 buf[i] = v++; |
| 23 } | 24 } |
| 24 | 25 |
| 25 /* Do a fletcher-16 checksum so that the order of the values matter. | 26 /* Do a fletcher-16 checksum so that the order of the values matter. |
| 26 * (Not doing a fletcher-32 checksum, since we are working with | 27 * (Not doing a fletcher-32 checksum, since we are working with |
| 27 * smaller buffers, whose total won't approach 2**16). | 28 * smaller buffers, whose total won't approach 2**16). |
| 28 */ | 29 */ |
| 29 static int __attribute__((noinline)) | 30 static int __attribute__((noinline)) |
| 30 fletcher_checksum(uint8_t *buf, size_t length) { | 31 fletcher_checksum(uint8_t *buf, SizeT length) { |
| 31 size_t i; | 32 SizeT i; |
| 32 int sum = 0; | 33 int sum = 0; |
| 33 int sum_of_sums = 0; | 34 int sum_of_sums = 0; |
| 34 const int kModulus = 255; | 35 const int kModulus = 255; |
| 35 for (i = 0; i < length; ++i) { | 36 for (i = 0; i < length; ++i) { |
| 36 sum = (sum + buf[i]) % kModulus; | 37 sum = (sum + buf[i]) % kModulus; |
| 37 sum_of_sums = (sum_of_sums + sum) % kModulus; | 38 sum_of_sums = (sum_of_sums + sum) % kModulus; |
| 38 } | 39 } |
| 39 return (sum_of_sums << 8) | sum; | 40 return (sum_of_sums << 8) | sum; |
| 40 } | 41 } |
| 41 | 42 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 56 memmove((void *)(buf + 4), (void *)buf, BYTE_LENGTH - (4 * sizeof(elem_t))); | 57 memmove((void *)(buf + 4), (void *)buf, BYTE_LENGTH - (4 * sizeof(elem_t))); |
| 57 return fletcher_checksum((uint8_t *)buf + 4, BYTE_LENGTH - 4); | 58 return fletcher_checksum((uint8_t *)buf + 4, BYTE_LENGTH - 4); |
| 58 } | 59 } |
| 59 | 60 |
| 60 int memset_test_fixed_len(uint8_t init) { | 61 int memset_test_fixed_len(uint8_t init) { |
| 61 elem_t buf[NWORDS]; | 62 elem_t buf[NWORDS]; |
| 62 memset((void *)buf, init, BYTE_LENGTH); | 63 memset((void *)buf, init, BYTE_LENGTH); |
| 63 return fletcher_checksum((uint8_t *)buf, BYTE_LENGTH); | 64 return fletcher_checksum((uint8_t *)buf, BYTE_LENGTH); |
| 64 } | 65 } |
| 65 | 66 |
| 66 int memcpy_test(uint8_t *buf, uint8_t *buf2, uint8_t init, size_t length) { | 67 int memcpy_test(uint8_t *buf, uint8_t *buf2, uint8_t init, SizeT length) { |
| 67 reset_buf(buf, init, length); | 68 reset_buf(buf, init, length); |
| 68 memcpy((void *)buf2, (void *)buf, length); | 69 memcpy((void *)buf2, (void *)buf, length); |
| 69 return fletcher_checksum(buf2, length); | 70 return fletcher_checksum(buf2, length); |
| 70 } | 71 } |
| 71 | 72 |
| 72 int memmove_test(uint8_t *buf, uint8_t *buf2, uint8_t init, size_t length) { | 73 int memmove_test(uint8_t *buf, uint8_t *buf2, uint8_t init, SizeT length) { |
| 73 int sum1; | 74 int sum1; |
| 74 int sum2; | 75 int sum2; |
| 75 const int overlap_bytes = 4 * sizeof(elem_t); | 76 const int overlap_bytes = 4 * sizeof(elem_t); |
| 76 if (length <= overlap_bytes) | 77 if (length <= overlap_bytes) |
| 77 return 0; | 78 return 0; |
| 78 uint8_t *overlap_buf = buf + overlap_bytes; | 79 uint8_t *overlap_buf = buf + overlap_bytes; |
| 79 size_t reduced_length = length - overlap_bytes; | 80 SizeT reduced_length = length - overlap_bytes; |
| 80 reset_buf(buf, init, length); | 81 reset_buf(buf, init, length); |
| 81 | 82 |
| 82 /* Test w/ overlap. */ | 83 /* Test w/ overlap. */ |
| 83 memmove((void *)overlap_buf, (void *)buf, reduced_length); | 84 memmove((void *)overlap_buf, (void *)buf, reduced_length); |
| 84 sum1 = fletcher_checksum(overlap_buf, reduced_length); | 85 sum1 = fletcher_checksum(overlap_buf, reduced_length); |
| 85 /* Test w/out overlap. */ | 86 /* Test w/out overlap. */ |
| 86 memmove((void *)buf2, (void *)buf, length); | 87 memmove((void *)buf2, (void *)buf, length); |
| 87 sum2 = fletcher_checksum(buf2, length); | 88 sum2 = fletcher_checksum(buf2, length); |
| 88 return sum1 + sum2; | 89 return sum1 + sum2; |
| 89 } | 90 } |
| 90 | 91 |
| 91 int memset_test(uint8_t *buf, uint8_t *buf2, uint8_t init, size_t length) { | 92 int memset_test(uint8_t *buf, uint8_t *buf2, uint8_t init, SizeT length) { |
| 92 memset((void *)buf, init, length); | 93 memset((void *)buf, init, length); |
| 93 memset((void *)buf2, init + 4, length); | 94 memset((void *)buf2, init + 4, length); |
| 94 return fletcher_checksum(buf, length) + fletcher_checksum(buf2, length); | 95 return fletcher_checksum(buf, length) + fletcher_checksum(buf2, length); |
| 95 } | 96 } |
| OLD | NEW |