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.
 
 
 

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