mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [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 | */ |
reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 7 | |
milko.leporis | 401e77c | 2016-06-05 13:14:21 -0700 | [diff] [blame] | 8 | #include "SkRandom.h" |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 9 | #include "SkUtils.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 10 | #include "Test.h" |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 11 | |
| 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 | |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 27 | static void compare16(skiatest::Reporter* r, const uint16_t base[], |
| 28 | uint16_t value, int count) { |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 29 | for (int i = 0; i < count; ++i) { |
| 30 | if (base[i] != value) { |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 31 | ERRORF(r, "[%d] expected %x found %x\n", i, value, base[i]); |
| 32 | return; |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 33 | } |
| 34 | } |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 35 | } |
| 36 | |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 37 | static void compare32(skiatest::Reporter* r, const uint32_t base[], |
| 38 | uint32_t value, int count) { |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 39 | for (int i = 0; i < count; ++i) { |
| 40 | if (base[i] != value) { |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 41 | ERRORF(r, "[%d] expected %x found %x\n", i, value, base[i]); |
| 42 | return; |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 43 | } |
| 44 | } |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static void test_16(skiatest::Reporter* reporter) { |
| 48 | uint16_t buffer[TOTAL]; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 49 | |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 50 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 53 | |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 54 | uint16_t* base = &buffer[PAD + alignment]; |
| 55 | sk_memset16(base, VALUE16, count); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 56 | |
halcanary | 7d57124 | 2016-02-24 17:59:16 -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); |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | static void test_32(skiatest::Reporter* reporter) { |
| 65 | uint32_t buffer[TOTAL]; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 66 | |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 67 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 70 | |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 71 | uint32_t* base = &buffer[PAD + alignment]; |
| 72 | sk_memset32(base, VALUE32, count); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 73 | |
halcanary | 7d57124 | 2016-02-24 17:59:16 -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); |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [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 | */ |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 86 | DEF_TEST(Memset, reporter) { |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 87 | test_16(reporter); |
| 88 | test_32(reporter); |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 89 | } |