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.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. 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. float f = (float)h;
  138. half g = (half)f;
  139. LOLUNIT_SET_CONTEXT(i);
  140. if (h.is_nan())
  141. {
  142. LOLUNIT_ASSERT(isnan(f));
  143. LOLUNIT_ASSERT(g.is_nan());
  144. }
  145. else
  146. {
  147. LOLUNIT_ASSERT(!isnan(f));
  148. LOLUNIT_ASSERT_EQUAL(g.bits, h.bits);
  149. }
  150. }
  151. }
  152. LOLUNIT_TEST(HalfToInt)
  153. {
  154. LOLUNIT_ASSERT_EQUAL((int)(half)(0.0f), 0);
  155. LOLUNIT_ASSERT_EQUAL((int)(half)(-0.0f), 0);
  156. LOLUNIT_ASSERT_EQUAL((int)(half)(0.9f), 0);
  157. LOLUNIT_ASSERT_EQUAL((int)(half)(-0.9f), 0);
  158. LOLUNIT_ASSERT_EQUAL((int)(half)(1.0f), 1);
  159. LOLUNIT_ASSERT_EQUAL((int)(half)(-1.0f), -1);
  160. LOLUNIT_ASSERT_EQUAL((int)(half)(1.9f), 1);
  161. LOLUNIT_ASSERT_EQUAL((int)(half)(-1.9f), -1);
  162. LOLUNIT_ASSERT_EQUAL((int)(half)(65504.0f), 65504);
  163. LOLUNIT_ASSERT_EQUAL((int)(half)(-65504.0f), -65504);
  164. }
  165. LOLUNIT_TEST(FloatOpHalf)
  166. {
  167. half zero = 0;
  168. half one = 1;
  169. half two = 2;
  170. float a = zero + one;
  171. LOLUNIT_ASSERT_EQUAL(1.0f, a);
  172. a += zero;
  173. LOLUNIT_ASSERT_EQUAL(1.0f, a);
  174. a -= zero;
  175. LOLUNIT_ASSERT_EQUAL(1.0f, a);
  176. a *= one;
  177. LOLUNIT_ASSERT_EQUAL(1.0f, a);
  178. a /= one;
  179. LOLUNIT_ASSERT_EQUAL(1.0f, a);
  180. float b = one + zero;
  181. LOLUNIT_ASSERT_EQUAL(1.0f, b);
  182. b += one;
  183. LOLUNIT_ASSERT_EQUAL(2.0f, b);
  184. b *= two;
  185. LOLUNIT_ASSERT_EQUAL(4.0f, b);
  186. b -= two;
  187. LOLUNIT_ASSERT_EQUAL(2.0f, b);
  188. b /= two;
  189. LOLUNIT_ASSERT_EQUAL(1.0f, b);
  190. float c = one - zero;
  191. LOLUNIT_ASSERT_EQUAL(1.0f, c);
  192. float d = two - one;
  193. LOLUNIT_ASSERT_EQUAL(1.0f, d);
  194. float e = two + (-one);
  195. LOLUNIT_ASSERT_EQUAL(1.0f, e);
  196. float f = (two * two) / (one + one);
  197. LOLUNIT_ASSERT_EQUAL(2.0f, f);
  198. }
  199. LOLUNIT_TEST(HalfOpFloat)
  200. {
  201. half zero = 0;
  202. half one = 1;
  203. half two = 2;
  204. half four = 4;
  205. half a = one + 0.0f;
  206. LOLUNIT_ASSERT_EQUAL(one.bits, a.bits);
  207. a += 0.0f;
  208. LOLUNIT_ASSERT_EQUAL(one.bits, a.bits);
  209. a -= 0.0f;
  210. LOLUNIT_ASSERT_EQUAL(one.bits, a.bits);
  211. a *= 1.0f;
  212. LOLUNIT_ASSERT_EQUAL(one.bits, a.bits);
  213. a /= 1.0f;
  214. LOLUNIT_ASSERT_EQUAL(one.bits, a.bits);
  215. half b = one + 0.0f;
  216. LOLUNIT_ASSERT_EQUAL(one.bits, b.bits);
  217. b += 1.0f;
  218. LOLUNIT_ASSERT_EQUAL(two.bits, b.bits);
  219. b *= 2.0f;
  220. LOLUNIT_ASSERT_EQUAL(four.bits, b.bits);
  221. b -= 2.0f;
  222. LOLUNIT_ASSERT_EQUAL(two.bits, b.bits);
  223. b /= 2.0f;
  224. LOLUNIT_ASSERT_EQUAL(one.bits, b.bits);
  225. half c = 1.0f - zero;
  226. LOLUNIT_ASSERT_EQUAL(one.bits, c.bits);
  227. half d = 2.0f - one;
  228. LOLUNIT_ASSERT_EQUAL(one.bits, d.bits);
  229. half e = 2.0f + (-one);
  230. LOLUNIT_ASSERT_EQUAL(one.bits, e.bits);
  231. half f = (2.0f * two) / (1.0f + one);
  232. LOLUNIT_ASSERT_EQUAL(two.bits, f.bits);
  233. }
  234. struct TestPair { float f; uint16_t x; };
  235. static TestPair const pairs[11];
  236. };
  237. HalfTest::TestPair const HalfTest::pairs[] =
  238. {
  239. /* All these values have exact half representations */
  240. { 0.0f, 0x0000 },
  241. { -0.0f, 0x8000 }, /* negative zero */
  242. { 1.0f, 0x3c00 },
  243. { -1.0f, 0xbc00 },
  244. { 2.0f, 0x4000 },
  245. { 0.5f, 0x3800 },
  246. { 0.125f, 0x3000 },
  247. { 15.9375f, 0x4bf8 },
  248. { 31.0f / (1 << 14), 0x17c0 }, /* 0x1.fp-10 */
  249. { 31.0f / (1 << 18), 0x07c0 }, /* 0x1.fp-14, normal float, denormal half */
  250. { 31.0f / (1 << 19), 0x03e0 }, /* 0x1.fp-15, normal float, denormal half */
  251. };
  252. } /* namespace lol */