blob: 60de9abbdc8770904b79ea7dc943a5a0ab33391d [file] [log] [blame]
David Ghandehari9e5b5872016-07-28 09:50:04 -07001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Xiaoming Shi73dfa202020-03-12 11:31:35 -07008#include "include/utils/SkRandom.h"
Kaido Kertb1089432024-03-18 19:46:49 -07009#include "src/core/SkOpts.h"
Xiaoming Shi73dfa202020-03-12 11:31:35 -070010#include "tests/Test.h"
David Ghandehari9e5b5872016-07-28 09:50:04 -070011
David Ghandehari9e5b5872016-07-28 09:50:04 -070012static void set_zero(void* dst, size_t bytes) {
13 char* ptr = (char*)dst;
14 for (size_t i = 0; i < bytes; ++i) {
15 ptr[i] = 0;
16 }
17}
18
19#define MAX_ALIGNMENT 64
20#define MAX_COUNT ((MAX_ALIGNMENT) * 32)
21#define PAD 32
22#define TOTAL (PAD + MAX_ALIGNMENT + MAX_COUNT + PAD)
23
24#define VALUE16 0x1234
25#define VALUE32 0x12345678
26
Andrew Top200ce4b2018-01-29 13:43:50 -080027static void compare16(skiatest::Reporter* r, const uint16_t base[],
28 uint16_t value, int count) {
David Ghandehari9e5b5872016-07-28 09:50:04 -070029 for (int i = 0; i < count; ++i) {
30 if (base[i] != value) {
Andrew Top200ce4b2018-01-29 13:43:50 -080031 ERRORF(r, "[%d] expected %x found %x\n", i, value, base[i]);
32 return;
David Ghandehari9e5b5872016-07-28 09:50:04 -070033 }
34 }
David Ghandehari9e5b5872016-07-28 09:50:04 -070035}
36
Andrew Top200ce4b2018-01-29 13:43:50 -080037static void compare32(skiatest::Reporter* r, const uint32_t base[],
38 uint32_t value, int count) {
David Ghandehari9e5b5872016-07-28 09:50:04 -070039 for (int i = 0; i < count; ++i) {
40 if (base[i] != value) {
Andrew Top200ce4b2018-01-29 13:43:50 -080041 ERRORF(r, "[%d] expected %x found %x\n", i, value, base[i]);
42 return;
David Ghandehari9e5b5872016-07-28 09:50:04 -070043 }
44 }
David Ghandehari9e5b5872016-07-28 09:50:04 -070045}
46
47static void test_16(skiatest::Reporter* reporter) {
48 uint16_t buffer[TOTAL];
49
50 for (int count = 0; count < MAX_COUNT; ++count) {
51 for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
52 set_zero(buffer, sizeof(buffer));
53
54 uint16_t* base = &buffer[PAD + alignment];
55 sk_memset16(base, VALUE16, count);
56
Andrew Top200ce4b2018-01-29 13:43:50 -080057 compare16(reporter, buffer, 0, PAD + alignment);
58 compare16(reporter, base, VALUE16, count);
59 compare16(reporter, base + count, 0, TOTAL - count - PAD - alignment);
David Ghandehari9e5b5872016-07-28 09:50:04 -070060 }
61 }
62}
63
64static void test_32(skiatest::Reporter* reporter) {
65 uint32_t buffer[TOTAL];
66
67 for (int count = 0; count < MAX_COUNT; ++count) {
68 for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
69 set_zero(buffer, sizeof(buffer));
70
71 uint32_t* base = &buffer[PAD + alignment];
72 sk_memset32(base, VALUE32, count);
73
Andrew Top200ce4b2018-01-29 13:43:50 -080074 compare32(reporter, buffer, 0, PAD + alignment);
75 compare32(reporter, base, VALUE32, count);
76 compare32(reporter, base + count, 0, TOTAL - count - PAD - alignment);
David Ghandehari9e5b5872016-07-28 09:50:04 -070077 }
78 }
79}
80
81/**
82 * Test sk_memset16 and sk_memset32.
83 * For performance considerations, implementations may take different paths
84 * depending on the alignment of the dst, and/or the size of the count.
85 */
86DEF_TEST(Memset, reporter) {
87 test_16(reporter);
88 test_32(reporter);
David Ghandehari9e5b5872016-07-28 09:50:04 -070089}