blob: 0fe03872cd7d34e9342e0a8cc6ff785be6ead366 [file] [log] [blame]
Jan Voung3bd9f1a2014-06-18 10:50:57 -07001/*
2 * Simple sanity test of memcpy, memmove, and memset intrinsics.
3 * (fixed length buffers, variable length buffers, etc.)
4 */
5
6#include <stdint.h> /* cstdint requires -std=c++0x or higher */
7#include <cstdlib>
8#include <cstring>
9
10#include "mem_intrin.h"
John Porto1d235422015-08-12 12:37:53 -070011#include "xdefs.h"
Jan Voung3bd9f1a2014-06-18 10:50:57 -070012
13typedef int elem_t;
14
15/*
16 * Reset buf to the sequence of bytes: n, n+1, n+2 ... length - 1
17 */
Jim Stichnothdd842db2015-01-27 12:53:53 -080018static void __attribute__((noinline))
John Porto1d235422015-08-12 12:37:53 -070019reset_buf(uint8_t *buf, uint8_t init, SizeT length) {
20 SizeT i;
21 SizeT v = init;
Jan Voung3bd9f1a2014-06-18 10:50:57 -070022 for (i = 0; i < length; ++i)
23 buf[i] = v++;
24}
25
26/* Do a fletcher-16 checksum so that the order of the values matter.
27 * (Not doing a fletcher-32 checksum, since we are working with
28 * smaller buffers, whose total won't approach 2**16).
29 */
Jim Stichnothdd842db2015-01-27 12:53:53 -080030static int __attribute__((noinline))
John Porto1d235422015-08-12 12:37:53 -070031fletcher_checksum(uint8_t *buf, SizeT length) {
32 SizeT i;
Jan Voung3bd9f1a2014-06-18 10:50:57 -070033 int sum = 0;
34 int sum_of_sums = 0;
35 const int kModulus = 255;
36 for (i = 0; i < length; ++i) {
37 sum = (sum + buf[i]) % kModulus;
38 sum_of_sums = (sum_of_sums + sum) % kModulus;
39 }
40 return (sum_of_sums << 8) | sum;
41}
42
43#define NWORDS 32
44#define BYTE_LENGTH (NWORDS * sizeof(elem_t))
45
46int memcpy_test_fixed_len(uint8_t init) {
47 elem_t buf[NWORDS];
48 elem_t buf2[NWORDS];
49 reset_buf((uint8_t *)buf, init, BYTE_LENGTH);
50 memcpy((void *)buf2, (void *)buf, BYTE_LENGTH);
Jan Voung140bb0d2014-07-14 11:51:44 -070051 return fletcher_checksum((uint8_t *)buf2, BYTE_LENGTH);
Jan Voung3bd9f1a2014-06-18 10:50:57 -070052}
53
54int memmove_test_fixed_len(uint8_t init) {
55 elem_t buf[NWORDS];
56 reset_buf((uint8_t *)buf, init, BYTE_LENGTH);
57 memmove((void *)(buf + 4), (void *)buf, BYTE_LENGTH - (4 * sizeof(elem_t)));
Jan Voung140bb0d2014-07-14 11:51:44 -070058 return fletcher_checksum((uint8_t *)buf + 4, BYTE_LENGTH - 4);
Jan Voung3bd9f1a2014-06-18 10:50:57 -070059}
60
61int memset_test_fixed_len(uint8_t init) {
62 elem_t buf[NWORDS];
63 memset((void *)buf, init, BYTE_LENGTH);
Jan Voung140bb0d2014-07-14 11:51:44 -070064 return fletcher_checksum((uint8_t *)buf, BYTE_LENGTH);
Jan Voung3bd9f1a2014-06-18 10:50:57 -070065}
66
John Porto1d235422015-08-12 12:37:53 -070067int memcpy_test(uint8_t *buf, uint8_t *buf2, uint8_t init, SizeT length) {
Jan Voung3bd9f1a2014-06-18 10:50:57 -070068 reset_buf(buf, init, length);
Jan Voung140bb0d2014-07-14 11:51:44 -070069 memcpy((void *)buf2, (void *)buf, length);
70 return fletcher_checksum(buf2, length);
Jan Voung3bd9f1a2014-06-18 10:50:57 -070071}
72
John Porto1d235422015-08-12 12:37:53 -070073int memmove_test(uint8_t *buf, uint8_t *buf2, uint8_t init, SizeT length) {
Jan Voung3bd9f1a2014-06-18 10:50:57 -070074 int sum1;
75 int sum2;
76 const int overlap_bytes = 4 * sizeof(elem_t);
77 if (length <= overlap_bytes)
78 return 0;
79 uint8_t *overlap_buf = buf + overlap_bytes;
John Porto1d235422015-08-12 12:37:53 -070080 SizeT reduced_length = length - overlap_bytes;
Jan Voung3bd9f1a2014-06-18 10:50:57 -070081 reset_buf(buf, init, length);
82
83 /* Test w/ overlap. */
84 memmove((void *)overlap_buf, (void *)buf, reduced_length);
85 sum1 = fletcher_checksum(overlap_buf, reduced_length);
86 /* Test w/out overlap. */
Jan Voung140bb0d2014-07-14 11:51:44 -070087 memmove((void *)buf2, (void *)buf, length);
88 sum2 = fletcher_checksum(buf2, length);
Jan Voung3bd9f1a2014-06-18 10:50:57 -070089 return sum1 + sum2;
90}
91
John Porto1d235422015-08-12 12:37:53 -070092int memset_test(uint8_t *buf, uint8_t *buf2, uint8_t init, SizeT length) {
Jan Voung3bd9f1a2014-06-18 10:50:57 -070093 memset((void *)buf, init, length);
Jan Voung140bb0d2014-07-14 11:51:44 -070094 memset((void *)buf2, init + 4, length);
95 return fletcher_checksum(buf, length) + fletcher_checksum(buf2, length);
Jan Voung3bd9f1a2014-06-18 10:50:57 -070096}