float4_t.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright 2010-2014 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "test.h"
  6. #include <bx/float4_t.h>
  7. #include <string.h>
  8. using namespace bx;
  9. union float4_cast
  10. {
  11. bx::float4_t f4;
  12. float f[4];
  13. uint32_t ui[4];
  14. int32_t i[4];
  15. char c[16];
  16. };
  17. void float4_check_bool(const char* _str, bool _a, bool _0)
  18. {
  19. DBG("%s %d == %d"
  20. , _str
  21. , _a
  22. , _0
  23. );
  24. CHECK_EQUAL(_a, _0);
  25. }
  26. void float4_check_int32(const char* _str, bx::float4_t _a, int32_t _0, int32_t _1, int32_t _2, int32_t _3)
  27. {
  28. float4_cast c; c.f4 = _a;
  29. DBG("%s (%d, %d, %d, %d) == (%d, %d, %d, %d)"
  30. , _str
  31. , c.i[0], c.i[1], c.i[2], c.i[3]
  32. , _0, _1, _2, _3
  33. );
  34. CHECK_EQUAL(c.i[0], _0);
  35. CHECK_EQUAL(c.i[1], _1);
  36. CHECK_EQUAL(c.i[2], _2);
  37. CHECK_EQUAL(c.i[3], _3);
  38. }
  39. void float4_check_uint32(const char* _str, bx::float4_t _a, uint32_t _0, uint32_t _1, uint32_t _2, uint32_t _3)
  40. {
  41. float4_cast c; c.f4 = _a;
  42. DBG("%s (0x%08x, 0x%08x, 0x%08x, 0x%08x) == (0x%08x, 0x%08x, 0x%08x, 0x%08x)"
  43. , _str
  44. , c.ui[0], c.ui[1], c.ui[2], c.ui[3]
  45. , _0, _1, _2, _3
  46. );
  47. CHECK_EQUAL(c.ui[0], _0);
  48. CHECK_EQUAL(c.ui[1], _1);
  49. CHECK_EQUAL(c.ui[2], _2);
  50. CHECK_EQUAL(c.ui[3], _3);
  51. }
  52. void float4_check_float(const char* _str, bx::float4_t _a, float _0, float _1, float _2, float _3)
  53. {
  54. float4_cast c; c.f4 = _a;
  55. DBG("%s (%f, %f, %f, %f) == (%f, %f, %f, %f)"
  56. , _str
  57. , c.f[0], c.f[1], c.f[2], c.f[3]
  58. , _0, _1, _2, _3
  59. );
  60. CHECK_EQUAL(c.f[0], _0);
  61. CHECK_EQUAL(c.f[1], _1);
  62. CHECK_EQUAL(c.f[2], _2);
  63. CHECK_EQUAL(c.f[3], _3);
  64. }
  65. void float4_check_string(const char* _str, bx::float4_t _a)
  66. {
  67. float4_cast c; c.f4 = _a;
  68. const char test[5] = { c.c[0], c.c[4], c.c[8], c.c[12], '\0' };
  69. DBG("%s %s", _str, test);
  70. CHECK(0 == strcmp(_str, test) );
  71. }
  72. TEST(float4_swizzle)
  73. {
  74. const float4_t xyzw = float4_ild(0x78787878, 0x79797979, 0x7a7a7a7a, 0x77777777);
  75. #define ELEMx 0
  76. #define ELEMy 1
  77. #define ELEMz 2
  78. #define ELEMw 3
  79. #define IMPLEMENT_SWIZZLE(_x, _y, _z, _w) \
  80. float4_check_string("" #_x #_y #_z #_w "", float4_swiz_##_x##_y##_z##_w(xyzw) ); \
  81. #include <bx/float4_swizzle.inl>
  82. #undef IMPLEMENT_SWIZZLE
  83. #undef ELEMw
  84. #undef ELEMz
  85. #undef ELEMy
  86. #undef ELEMx
  87. }
  88. TEST(float4_shuffle)
  89. {
  90. const float4_t xyzw = float4_ild(0x78787878, 0x79797979, 0x7a7a7a7a, 0x77777777);
  91. const float4_t ABCD = float4_ild(0x41414141, 0x42424242, 0x43434343, 0x44444444);
  92. float4_check_string("xyAB", float4_shuf_xyAB(xyzw, ABCD) );
  93. float4_check_string("ABxy", float4_shuf_ABxy(xyzw, ABCD) );
  94. float4_check_string("zwCD", float4_shuf_zwCD(xyzw, ABCD) );
  95. float4_check_string("CDzw", float4_shuf_CDzw(xyzw, ABCD) );
  96. float4_check_string("xAyB", float4_shuf_xAyB(xyzw, ABCD) );
  97. float4_check_string("zCwD", float4_shuf_zCwD(xyzw, ABCD) );
  98. float4_check_string("xAzC", float4_shuf_xAzC(xyzw, ABCD) );
  99. float4_check_string("yBwD", float4_shuf_yBwD(xyzw, ABCD) );
  100. float4_check_string("CzDw", float4_shuf_CzDw(xyzw, ABCD) );
  101. }
  102. TEST(float4_compare)
  103. {
  104. float4_check_uint32("cmpeq"
  105. , float4_cmpeq(float4_ld(1.0f, 2.0f, 3.0f, 4.0f), float4_ld(0.0f, 2.0f, 0.0f, 3.0f) )
  106. , 0, -1, 0, 0
  107. );
  108. float4_check_uint32("cmplt"
  109. , float4_cmplt(float4_ld(1.0f, 2.0f, 3.0f, 4.0f), float4_ld(0.0f, 2.0f, 0.0f, 3.0f) )
  110. , 0, 0, 0, 0
  111. );
  112. float4_check_uint32("cmple"
  113. , float4_cmple(float4_ld(1.0f, 2.0f, 3.0f, 4.0f), float4_ld(0.0f, 2.0f, 0.0f, 3.0f) )
  114. , 0, -1, 0, 0
  115. );
  116. float4_check_uint32("cmpgt"
  117. , float4_cmpgt(float4_ld(1.0f, 2.0f, 3.0f, 4.0f), float4_ld(0.0f, 2.0f, 0.0f, 3.0f) )
  118. , -1, 0, -1, -1
  119. );
  120. float4_check_uint32("cmpge"
  121. , float4_cmpge(float4_ld(1.0f, 2.0f, 3.0f, 4.0f), float4_ld(0.0f, 2.0f, 0.0f, 3.0f) )
  122. , -1, -1, -1, -1
  123. );
  124. float4_check_uint32("icmpeq"
  125. , float4_icmpeq(float4_ild(0, 1, 2, 3), float4_ild(0, -2, 1, 3) )
  126. , -1, 0, 0, -1
  127. );
  128. float4_check_uint32("icmplt"
  129. , float4_icmplt(float4_ild(0, 1, 2, 3), float4_ild(0, -2, 1, 3) )
  130. , 0, 0, 0, 0
  131. );
  132. float4_check_uint32("icmpgt"
  133. , float4_icmpgt(float4_ild(0, 1, 2, 3), float4_ild(0, -2, 1, 3) )
  134. , 0, -1, -1, 0
  135. );
  136. }
  137. TEST(float4_test)
  138. {
  139. float4_check_bool("test_any_xyzw"
  140. , float4_test_any_xyzw(float4_ild(-1, 0, 0, 0) )
  141. , true
  142. );
  143. float4_check_bool("test_all_xyzw"
  144. , float4_test_all_xyzw(float4_ild(-1, 0, -1, 0) )
  145. , false
  146. );
  147. float4_check_bool("test_all_xyzw"
  148. , float4_test_all_xyzw(float4_ild(-1, -1, -1, -1) )
  149. , true
  150. );
  151. float4_check_bool("test_all_xw"
  152. , float4_test_all_xw(float4_ild(-1, 0, 0, -1) )
  153. , true
  154. );
  155. float4_check_bool("test_all_xzw"
  156. , float4_test_all_xzw(float4_ild(-1, 0, 0, -1) )
  157. , false
  158. );
  159. }
  160. TEST(float4_load)
  161. {
  162. float4_check_float("ld"
  163. , float4_ld(0.0f, 1.0f, 2.0f, 3.0f)
  164. , 0.0f, 1.0f, 2.0f, 3.0f
  165. );
  166. float4_check_int32("ild"
  167. , float4_ild(-1, 0, 1, 2)
  168. , -1, 0, 1, 2
  169. );
  170. float4_check_int32("ild"
  171. , float4_ild(-1, -2, -3, -4)
  172. , -1, -2, -3, -4
  173. );
  174. float4_check_uint32("zero", float4_zero()
  175. , 0, 0, 0, 0
  176. );
  177. float4_check_uint32("isplat", float4_isplat(0x80000001)
  178. , 0x80000001, 0x80000001, 0x80000001, 0x80000001
  179. );
  180. float4_check_float("isplat", float4_splat(1.0f)
  181. , 1.0f, 1.0f, 1.0f, 1.0f
  182. );
  183. }
  184. TEST(float4_arithmetic)
  185. {
  186. float4_check_float("madd"
  187. , float4_madd(float4_ld(0.0f, 1.0f, 2.0f, 3.0f), float4_ld(4.0f, 5.0f, 6.0f, 7.0f), float4_ld(8.0f, 9.0f, 10.0f, 11.0f) )
  188. , 8.0f, 14.0f, 22.0f, 32.0f
  189. );
  190. }
  191. TEST(float4)
  192. {
  193. const float4_t isplat = float4_isplat(0x80000001);
  194. float4_check_uint32("sll"
  195. , float4_sll(isplat, 1)
  196. , 0x00000002, 0x00000002, 0x00000002, 0x00000002
  197. );
  198. float4_check_uint32("srl"
  199. , float4_srl(isplat, 1)
  200. , 0x40000000, 0x40000000, 0x40000000, 0x40000000
  201. );
  202. float4_check_uint32("sra"
  203. , float4_sra(isplat, 1)
  204. , 0xc0000000, 0xc0000000, 0xc0000000, 0xc0000000
  205. );
  206. float4_check_uint32("and"
  207. , float4_and(float4_isplat(0x55555555), float4_isplat(0xaaaaaaaa) )
  208. , 0, 0, 0, 0
  209. );
  210. float4_check_uint32("or "
  211. , float4_or(float4_isplat(0x55555555), float4_isplat(0xaaaaaaaa) )
  212. , -1, -1, -1, -1
  213. );
  214. float4_check_uint32("xor"
  215. , float4_or(float4_isplat(0x55555555), float4_isplat(0xaaaaaaaa) )
  216. , -1, -1, -1, -1
  217. );
  218. float4_check_int32("imin"
  219. , float4_imin(float4_ild(0, 1, 2, 3), float4_ild(-1, 2, -2, 1) )
  220. , -1, 1, -2, 1
  221. );
  222. float4_check_float("min"
  223. , float4_min(float4_ld(0.0f, 1.0f, 2.0f, 3.0f), float4_ld(-1.0f, 2.0f, -2.0f, 1.0f) )
  224. , -1.0f, 1.0f, -2.0f, 1.0f
  225. );
  226. float4_check_int32("imax"
  227. , float4_imax(float4_ild(0, 1, 2, 3), float4_ild(-1, 2, -2, 1) )
  228. , 0, 2, 2, 3
  229. );
  230. float4_check_float("max"
  231. , float4_max(float4_ld(0.0f, 1.0f, 2.0f, 3.0f), float4_ld(-1.0f, 2.0f, -2.0f, 1.0f) )
  232. , 0.0f, 2.0f, 2.0f, 3.0f
  233. );
  234. }