float4_t.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright 2010-2013 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_int32(const char* _str, bx::float4_t _a, int32_t _0, int32_t _1, int32_t _2, int32_t _3)
  18. {
  19. float4_cast c; c.f4 = _a;
  20. DBG("%s (%d, %d, %d, %d) == (%d, %d, %d, %d)"
  21. , _str
  22. , c.i[0], c.i[1], c.i[2], c.i[3]
  23. , _0, _1, _2, _3
  24. );
  25. CHECK_EQUAL(c.i[0], _0);
  26. CHECK_EQUAL(c.i[1], _1);
  27. CHECK_EQUAL(c.i[2], _2);
  28. CHECK_EQUAL(c.i[3], _3);
  29. }
  30. void float4_check_uint32(const char* _str, bx::float4_t _a, uint32_t _0, uint32_t _1, uint32_t _2, uint32_t _3)
  31. {
  32. float4_cast c; c.f4 = _a;
  33. DBG("%s (0x%08x, 0x%08x, 0x%08x, 0x%08x) == (0x%08x, 0x%08x, 0x%08x, 0x%08x)"
  34. , _str
  35. , c.ui[0], c.ui[1], c.ui[2], c.ui[3]
  36. , _0, _1, _2, _3
  37. );
  38. CHECK_EQUAL(c.ui[0], _0);
  39. CHECK_EQUAL(c.ui[1], _1);
  40. CHECK_EQUAL(c.ui[2], _2);
  41. CHECK_EQUAL(c.ui[3], _3);
  42. }
  43. void float4_check_float(const char* _str, bx::float4_t _a, float _0, float _1, float _2, float _3)
  44. {
  45. float4_cast c; c.f4 = _a;
  46. DBG("%s (%f, %f, %f, %f) == (%f, %f, %f, %f)"
  47. , _str
  48. , c.f[0], c.f[1], c.f[2], c.f[3]
  49. , _0, _1, _2, _3
  50. );
  51. CHECK_EQUAL(c.f[0], _0);
  52. CHECK_EQUAL(c.f[1], _1);
  53. CHECK_EQUAL(c.f[2], _2);
  54. CHECK_EQUAL(c.f[3], _3);
  55. }
  56. void float4_check_string(const char* _str, bx::float4_t _a)
  57. {
  58. float4_cast c; c.f4 = _a;
  59. const char test[5] = { c.c[0], c.c[4], c.c[8], c.c[12], '\0' };
  60. DBG("%s %s", _str, test);
  61. CHECK(0 == strcmp(_str, test) );
  62. }
  63. TEST(float4_swizzle)
  64. {
  65. const float4_t xyzw = float4_ild(0x78787878, 0x79797979, 0x7a7a7a7a, 0x77777777);
  66. #define ELEMx 0
  67. #define ELEMy 1
  68. #define ELEMz 2
  69. #define ELEMw 3
  70. #define IMPLEMENT_SWIZZLE(_x, _y, _z, _w) \
  71. float4_check_string("" #_x #_y #_z #_w "", float4_swiz_##_x##_y##_z##_w(xyzw) ); \
  72. #include <bx/float4_swizzle.inl>
  73. #undef IMPLEMENT_SWIZZLE
  74. #undef ELEMw
  75. #undef ELEMz
  76. #undef ELEMy
  77. #undef ELEMx
  78. }
  79. TEST(float4_shuffle)
  80. {
  81. const float4_t xyzw = float4_ild(0x78787878, 0x79797979, 0x7a7a7a7a, 0x77777777);
  82. const float4_t ABCD = float4_ild(0x41414141, 0x42424242, 0x43434343, 0x44444444);
  83. float4_check_string("xyAB", float4_shuf_xyAB(xyzw, ABCD) );
  84. float4_check_string("ABxy", float4_shuf_ABxy(xyzw, ABCD) );
  85. float4_check_string("zwCD", float4_shuf_zwCD(xyzw, ABCD) );
  86. float4_check_string("CDzw", float4_shuf_CDzw(xyzw, ABCD) );
  87. float4_check_string("xAyB", float4_shuf_xAyB(xyzw, ABCD) );
  88. float4_check_string("zCwD", float4_shuf_zCwD(xyzw, ABCD) );
  89. float4_check_string("xAzC", float4_shuf_xAzC(xyzw, ABCD) );
  90. float4_check_string("yBwD", float4_shuf_yBwD(xyzw, ABCD) );
  91. float4_check_string("CzDw", float4_shuf_CzDw(xyzw, ABCD) );
  92. }
  93. TEST(float4_compare)
  94. {
  95. float4_check_uint32("cmpeq"
  96. , float4_cmpeq(float4_ld(1.0f, 2.0f, 3.0f, 4.0f), float4_ld(0.0f, 2.0f, 0.0f, 3.0f) )
  97. , 0, -1, 0, 0
  98. );
  99. float4_check_uint32("cmplt"
  100. , float4_cmplt(float4_ld(1.0f, 2.0f, 3.0f, 4.0f), float4_ld(0.0f, 2.0f, 0.0f, 3.0f) )
  101. , 0, 0, 0, 0
  102. );
  103. float4_check_uint32("cmple"
  104. , float4_cmple(float4_ld(1.0f, 2.0f, 3.0f, 4.0f), float4_ld(0.0f, 2.0f, 0.0f, 3.0f) )
  105. , 0, -1, 0, 0
  106. );
  107. float4_check_uint32("cmpgt"
  108. , float4_cmpgt(float4_ld(1.0f, 2.0f, 3.0f, 4.0f), float4_ld(0.0f, 2.0f, 0.0f, 3.0f) )
  109. , -1, 0, -1, -1
  110. );
  111. float4_check_uint32("cmpge"
  112. , float4_cmpge(float4_ld(1.0f, 2.0f, 3.0f, 4.0f), float4_ld(0.0f, 2.0f, 0.0f, 3.0f) )
  113. , -1, -1, -1, -1
  114. );
  115. float4_check_uint32("icmpeq"
  116. , float4_icmpeq(float4_ild(0, 1, 2, 3), float4_ild(0, -2, 1, 3) )
  117. , -1, 0, 0, -1
  118. );
  119. float4_check_uint32("icmplt"
  120. , float4_icmplt(float4_ild(0, 1, 2, 3), float4_ild(0, -2, 1, 3) )
  121. , 0, 0, 0, 0
  122. );
  123. float4_check_uint32("icmpgt"
  124. , float4_icmpgt(float4_ild(0, 1, 2, 3), float4_ild(0, -2, 1, 3) )
  125. , 0, -1, -1, 0
  126. );
  127. }
  128. TEST(float4_load)
  129. {
  130. float4_check_float("ld"
  131. , float4_ld(0.0f, 1.0f, 2.0f, 3.0f)
  132. , 0.0f, 1.0f, 2.0f, 3.0f
  133. );
  134. float4_check_int32("ild"
  135. , float4_ild(-1, 0, 1, 2)
  136. , -1, 0, 1, 2
  137. );
  138. float4_check_int32("ild"
  139. , float4_ild(-1, -2, -3, -4)
  140. , -1, -2, -3, -4
  141. );
  142. float4_check_uint32("zero", float4_zero()
  143. , 0, 0, 0, 0
  144. );
  145. float4_check_uint32("isplat", float4_isplat(0x80000001)
  146. , 0x80000001, 0x80000001, 0x80000001, 0x80000001
  147. );
  148. float4_check_float("isplat", float4_splat(1.0f)
  149. , 1.0f, 1.0f, 1.0f, 1.0f
  150. );
  151. }
  152. TEST(float4)
  153. {
  154. const float4_t isplat = float4_isplat(0x80000001);
  155. float4_check_uint32("sll"
  156. , float4_sll(isplat, 1)
  157. , 0x00000002, 0x00000002, 0x00000002, 0x00000002
  158. );
  159. float4_check_uint32("srl"
  160. , float4_srl(isplat, 1)
  161. , 0x40000000, 0x40000000, 0x40000000, 0x40000000
  162. );
  163. float4_check_uint32("sra"
  164. , float4_sra(isplat, 1)
  165. , 0xc0000000, 0xc0000000, 0xc0000000, 0xc0000000
  166. );
  167. float4_check_uint32("and"
  168. , float4_and(float4_isplat(0x55555555), float4_isplat(0xaaaaaaaa) )
  169. , 0, 0, 0, 0
  170. );
  171. float4_check_uint32("or "
  172. , float4_or(float4_isplat(0x55555555), float4_isplat(0xaaaaaaaa) )
  173. , -1, -1, -1, -1
  174. );
  175. float4_check_uint32("xor"
  176. , float4_or(float4_isplat(0x55555555), float4_isplat(0xaaaaaaaa) )
  177. , -1, -1, -1, -1
  178. );
  179. float4_check_int32("imin"
  180. , float4_imin(float4_ild(0, 1, 2, 3), float4_ild(-1, 2, -2, 1) )
  181. , -1, 1, -2, 1
  182. );
  183. float4_check_float("min"
  184. , float4_min(float4_ld(0.0f, 1.0f, 2.0f, 3.0f), float4_ld(-1.0f, 2.0f, -2.0f, 1.0f) )
  185. , -1.0f, 1.0f, -2.0f, 1.0f
  186. );
  187. float4_check_int32("imax"
  188. , float4_imax(float4_ild(0, 1, 2, 3), float4_ild(-1, 2, -2, 1) )
  189. , 0, 2, 2, 3
  190. );
  191. float4_check_float("max"
  192. , float4_max(float4_ld(0.0f, 1.0f, 2.0f, 3.0f), float4_ld(-1.0f, 2.0f, -2.0f, 1.0f) )
  193. , 0.0f, 2.0f, 2.0f, 3.0f
  194. );
  195. }