SkVxTest.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright 2019 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 "include/private/SkVx.h"
  8. #include "tests/Test.h"
  9. using float2 = skvx::Vec<2,float>;
  10. using float4 = skvx::Vec<4,float>;
  11. using float8 = skvx::Vec<8,float>;
  12. using double2 = skvx::Vec<2,double>;
  13. using double4 = skvx::Vec<4,double>;
  14. using double8 = skvx::Vec<8,double>;
  15. using byte2 = skvx::Vec< 2,uint8_t>;
  16. using byte4 = skvx::Vec< 4,uint8_t>;
  17. using byte8 = skvx::Vec< 8,uint8_t>;
  18. using byte16 = skvx::Vec<16,uint8_t>;
  19. using int2 = skvx::Vec<2,int32_t>;
  20. using int4 = skvx::Vec<4,int32_t>;
  21. using int8 = skvx::Vec<8,int32_t>;
  22. using long2 = skvx::Vec<2,int64_t>;
  23. using long4 = skvx::Vec<4,int64_t>;
  24. using long8 = skvx::Vec<8,int64_t>;
  25. // These are unused, and just here so I can look at the disassembly.
  26. float2 Sqrt(float2 x) { return sqrt(x); }
  27. float4 Sqrt(float4 x) { return sqrt(x); }
  28. float8 Sqrt(float8 x) { return sqrt(x); }
  29. float4 RSqrt(float4 x) { return rsqrt(x); }
  30. float4 Rcp(float4 x) { return rcp(x); }
  31. float4 Ceil(float4 x) { return ceil(x); }
  32. float4 Floor(float4 x) { return floor(x); }
  33. float4 Trunc(float4 x) { return trunc(x); }
  34. float4 Round(float4 x) { return round(x); }
  35. float4 Abs(float4 x) { return abs(x); }
  36. float4 Min(float4 x, float4 y) { return min(x,y); }
  37. float4 Max(float4 x, float4 y) { return max(x,y); }
  38. float4 IfThenElse(int4 c, float4 t, float4 e) { return if_then_else(c,t,e); }
  39. DEF_TEST(SkVx, r) {
  40. static_assert(sizeof(float2) == 8, "");
  41. static_assert(sizeof(float4) == 16, "");
  42. static_assert(sizeof(float8) == 32, "");
  43. static_assert(sizeof(byte2) == 2, "");
  44. static_assert(sizeof(byte4) == 4, "");
  45. static_assert(sizeof(byte8) == 8, "");
  46. {
  47. int4 mask = float4{1,2,3,4} < float4{1,2,4,8};
  48. REPORTER_ASSERT(r, mask[0] == int32_t( 0));
  49. REPORTER_ASSERT(r, mask[1] == int32_t( 0));
  50. REPORTER_ASSERT(r, mask[2] == int32_t(-1));
  51. REPORTER_ASSERT(r, mask[3] == int32_t(-1));
  52. REPORTER_ASSERT(r, any(mask));
  53. REPORTER_ASSERT(r, !all(mask));
  54. }
  55. {
  56. long4 mask = double4{1,2,3,4} < double4{1,2,4,8};
  57. REPORTER_ASSERT(r, mask[0] == int64_t( 0));
  58. REPORTER_ASSERT(r, mask[1] == int64_t( 0));
  59. REPORTER_ASSERT(r, mask[2] == int64_t(-1));
  60. REPORTER_ASSERT(r, mask[3] == int64_t(-1));
  61. REPORTER_ASSERT(r, any(mask));
  62. REPORTER_ASSERT(r, !all(mask));
  63. }
  64. REPORTER_ASSERT(r, min(float4{1,2,3,4}) == 1);
  65. REPORTER_ASSERT(r, max(float4{1,2,3,4}) == 4);
  66. REPORTER_ASSERT(r, all(int4{1,2,3,4,5} == int4{1,2,3,4}));
  67. REPORTER_ASSERT(r, all(int4{1,2,3,4} == int4{1,2,3,4}));
  68. REPORTER_ASSERT(r, all(int4{1,2,3} == int4{1,2,3,0}));
  69. REPORTER_ASSERT(r, all(int4{1,2} == int4{1,2,0,0}));
  70. REPORTER_ASSERT(r, all(int4{1} == int4{1,0,0,0}));
  71. REPORTER_ASSERT(r, all(int4(1) == int4{1,1,1,1}));
  72. REPORTER_ASSERT(r, all(int4{} == int4{0,0,0,0}));
  73. REPORTER_ASSERT(r, all(int4() == int4{0,0,0,0}));
  74. REPORTER_ASSERT(r, all(int4{1,2,2,1} == min(int4{1,2,3,4}, int4{4,3,2,1})));
  75. REPORTER_ASSERT(r, all(int4{4,3,3,4} == max(int4{1,2,3,4}, int4{4,3,2,1})));
  76. REPORTER_ASSERT(r, all(if_then_else(float4{1,2,3,2} <= float4{2,2,2,2}, float4(42), float4(47))
  77. == float4{42,42,47,42}));
  78. REPORTER_ASSERT(r, all(floor(float4{-1.5f,1.5f,1.0f,-1.0f}) == float4{-2.0f,1.0f,1.0f,-1.0f}));
  79. REPORTER_ASSERT(r, all( ceil(float4{-1.5f,1.5f,1.0f,-1.0f}) == float4{-1.0f,2.0f,1.0f,-1.0f}));
  80. REPORTER_ASSERT(r, all(trunc(float4{-1.5f,1.5f,1.0f,-1.0f}) == float4{-1.0f,1.0f,1.0f,-1.0f}));
  81. REPORTER_ASSERT(r, all(round(float4{-1.5f,1.5f,1.0f,-1.0f}) == float4{-2.0f,2.0f,1.0f,-1.0f}));
  82. REPORTER_ASSERT(r, all(abs(float4{-2,-1,0,1}) == float4{2,1,0,1}));
  83. // TODO(mtklein): these tests could be made less loose.
  84. REPORTER_ASSERT(r, all( sqrt(float4{2,3,4,5}) < float4{2,2,3,3}));
  85. REPORTER_ASSERT(r, all( rcp(float4{2,3,4,5}) < float4{1.0f,0.5f,0.5f,0.3f}));
  86. REPORTER_ASSERT(r, all(rsqrt(float4{2,3,4,5}) < float4{1.0f,1.0f,1.0f,0.5f}));
  87. REPORTER_ASSERT(r, all( sqrt(float2{2,3}) < float2{2,2}));
  88. REPORTER_ASSERT(r, all( rcp(float2{2,3}) < float2{1.0f,0.5f}));
  89. REPORTER_ASSERT(r, all(rsqrt(float2{2,3}) < float2{1.0f,1.0f}));
  90. REPORTER_ASSERT(r, all(skvx::cast<int>(float4{-1.5f,0.5f,1.0f,1.5f}) == int4{-1,0,1,1}));
  91. float buf[] = {1,2,3,4,5,6};
  92. REPORTER_ASSERT(r, all(float4::Load(buf) == float4{1,2,3,4}));
  93. float4{2,3,4,5}.store(buf);
  94. REPORTER_ASSERT(r, buf[0] == 2
  95. && buf[1] == 3
  96. && buf[2] == 4
  97. && buf[3] == 5
  98. && buf[4] == 5
  99. && buf[5] == 6);
  100. REPORTER_ASSERT(r, all(float4::Load(buf+0) == float4{2,3,4,5}));
  101. REPORTER_ASSERT(r, all(float4::Load(buf+2) == float4{4,5,5,6}));
  102. REPORTER_ASSERT(r, all(mad(float4{1,2,3,4}, 2.0f, 3.0f) == float4{5,7,9,11}));
  103. REPORTER_ASSERT(r, all(skvx::shuffle<2,1,0,3> (float4{1,2,3,4}) == float4{3,2,1,4}));
  104. REPORTER_ASSERT(r, all(skvx::shuffle<2,1> (float4{1,2,3,4}) == float2{3,2}));
  105. REPORTER_ASSERT(r, all(skvx::shuffle<3,3,3,3> (float4{1,2,3,4}) == float4{4,4,4,4}));
  106. REPORTER_ASSERT(r, all(skvx::shuffle<2,1,2,1,2,1,2,1>(float4{1,2,3,4})
  107. == float8{3,2,3,2,3,2,3,2}));
  108. // Test that mixed types can be used where they make sense. Mostly about ergonomics.
  109. REPORTER_ASSERT(r, all(float4{1,2,3,4} < 5));
  110. REPORTER_ASSERT(r, all( byte4{1,2,3,4} < 5));
  111. REPORTER_ASSERT(r, all( int4{1,2,3,4} < 5.0f));
  112. float4 five = 5;
  113. REPORTER_ASSERT(r, all(five == 5.0f));
  114. REPORTER_ASSERT(r, all(five == 5));
  115. REPORTER_ASSERT(r, all(max(2, min(float4{1,2,3,4}, 3)) == float4{2,2,3,3}));
  116. for (int x = 0; x < 256; x++)
  117. for (int y = 0; y < 256; y++) {
  118. uint8_t want = (uint8_t)( 255*(x/255.0 * y/255.0) + 0.5 );
  119. {
  120. uint8_t got = skvx::div255(skvx::Vec<8, uint16_t>(x) *
  121. skvx::Vec<8, uint16_t>(y) )[0];
  122. REPORTER_ASSERT(r, got == want);
  123. }
  124. {
  125. uint8_t got = skvx::approx_scale(skvx::Vec<8,uint8_t>(x),
  126. skvx::Vec<8,uint8_t>(y))[0];
  127. REPORTER_ASSERT(r, got == want-1 ||
  128. got == want ||
  129. got == want+1);
  130. if (x == 0 || y == 0 || x == 255 || y == 255) {
  131. REPORTER_ASSERT(r, got == want);
  132. }
  133. }
  134. }
  135. for (int x = 0; x < 256; x++)
  136. for (int y = 0; y < 256; y++) {
  137. uint16_t xy = x*y;
  138. // Make sure to cover implementation cases N=8, N<8, and N>8.
  139. REPORTER_ASSERT(r, all(mull(byte2 (x), byte2 (y)) == xy));
  140. REPORTER_ASSERT(r, all(mull(byte4 (x), byte4 (y)) == xy));
  141. REPORTER_ASSERT(r, all(mull(byte8 (x), byte8 (y)) == xy));
  142. REPORTER_ASSERT(r, all(mull(byte16(x), byte16(y)) == xy));
  143. }
  144. }