mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
| 8 | #include "Benchmark.h" |
| 9 | #include "SkColor.h" |
| 10 | #include "SkNx.h" |
| 11 | |
| 12 | // Used to prevent the compiler from optimizing away the whole loop. |
mtklein | 2d340f2 | 2016-02-06 19:38:39 -0800 | [diff] [blame^] | 13 | volatile uint64_t blackhole = 0; |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 14 | |
| 15 | // Not a great random number generator, but it's very fast. |
| 16 | // The code we're measuring is quite fast, so low overhead is essential. |
mtklein | 2d340f2 | 2016-02-06 19:38:39 -0800 | [diff] [blame^] | 17 | static uint64_t lcg_rand(uint64_t* seed) { |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 18 | *seed *= 1664525; |
| 19 | *seed += 1013904223; |
| 20 | return *seed; |
| 21 | } |
| 22 | |
mtklein | 2d340f2 | 2016-02-06 19:38:39 -0800 | [diff] [blame^] | 23 | template <typename T> |
| 24 | struct Sk4fRoundtripBench : public Benchmark { |
| 25 | Sk4fRoundtripBench() {} |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 26 | |
mtklein | 2d340f2 | 2016-02-06 19:38:39 -0800 | [diff] [blame^] | 27 | const char* onGetName() override { |
| 28 | switch (sizeof(T)) { |
| 29 | case 1: return "Sk4f_roundtrip_u8"; |
| 30 | case 2: return "Sk4f_roundtrip_u16"; |
| 31 | } |
| 32 | SkASSERT(false); |
| 33 | return ""; |
| 34 | } |
| 35 | |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 36 | bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; } |
| 37 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 38 | void onDraw(int loops, SkCanvas* canvas) override { |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 39 | // Unlike blackhole, junk can and probably will be a register. |
mtklein | 2d340f2 | 2016-02-06 19:38:39 -0800 | [diff] [blame^] | 40 | uint64_t junk = 0; |
| 41 | uint64_t seed = 0; |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 42 | for (int i = 0; i < loops; i++) { |
mtklein | 2d340f2 | 2016-02-06 19:38:39 -0800 | [diff] [blame^] | 43 | uint64_t src = lcg_rand(&seed), |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 44 | back; |
mtklein | 2d340f2 | 2016-02-06 19:38:39 -0800 | [diff] [blame^] | 45 | auto f = SkNx_cast<float>(SkNx<4,T>::Load(&src)); |
| 46 | SkNx_cast<T>(f).store(&back); |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 47 | junk ^= back; |
| 48 | } |
| 49 | blackhole ^= junk; |
| 50 | } |
| 51 | }; |
mtklein | 2d340f2 | 2016-02-06 19:38:39 -0800 | [diff] [blame^] | 52 | DEF_BENCH(return new Sk4fRoundtripBench<uint8_t>;) |
| 53 | DEF_BENCH(return new Sk4fRoundtripBench<uint16_t>;) |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 54 | |
| 55 | struct Sk4fGradientBench : public Benchmark { |
| 56 | const char* onGetName() override { return "Sk4f_gradient"; } |
| 57 | bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; } |
| 58 | |
| 59 | SkPMColor fDevice[100]; |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 60 | void onDraw(int loops, SkCanvas*) override { |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 61 | Sk4f c0(0,0,255,255), |
| 62 | c1(255,0,0,255), |
| 63 | dc = c1 - c0, |
| 64 | fx(0.1f), |
| 65 | dx(0.002f), |
| 66 | dcdx(dc*dx), |
| 67 | dcdx4(dcdx+dcdx+dcdx+dcdx); |
| 68 | |
| 69 | for (int n = 0; n < loops; n++) { |
| 70 | Sk4f a = c0 + dc*fx + Sk4f(0.5f), // add an extra 0.5f to get rounding for free. |
| 71 | b = a + dcdx, |
| 72 | c = b + dcdx, |
| 73 | d = c + dcdx; |
| 74 | for (size_t i = 0; i < SK_ARRAY_COUNT(fDevice); i += 4) { |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 75 | Sk4f_ToBytes((uint8_t*)(fDevice+i), a, b, c, d); |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 76 | a = a + dcdx4; |
| 77 | b = b + dcdx4; |
| 78 | c = c + dcdx4; |
| 79 | d = d + dcdx4; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | }; |
| 84 | DEF_BENCH(return new Sk4fGradientBench;) |