gtc_half_float.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2013 G-Truc Creation (www.g-truc.net)
  5. /// Permission is hereby granted, free of charge, to any person obtaining a copy
  6. /// of this software and associated documentation files (the "Software"), to deal
  7. /// in the Software without restriction, including without limitation the rights
  8. /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. /// copies of the Software, and to permit persons to whom the Software is
  10. /// furnished to do so, subject to the following conditions:
  11. ///
  12. /// The above copyright notice and this permission notice shall be included in
  13. /// all copies or substantial portions of the Software.
  14. ///
  15. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. /// THE SOFTWARE.
  22. ///
  23. /// @ref test
  24. /// @file test/gtc/half_float.cpp
  25. /// @date 2011-05-32 / 2012-04-07
  26. /// @author Christophe Riccio
  27. ///////////////////////////////////////////////////////////////////////////////////
  28. #include <glm/glm.hpp>
  29. #include <glm/gtc/half_float.hpp>
  30. #include <cstdio>
  31. int test_half_precision_scalar()
  32. {
  33. int Error = 0;
  34. Error += sizeof(glm::half) == 2 ? 0 : 1;
  35. return Error;
  36. }
  37. int test_half_precision_vec()
  38. {
  39. int Error = 0;
  40. Error += sizeof(glm::hvec2) == 4 ? 0 : 1;
  41. Error += sizeof(glm::hvec3) == 6 ? 0 : 1;
  42. Error += sizeof(glm::hvec4) == 8 ? 0 : 1;
  43. return Error;
  44. }
  45. int test_half_precision_mat()
  46. {
  47. int Error = 0;
  48. Error += sizeof(glm::hmat2) == 8 ? 0 : 1;
  49. Error += sizeof(glm::hmat3) == 18 ? 0 : 1;
  50. Error += sizeof(glm::hmat4) == 32 ? 0 : 1;
  51. Error += sizeof(glm::hmat2x2) == 8 ? 0 : 1;
  52. Error += sizeof(glm::hmat2x3) == 12 ? 0 : 1;
  53. Error += sizeof(glm::hmat2x4) == 16 ? 0 : 1;
  54. Error += sizeof(glm::hmat3x2) == 12 ? 0 : 1;
  55. Error += sizeof(glm::hmat3x3) == 18 ? 0 : 1;
  56. Error += sizeof(glm::hmat3x4) == 24 ? 0 : 1;
  57. Error += sizeof(glm::hmat4x2) == 16 ? 0 : 1;
  58. Error += sizeof(glm::hmat4x3) == 24 ? 0 : 1;
  59. Error += sizeof(glm::hmat4x4) == 32 ? 0 : 1;
  60. return Error;
  61. }
  62. int test_half_ctor_mat2x2()
  63. {
  64. int Error = 0;
  65. {
  66. glm::hvec2 A(1, 2);
  67. glm::hvec2 B(3, 4);
  68. glm::hmat2 C(A, B);//, 2.0f, 3.0f, 4.0f);
  69. glm::hmat2 D(1, 2, 3, 4);
  70. Error += C[0] == D[0] ? 0 : 1;
  71. Error += C[1] == D[1] ? 0 : 1;
  72. }
  73. {
  74. glm::hvec2 A(1, 2.0);
  75. glm::hvec2 B(3, 4.0);
  76. glm::hmat2 C(A, B);//, 2.0f, 3.0f, 4.0f);
  77. glm::hmat2 D(1, 2.0, 3u, 4.0f);
  78. Error += C[0] == D[0] ? 0 : 1;
  79. Error += C[1] == D[1] ? 0 : 1;
  80. }
  81. {
  82. glm::hmat2 A(1);
  83. glm::mat2 B(1);
  84. glm::hmat2 C(A);
  85. Error += A == C ? 0 : 1;
  86. }
  87. return Error;
  88. }
  89. int test_half_ctor_mat2x3()
  90. {
  91. int Error = 0;
  92. {
  93. glm::hvec3 A(1, 2, 3);
  94. glm::hvec3 B(4, 5, 6);
  95. glm::hmat2x3 C(A, B);
  96. glm::hmat2x3 D(1, 2, 3, 4, 5, 6);
  97. Error += C[0] == D[0] ? 0 : 1;
  98. Error += C[1] == D[1] ? 0 : 1;
  99. }
  100. {
  101. glm::hvec3 A(1.0, 2.0f, 3u);
  102. glm::hvec3 B(4, 5u, 6u);
  103. glm::hmat2x3 C(A, B);
  104. glm::hmat2x3 D(1, 2.0, 3u, 4.0f, 5.0, 6);
  105. Error += C[0] == D[0] ? 0 : 1;
  106. Error += C[1] == D[1] ? 0 : 1;
  107. }
  108. {
  109. glm::hmat2x3 A(1);
  110. glm::mat2x3 B(1);
  111. glm::hmat2x3 C(A);
  112. Error += A == C ? 0 : 1;
  113. }
  114. return Error;
  115. }
  116. int test_half_ctor_mat2x4()
  117. {
  118. int Error = 0;
  119. {
  120. glm::hvec4 A(1, 2, 3, 4);
  121. glm::hvec4 B(5, 6, 7, 8);
  122. glm::hmat2x4 C(A, B);
  123. glm::hmat2x4 D(1, 2, 3, 4, 5, 6, 7, 8);
  124. Error += C[0] == D[0] ? 0 : 1;
  125. Error += C[1] == D[1] ? 0 : 1;
  126. }
  127. {
  128. glm::hvec4 A(1.0, 2.0f, 3u, 4u);
  129. glm::hvec4 B(5u, 6u, 7.0, 8.0);
  130. glm::hmat2x4 C(A, B);
  131. glm::hmat2x4 D(1, 2.0, 3u, 4.0f, 5.0, 6, 7.0f, 8.0f);
  132. Error += C[0] == D[0] ? 0 : 1;
  133. Error += C[1] == D[1] ? 0 : 1;
  134. }
  135. {
  136. glm::hmat2x4 A(1);
  137. glm::mat2x4 B(1);
  138. glm::hmat2x4 C(A);
  139. Error += A == C ? 0 : 1;
  140. }
  141. return Error;
  142. }
  143. int test_half_ctor_mat3x2()
  144. {
  145. int Error = 0;
  146. {
  147. glm::hvec2 A(1, 2);
  148. glm::hvec2 B(3, 4);
  149. glm::hvec2 C(5, 6);
  150. glm::hmat3x2 M(A, B, C);
  151. glm::hmat3x2 N(1, 2, 3, 4, 5, 6);
  152. Error += M == N ? 0 : 1;
  153. }
  154. {
  155. glm::hvec2 A(1, 2.0);
  156. glm::hvec2 B(3, 4.0f);
  157. glm::hvec2 C(5u, 6.0f);
  158. glm::hmat3x2 M(A, B, C);
  159. glm::hmat3x2 N(1, 2.0, 3u, 4.0f, 5, 6);
  160. Error += M == N ? 0 : 1;
  161. }
  162. {
  163. glm::hmat3x2 A(1);
  164. glm::mat3x2 B(1);
  165. glm::hmat3x2 C(A);
  166. Error += A == C ? 0 : 1;
  167. }
  168. return Error;
  169. }
  170. int test_half_ctor_mat3x3()
  171. {
  172. int Error = 0;
  173. {
  174. glm::hvec3 A(1, 2, 3);
  175. glm::hvec3 B(4, 5, 6);
  176. glm::hvec3 C(7, 8, 9);
  177. glm::hmat3x3 M(A, B, C);
  178. glm::hmat3x3 N(1, 2, 3, 4, 5, 6, 7, 8, 9);
  179. Error += M == N ? 0 : 1;
  180. }
  181. {
  182. glm::hvec3 A(1, 2.0, 3.0f);
  183. glm::hvec3 B(4, 5.0f, 6.0);
  184. glm::hvec3 C(7u, 8.0f, 9);
  185. glm::hmat3x3 M(A, B, C);
  186. glm::hmat3x3 N(1, 2.0, 3u, 4.0f, 5, 6, 7.0f, 8.0, 9u);
  187. Error += M == N ? 0 : 1;
  188. }
  189. {
  190. glm::hmat3x3 A(1);
  191. glm::mat3x3 B(1);
  192. glm::hmat3x3 C(A);
  193. Error += A == C ? 0 : 1;
  194. }
  195. return Error;
  196. }
  197. int test_half_ctor_mat3x4()
  198. {
  199. int Error = 0;
  200. {
  201. glm::hvec4 A(1, 2, 3, 4);
  202. glm::hvec4 B(5, 6, 7, 8);
  203. glm::hvec4 C(9, 10, 11, 12);
  204. glm::hmat3x4 M(A, B, C);
  205. glm::hmat3x4 N(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
  206. Error += M == N ? 0 : 1;
  207. }
  208. {
  209. glm::hvec4 A(1, 2.0, 3.0f, 4u);
  210. glm::hvec4 B(5, 6.0f, 7.0, 8);
  211. glm::hvec4 C(9u, 10.0f, 11, 12.f);
  212. glm::hmat3x4 M(A, B, C);
  213. glm::hmat3x4 N(1, 2.0, 3u, 4.0f, 5, 6, 7.0f, 8.0, 9u, 10, 11.f, 12.0);
  214. Error += M == N ? 0 : 1;
  215. }
  216. {
  217. glm::hmat3x4 A(1);
  218. glm::mat3x4 B(1);
  219. glm::hmat3x4 C(A);
  220. Error += A == C ? 0 : 1;
  221. }
  222. return Error;
  223. }
  224. int test_half_ctor_mat4x2()
  225. {
  226. int Error = 0;
  227. {
  228. glm::hvec2 A(1, 2);
  229. glm::hvec2 B(3, 4);
  230. glm::hvec2 C(5, 6);
  231. glm::hvec2 D(7, 8);
  232. glm::hmat4x2 M(A, B, C, D);
  233. glm::hmat4x2 N(1, 2, 3, 4, 5, 6, 7, 8);
  234. Error += M == N ? 0 : 1;
  235. }
  236. {
  237. glm::hvec2 A(1, 2.0);
  238. glm::hvec2 B(3.0f, 4);
  239. glm::hvec2 C(5.0, 6u);
  240. glm::hvec2 D(7, 8u);
  241. glm::hmat4x2 M(A, B, C, D);
  242. glm::hmat4x2 N(1, 2.0, 3u, 4.0f, 5u, 6.0, 7, 8.0f);
  243. Error += M == N ? 0 : 1;
  244. }
  245. {
  246. glm::hmat4x2 A(1);
  247. glm::mat4x2 B(1);
  248. glm::hmat4x2 C(A);
  249. Error += A == C ? 0 : 1;
  250. }
  251. return Error;
  252. }
  253. int test_half_ctor_mat4x3()
  254. {
  255. int Error = 0;
  256. {
  257. glm::hvec3 A(1, 2, 3);
  258. glm::hvec3 B(4, 5, 6);
  259. glm::hvec3 C(7, 8, 9);
  260. glm::hvec3 D(10, 11, 12);
  261. glm::hmat4x3 M(A, B, C, D);
  262. glm::hmat4x3 N(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
  263. Error += M == N ? 0 : 1;
  264. }
  265. {
  266. glm::hvec3 A(1, 2.0, 3u);
  267. glm::hvec3 B(4.0f, 5, 6u);
  268. glm::hvec3 C(7.0, 8u, 9.f);
  269. glm::hvec3 D(10, 11u, 12.0);
  270. glm::hmat4x3 M(A, B, C, D);
  271. glm::hmat4x3 N(1, 2.0, 3u, 4.0f, 5u, 6.0, 7, 8.0f, 9, 10u, 11.f, 12.0);
  272. Error += M == N ? 0 : 1;
  273. }
  274. {
  275. glm::hmat4x3 A(1);
  276. glm::mat4x3 B(1);
  277. glm::hmat4x3 C(A);
  278. Error += A == C ? 0 : 1;
  279. }
  280. return Error;
  281. }
  282. int test_half_ctor_mat4x4()
  283. {
  284. int Error = 0;
  285. {
  286. glm::hvec4 A(1, 2, 3, 4);
  287. glm::hvec4 B(5, 6, 7, 8);
  288. glm::hvec4 C(9, 10, 11, 12);
  289. glm::hvec4 D(13, 14, 15, 16);
  290. glm::hmat4x4 M(A, B, C, D);
  291. glm::hmat4x4 N(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  292. Error += M == N ? 0 : 1;
  293. }
  294. {
  295. glm::hvec4 A(1, 2.0, 3u, 4);
  296. glm::hvec4 B(5.0f, 6, 7u, 8.0);
  297. glm::hvec4 C(9.0, 10u, 11.f, 12);
  298. glm::hvec4 D(13, 14u, 15.0, 16u);
  299. glm::hmat4x4 M(A, B, C, D);
  300. glm::hmat4x4 N(1, 2.0, 3u, 4.0f, 5u, 6.0, 7, 8.0f, 9, 10u, 11.f, 12.0, 13, 14u, 15.0f, 16.0);
  301. Error += M == N ? 0 : 1;
  302. }
  303. {
  304. glm::hmat4x4 A(1);
  305. glm::mat4x4 B(1);
  306. glm::hmat4x4 C(A);
  307. Error += A == C ? 0 : 1;
  308. }
  309. return Error;
  310. }
  311. int test_half_ctor_vec2()
  312. {
  313. int Error = 0;
  314. {
  315. glm::hvec2 A;
  316. A.x = glm::half(1);
  317. A.y = glm::half(2);
  318. //glm::hvec2 A(1, 2);
  319. glm::hvec2 B(A);
  320. glm::vec2 C(1, 2);
  321. glm::hvec2 D(C);
  322. glm::dvec2 E(1, 2);
  323. glm::hvec2 F(E);
  324. glm::hvec2 G(1, 2.0);
  325. glm::hvec2 H;
  326. H = A;
  327. Error += A == B ? 0 : 1;
  328. //Error += C == D ? 0 : 1; //Error
  329. //Error += E == F ? 0 : 1; //Error
  330. Error += A == G ? 0 : 1;
  331. Error += A == H ? 0 : 1;
  332. }
  333. {
  334. glm::hvec2 A(1);
  335. glm::vec2 B(1);
  336. glm::hvec2 C(A);
  337. Error += A == C ? 0 : 1;
  338. }
  339. return Error;
  340. }
  341. int test_half_ctor_vec3()
  342. {
  343. int Error = 0;
  344. {
  345. glm::hvec3 A(1, 2, 3);
  346. glm::hvec3 B(A);
  347. glm::vec3 C(1, 2, 3);
  348. glm::hvec3 D(C);
  349. glm::dvec3 E(1, 2, 3);
  350. glm::hvec3 F(E);
  351. glm::hvec3 G(1, 2.0, 3);
  352. glm::hvec3 H;
  353. H = A;
  354. Error += A == B ? 0 : 1;
  355. //Error += C == D ? 0 : 1;
  356. //Error += E == F ? 0 : 1;
  357. Error += A == G ? 0 : 1;
  358. Error += A == H ? 0 : 1;
  359. }
  360. {
  361. glm::hvec3 A(1);
  362. glm::vec3 B(1);
  363. glm::hvec3 C(B);
  364. Error += A == C ? 0 : 1;
  365. }
  366. return Error;
  367. }
  368. int test_half_ctor_vec4()
  369. {
  370. int Error = 0;
  371. {
  372. glm::hvec4 A(1, 2, 3, 4);
  373. glm::hvec4 B(A);
  374. glm::vec4 C(1, 2, 3, 4);
  375. glm::hvec4 D(C);
  376. glm::dvec4 E(1, 2, 3, 4);
  377. glm::hvec4 F(E);
  378. glm::hvec4 G(1, 2.0, 3, 4);
  379. glm::hvec4 H;
  380. H = A;
  381. Error += A == B ? 0 : 1;
  382. //Error += C == D ? 0 : 1;
  383. //Error += E == F ? 0 : 1;
  384. Error += A == G ? 0 : 1;
  385. Error += A == H ? 0 : 1;
  386. }
  387. {
  388. glm::hvec4 A(1);
  389. glm::vec4 B(1);
  390. glm::hvec4 C(B);
  391. Error += A == C ? 0 : 1;
  392. }
  393. return Error;
  394. }
  395. int test_hvec2_size()
  396. {
  397. int Error = 0;
  398. Error += sizeof(glm::hvec2) <= sizeof(glm::lowp_vec2) ? 0 : 1;
  399. Error += 4 == sizeof(glm::hvec2) ? 0 : 1;
  400. Error += glm::hvec2().length() == 2 ? 0 : 1;
  401. return Error;
  402. }
  403. int test_hvec3_size()
  404. {
  405. int Error = 0;
  406. Error += sizeof(glm::hvec3) <= sizeof(glm::lowp_vec3) ? 0 : 1;
  407. Error += 6 <= sizeof(glm::hvec3) ? 0 : 1;
  408. Error += glm::hvec3().length() == 3 ? 0 : 1;
  409. return Error;
  410. }
  411. int test_hvec4_size()
  412. {
  413. int Error = 0;
  414. Error += sizeof(glm::hvec4) <= sizeof(glm::lowp_vec4) ? 0 : 1;
  415. Error += 8 <= sizeof(glm::hvec4) ? 0 : 1;
  416. Error += glm::hvec4().length() == 4 ? 0 : 1;
  417. return Error;
  418. }
  419. static int test_hvec_precision()
  420. {
  421. int Error = 0;
  422. Error += sizeof(glm::hvec2) == sizeof(glm::highp_hvec2) ? 0 : 1;
  423. Error += sizeof(glm::hvec3) == sizeof(glm::highp_hvec3) ? 0 : 1;
  424. Error += sizeof(glm::hvec4) == sizeof(glm::highp_hvec4) ? 0 : 1;
  425. return Error;
  426. }
  427. static int test_hmat_precision()
  428. {
  429. int Error = 0;
  430. Error += sizeof(glm::hmat2) == sizeof(glm::lowp_hmat2) ? 0 : 1;
  431. Error += sizeof(glm::hmat3) == sizeof(glm::lowp_hmat3) ? 0 : 1;
  432. Error += sizeof(glm::hmat4) == sizeof(glm::lowp_hmat4) ? 0 : 1;
  433. Error += sizeof(glm::hmat2) == sizeof(glm::mediump_hmat2) ? 0 : 1;
  434. Error += sizeof(glm::hmat3) == sizeof(glm::mediump_hmat3) ? 0 : 1;
  435. Error += sizeof(glm::hmat4) == sizeof(glm::mediump_hmat4) ? 0 : 1;
  436. Error += sizeof(glm::hmat2) == sizeof(glm::highp_hmat2) ? 0 : 1;
  437. Error += sizeof(glm::hmat3) == sizeof(glm::highp_hmat3) ? 0 : 1;
  438. Error += sizeof(glm::hmat4) == sizeof(glm::highp_hmat4) ? 0 : 1;
  439. Error += sizeof(glm::hmat2x2) == sizeof(glm::lowp_hmat2x2) ? 0 : 1;
  440. Error += sizeof(glm::hmat3x2) == sizeof(glm::lowp_hmat3x2) ? 0 : 1;
  441. Error += sizeof(glm::hmat4x2) == sizeof(glm::lowp_hmat4x2) ? 0 : 1;
  442. Error += sizeof(glm::hmat2x2) == sizeof(glm::mediump_hmat2x2) ? 0 : 1;
  443. Error += sizeof(glm::hmat3x2) == sizeof(glm::mediump_hmat3x2) ? 0 : 1;
  444. Error += sizeof(glm::hmat4x2) == sizeof(glm::mediump_hmat4x2) ? 0 : 1;
  445. Error += sizeof(glm::hmat2x2) == sizeof(glm::highp_hmat2x2) ? 0 : 1;
  446. Error += sizeof(glm::hmat3x2) == sizeof(glm::highp_hmat3x2) ? 0 : 1;
  447. Error += sizeof(glm::hmat4x2) == sizeof(glm::highp_hmat4x2) ? 0 : 1;
  448. Error += sizeof(glm::hmat2x3) == sizeof(glm::lowp_hmat2x3) ? 0 : 1;
  449. Error += sizeof(glm::hmat3x3) == sizeof(glm::lowp_hmat3x3) ? 0 : 1;
  450. Error += sizeof(glm::hmat4x3) == sizeof(glm::lowp_hmat4x3) ? 0 : 1;
  451. Error += sizeof(glm::hmat2x3) == sizeof(glm::mediump_hmat2x3) ? 0 : 1;
  452. Error += sizeof(glm::hmat3x3) == sizeof(glm::mediump_hmat3x3) ? 0 : 1;
  453. Error += sizeof(glm::hmat4x3) == sizeof(glm::mediump_hmat4x3) ? 0 : 1;
  454. Error += sizeof(glm::hmat2x3) == sizeof(glm::highp_hmat2x3) ? 0 : 1;
  455. Error += sizeof(glm::hmat3x3) == sizeof(glm::highp_hmat3x3) ? 0 : 1;
  456. Error += sizeof(glm::hmat4x3) == sizeof(glm::highp_hmat4x3) ? 0 : 1;
  457. Error += sizeof(glm::hmat2x4) == sizeof(glm::lowp_hmat2x4) ? 0 : 1;
  458. Error += sizeof(glm::hmat3x4) == sizeof(glm::lowp_hmat3x4) ? 0 : 1;
  459. Error += sizeof(glm::hmat4x4) == sizeof(glm::lowp_hmat4x4) ? 0 : 1;
  460. Error += sizeof(glm::hmat2x4) == sizeof(glm::mediump_hmat2x4) ? 0 : 1;
  461. Error += sizeof(glm::hmat3x4) == sizeof(glm::mediump_hmat3x4) ? 0 : 1;
  462. Error += sizeof(glm::hmat4x4) == sizeof(glm::mediump_hmat4x4) ? 0 : 1;
  463. Error += sizeof(glm::hmat2x4) == sizeof(glm::highp_hmat2x4) ? 0 : 1;
  464. Error += sizeof(glm::hmat3x4) == sizeof(glm::highp_hmat3x4) ? 0 : 1;
  465. Error += sizeof(glm::hmat4x4) == sizeof(glm::highp_hmat4x4) ? 0 : 1;
  466. return Error;
  467. }
  468. namespace detail
  469. {
  470. glm::uint16 float2half(glm::uint32 const & f)
  471. {
  472. // 10 bits => EE EEEFFFFF
  473. // 11 bits => EEE EEFFFFFF
  474. // Half bits => SEEEEEFF FFFFFFFF
  475. // Float bits => SEEEEEEE EFFFFFFF FFFFFFFF FFFFFFFF
  476. // 0x00007c00 => 00000000 00000000 01111100 00000000
  477. // 0x000003ff => 00000000 00000000 00000011 11111111
  478. // 0x38000000 => 00111000 00000000 00000000 00000000
  479. // 0x7f800000 => 01111111 10000000 00000000 00000000
  480. // 0x00008000 => 00000000 00000000 10000000 00000000
  481. return
  482. ((f >> 16) & 0x8000) | // sign
  483. ((((f & 0x7f800000) - 0x38000000) >> 13) & 0x7c00) | // exponential
  484. ((f >> 13) & 0x03ff); // Mantissa
  485. }
  486. glm::uint16 float2packed11(glm::uint32 const & f)
  487. {
  488. // 10 bits => EE EEEFFFFF
  489. // 11 bits => EEE EEFFFFFF
  490. // Half bits => SEEEEEFF FFFFFFFF
  491. // Float bits => SEEEEEEE EFFFFFFF FFFFFFFF FFFFFFFF
  492. // 0x000007c0 => 00000000 00000000 00000111 11000000
  493. // 0x00007c00 => 00000000 00000000 01111100 00000000
  494. // 0x000003ff => 00000000 00000000 00000011 11111111
  495. // 0x38000000 => 00111000 00000000 00000000 00000000
  496. // 0x7f800000 => 01111111 10000000 00000000 00000000
  497. // 0x00008000 => 00000000 00000000 10000000 00000000
  498. return
  499. ((((f & 0x7f800000) - 0x38000000) >> 17) & 0x07c0) | // exponential
  500. ((f >> 17) & 0x003f); // Mantissa
  501. }
  502. glm::uint float2packed10(glm::uint const & f)
  503. {
  504. // 10 bits => EE EEEFFFFF
  505. // 11 bits => EEE EEFFFFFF
  506. // Half bits => SEEEEEFF FFFFFFFF
  507. // Float bits => SEEEEEEE EFFFFFFF FFFFFFFF FFFFFFFF
  508. // 0x0000001F => 00000000 00000000 00000000 00011111
  509. // 0x0000003F => 00000000 00000000 00000000 00111111
  510. // 0x000003E0 => 00000000 00000000 00000011 11100000
  511. // 0x000007C0 => 00000000 00000000 00000111 11000000
  512. // 0x00007C00 => 00000000 00000000 01111100 00000000
  513. // 0x000003FF => 00000000 00000000 00000011 11111111
  514. // 0x38000000 => 00111000 00000000 00000000 00000000
  515. // 0x7f800000 => 01111111 10000000 00000000 00000000
  516. // 0x00008000 => 00000000 00000000 10000000 00000000
  517. return
  518. ((((f & 0x7f800000) - 0x38000000) >> 18) & 0x03E0) | // exponential
  519. ((f >> 18) & 0x001f); // Mantissa
  520. }
  521. glm::uint half2float(glm::uint const & h)
  522. {
  523. return ((h & 0x8000) << 16) | ((( h & 0x7c00) + 0x1C000) << 13) | ((h & 0x03FF) << 13);
  524. }
  525. union uif
  526. {
  527. glm::uint i;
  528. float f;
  529. };
  530. glm::uint floatTo11bit(float x)
  531. {
  532. if(x == 0.0f)
  533. return 0;
  534. else if(glm::isnan(x))
  535. return ~0;
  536. else if(glm::isinf(x))
  537. return 0x1f << 6;
  538. uif Union;
  539. Union.f = x;
  540. return float2packed11(Union.i);
  541. }
  542. glm::uint floatTo10bit(float x)
  543. {
  544. if(x == 0.0f)
  545. return 0;
  546. else if(glm::isnan(x))
  547. return ~0;
  548. else if(glm::isinf(x))
  549. return 0x1f << 5;
  550. uif Union;
  551. Union.f = x;
  552. return float2packed10(Union.i);
  553. }
  554. glm::uint f11_f11_f10(float x, float y, float z)
  555. {
  556. return ((floatTo11bit(x) & ((1 << 11) - 1)) << 0) | ((floatTo11bit(y) & ((1 << 11) - 1)) << 11) | ((floatTo10bit(z) & ((1 << 10) - 1)) << 22);
  557. }
  558. }//namespace detail
  559. class f11f11f10
  560. {
  561. public:
  562. f11f11f10(float x, float y, float z) :
  563. x(detail::floatTo11bit(x)),
  564. y(detail::floatTo11bit(y)),
  565. z(detail::floatTo10bit(z))
  566. {}
  567. /*
  568. operator glm::vec3()
  569. {
  570. return glm::vec3(
  571. float(x) / 511.0f,
  572. float(y) / 511.0f,
  573. float(z) / 511.0f);
  574. }
  575. */
  576. private:
  577. int x : 11;
  578. int y : 11;
  579. int z : 10;
  580. };
  581. void print_bits(glm::half const & s)
  582. {
  583. union
  584. {
  585. glm::detail::hdata h;
  586. unsigned short i;
  587. } uif;
  588. uif.h = s._data();
  589. printf("f16: ");
  590. for(std::size_t j = sizeof(s) * 8; j > 0; --j)
  591. {
  592. if(j == 10 || j == 15)
  593. printf(" ");
  594. printf("%d", (uif.i & (1 << (j - 1))) ? 1 : 0);
  595. }
  596. }
  597. void print_bits(float const & s)
  598. {
  599. union
  600. {
  601. float f;
  602. unsigned int i;
  603. } uif;
  604. uif.f = s;
  605. printf("f32: ");
  606. for(std::size_t j = sizeof(s) * 8; j > 0; --j)
  607. {
  608. if(j == 23 || j == 31)
  609. printf(" ");
  610. printf("%d", (uif.i & (1 << (j - 1))) ? 1 : 0);
  611. }
  612. }
  613. void print_10bits(glm::uint const & s)
  614. {
  615. printf("10b: ");
  616. for(std::size_t j = 10; j > 0; --j)
  617. {
  618. if(j == 5)
  619. printf(" ");
  620. printf("%d", (s & (1 << (j - 1))) ? 1 : 0);
  621. }
  622. }
  623. void print_11bits(glm::uint const & s)
  624. {
  625. printf("11b: ");
  626. for(std::size_t j = 11; j > 0; --j)
  627. {
  628. if(j == 6)
  629. printf(" ");
  630. printf("%d", (s & (1 << (j - 1))) ? 1 : 0);
  631. }
  632. }
  633. void print_value(float const & s)
  634. {
  635. printf("%2.5f, ", s);
  636. print_bits(s);
  637. printf(", ");
  638. print_bits(glm::half(s));
  639. printf(", ");
  640. print_11bits(detail::floatTo11bit(s));
  641. printf(", ");
  642. print_10bits(detail::floatTo10bit(s));
  643. printf("\n");
  644. }
  645. int test_half()
  646. {
  647. int Error = 0;
  648. print_value(0.0);
  649. print_value(0.1);
  650. print_value(0.2);
  651. print_value(0.3);
  652. print_value(0.4);
  653. print_value(0.5);
  654. print_value(0.6);
  655. print_value(1.0);
  656. print_value(1.1);
  657. print_value(1.2);
  658. print_value(1.3);
  659. print_value(1.4);
  660. print_value(1.5);
  661. print_value(1.6);
  662. print_value(2.0);
  663. print_value(2.1);
  664. print_value(2.2);
  665. print_value(2.3);
  666. print_value(2.4);
  667. print_value(2.5);
  668. print_value(2.6);
  669. return Error;
  670. }
  671. int main()
  672. {
  673. int Error = 0;
  674. Error += test_half();
  675. Error += test_hvec_precision();
  676. Error += test_hvec2_size();
  677. Error += test_hvec3_size();
  678. Error += test_hvec4_size();
  679. Error += test_half_ctor_vec2();
  680. Error += test_half_ctor_vec3();
  681. Error += test_half_ctor_vec4();
  682. Error += test_half_ctor_mat2x2();
  683. Error += test_half_ctor_mat2x3();
  684. Error += test_half_ctor_mat2x4();
  685. Error += test_half_ctor_mat3x2();
  686. Error += test_half_ctor_mat3x3();
  687. Error += test_half_ctor_mat3x4();
  688. Error += test_half_ctor_mat4x2();
  689. Error += test_half_ctor_mat4x3();
  690. Error += test_half_ctor_mat4x4();
  691. Error += test_half_precision_scalar();
  692. Error += test_half_precision_vec();
  693. Error += test_half_precision_mat();
  694. return Error;
  695. }