blob: b68844c86650ff21fba7a1b0df151936403f03ef [file] [log] [blame]
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +00001/*
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 */
reed@google.comebd24962012-05-17 14:28:11 +00007
milko.leporis401e77c2016-06-05 13:14:21 -07008#include "SkRandom.h"
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +00009#include "SkUtils.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000010#include "Test.h"
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000011
12static 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
halcanary7d571242016-02-24 17:59:16 -080027static void compare16(skiatest::Reporter* r, const uint16_t base[],
28 uint16_t value, int count) {
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000029 for (int i = 0; i < count; ++i) {
30 if (base[i] != value) {
halcanary7d571242016-02-24 17:59:16 -080031 ERRORF(r, "[%d] expected %x found %x\n", i, value, base[i]);
32 return;
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000033 }
34 }
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000035}
36
halcanary7d571242016-02-24 17:59:16 -080037static void compare32(skiatest::Reporter* r, const uint32_t base[],
38 uint32_t value, int count) {
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000039 for (int i = 0; i < count; ++i) {
40 if (base[i] != value) {
halcanary7d571242016-02-24 17:59:16 -080041 ERRORF(r, "[%d] expected %x found %x\n", i, value, base[i]);
42 return;
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000043 }
44 }
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000045}
46
47static void test_16(skiatest::Reporter* reporter) {
48 uint16_t buffer[TOTAL];
rmistry@google.comd6176b02012-08-23 18:14:13 +000049
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000050 for (int count = 0; count < MAX_COUNT; ++count) {
51 for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
52 set_zero(buffer, sizeof(buffer));
rmistry@google.comd6176b02012-08-23 18:14:13 +000053
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000054 uint16_t* base = &buffer[PAD + alignment];
55 sk_memset16(base, VALUE16, count);
rmistry@google.comd6176b02012-08-23 18:14:13 +000056
halcanary7d571242016-02-24 17:59:16 -080057 compare16(reporter, buffer, 0, PAD + alignment);
58 compare16(reporter, base, VALUE16, count);
59 compare16(reporter, base + count, 0, TOTAL - count - PAD - alignment);
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000060 }
61 }
62}
63
64static void test_32(skiatest::Reporter* reporter) {
65 uint32_t buffer[TOTAL];
rmistry@google.comd6176b02012-08-23 18:14:13 +000066
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000067 for (int count = 0; count < MAX_COUNT; ++count) {
68 for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
69 set_zero(buffer, sizeof(buffer));
rmistry@google.comd6176b02012-08-23 18:14:13 +000070
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000071 uint32_t* base = &buffer[PAD + alignment];
72 sk_memset32(base, VALUE32, count);
rmistry@google.comd6176b02012-08-23 18:14:13 +000073
halcanary7d571242016-02-24 17:59:16 -080074 compare32(reporter, buffer, 0, PAD + alignment);
75 compare32(reporter, base, VALUE32, count);
76 compare32(reporter, base + count, 0, TOTAL - count - PAD - alignment);
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000077 }
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 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000086DEF_TEST(Memset, reporter) {
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000087 test_16(reporter);
88 test_32(reporter);
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000089}