25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

half.cpp 8.5 KiB

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