blob: 612edce481722e3cba8804d1e3a7b1ddec13bc43 [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"
11
12typedef int elem_t;
13
14/*
15 * Reset buf to the sequence of bytes: n, n+1, n+2 ... length - 1
16 */
Jim Stichnothdd842db2015-01-27 12:53:53 -080017static void __attribute__((noinline))
18reset_buf(uint8_t *buf, uint8_t init, size_t length) {
Jan Voung3bd9f1a2014-06-18 10:50:57 -070019 size_t i;
20 size_t v = init;
21 for (i = 0; i < length; ++i)
22 buf[i] = v++;
23}
24
25/* 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 * smaller buffers, whose total won't approach 2**16).
28 */
Jim Stichnothdd842db2015-01-27 12:53:53 -080029static int __attribute__((noinline))
30fletcher_checksum(uint8_t *buf, size_t length) {
Jan Voung3bd9f1a2014-06-18 10:50:57 -070031 size_t i;
32 int sum = 0;
33 int sum_of_sums = 0;
34 const int kModulus = 255;
35 for (i = 0; i < length; ++i) {
36 sum = (sum + buf[i]) % kModulus;
37 sum_of_sums = (sum_of_sums + sum) % kModulus;
38 }
39 return (sum_of_sums << 8) | sum;
40}
41
42#define NWORDS 32
43#define BYTE_LENGTH (NWORDS * sizeof(elem_t))
44
45int memcpy_test_fixed_len(uint8_t init) {
46 elem_t buf[NWORDS];
47 elem_t buf2[NWORDS];
48 reset_buf((uint8_t *)buf, init, BYTE_LENGTH);
49 memcpy((void *)buf2, (void *)buf, BYTE_LENGTH);
Jan Voung140bb0d2014-07-14 11:51:44 -070050 return fletcher_checksum((uint8_t *)buf2, BYTE_LENGTH);
Jan Voung3bd9f1a2014-06-18 10:50:57 -070051}
52
53int memmove_test_fixed_len(uint8_t init) {
54 elem_t buf[NWORDS];
55 reset_buf((uint8_t *)buf, init, BYTE_LENGTH);
56 memmove((void *)(buf + 4), (void *)buf, BYTE_LENGTH - (4 * sizeof(elem_t)));
Jan Voung140bb0d2014-07-14 11:51:44 -070057 return fletcher_checksum((uint8_t *)buf + 4, BYTE_LENGTH - 4);
Jan Voung3bd9f1a2014-06-18 10:50:57 -070058}
59
60int memset_test_fixed_len(uint8_t init) {
61 elem_t buf[NWORDS];
62 memset((void *)buf, init, BYTE_LENGTH);
Jan Voung140bb0d2014-07-14 11:51:44 -070063 return fletcher_checksum((uint8_t *)buf, BYTE_LENGTH);
Jan Voung3bd9f1a2014-06-18 10:50:57 -070064}
65
Jan Voung140bb0d2014-07-14 11:51:44 -070066int memcpy_test(uint8_t *buf, uint8_t *buf2, uint8_t init, size_t length) {
Jan Voung3bd9f1a2014-06-18 10:50:57 -070067 reset_buf(buf, init, length);
Jan Voung140bb0d2014-07-14 11:51:44 -070068 memcpy((void *)buf2, (void *)buf, length);
69 return fletcher_checksum(buf2, length);
Jan Voung3bd9f1a2014-06-18 10:50:57 -070070}
71
Jan Voung140bb0d2014-07-14 11:51:44 -070072int memmove_test(uint8_t *buf, uint8_t *buf2, uint8_t init, size_t length) {
Jan Voung3bd9f1a2014-06-18 10:50:57 -070073 int sum1;
74 int sum2;
75 const int overlap_bytes = 4 * sizeof(elem_t);
76 if (length <= overlap_bytes)
77 return 0;
78 uint8_t *overlap_buf = buf + overlap_bytes;
79 size_t reduced_length = length - overlap_bytes;
80 reset_buf(buf, init, length);
81
82 /* Test w/ overlap. */
83 memmove((void *)overlap_buf, (void *)buf, reduced_length);
84 sum1 = fletcher_checksum(overlap_buf, reduced_length);
85 /* Test w/out overlap. */
Jan Voung140bb0d2014-07-14 11:51:44 -070086 memmove((void *)buf2, (void *)buf, length);
87 sum2 = fletcher_checksum(buf2, length);
Jan Voung3bd9f1a2014-06-18 10:50:57 -070088 return sum1 + sum2;
89}
90
Jan Voung140bb0d2014-07-14 11:51:44 -070091int memset_test(uint8_t *buf, uint8_t *buf2, uint8_t init, size_t length) {
Jan Voung3bd9f1a2014-06-18 10:50:57 -070092 memset((void *)buf, init, length);
Jan Voung140bb0d2014-07-14 11:51:44 -070093 memset((void *)buf2, init + 4, length);
94 return fletcher_checksum(buf, length) + fletcher_checksum(buf2, length);
Jan Voung3bd9f1a2014-06-18 10:50:57 -070095}