Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 "SkSLJIT.h" |
| 9 | |
| 10 | #include "Test.h" |
| 11 | |
| 12 | #ifdef SK_LLVM_AVAILABLE |
| 13 | |
| 14 | template<typename type> |
| 15 | void test(skiatest::Reporter* r, const char* src, type x, type y, type result) { |
| 16 | SkSL::Compiler compiler; |
| 17 | SkSL::Program::Settings settings; |
| 18 | std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kCPU_Kind, |
| 19 | SkSL::String(src), settings); |
| 20 | REPORTER_ASSERT(r, program); |
| 21 | if (program) { |
| 22 | SkSL::JIT jit(&compiler); |
| 23 | std::unique_ptr<SkSL::JIT::Module> module = jit.compile(std::move(program)); |
| 24 | type (*test)(type, type) = (type(*)(type, type)) module->getSymbol("test"); |
| 25 | REPORTER_ASSERT(r, test(x, y) == result); |
| 26 | } else { |
| 27 | printf("%s", compiler.errorText().c_str()); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | DEF_TEST(SkSLJITAdd, r) { |
| 32 | test<int>(r, "int test(int x, int y) { return x + y; }", 12, 5, 17); |
| 33 | test<float>(r, "float test(float x, float y) { return x + y; }", -1, 76, 75); |
| 34 | test<int>(r, "int test(int x, int y) { x += y; return x; }", 12, 5, 17); |
| 35 | test<float>(r, "float test(float x, float y) { x += y; return x; }", -1, 76, 75); |
| 36 | test<int>(r, "int test(int x, int y) { return (int2(x) + int2(y)).x; }", 0, -100, -100); |
| 37 | test<float>(r, "float test(float x, float y) { return (float2(x) + float2(y)).x; }", 36, 6, 42); |
| 38 | } |
| 39 | |
| 40 | DEF_TEST(SkSLJITSub, r) { |
| 41 | test<int>(r, "int test(int x, int y) { return x - y; }", 12, 5, 7); |
| 42 | test<float>(r, "float test(float x, float y) { return x - y; }", -1, 76, -77); |
| 43 | test<int>(r, "int test(int x, int y) { x -= y; return x; }", 12, 5, 7); |
| 44 | test<float>(r, "float test(float x, float y) { x -= y; return x; }", -1, 76, -77); |
| 45 | test<int>(r, "int test(int x, int y) { return (int2(x) - int2(y)).x; }", 0, -100, 100); |
| 46 | test<float>(r, "float test(float x, float y) { return (float2(x) - float2(y)).x; }", 36, 6, 30); |
| 47 | } |
| 48 | |
| 49 | DEF_TEST(SkSLJITMul, r) { |
| 50 | test<int>(r, "int test(int x, int y) { return x * y; }", 12, 5, 60); |
| 51 | test<float>(r, "float test(float x, float y) { return x * y; }", -1, 76, -76); |
| 52 | test<int>(r, "int test(int x, int y) { x *= y; return x; }", 12, 5, 60); |
| 53 | test<float>(r, "float test(float x, float y) { x *= y; return x; }", -1, 76, -76); |
| 54 | test<int>(r, "int test(int x, int y) { return (int2(x) * int2(y)).x; }", 0, -100, 0); |
| 55 | test<float>(r, "float test(float x, float y) { return (float2(x) * float2(y)).x; }", 36, 6, |
| 56 | 216); |
| 57 | } |
| 58 | |
| 59 | DEF_TEST(SkSLJITDiv, r) { |
| 60 | test<int>(r, "int test(int x, int y) { return x / y; }", 12, 5, 2); |
| 61 | test<float>(r, "float test(float x, float y) { return x / y; }", -1, 76, -1.0 / 76.0); |
| 62 | test<int>(r, "int test(int x, int y) { x /= y; return x; }", 12, 5, 2); |
| 63 | test<float>(r, "float test(float x, float y) { x /= y; return x; }", -1, 76, -1.0 / 76.0); |
| 64 | test<int>(r, "int test(int x, int y) { return (int2(x) / int2(y)).x; }", 0, -100, 0); |
| 65 | test<float>(r, "float test(float x, float y) { return (float2(x) / float2(y)).x; }", 36, 6, |
| 66 | 6); |
| 67 | } |
| 68 | |
| 69 | DEF_TEST(SkSLJITOr, r) { |
| 70 | test<int>(r, "int test(int x, int y) { return x | y; }", 45, 15, 47); |
| 71 | test<int>(r, "int test(int x, int y) { x |= y; return x; }", 45, 15, 47); |
| 72 | } |
| 73 | |
| 74 | DEF_TEST(SkSLJITAnd, r) { |
| 75 | test<int>(r, "int test(int x, int y) { return x & y; }", 45, 15, 13); |
| 76 | test<int>(r, "int test(int x, int y) { x &= y; return x; }", 45, 15, 13); |
| 77 | } |
| 78 | |
| 79 | DEF_TEST(SkSLJITIf, r) { |
| 80 | test<int>(r, "int test(int x, int y) { if (x > y) return x; else return y; }", 17, 8, 17); |
| 81 | test<int>(r, "int test(int x, int y) { if (x > y) return x; else return y; }", 8, 17, 17); |
| 82 | test<int>(r, "int test(int x, int y) { if (x > y) if (x > 0) return x; else return -x; " |
| 83 | "else if (y > 0) return y; else return -y; }", -8, -17, 8); |
| 84 | } |
| 85 | |
| 86 | DEF_TEST(SkSLJITTernary, r) { |
| 87 | test<int>(r, "int test(int x, int y) { return x > y ? x : y; }", 17, 8, 17); |
| 88 | test<int>(r, "int test(int x, int y) { return x > y ? x : y; }", 8, 17, 17); |
| 89 | test<int>(r, "int test(int x, int y) { return x > y ? (x > 0 ? x : -x) :" |
| 90 | "(y > 0 ? y : -y); }", -8, -17, 8); |
| 91 | } |
| 92 | |
| 93 | DEF_TEST(SkSLJITFor, r) { |
| 94 | test<int>(r, "int test(int x, int y) {" |
| 95 | " int result = 0;" |
| 96 | " for (int i = 0; i < x; ++i)" |
| 97 | " result += y;" |
| 98 | " return result;" |
| 99 | "}", 124, 17, 2108); |
| 100 | } |
| 101 | |
| 102 | DEF_TEST(SkSLJITDo, r) { |
| 103 | test<int>(r, "int test(int x, int y) {" |
| 104 | " int result = -10;" |
| 105 | " do { result = 0; } while (false);" |
| 106 | " do { result += x; } while (result < y);" |
| 107 | " return result;" |
| 108 | "}", 96, 200, 288); |
| 109 | } |
| 110 | |
| 111 | DEF_TEST(SkSLJITWhile, r) { |
| 112 | test<int>(r, "int test(int x, int y) {" |
| 113 | " int result = 0;" |
| 114 | " while (false) { result = -10; }" |
| 115 | " while (result < y) { result += x; }" |
| 116 | " return result;" |
| 117 | "}", 96, 200, 288); |
| 118 | } |
| 119 | |
| 120 | #endif |