Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

316 řádky
8.8 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 <cmath>
  14. #include "core.h"
  15. #include "lol/unit.h"
  16. /* Ensure isnan() is present even on systems that don't define it, or
  17. * when -ffast-math is being used. */
  18. #if defined __FAST_MATH__
  19. # undef isnan
  20. #endif
  21. #if !defined isnan
  22. static inline int isnan(float f)
  23. {
  24. union { float f; uint32_t x; } u = { f };
  25. return (u.x << 1) > 0xff000000u;
  26. }
  27. #endif
  28. namespace lol
  29. {
  30. LOLUNIT_FIXTURE(HalfTest)
  31. {
  32. public:
  33. void setUp() {}
  34. void tearDown() {}
  35. LOLUNIT_TEST(test_half_from_float)
  36. {
  37. for (size_t i = 0; i < sizeof(pairs) / sizeof(*pairs); i++)
  38. {
  39. half a = (half)pairs[i].f;
  40. uint16_t b = pairs[i].x;
  41. LOLUNIT_SET_CONTEXT(i);
  42. LOLUNIT_ASSERT_EQUAL(a.bits, b);
  43. }
  44. }
  45. LOLUNIT_TEST(test_half_makeaccurate)
  46. {
  47. for (size_t i = 0; i < sizeof(pairs) / sizeof(*pairs); i++)
  48. {
  49. half a = half::makeaccurate(pairs[i].f);
  50. uint16_t b = pairs[i].x;
  51. LOLUNIT_SET_CONTEXT(i);
  52. LOLUNIT_ASSERT_EQUAL(a.bits, b);
  53. }
  54. }
  55. LOLUNIT_TEST(test_half_makebits)
  56. {
  57. for (unsigned int i = 0; i < 0x10000; i++)
  58. {
  59. half a = half::makebits(i);
  60. uint16_t b = i;
  61. LOLUNIT_SET_CONTEXT(i);
  62. LOLUNIT_ASSERT_EQUAL(a.bits, b);
  63. }
  64. }
  65. LOLUNIT_TEST(test_half_is_nan)
  66. {
  67. LOLUNIT_ASSERT(half::makebits(0x7c01).is_nan());
  68. LOLUNIT_ASSERT(half::makebits(0xfc01).is_nan());
  69. LOLUNIT_ASSERT(half::makebits(0x7e00).is_nan());
  70. LOLUNIT_ASSERT(half::makebits(0xfe00).is_nan());
  71. LOLUNIT_ASSERT(!half::makebits(0x7c00).is_nan());
  72. LOLUNIT_ASSERT(!half::makebits(0xfc00).is_nan());
  73. LOLUNIT_ASSERT(!half(0.0f).is_nan());
  74. LOLUNIT_ASSERT(!half(-0.0f).is_nan());
  75. LOLUNIT_ASSERT(!half(2.0f).is_nan());
  76. LOLUNIT_ASSERT(!half(-2.0f).is_nan());
  77. }
  78. LOLUNIT_TEST(test_half_is_inf)
  79. {
  80. LOLUNIT_ASSERT(half(65536.0f).is_inf());
  81. LOLUNIT_ASSERT(half(-65536.0f).is_inf());
  82. LOLUNIT_ASSERT(!half(0.0f).is_inf());
  83. LOLUNIT_ASSERT(!half(-0.0f).is_inf());
  84. LOLUNIT_ASSERT(!half(65535.0f).is_inf());
  85. LOLUNIT_ASSERT(!half(-65535.0f).is_inf());
  86. LOLUNIT_ASSERT(half::makebits(0x7c00).is_inf());
  87. LOLUNIT_ASSERT(half::makebits(0xfc00).is_inf());
  88. LOLUNIT_ASSERT(!half::makebits(0x7e00).is_inf());
  89. LOLUNIT_ASSERT(!half::makebits(0xfe00).is_inf());
  90. }
  91. LOLUNIT_TEST(test_half_is_finite)
  92. {
  93. LOLUNIT_ASSERT(half(0.0f).is_finite());
  94. LOLUNIT_ASSERT(half(-0.0f).is_finite());
  95. LOLUNIT_ASSERT(half(65535.0f).is_finite());
  96. LOLUNIT_ASSERT(half(-65535.0f).is_finite());
  97. LOLUNIT_ASSERT(!half(65536.0f).is_finite());
  98. LOLUNIT_ASSERT(!half(-65536.0f).is_finite());
  99. LOLUNIT_ASSERT(!half::makebits(0x7c00).is_finite());
  100. LOLUNIT_ASSERT(!half::makebits(0xfc00).is_finite());
  101. LOLUNIT_ASSERT(!half::makebits(0x7e00).is_finite());
  102. LOLUNIT_ASSERT(!half::makebits(0xfe00).is_finite());
  103. }
  104. LOLUNIT_TEST(test_half_is_normal)
  105. {
  106. LOLUNIT_ASSERT(half(0.0f).is_normal());
  107. LOLUNIT_ASSERT(half(-0.0f).is_normal());
  108. LOLUNIT_ASSERT(half(65535.0f).is_normal());
  109. LOLUNIT_ASSERT(half(-65535.0f).is_normal());
  110. LOLUNIT_ASSERT(!half(65536.0f).is_normal());
  111. LOLUNIT_ASSERT(!half(-65536.0f).is_normal());
  112. LOLUNIT_ASSERT(!half::makebits(0x7c00).is_normal());
  113. LOLUNIT_ASSERT(!half::makebits(0xfc00).is_normal());
  114. LOLUNIT_ASSERT(!half::makebits(0x7e00).is_normal());
  115. LOLUNIT_ASSERT(!half::makebits(0xfe00).is_normal());
  116. }
  117. LOLUNIT_TEST(test_half_classify)
  118. {
  119. for (uint32_t i = 0; i < 0x10000; i++)
  120. {
  121. LOLUNIT_SET_CONTEXT(i);
  122. half h = half::makebits(i);
  123. if (h.is_nan())
  124. {
  125. LOLUNIT_ASSERT(!h.is_inf());
  126. LOLUNIT_ASSERT(!h.is_normal());
  127. LOLUNIT_ASSERT(!h.is_finite());
  128. }
  129. else if (h.is_inf())
  130. {
  131. LOLUNIT_ASSERT(!h.is_normal());
  132. LOLUNIT_ASSERT(!h.is_finite());
  133. }
  134. else
  135. {
  136. LOLUNIT_ASSERT(h.is_finite());
  137. }
  138. }
  139. }
  140. LOLUNIT_TEST(test_half_to_float)
  141. {
  142. for (size_t i = 0; i < sizeof(pairs) / sizeof(*pairs); i++)
  143. {
  144. float a = (float)half::makebits(pairs[i].x);
  145. float b = pairs[i].f;
  146. LOLUNIT_SET_CONTEXT(i);
  147. LOLUNIT_ASSERT_EQUAL(a, b);
  148. }
  149. for (uint32_t i = 0; i < 0x10000; i++)
  150. {
  151. half h = half::makebits(i);
  152. float f = (float)h;
  153. half g = (half)f;
  154. LOLUNIT_SET_CONTEXT(i);
  155. if (h.is_nan())
  156. {
  157. LOLUNIT_ASSERT(isnan(f));
  158. LOLUNIT_ASSERT(g.is_nan());
  159. }
  160. else
  161. {
  162. LOLUNIT_ASSERT(!isnan(f));
  163. LOLUNIT_ASSERT_EQUAL(g.bits, h.bits);
  164. }
  165. }
  166. }
  167. LOLUNIT_TEST(test_half_to_int)
  168. {
  169. LOLUNIT_ASSERT_EQUAL((int)(half)(0.0f), 0);
  170. LOLUNIT_ASSERT_EQUAL((int)(half)(-0.0f), 0);
  171. LOLUNIT_ASSERT_EQUAL((int)(half)(0.9f), 0);
  172. LOLUNIT_ASSERT_EQUAL((int)(half)(-0.9f), 0);
  173. LOLUNIT_ASSERT_EQUAL((int)(half)(1.0f), 1);
  174. LOLUNIT_ASSERT_EQUAL((int)(half)(-1.0f), -1);
  175. LOLUNIT_ASSERT_EQUAL((int)(half)(1.9f), 1);
  176. LOLUNIT_ASSERT_EQUAL((int)(half)(-1.9f), -1);
  177. LOLUNIT_ASSERT_EQUAL((int)(half)(65504.0f), 65504);
  178. LOLUNIT_ASSERT_EQUAL((int)(half)(-65504.0f), -65504);
  179. }
  180. LOLUNIT_TEST(test_float_op_half)
  181. {
  182. half zero = 0;
  183. half one = 1;
  184. half two = 2;
  185. float a = zero + one;
  186. LOLUNIT_ASSERT_EQUAL(1.0f, a);
  187. a += zero;
  188. LOLUNIT_ASSERT_EQUAL(1.0f, a);
  189. a -= zero;
  190. LOLUNIT_ASSERT_EQUAL(1.0f, a);
  191. a *= one;
  192. LOLUNIT_ASSERT_EQUAL(1.0f, a);
  193. a /= one;
  194. LOLUNIT_ASSERT_EQUAL(1.0f, a);
  195. float b = one + zero;
  196. LOLUNIT_ASSERT_EQUAL(1.0f, b);
  197. b += one;
  198. LOLUNIT_ASSERT_EQUAL(2.0f, b);
  199. b *= two;
  200. LOLUNIT_ASSERT_EQUAL(4.0f, b);
  201. b -= two;
  202. LOLUNIT_ASSERT_EQUAL(2.0f, b);
  203. b /= two;
  204. LOLUNIT_ASSERT_EQUAL(1.0f, b);
  205. float c = one - zero;
  206. LOLUNIT_ASSERT_EQUAL(1.0f, c);
  207. float d = two - one;
  208. LOLUNIT_ASSERT_EQUAL(1.0f, d);
  209. float e = two + (-one);
  210. LOLUNIT_ASSERT_EQUAL(1.0f, e);
  211. float f = (two * two) / (one + one);
  212. LOLUNIT_ASSERT_EQUAL(2.0f, f);
  213. }
  214. LOLUNIT_TEST(test_half_op_float)
  215. {
  216. half zero = 0;
  217. half one = 1;
  218. half two = 2;
  219. half four = 4;
  220. half a = one + 0.0f;
  221. LOLUNIT_ASSERT_EQUAL(one.bits, a.bits);
  222. a += 0.0f;
  223. LOLUNIT_ASSERT_EQUAL(one.bits, a.bits);
  224. a -= 0.0f;
  225. LOLUNIT_ASSERT_EQUAL(one.bits, a.bits);
  226. a *= 1.0f;
  227. LOLUNIT_ASSERT_EQUAL(one.bits, a.bits);
  228. a /= 1.0f;
  229. LOLUNIT_ASSERT_EQUAL(one.bits, a.bits);
  230. half b = one + 0.0f;
  231. LOLUNIT_ASSERT_EQUAL(one.bits, b.bits);
  232. b += 1.0f;
  233. LOLUNIT_ASSERT_EQUAL(two.bits, b.bits);
  234. b *= 2.0f;
  235. LOLUNIT_ASSERT_EQUAL(four.bits, b.bits);
  236. b -= 2.0f;
  237. LOLUNIT_ASSERT_EQUAL(two.bits, b.bits);
  238. b /= 2.0f;
  239. LOLUNIT_ASSERT_EQUAL(one.bits, b.bits);
  240. half c = 1.0f - zero;
  241. LOLUNIT_ASSERT_EQUAL(one.bits, c.bits);
  242. half d = 2.0f - one;
  243. LOLUNIT_ASSERT_EQUAL(one.bits, d.bits);
  244. half e = 2.0f + (-one);
  245. LOLUNIT_ASSERT_EQUAL(one.bits, e.bits);
  246. half f = (2.0f * two) / (1.0f + one);
  247. LOLUNIT_ASSERT_EQUAL(two.bits, f.bits);
  248. }
  249. private:
  250. struct TestPair { float f; uint16_t x; };
  251. static TestPair const pairs[11];
  252. };
  253. HalfTest::TestPair const HalfTest::pairs[] =
  254. {
  255. /* All these values have exact half representations */
  256. { 0.0f, 0x0000 },
  257. { -0.0f, 0x8000 }, /* negative zero */
  258. { 1.0f, 0x3c00 },
  259. { -1.0f, 0xbc00 },
  260. { 2.0f, 0x4000 },
  261. { 0.5f, 0x3800 },
  262. { 0.125f, 0x3000 },
  263. { 15.9375f, 0x4bf8 },
  264. { 31.0f / (1 << 14), 0x17c0 }, /* 0x1.fp-10 */
  265. { 31.0f / (1 << 18), 0x07c0 }, /* 0x1.fp-14, normal float, denormal half */
  266. { 31.0f / (1 << 19), 0x03e0 }, /* 0x1.fp-15, normal float, denormal half */
  267. };
  268. } /* namespace lol */