You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

329 lines
9.0 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cppunit/extensions/HelperMacros.h>
  14. #include <cppunit/TestCaller.h>
  15. #include <cppunit/TestCase.h>
  16. #include <cppunit/TestSuite.h>
  17. #include <cmath>
  18. #include "core.h"
  19. /* This will not work with aggressive optimisation, but a reasonable
  20. * assumption is that such environments do have a proper isnan(). */
  21. #if !defined isnan
  22. # define isnan(x) (!((x) == (x)))
  23. #endif
  24. namespace lol
  25. {
  26. class HalfTest : public CppUnit::TestCase
  27. {
  28. CPPUNIT_TEST_SUITE(HalfTest);
  29. CPPUNIT_TEST(test_half_from_float);
  30. CPPUNIT_TEST(test_half_makeaccurate);
  31. CPPUNIT_TEST(test_half_makebits);
  32. CPPUNIT_TEST(test_half_is_nan);
  33. CPPUNIT_TEST(test_half_is_inf);
  34. CPPUNIT_TEST(test_half_is_finite);
  35. CPPUNIT_TEST(test_half_is_normal);
  36. CPPUNIT_TEST(test_half_classify);
  37. CPPUNIT_TEST(test_half_to_float);
  38. CPPUNIT_TEST(test_half_to_int);
  39. CPPUNIT_TEST(test_float_op_half);
  40. CPPUNIT_TEST(test_half_op_float);
  41. CPPUNIT_TEST_SUITE_END();
  42. public:
  43. HalfTest() : CppUnit::TestCase("Matrix Test") {}
  44. void setUp()
  45. {
  46. }
  47. void tearDown() {}
  48. void test_half_from_float()
  49. {
  50. for (size_t i = 0; i < sizeof(pairs) / sizeof(*pairs); i++)
  51. {
  52. half a = (half)pairs[i].f;
  53. uint16_t b = pairs[i].x;
  54. CPPUNIT_ASSERT_EQUAL(a.bits, b);
  55. }
  56. }
  57. void test_half_makeaccurate()
  58. {
  59. for (size_t i = 0; i < sizeof(pairs) / sizeof(*pairs); i++)
  60. {
  61. half a = half::makeaccurate(pairs[i].f);
  62. uint16_t b = pairs[i].x;
  63. CPPUNIT_ASSERT_EQUAL(a.bits, b);
  64. }
  65. }
  66. void test_half_makebits()
  67. {
  68. for (unsigned int i = 0; i < 0x10000; i++)
  69. {
  70. half a = half::makebits(i);
  71. uint16_t b = i;
  72. CPPUNIT_ASSERT_EQUAL(a.bits, b);
  73. }
  74. }
  75. void test_half_is_nan()
  76. {
  77. CPPUNIT_ASSERT(half::makebits(0x7c01).is_nan());
  78. CPPUNIT_ASSERT(half::makebits(0xfc01).is_nan());
  79. CPPUNIT_ASSERT(half::makebits(0x7e00).is_nan());
  80. CPPUNIT_ASSERT(half::makebits(0xfe00).is_nan());
  81. CPPUNIT_ASSERT(!half::makebits(0x7c00).is_nan());
  82. CPPUNIT_ASSERT(!half::makebits(0xfc00).is_nan());
  83. CPPUNIT_ASSERT(!half(0.0f).is_nan());
  84. CPPUNIT_ASSERT(!half(-0.0f).is_nan());
  85. CPPUNIT_ASSERT(!half(2.0f).is_nan());
  86. CPPUNIT_ASSERT(!half(-2.0f).is_nan());
  87. }
  88. void test_half_is_inf()
  89. {
  90. CPPUNIT_ASSERT(half(65536.0f).is_inf());
  91. CPPUNIT_ASSERT(half(-65536.0f).is_inf());
  92. CPPUNIT_ASSERT(!half(0.0f).is_inf());
  93. CPPUNIT_ASSERT(!half(-0.0f).is_inf());
  94. CPPUNIT_ASSERT(!half(65535.0f).is_inf());
  95. CPPUNIT_ASSERT(!half(-65535.0f).is_inf());
  96. CPPUNIT_ASSERT(half::makebits(0x7c00).is_inf());
  97. CPPUNIT_ASSERT(half::makebits(0xfc00).is_inf());
  98. CPPUNIT_ASSERT(!half::makebits(0x7e00).is_inf());
  99. CPPUNIT_ASSERT(!half::makebits(0xfe00).is_inf());
  100. }
  101. void test_half_is_finite()
  102. {
  103. CPPUNIT_ASSERT(half(0.0f).is_finite());
  104. CPPUNIT_ASSERT(half(-0.0f).is_finite());
  105. CPPUNIT_ASSERT(half(65535.0f).is_finite());
  106. CPPUNIT_ASSERT(half(-65535.0f).is_finite());
  107. CPPUNIT_ASSERT(!half(65536.0f).is_finite());
  108. CPPUNIT_ASSERT(!half(-65536.0f).is_finite());
  109. CPPUNIT_ASSERT(!half::makebits(0x7c00).is_finite());
  110. CPPUNIT_ASSERT(!half::makebits(0xfc00).is_finite());
  111. CPPUNIT_ASSERT(!half::makebits(0x7e00).is_finite());
  112. CPPUNIT_ASSERT(!half::makebits(0xfe00).is_finite());
  113. }
  114. void test_half_is_normal()
  115. {
  116. CPPUNIT_ASSERT(half(0.0f).is_normal());
  117. CPPUNIT_ASSERT(half(-0.0f).is_normal());
  118. CPPUNIT_ASSERT(half(65535.0f).is_normal());
  119. CPPUNIT_ASSERT(half(-65535.0f).is_normal());
  120. CPPUNIT_ASSERT(!half(65536.0f).is_normal());
  121. CPPUNIT_ASSERT(!half(-65536.0f).is_normal());
  122. CPPUNIT_ASSERT(!half::makebits(0x7c00).is_normal());
  123. CPPUNIT_ASSERT(!half::makebits(0xfc00).is_normal());
  124. CPPUNIT_ASSERT(!half::makebits(0x7e00).is_normal());
  125. CPPUNIT_ASSERT(!half::makebits(0xfe00).is_normal());
  126. }
  127. void test_half_classify()
  128. {
  129. for (uint32_t i = 0; i < 0x10000; i++)
  130. {
  131. half h = half::makebits(i);
  132. if (h.is_nan())
  133. {
  134. CPPUNIT_ASSERT(!h.is_inf());
  135. CPPUNIT_ASSERT(!h.is_normal());
  136. CPPUNIT_ASSERT(!h.is_finite());
  137. }
  138. else if (h.is_inf())
  139. {
  140. CPPUNIT_ASSERT(!h.is_normal());
  141. CPPUNIT_ASSERT(!h.is_finite());
  142. }
  143. else
  144. {
  145. CPPUNIT_ASSERT(h.is_finite());
  146. }
  147. }
  148. }
  149. void test_half_to_float()
  150. {
  151. for (size_t i = 0; i < sizeof(pairs) / sizeof(*pairs); i++)
  152. {
  153. float a = (float)half::makebits(pairs[i].x);
  154. float b = pairs[i].f;
  155. CPPUNIT_ASSERT_EQUAL(a, b);
  156. }
  157. for (uint32_t i = 0; i < 0x10000; i++)
  158. {
  159. half h = half::makebits(i);
  160. float f = (float)h;
  161. half g = (half)f;
  162. if (h.is_nan())
  163. {
  164. CPPUNIT_ASSERT(isnan(f));
  165. CPPUNIT_ASSERT(g.is_nan());
  166. }
  167. else
  168. {
  169. CPPUNIT_ASSERT(!isnan(f));
  170. CPPUNIT_ASSERT_EQUAL(g.bits, h.bits);
  171. }
  172. }
  173. }
  174. void test_half_to_int()
  175. {
  176. CPPUNIT_ASSERT_EQUAL((int)(half)(0.0f), 0);
  177. CPPUNIT_ASSERT_EQUAL((int)(half)(-0.0f), 0);
  178. CPPUNIT_ASSERT_EQUAL((int)(half)(0.9f), 0);
  179. CPPUNIT_ASSERT_EQUAL((int)(half)(-0.9f), 0);
  180. CPPUNIT_ASSERT_EQUAL((int)(half)(1.0f), 1);
  181. CPPUNIT_ASSERT_EQUAL((int)(half)(-1.0f), -1);
  182. CPPUNIT_ASSERT_EQUAL((int)(half)(1.9f), 1);
  183. CPPUNIT_ASSERT_EQUAL((int)(half)(-1.9f), -1);
  184. CPPUNIT_ASSERT_EQUAL((int)(half)(65504.0f), 65504);
  185. CPPUNIT_ASSERT_EQUAL((int)(half)(-65504.0f), -65504);
  186. }
  187. void test_float_op_half()
  188. {
  189. half zero = 0;
  190. half one = 1;
  191. half two = 2;
  192. float a = zero + one;
  193. CPPUNIT_ASSERT_EQUAL(1.0f, a);
  194. a += zero;
  195. CPPUNIT_ASSERT_EQUAL(1.0f, a);
  196. a -= zero;
  197. CPPUNIT_ASSERT_EQUAL(1.0f, a);
  198. a *= one;
  199. CPPUNIT_ASSERT_EQUAL(1.0f, a);
  200. a /= one;
  201. CPPUNIT_ASSERT_EQUAL(1.0f, a);
  202. float b = one + zero;
  203. CPPUNIT_ASSERT_EQUAL(1.0f, b);
  204. b += one;
  205. CPPUNIT_ASSERT_EQUAL(2.0f, b);
  206. b *= two;
  207. CPPUNIT_ASSERT_EQUAL(4.0f, b);
  208. b -= two;
  209. CPPUNIT_ASSERT_EQUAL(2.0f, b);
  210. b /= two;
  211. CPPUNIT_ASSERT_EQUAL(1.0f, b);
  212. float c = one - zero;
  213. CPPUNIT_ASSERT_EQUAL(1.0f, c);
  214. float d = two - one;
  215. CPPUNIT_ASSERT_EQUAL(1.0f, d);
  216. float e = two + (-one);
  217. CPPUNIT_ASSERT_EQUAL(1.0f, e);
  218. float f = (two * two) / (one + one);
  219. CPPUNIT_ASSERT_EQUAL(2.0f, f);
  220. }
  221. void test_half_op_float()
  222. {
  223. half zero = 0;
  224. half one = 1;
  225. half two = 2;
  226. half four = 4;
  227. half a = one + 0.0f;
  228. CPPUNIT_ASSERT_EQUAL(one.bits, a.bits);
  229. a += 0.0f;
  230. CPPUNIT_ASSERT_EQUAL(one.bits, a.bits);
  231. a -= 0.0f;
  232. CPPUNIT_ASSERT_EQUAL(one.bits, a.bits);
  233. a *= 1.0f;
  234. CPPUNIT_ASSERT_EQUAL(one.bits, a.bits);
  235. a /= 1.0f;
  236. CPPUNIT_ASSERT_EQUAL(one.bits, a.bits);
  237. half b = one + 0.0f;
  238. CPPUNIT_ASSERT_EQUAL(one.bits, b.bits);
  239. b += 1.0f;
  240. CPPUNIT_ASSERT_EQUAL(two.bits, b.bits);
  241. b *= 2.0f;
  242. CPPUNIT_ASSERT_EQUAL(four.bits, b.bits);
  243. b -= 2.0f;
  244. CPPUNIT_ASSERT_EQUAL(two.bits, b.bits);
  245. b /= 2.0f;
  246. CPPUNIT_ASSERT_EQUAL(one.bits, b.bits);
  247. half c = 1.0f - zero;
  248. CPPUNIT_ASSERT_EQUAL(one.bits, c.bits);
  249. half d = 2.0f - one;
  250. CPPUNIT_ASSERT_EQUAL(one.bits, d.bits);
  251. half e = 2.0f + (-one);
  252. CPPUNIT_ASSERT_EQUAL(one.bits, e.bits);
  253. half f = (2.0f * two) / (1.0f + one);
  254. CPPUNIT_ASSERT_EQUAL(two.bits, f.bits);
  255. }
  256. private:
  257. struct TestPair { float f; uint16_t x; };
  258. static TestPair const pairs[11];
  259. };
  260. HalfTest::TestPair const HalfTest::pairs[] =
  261. {
  262. /* All these values have exact half representations */
  263. { 0.0f, 0x0000 },
  264. { -0.0f, 0x8000 }, /* negative zero */
  265. { 1.0f, 0x3c00 },
  266. { -1.0f, 0xbc00 },
  267. { 2.0f, 0x4000 },
  268. { 0.5f, 0x3800 },
  269. { 0.125f, 0x3000 },
  270. { 15.9375f, 0x4bf8 },
  271. { 0x1.fp-10, 0x17c0 },
  272. { 0x1.fp-14, 0x07c0 }, /* denormal */
  273. { 0x1.fp-15, 0x03e0 }, /* denormal */
  274. };
  275. CPPUNIT_TEST_SUITE_REGISTRATION(HalfTest);
  276. } /* namespace lol */