SkSLJITTest.cpp 5.1 KB

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