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