David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Shi | 73dfa20 | 2020-03-12 11:31:35 -0700 | [diff] [blame] | 8 | #include "include/utils/SkRandom.h" |
Kaido Kert | b108943 | 2024-03-18 19:46:49 -0700 | [diff] [blame] | 9 | #include "src/core/SkOpts.h" |
Xiaoming Shi | 73dfa20 | 2020-03-12 11:31:35 -0700 | [diff] [blame] | 10 | #include "tests/Test.h" |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 11 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 12 | static 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 Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 27 | static void compare16(skiatest::Reporter* r, const uint16_t base[], |
| 28 | uint16_t value, int count) { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 29 | for (int i = 0; i < count; ++i) { |
| 30 | if (base[i] != value) { |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 31 | ERRORF(r, "[%d] expected %x found %x\n", i, value, base[i]); |
| 32 | return; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 33 | } |
| 34 | } |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 37 | static void compare32(skiatest::Reporter* r, const uint32_t base[], |
| 38 | uint32_t value, int count) { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 39 | for (int i = 0; i < count; ++i) { |
| 40 | if (base[i] != value) { |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 41 | ERRORF(r, "[%d] expected %x found %x\n", i, value, base[i]); |
| 42 | return; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 43 | } |
| 44 | } |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static 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 Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 57 | compare16(reporter, buffer, 0, PAD + alignment); |
| 58 | compare16(reporter, base, VALUE16, count); |
| 59 | compare16(reporter, base + count, 0, TOTAL - count - PAD - alignment); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | static 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 Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 74 | compare32(reporter, buffer, 0, PAD + alignment); |
| 75 | compare32(reporter, base, VALUE32, count); |
| 76 | compare32(reporter, base + count, 0, TOTAL - count - PAD - alignment); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 77 | } |
| 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 | */ |
| 86 | DEF_TEST(Memset, reporter) { |
| 87 | test_16(reporter); |
| 88 | test_32(reporter); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 89 | } |