Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

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