Mike Klein | b5f95cd | 2019-07-02 14:16:26 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google LLC |
| 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 "src/core/SkCpu.h" |
| 9 | #include "src/core/SkVM.h" |
Mike Klein | 84bcd0c | 2019-07-23 11:46:09 -0500 | [diff] [blame] | 10 | #include "tools/SkVMBuilders.h" |
| 11 | #include <chrono> |
Mike Klein | b5f95cd | 2019-07-02 14:16:26 -0500 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | |
| 15 | void sk_abort_no_print() { |
| 16 | abort(); |
| 17 | } |
| 18 | |
| 19 | void SkDebugf(const char* fmt, ...) { |
| 20 | va_list args; |
| 21 | va_start(args, fmt); |
| 22 | vfprintf(stderr, fmt, args); |
| 23 | va_end(args); |
| 24 | } |
| 25 | |
Mike Klein | 84bcd0c | 2019-07-23 11:46:09 -0500 | [diff] [blame] | 26 | static skvm::Program plus_one() { |
| 27 | skvm::Builder b; |
Mike Klein | b5f95cd | 2019-07-02 14:16:26 -0500 | [diff] [blame] | 28 | |
Mike Klein | 84bcd0c | 2019-07-23 11:46:09 -0500 | [diff] [blame] | 29 | skvm::Arg ptr = b.arg<int>(); |
| 30 | skvm::I32 v = b.load32(ptr); |
| 31 | b.store32(ptr, b.add(v, b.splat(1))); |
Mike Klein | b5f95cd | 2019-07-02 14:16:26 -0500 | [diff] [blame] | 32 | |
Mike Klein | 84bcd0c | 2019-07-23 11:46:09 -0500 | [diff] [blame] | 33 | return b.done("plus_one"); |
| 34 | } |
| 35 | |
| 36 | static skvm::Program square() { |
| 37 | skvm::Builder b; |
| 38 | |
| 39 | skvm::Arg ptr = b.arg<int>(); |
| 40 | skvm::I32 v = b.load32(ptr); |
Mike Klein | b5f95cd | 2019-07-02 14:16:26 -0500 | [diff] [blame] | 41 | b.store32(ptr, b.mul(v,v)); |
| 42 | |
Mike Klein | 84bcd0c | 2019-07-23 11:46:09 -0500 | [diff] [blame] | 43 | return b.done("square"); |
| 44 | } |
| 45 | |
| 46 | static void print(double val, const char* units) { |
| 47 | const char* scales[] = { "", "K", "M", "G", "T" }; |
| 48 | const char** scale = scales; |
| 49 | |
| 50 | while (val > 10000.0) { |
| 51 | val *= 1/1000.0; |
| 52 | scale++; |
| 53 | } |
| 54 | |
| 55 | printf("%4d %s%s", (int)val, *scale, units); |
| 56 | } |
| 57 | |
| 58 | template <typename Fn> |
| 59 | static double measure(Fn&& fn) { |
| 60 | using clock = std::chrono::steady_clock; |
| 61 | |
| 62 | int loops = 0; |
| 63 | auto start = clock::now(); |
| 64 | std::chrono::duration<double> elapsed; |
| 65 | do { |
| 66 | fn(); |
| 67 | loops++; |
| 68 | elapsed = clock::now() - start; |
| 69 | } while (elapsed < std::chrono::milliseconds(100)); |
| 70 | |
| 71 | return loops / elapsed.count(); |
| 72 | } |
| 73 | |
| 74 | template <typename... Args> |
| 75 | static void time(const char* name, const skvm::Program& program, Args... args) { |
| 76 | printf("%20s", name); |
| 77 | |
| 78 | for (int N : { 15, 255, 4095 }) { |
| 79 | double loops_per_sec = measure([&]{ |
| 80 | program.eval(N, args...); |
| 81 | }); |
| 82 | |
| 83 | printf("\t"); |
| 84 | print(N*loops_per_sec, "px/s"); |
| 85 | } |
| 86 | printf("\n"); |
Mike Klein | b5f95cd | 2019-07-02 14:16:26 -0500 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | int main(int argc, char** argv) { |
| 90 | #if defined(__x86_64__) |
| 91 | SkCpu::CacheRuntimeFeatures(); |
| 92 | #endif |
Mike Klein | b5f95cd | 2019-07-02 14:16:26 -0500 | [diff] [blame] | 93 | |
Mike Klein | 84bcd0c | 2019-07-23 11:46:09 -0500 | [diff] [blame] | 94 | int src[4096], |
| 95 | dst[4096]; |
| 96 | time("plus_one", plus_one(), dst); |
| 97 | time( "square", square(), dst); |
Mike Klein | b5f95cd | 2019-07-02 14:16:26 -0500 | [diff] [blame] | 98 | |
Mike Klein | 84bcd0c | 2019-07-23 11:46:09 -0500 | [diff] [blame] | 99 | time("srcover_f32" , SrcoverBuilder_F32 ().done("srcover_f32" ), src, dst); |
| 100 | time("srcover_i32" , SrcoverBuilder_I32 ().done("srcover_i32" ), src, dst); |
| 101 | time("srcover_i32_naive", SrcoverBuilder_I32_Naive().done("srcover_i32_naive"), src, dst); |
| 102 | time("srcover_i32_SWAR" , SrcoverBuilder_I32_SWAR ().done("srcover_i32_SWAR" ), src, dst); |
Mike Klein | b5f95cd | 2019-07-02 14:16:26 -0500 | [diff] [blame] | 103 | |
Mike Klein | b5f95cd | 2019-07-02 14:16:26 -0500 | [diff] [blame] | 104 | return 0; |
| 105 | } |