blob: a435b3a25d4418a5080e920985e878d8caa75140 [file] [log] [blame]
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLJIT.h"
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "tests/Test.h"
Ethan Nicholas26a9aad2018-03-27 14:10:52 -040011
12#ifdef SK_LLVM_AVAILABLE
13
14template<typename type>
15void test(skiatest::Reporter* r, const char* src, type x, type y, type result) {
16 SkSL::Compiler compiler;
17 SkSL::Program::Settings settings;
Ethan Nicholas00543112018-07-31 09:44:36 -040018 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
19 SkSL::Program::kPipelineStage_Kind,
20 SkSL::String(src), settings);
Ethan Nicholas26a9aad2018-03-27 14:10:52 -040021 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
32DEF_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
41DEF_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
50DEF_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
60DEF_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
70DEF_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
75DEF_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
80DEF_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
87DEF_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
94DEF_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
103DEF_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
112DEF_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