core_func_common.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2015 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. /// Restrictions:
  16. /// By making use of the Software for military purposes, you choose to make
  17. /// a Bunny unhappy.
  18. ///
  19. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. /// THE SOFTWARE.
  26. ///
  27. /// @file test/core/func_common.cpp
  28. /// @date 2011-01-15 / 2011-09-13
  29. /// @author Christophe Riccio
  30. ///////////////////////////////////////////////////////////////////////////////////
  31. #define GLM_FORCE_EXPLICIT_CTOR
  32. #include <glm/common.hpp>
  33. #include <glm/gtc/constants.hpp>
  34. #include <glm/gtc/epsilon.hpp>
  35. #include <glm/gtc/vec1.hpp>
  36. #include <glm/gtc/random.hpp>
  37. #include <vector>
  38. #include <cstdio>
  39. #include <cmath>
  40. #include <ctime>
  41. namespace floor_
  42. {
  43. int test()
  44. {
  45. int Error(0);
  46. {
  47. float A(1.1f);
  48. float B = glm::floor(A);
  49. }
  50. {
  51. double A(1.1f);
  52. double B = glm::floor(A);
  53. }
  54. {
  55. glm::vec1 A(1.1f);
  56. glm::vec1 B = glm::floor(A);
  57. Error += glm::all(glm::epsilonEqual(B, glm::vec1(1.0), 0.0001f)) ? 0 : 1;
  58. }
  59. {
  60. glm::dvec1 A(1.1f);
  61. glm::dvec1 B = glm::floor(A);
  62. Error += glm::all(glm::epsilonEqual(B, glm::dvec1(1.0), 0.0001)) ? 0 : 1;
  63. }
  64. {
  65. glm::vec2 A(1.1f);
  66. glm::vec2 B = glm::floor(A);
  67. Error += glm::all(glm::epsilonEqual(B, glm::vec2(1.0), 0.0001f)) ? 0 : 1;
  68. }
  69. {
  70. glm::dvec2 A(1.1f);
  71. glm::dvec2 B = glm::floor(A);
  72. Error += glm::all(glm::epsilonEqual(B, glm::dvec2(1.0), 0.0001)) ? 0 : 1;
  73. }
  74. {
  75. glm::vec3 A(1.1f);
  76. glm::vec3 B = glm::floor(A);
  77. Error += glm::all(glm::epsilonEqual(B, glm::vec3(1.0), 0.0001f)) ? 0 : 1;
  78. }
  79. {
  80. glm::dvec3 A(1.1f);
  81. glm::dvec3 B = glm::floor(A);
  82. Error += glm::all(glm::epsilonEqual(B, glm::dvec3(1.0), 0.0001)) ? 0 : 1;
  83. }
  84. {
  85. glm::vec4 A(1.1f);
  86. glm::vec4 B = glm::floor(A);
  87. Error += glm::all(glm::epsilonEqual(B, glm::vec4(1.0), 0.0001f)) ? 0 : 1;
  88. }
  89. {
  90. glm::dvec4 A(1.1f);
  91. glm::dvec4 B = glm::floor(A);
  92. Error += glm::all(glm::epsilonEqual(B, glm::dvec4(1.0), 0.0001)) ? 0 : 1;
  93. }
  94. return Error;
  95. }
  96. }//namespace floor
  97. namespace modf_
  98. {
  99. int test()
  100. {
  101. int Error(0);
  102. {
  103. float X(1.5f);
  104. float I(0.0f);
  105. float A = glm::modf(X, I);
  106. Error += I == 1.0f ? 0 : 1;
  107. Error += A == 0.5f ? 0 : 1;
  108. }
  109. {
  110. glm::vec4 X(1.1f, 1.2f, 1.5f, 1.7f);
  111. glm::vec4 I(0.0f);
  112. glm::vec4 A = glm::modf(X, I);
  113. Error += I == glm::vec4(1.0f) ? 0 : 1;
  114. Error += glm::all(glm::epsilonEqual(A, glm::vec4(0.1f, 0.2f, 0.5f, 0.7f), 0.00001f)) ? 0 : 1;
  115. }
  116. {
  117. glm::dvec4 X(1.1, 1.2, 1.5, 1.7);
  118. glm::dvec4 I(0.0);
  119. glm::dvec4 A = glm::modf(X, I);
  120. Error += I == glm::dvec4(1.0) ? 0 : 1;
  121. Error += glm::all(glm::epsilonEqual(A, glm::dvec4(0.1, 0.2, 0.5, 0.7), 0.000000001)) ? 0 : 1;
  122. }
  123. {
  124. double X(1.5);
  125. double I(0.0);
  126. double A = glm::modf(X, I);
  127. Error += I == 1.0 ? 0 : 1;
  128. Error += A == 0.5 ? 0 : 1;
  129. }
  130. return Error;
  131. }
  132. }//namespace modf
  133. namespace mod_
  134. {
  135. int test()
  136. {
  137. int Error(0);
  138. {
  139. float A(1.5f);
  140. float B(1.0f);
  141. float C = glm::mod(A, B);
  142. Error += glm::abs(C - 0.5f) < 0.00001f ? 0 : 1;
  143. }
  144. {
  145. float A(-0.2f);
  146. float B(1.0f);
  147. float C = glm::mod(A, B);
  148. Error += glm::abs(C - 0.8f) < 0.00001f ? 0 : 1;
  149. }
  150. {
  151. float A(3.0);
  152. float B(2.0f);
  153. float C = glm::mod(A, B);
  154. Error += glm::abs(C - 1.0f) < 0.00001f ? 0 : 1;
  155. }
  156. {
  157. glm::vec4 A(3.0);
  158. float B(2.0f);
  159. glm::vec4 C = glm::mod(A, B);
  160. Error += glm::all(glm::epsilonEqual(C, glm::vec4(1.0f), 0.00001f)) ? 0 : 1;
  161. }
  162. {
  163. glm::vec4 A(3.0);
  164. glm::vec4 B(2.0f);
  165. glm::vec4 C = glm::mod(A, B);
  166. Error += glm::all(glm::epsilonEqual(C, glm::vec4(1.0f), 0.00001f)) ? 0 : 1;
  167. }
  168. return Error;
  169. }
  170. }//namespace mod_
  171. namespace floatBitsToInt
  172. {
  173. int test()
  174. {
  175. int Error = 0;
  176. {
  177. float A = 1.0f;
  178. int B = glm::floatBitsToInt(A);
  179. float C = glm::intBitsToFloat(B);
  180. int D = *(int*)&A;
  181. Error += B == D ? 0 : 1;
  182. Error += A == C ? 0 : 1;
  183. }
  184. {
  185. glm::vec2 A(1.0f, 2.0f);
  186. glm::ivec2 B = glm::floatBitsToInt(A);
  187. glm::vec2 C = glm::intBitsToFloat(B);
  188. Error += B.x == *(int*)&(A.x) ? 0 : 1;
  189. Error += B.y == *(int*)&(A.y) ? 0 : 1;
  190. Error += A == C? 0 : 1;
  191. }
  192. {
  193. glm::vec3 A(1.0f, 2.0f, 3.0f);
  194. glm::ivec3 B = glm::floatBitsToInt(A);
  195. glm::vec3 C = glm::intBitsToFloat(B);
  196. Error += B.x == *(int*)&(A.x) ? 0 : 1;
  197. Error += B.y == *(int*)&(A.y) ? 0 : 1;
  198. Error += B.z == *(int*)&(A.z) ? 0 : 1;
  199. Error += A == C? 0 : 1;
  200. }
  201. {
  202. glm::vec4 A(1.0f, 2.0f, 3.0f, 4.0f);
  203. glm::ivec4 B = glm::floatBitsToInt(A);
  204. glm::vec4 C = glm::intBitsToFloat(B);
  205. Error += B.x == *(int*)&(A.x) ? 0 : 1;
  206. Error += B.y == *(int*)&(A.y) ? 0 : 1;
  207. Error += B.z == *(int*)&(A.z) ? 0 : 1;
  208. Error += B.w == *(int*)&(A.w) ? 0 : 1;
  209. Error += A == C? 0 : 1;
  210. }
  211. return Error;
  212. }
  213. }//namespace floatBitsToInt
  214. namespace floatBitsToUint
  215. {
  216. int test()
  217. {
  218. int Error = 0;
  219. {
  220. float A = 1.0f;
  221. glm::uint B = glm::floatBitsToUint(A);
  222. float C = glm::intBitsToFloat(B);
  223. Error += B == *(glm::uint*)&A ? 0 : 1;
  224. Error += A == C? 0 : 1;
  225. }
  226. {
  227. glm::vec2 A(1.0f, 2.0f);
  228. glm::uvec2 B = glm::floatBitsToUint(A);
  229. glm::vec2 C = glm::uintBitsToFloat(B);
  230. Error += B.x == *(glm::uint*)&(A.x) ? 0 : 1;
  231. Error += B.y == *(glm::uint*)&(A.y) ? 0 : 1;
  232. Error += A == C ? 0 : 1;
  233. }
  234. {
  235. glm::vec3 A(1.0f, 2.0f, 3.0f);
  236. glm::uvec3 B = glm::floatBitsToUint(A);
  237. glm::vec3 C = glm::uintBitsToFloat(B);
  238. Error += B.x == *(glm::uint*)&(A.x) ? 0 : 1;
  239. Error += B.y == *(glm::uint*)&(A.y) ? 0 : 1;
  240. Error += B.z == *(glm::uint*)&(A.z) ? 0 : 1;
  241. Error += A == C? 0 : 1;
  242. }
  243. {
  244. glm::vec4 A(1.0f, 2.0f, 3.0f, 4.0f);
  245. glm::uvec4 B = glm::floatBitsToUint(A);
  246. glm::vec4 C = glm::uintBitsToFloat(B);
  247. Error += B.x == *(glm::uint*)&(A.x) ? 0 : 1;
  248. Error += B.y == *(glm::uint*)&(A.y) ? 0 : 1;
  249. Error += B.z == *(glm::uint*)&(A.z) ? 0 : 1;
  250. Error += B.w == *(glm::uint*)&(A.w) ? 0 : 1;
  251. Error += A == C? 0 : 1;
  252. }
  253. return Error;
  254. }
  255. }//namespace floatBitsToUint
  256. namespace min_
  257. {
  258. int test()
  259. {
  260. int Error = 0;
  261. glm::vec1 A0 = glm::min(glm::vec1(1), glm::vec1(1));
  262. glm::vec2 B0 = glm::min(glm::vec2(1), glm::vec2(1));
  263. glm::vec2 B1 = glm::min(glm::vec2(1), 1.0f);
  264. bool B2 = glm::all(glm::equal(B0, B1));
  265. Error += B2 ? 0 : 1;
  266. glm::vec3 C0 = glm::min(glm::vec3(1), glm::vec3(1));
  267. glm::vec3 C1 = glm::min(glm::vec3(1), 1.0f);
  268. bool C2 = glm::all(glm::equal(C0, C1));
  269. Error += C2 ? 0 : 1;
  270. glm::vec4 D0 = glm::min(glm::vec4(1), glm::vec4(1));
  271. glm::vec4 D1 = glm::min(glm::vec4(1), 1.0f);
  272. bool D2 = glm::all(glm::equal(D0, D1));
  273. Error += D2 ? 0 : 1;
  274. return Error;
  275. }
  276. }//namespace min_
  277. namespace max_
  278. {
  279. int test()
  280. {
  281. int Error = 0;
  282. glm::vec1 A0 = glm::max(glm::vec1(1), glm::vec1(1));
  283. glm::vec2 B0 = glm::max(glm::vec2(1), glm::vec2(1));
  284. glm::vec2 B1 = glm::max(glm::vec2(1), 1.0f);
  285. bool B2 = glm::all(glm::equal(B0, B1));
  286. Error += B2 ? 0 : 1;
  287. glm::vec3 C0 = glm::max(glm::vec3(1), glm::vec3(1));
  288. glm::vec3 C1 = glm::max(glm::vec3(1), 1.0f);
  289. bool C2 = glm::all(glm::equal(C0, C1));
  290. Error += C2 ? 0 : 1;
  291. glm::vec4 D0 = glm::max(glm::vec4(1), glm::vec4(1));
  292. glm::vec4 D1 = glm::max(glm::vec4(1), 1.0f);
  293. bool D2 = glm::all(glm::equal(D0, D1));
  294. Error += D2 ? 0 : 1;
  295. return Error;
  296. }
  297. }//namespace max_
  298. namespace clamp_
  299. {
  300. int test()
  301. {
  302. int Error = 0;
  303. return Error;
  304. }
  305. }//namespace clamp_
  306. namespace mix_
  307. {
  308. template <typename T, typename B>
  309. struct entry
  310. {
  311. T x;
  312. T y;
  313. B a;
  314. T Result;
  315. };
  316. entry<float, bool> TestBool[] =
  317. {
  318. {0.0f, 1.0f, false, 0.0f},
  319. {0.0f, 1.0f, true, 1.0f},
  320. {-1.0f, 1.0f, false, -1.0f},
  321. {-1.0f, 1.0f, true, 1.0f}
  322. };
  323. entry<float, float> TestFloat[] =
  324. {
  325. {0.0f, 1.0f, 0.0f, 0.0f},
  326. {0.0f, 1.0f, 1.0f, 1.0f},
  327. {-1.0f, 1.0f, 0.0f, -1.0f},
  328. {-1.0f, 1.0f, 1.0f, 1.0f}
  329. };
  330. entry<glm::vec2, bool> TestVec2Bool[] =
  331. {
  332. {glm::vec2(0.0f), glm::vec2(1.0f), false, glm::vec2(0.0f)},
  333. {glm::vec2(0.0f), glm::vec2(1.0f), true, glm::vec2(1.0f)},
  334. {glm::vec2(-1.0f), glm::vec2(1.0f), false, glm::vec2(-1.0f)},
  335. {glm::vec2(-1.0f), glm::vec2(1.0f), true, glm::vec2(1.0f)}
  336. };
  337. entry<glm::vec2, glm::bvec2> TestBVec2[] =
  338. {
  339. {glm::vec2(0.0f), glm::vec2(1.0f), glm::bvec2(false), glm::vec2(0.0f)},
  340. {glm::vec2(0.0f), glm::vec2(1.0f), glm::bvec2(true), glm::vec2(1.0f)},
  341. {glm::vec2(-1.0f), glm::vec2(1.0f), glm::bvec2(false), glm::vec2(-1.0f)},
  342. {glm::vec2(-1.0f), glm::vec2(1.0f), glm::bvec2(true), glm::vec2(1.0f)},
  343. {glm::vec2(-1.0f), glm::vec2(1.0f), glm::bvec2(true, false), glm::vec2(1.0f, -1.0f)}
  344. };
  345. entry<glm::vec3, bool> TestVec3Bool[] =
  346. {
  347. {glm::vec3(0.0f), glm::vec3(1.0f), false, glm::vec3(0.0f)},
  348. {glm::vec3(0.0f), glm::vec3(1.0f), true, glm::vec3(1.0f)},
  349. {glm::vec3(-1.0f), glm::vec3(1.0f), false, glm::vec3(-1.0f)},
  350. {glm::vec3(-1.0f), glm::vec3(1.0f), true, glm::vec3(1.0f)}
  351. };
  352. entry<glm::vec3, glm::bvec3> TestBVec3[] =
  353. {
  354. {glm::vec3(0.0f), glm::vec3(1.0f), glm::bvec3(false), glm::vec3(0.0f)},
  355. {glm::vec3(0.0f), glm::vec3(1.0f), glm::bvec3(true), glm::vec3(1.0f)},
  356. {glm::vec3(-1.0f), glm::vec3(1.0f), glm::bvec3(false), glm::vec3(-1.0f)},
  357. {glm::vec3(-1.0f), glm::vec3(1.0f), glm::bvec3(true), glm::vec3(1.0f)},
  358. {glm::vec3(1.0f, 2.0f, 3.0f), glm::vec3(4.0f, 5.0f, 6.0f), glm::bvec3(true, false, true), glm::vec3(4.0f, 2.0f, 6.0f)}
  359. };
  360. entry<glm::vec4, bool> TestVec4Bool[] =
  361. {
  362. {glm::vec4(0.0f), glm::vec4(1.0f), false, glm::vec4(0.0f)},
  363. {glm::vec4(0.0f), glm::vec4(1.0f), true, glm::vec4(1.0f)},
  364. {glm::vec4(-1.0f), glm::vec4(1.0f), false, glm::vec4(-1.0f)},
  365. {glm::vec4(-1.0f), glm::vec4(1.0f), true, glm::vec4(1.0f)}
  366. };
  367. entry<glm::vec4, glm::bvec4> TestBVec4[] =
  368. {
  369. {glm::vec4(0.0f), glm::vec4(1.0f), glm::bvec4(false), glm::vec4(0.0f)},
  370. {glm::vec4(0.0f), glm::vec4(1.0f), glm::bvec4(true), glm::vec4(1.0f)},
  371. {glm::vec4(-1.0f), glm::vec4(1.0f), glm::bvec4(false), glm::vec4(-1.0f)},
  372. {glm::vec4(-1.0f), glm::vec4(1.0f), glm::bvec4(true), glm::vec4(1.0f)},
  373. {glm::vec4(1.0f, 2.0f, 3.0f, 4.0f), glm::vec4(5.0f, 6.0f, 7.0f, 8.0f), glm::bvec4(true, false, true, false), glm::vec4(5.0f, 2.0f, 7.0f, 4.0f)}
  374. };
  375. int test()
  376. {
  377. int Error = 0;
  378. // Float with bool
  379. {
  380. for(std::size_t i = 0; i < sizeof(TestBool) / sizeof(entry<float, bool>); ++i)
  381. {
  382. float Result = glm::mix(TestBool[i].x, TestBool[i].y, TestBool[i].a);
  383. Error += glm::epsilonEqual(Result, TestBool[i].Result, glm::epsilon<float>()) ? 0 : 1;
  384. }
  385. }
  386. // Float with float
  387. {
  388. for(std::size_t i = 0; i < sizeof(TestFloat) / sizeof(entry<float, float>); ++i)
  389. {
  390. float Result = glm::mix(TestFloat[i].x, TestFloat[i].y, TestFloat[i].a);
  391. Error += glm::epsilonEqual(Result, TestFloat[i].Result, glm::epsilon<float>()) ? 0 : 1;
  392. }
  393. }
  394. // vec2 with bool
  395. {
  396. for(std::size_t i = 0; i < sizeof(TestVec2Bool) / sizeof(entry<glm::vec2, bool>); ++i)
  397. {
  398. glm::vec2 Result = glm::mix(TestVec2Bool[i].x, TestVec2Bool[i].y, TestVec2Bool[i].a);
  399. Error += glm::epsilonEqual(Result.x, TestVec2Bool[i].Result.x, glm::epsilon<float>()) ? 0 : 1;
  400. Error += glm::epsilonEqual(Result.y, TestVec2Bool[i].Result.y, glm::epsilon<float>()) ? 0 : 1;
  401. }
  402. }
  403. // vec2 with bvec2
  404. {
  405. for(std::size_t i = 0; i < sizeof(TestBVec2) / sizeof(entry<glm::vec2, glm::bvec2>); ++i)
  406. {
  407. glm::vec2 Result = glm::mix(TestBVec2[i].x, TestBVec2[i].y, TestBVec2[i].a);
  408. Error += glm::epsilonEqual(Result.x, TestBVec2[i].Result.x, glm::epsilon<float>()) ? 0 : 1;
  409. Error += glm::epsilonEqual(Result.y, TestBVec2[i].Result.y, glm::epsilon<float>()) ? 0 : 1;
  410. }
  411. }
  412. // vec3 with bool
  413. {
  414. for(std::size_t i = 0; i < sizeof(TestVec3Bool) / sizeof(entry<glm::vec3, bool>); ++i)
  415. {
  416. glm::vec3 Result = glm::mix(TestVec3Bool[i].x, TestVec3Bool[i].y, TestVec3Bool[i].a);
  417. Error += glm::epsilonEqual(Result.x, TestVec3Bool[i].Result.x, glm::epsilon<float>()) ? 0 : 1;
  418. Error += glm::epsilonEqual(Result.y, TestVec3Bool[i].Result.y, glm::epsilon<float>()) ? 0 : 1;
  419. Error += glm::epsilonEqual(Result.z, TestVec3Bool[i].Result.z, glm::epsilon<float>()) ? 0 : 1;
  420. }
  421. }
  422. // vec3 with bvec3
  423. {
  424. for(std::size_t i = 0; i < sizeof(TestBVec3) / sizeof(entry<glm::vec3, glm::bvec3>); ++i)
  425. {
  426. glm::vec3 Result = glm::mix(TestBVec3[i].x, TestBVec3[i].y, TestBVec3[i].a);
  427. Error += glm::epsilonEqual(Result.x, TestBVec3[i].Result.x, glm::epsilon<float>()) ? 0 : 1;
  428. Error += glm::epsilonEqual(Result.y, TestBVec3[i].Result.y, glm::epsilon<float>()) ? 0 : 1;
  429. Error += glm::epsilonEqual(Result.z, TestBVec3[i].Result.z, glm::epsilon<float>()) ? 0 : 1;
  430. }
  431. }
  432. // vec4 with bool
  433. {
  434. for(std::size_t i = 0; i < sizeof(TestVec4Bool) / sizeof(entry<glm::vec4, bool>); ++i)
  435. {
  436. glm::vec4 Result = glm::mix(TestVec4Bool[i].x, TestVec4Bool[i].y, TestVec4Bool[i].a);
  437. Error += glm::epsilonEqual(Result.x, TestVec4Bool[i].Result.x, glm::epsilon<float>()) ? 0 : 1;
  438. Error += glm::epsilonEqual(Result.y, TestVec4Bool[i].Result.y, glm::epsilon<float>()) ? 0 : 1;
  439. Error += glm::epsilonEqual(Result.z, TestVec4Bool[i].Result.z, glm::epsilon<float>()) ? 0 : 1;
  440. Error += glm::epsilonEqual(Result.w, TestVec4Bool[i].Result.w, glm::epsilon<float>()) ? 0 : 1;
  441. }
  442. }
  443. // vec4 with bvec4
  444. {
  445. for(std::size_t i = 0; i < sizeof(TestBVec4) / sizeof(entry<glm::vec4, glm::bvec4>); ++i)
  446. {
  447. glm::vec4 Result = glm::mix(TestBVec4[i].x, TestBVec4[i].y, TestBVec4[i].a);
  448. Error += glm::epsilonEqual(Result.x, TestBVec4[i].Result.x, glm::epsilon<float>()) ? 0 : 1;
  449. Error += glm::epsilonEqual(Result.y, TestBVec4[i].Result.y, glm::epsilon<float>()) ? 0 : 1;
  450. Error += glm::epsilonEqual(Result.z, TestBVec4[i].Result.z, glm::epsilon<float>()) ? 0 : 1;
  451. Error += glm::epsilonEqual(Result.w, TestBVec4[i].Result.w, glm::epsilon<float>()) ? 0 : 1;
  452. }
  453. }
  454. return Error;
  455. }
  456. }//namespace mix_
  457. namespace step_
  458. {
  459. template <typename EDGE, typename VEC>
  460. struct entry
  461. {
  462. EDGE edge;
  463. VEC x;
  464. VEC result;
  465. };
  466. entry<float, glm::vec4> TestVec4Scalar [] =
  467. {
  468. { 0.0f, glm::vec4(1.0f, 2.0f, 3.0f, 4.0f), glm::vec4(1.0f) },
  469. { 1.0f, glm::vec4(1.0f, 2.0f, 3.0f, 4.0f), glm::vec4(1.0f) },
  470. { 0.0f, glm::vec4(-1.0f, -2.0f, -3.0f, -4.0f), glm::vec4(0.0f) }
  471. };
  472. entry<glm::vec4, glm::vec4> TestVec4Vector [] =
  473. {
  474. { glm::vec4(-1.0f, -2.0f, -3.0f, -4.0f), glm::vec4(-2.0f, -3.0f, -4.0f, -5.0f), glm::vec4(0.0f) },
  475. { glm::vec4( 0.0f, 1.0f, 2.0f, 3.0f), glm::vec4( 1.0f, 2.0f, 3.0f, 4.0f), glm::vec4(1.0f) },
  476. { glm::vec4( 2.0f, 3.0f, 4.0f, 5.0f), glm::vec4( 1.0f, 2.0f, 3.0f, 4.0f), glm::vec4(0.0f) },
  477. { glm::vec4( 0.0f, 1.0f, 2.0f, 3.0f), glm::vec4(-1.0f,-2.0f,-3.0f,-4.0f), glm::vec4(0.0f) }
  478. };
  479. int test()
  480. {
  481. int Error = 0;
  482. // vec4 and float
  483. {
  484. for (std::size_t i = 0; i < sizeof(TestVec4Scalar) / sizeof(entry<float, glm::vec4>); ++i)
  485. {
  486. glm::vec4 Result = glm::step(TestVec4Scalar[i].edge, TestVec4Scalar[i].x);
  487. Error += glm::all(glm::epsilonEqual(Result, TestVec4Scalar[i].result, glm::epsilon<float>())) ? 0 : 1;
  488. }
  489. }
  490. // vec4 and vec4
  491. {
  492. for (std::size_t i = 0; i < sizeof(TestVec4Vector) / sizeof(entry<glm::vec4, glm::vec4>); ++i)
  493. {
  494. glm::vec4 Result = glm::step(TestVec4Vector[i].edge, TestVec4Vector[i].x);
  495. Error += glm::all(glm::epsilonEqual(Result, TestVec4Vector[i].result, glm::epsilon<float>())) ? 0 : 1;
  496. }
  497. }
  498. return Error;
  499. }
  500. }//namespace step_
  501. namespace round_
  502. {
  503. int test()
  504. {
  505. int Error = 0;
  506. {
  507. float A = glm::round(0.0f);
  508. Error += A == 0.0f ? 0 : 1;
  509. float B = glm::round(0.5f);
  510. Error += B == 1.0f ? 0 : 1;
  511. float C = glm::round(1.0f);
  512. Error += C == 1.0f ? 0 : 1;
  513. float D = glm::round(0.1f);
  514. Error += D == 0.0f ? 0 : 1;
  515. float E = glm::round(0.9f);
  516. Error += E == 1.0f ? 0 : 1;
  517. float F = glm::round(1.5f);
  518. Error += F == 2.0f ? 0 : 1;
  519. float G = glm::round(1.9f);
  520. Error += G == 2.0f ? 0 : 1;
  521. }
  522. {
  523. float A = glm::round(-0.0f);
  524. Error += A == 0.0f ? 0 : 1;
  525. float B = glm::round(-0.5f);
  526. Error += B == -1.0f ? 0 : 1;
  527. float C = glm::round(-1.0f);
  528. Error += C == -1.0f ? 0 : 1;
  529. float D = glm::round(-0.1f);
  530. Error += D == 0.0f ? 0 : 1;
  531. float E = glm::round(-0.9f);
  532. Error += E == -1.0f ? 0 : 1;
  533. float F = glm::round(-1.5f);
  534. Error += F == -2.0f ? 0 : 1;
  535. float G = glm::round(-1.9f);
  536. Error += G == -2.0f ? 0 : 1;
  537. }
  538. return Error;
  539. }
  540. }//namespace round_
  541. namespace roundEven
  542. {
  543. int test()
  544. {
  545. int Error = 0;
  546. {
  547. float A1 = glm::roundEven(-1.5f);
  548. Error += glm::epsilonEqual(A1, -2.0f, 0.0001f) ? 0 : 1;
  549. float A2 = glm::roundEven(1.5f);
  550. Error += glm::epsilonEqual(A2, 2.0f, 0.0001f) ? 0 : 1;
  551. float A5 = glm::roundEven(-2.5f);
  552. Error += glm::epsilonEqual(A5, -2.0f, 0.0001f) ? 0 : 1;
  553. float A6 = glm::roundEven(2.5f);
  554. Error += glm::epsilonEqual(A6, 2.0f, 0.0001f) ? 0 : 1;
  555. float A3 = glm::roundEven(-3.5f);
  556. Error += glm::epsilonEqual(A3, -4.0f, 0.0001f) ? 0 : 1;
  557. float A4 = glm::roundEven(3.5f);
  558. Error += glm::epsilonEqual(A4, 4.0f, 0.0001f) ? 0 : 1;
  559. float C7 = glm::roundEven(-4.5f);
  560. Error += glm::epsilonEqual(C7, -4.0f, 0.0001f) ? 0 : 1;
  561. float C8 = glm::roundEven(4.5f);
  562. Error += glm::epsilonEqual(C8, 4.0f, 0.0001f) ? 0 : 1;
  563. float C1 = glm::roundEven(-5.5f);
  564. Error += glm::epsilonEqual(C1, -6.0f, 0.0001f) ? 0 : 1;
  565. float C2 = glm::roundEven(5.5f);
  566. Error += glm::epsilonEqual(C2, 6.0f, 0.0001f) ? 0 : 1;
  567. float C3 = glm::roundEven(-6.5f);
  568. Error += glm::epsilonEqual(C3, -6.0f, 0.0001f) ? 0 : 1;
  569. float C4 = glm::roundEven(6.5f);
  570. Error += glm::epsilonEqual(C4, 6.0f, 0.0001f) ? 0 : 1;
  571. float C5 = glm::roundEven(-7.5f);
  572. Error += glm::epsilonEqual(C5, -8.0f, 0.0001f) ? 0 : 1;
  573. float C6 = glm::roundEven(7.5f);
  574. Error += glm::epsilonEqual(C6, 8.0f, 0.0001f) ? 0 : 1;
  575. Error += 0;
  576. }
  577. {
  578. float A7 = glm::roundEven(-2.4f);
  579. Error += glm::epsilonEqual(A7, -2.0f, 0.0001f) ? 0 : 1;
  580. float A8 = glm::roundEven(2.4f);
  581. Error += glm::epsilonEqual(A8, 2.0f, 0.0001f) ? 0 : 1;
  582. float B1 = glm::roundEven(-2.6f);
  583. Error += glm::epsilonEqual(B1, -3.0f, 0.0001f) ? 0 : 1;
  584. float B2 = glm::roundEven(2.6f);
  585. Error += glm::epsilonEqual(B2, 3.0f, 0.0001f) ? 0 : 1;
  586. float B3 = glm::roundEven(-2.0f);
  587. Error += glm::epsilonEqual(B3, -2.0f, 0.0001f) ? 0 : 1;
  588. float B4 = glm::roundEven(2.0f);
  589. Error += glm::epsilonEqual(B4, 2.0f, 0.0001f) ? 0 : 1;
  590. Error += 0;
  591. }
  592. {
  593. float A = glm::roundEven(0.0f);
  594. Error += A == 0.0f ? 0 : 1;
  595. float B = glm::roundEven(0.5f);
  596. Error += B == 0.0f ? 0 : 1;
  597. float C = glm::roundEven(1.0f);
  598. Error += C == 1.0f ? 0 : 1;
  599. float D = glm::roundEven(0.1f);
  600. Error += D == 0.0f ? 0 : 1;
  601. float E = glm::roundEven(0.9f);
  602. Error += E == 1.0f ? 0 : 1;
  603. float F = glm::roundEven(1.5f);
  604. Error += F == 2.0f ? 0 : 1;
  605. float G = glm::roundEven(1.9f);
  606. Error += G == 2.0f ? 0 : 1;
  607. }
  608. {
  609. float A = glm::roundEven(-0.0f);
  610. Error += A == 0.0f ? 0 : 1;
  611. float B = glm::roundEven(-0.5f);
  612. Error += B == -0.0f ? 0 : 1;
  613. float C = glm::roundEven(-1.0f);
  614. Error += C == -1.0f ? 0 : 1;
  615. float D = glm::roundEven(-0.1f);
  616. Error += D == 0.0f ? 0 : 1;
  617. float E = glm::roundEven(-0.9f);
  618. Error += E == -1.0f ? 0 : 1;
  619. float F = glm::roundEven(-1.5f);
  620. Error += F == -2.0f ? 0 : 1;
  621. float G = glm::roundEven(-1.9f);
  622. Error += G == -2.0f ? 0 : 1;
  623. }
  624. {
  625. float A = glm::roundEven(1.5f);
  626. Error += A == 2.0f ? 0 : 1;
  627. float B = glm::roundEven(2.5f);
  628. Error += B == 2.0f ? 0 : 1;
  629. float C = glm::roundEven(3.5f);
  630. Error += C == 4.0f ? 0 : 1;
  631. float D = glm::roundEven(4.5f);
  632. Error += D == 4.0f ? 0 : 1;
  633. float E = glm::roundEven(5.5f);
  634. Error += E == 6.0f ? 0 : 1;
  635. float F = glm::roundEven(6.5f);
  636. Error += F == 6.0f ? 0 : 1;
  637. float G = glm::roundEven(7.5f);
  638. Error += G == 8.0f ? 0 : 1;
  639. }
  640. {
  641. float A = glm::roundEven(-1.5f);
  642. Error += A == -2.0f ? 0 : 1;
  643. float B = glm::roundEven(-2.5f);
  644. Error += B == -2.0f ? 0 : 1;
  645. float C = glm::roundEven(-3.5f);
  646. Error += C == -4.0f ? 0 : 1;
  647. float D = glm::roundEven(-4.5f);
  648. Error += D == -4.0f ? 0 : 1;
  649. float E = glm::roundEven(-5.5f);
  650. Error += E == -6.0f ? 0 : 1;
  651. float F = glm::roundEven(-6.5f);
  652. Error += F == -6.0f ? 0 : 1;
  653. float G = glm::roundEven(-7.5f);
  654. Error += G == -8.0f ? 0 : 1;
  655. }
  656. return Error;
  657. }
  658. }//namespace roundEven
  659. namespace isnan_
  660. {
  661. int test()
  662. {
  663. int Error = 0;
  664. float Zero_f = 0.0;
  665. double Zero_d = 0.0;
  666. {
  667. Error += true == glm::isnan(0.0/Zero_d) ? 0 : 1;
  668. Error += true == glm::any(glm::isnan(glm::dvec2(0.0 / Zero_d))) ? 0 : 1;
  669. Error += true == glm::any(glm::isnan(glm::dvec3(0.0 / Zero_d))) ? 0 : 1;
  670. Error += true == glm::any(glm::isnan(glm::dvec4(0.0 / Zero_d))) ? 0 : 1;
  671. }
  672. {
  673. Error += true == glm::isnan(0.0f/Zero_f) ? 0 : 1;
  674. Error += true == glm::any(glm::isnan(glm::vec2(0.0f/Zero_f))) ? 0 : 1;
  675. Error += true == glm::any(glm::isnan(glm::vec3(0.0f/Zero_f))) ? 0 : 1;
  676. Error += true == glm::any(glm::isnan(glm::vec4(0.0f/Zero_f))) ? 0 : 1;
  677. }
  678. return Error;
  679. }
  680. }//namespace isnan_
  681. namespace isinf_
  682. {
  683. int test()
  684. {
  685. int Error = 0;
  686. float Zero_f = 0.0;
  687. double Zero_d = 0.0;
  688. {
  689. Error += true == glm::isinf( 1.0/Zero_d) ? 0 : 1;
  690. Error += true == glm::isinf(-1.0/Zero_d) ? 0 : 1;
  691. Error += true == glm::any(glm::isinf(glm::dvec2( 1.0/Zero_d))) ? 0 : 1;
  692. Error += true == glm::any(glm::isinf(glm::dvec2(-1.0/Zero_d))) ? 0 : 1;
  693. Error += true == glm::any(glm::isinf(glm::dvec3( 1.0/Zero_d))) ? 0 : 1;
  694. Error += true == glm::any(glm::isinf(glm::dvec3(-1.0/Zero_d))) ? 0 : 1;
  695. Error += true == glm::any(glm::isinf(glm::dvec4( 1.0/Zero_d))) ? 0 : 1;
  696. Error += true == glm::any(glm::isinf(glm::dvec4(-1.0/Zero_d))) ? 0 : 1;
  697. }
  698. {
  699. Error += true == glm::isinf( 1.0f/Zero_f) ? 0 : 1;
  700. Error += true == glm::isinf(-1.0f/Zero_f) ? 0 : 1;
  701. Error += true == glm::any(glm::isinf(glm::vec2( 1.0f/Zero_f))) ? 0 : 1;
  702. Error += true == glm::any(glm::isinf(glm::vec2(-1.0f/Zero_f))) ? 0 : 1;
  703. Error += true == glm::any(glm::isinf(glm::vec3( 1.0f/Zero_f))) ? 0 : 1;
  704. Error += true == glm::any(glm::isinf(glm::vec3(-1.0f/Zero_f))) ? 0 : 1;
  705. Error += true == glm::any(glm::isinf(glm::vec4( 1.0f/Zero_f))) ? 0 : 1;
  706. Error += true == glm::any(glm::isinf(glm::vec4(-1.0f/Zero_f))) ? 0 : 1;
  707. }
  708. return Error;
  709. }
  710. }//namespace isinf_
  711. namespace sign
  712. {
  713. template <typename genFIType>
  714. GLM_FUNC_QUALIFIER genFIType sign_if(genFIType x)
  715. {
  716. GLM_STATIC_ASSERT(
  717. std::numeric_limits<genFIType>::is_iec559 ||
  718. (std::numeric_limits<genFIType>::is_signed && std::numeric_limits<genFIType>::is_integer), "'sign' only accept signed inputs");
  719. genFIType result;
  720. if(x > genFIType(0))
  721. result = genFIType(1);
  722. else if(x < genFIType(0))
  723. result = genFIType(-1);
  724. else
  725. result = genFIType(0);
  726. return result;
  727. }
  728. template <typename genFIType>
  729. GLM_FUNC_QUALIFIER genFIType sign_alu1(genFIType x)
  730. {
  731. GLM_STATIC_ASSERT(
  732. std::numeric_limits<genFIType>::is_signed && std::numeric_limits<genFIType>::is_integer,
  733. "'sign' only accept integer inputs");
  734. return (x >> 31) | ((unsigned)-x >> 31);
  735. }
  736. template <typename genFIType>
  737. GLM_FUNC_QUALIFIER genFIType sign_alu2(genFIType x)
  738. {
  739. GLM_STATIC_ASSERT(
  740. std::numeric_limits<genFIType>::is_signed && std::numeric_limits<genFIType>::is_integer,
  741. "'sign' only accept integer inputs");
  742. return -((unsigned)x >> 31) | (-(unsigned)x >> 31);
  743. }
  744. template <typename genFIType>
  745. GLM_FUNC_QUALIFIER genFIType sign_sub(genFIType x)
  746. {
  747. GLM_STATIC_ASSERT(
  748. std::numeric_limits<genFIType>::is_signed && std::numeric_limits<genFIType>::is_integer,
  749. "'sign' only accept integer inputs");
  750. return ((unsigned)-x >> 31) - ((unsigned)x >> 31);
  751. }
  752. template <typename genFIType>
  753. GLM_FUNC_QUALIFIER genFIType sign_cmp(genFIType x)
  754. {
  755. GLM_STATIC_ASSERT(
  756. std::numeric_limits<genFIType>::is_signed && std::numeric_limits<genFIType>::is_integer,
  757. "'sign' only accept integer inputs");
  758. return (x > 0) - (x < 0);
  759. }
  760. template <typename genType>
  761. struct type
  762. {
  763. genType Value;
  764. genType Return;
  765. };
  766. int test_int32()
  767. {
  768. type<glm::int32> const Data[] =
  769. {
  770. { std::numeric_limits<glm::int32>::max(), 1},
  771. { std::numeric_limits<glm::int32>::min(), -1},
  772. { 0, 0},
  773. { 1, 1},
  774. { 2, 1},
  775. { 3, 1},
  776. {-1,-1},
  777. {-2,-1},
  778. {-3,-1}
  779. };
  780. int Error = 0;
  781. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<glm::int32>); ++i)
  782. {
  783. glm::int32 Result = glm::sign(Data[i].Value);
  784. Error += Data[i].Return == Result ? 0 : 1;
  785. }
  786. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<glm::int32>); ++i)
  787. {
  788. glm::int32 Result = sign_cmp(Data[i].Value);
  789. Error += Data[i].Return == Result ? 0 : 1;
  790. }
  791. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<glm::int32>); ++i)
  792. {
  793. glm::int32 Result = sign_if(Data[i].Value);
  794. Error += Data[i].Return == Result ? 0 : 1;
  795. }
  796. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<glm::int32>); ++i)
  797. {
  798. glm::int32 Result = sign_alu1(Data[i].Value);
  799. Error += Data[i].Return == Result ? 0 : 1;
  800. }
  801. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<glm::int32>); ++i)
  802. {
  803. glm::int32 Result = sign_alu2(Data[i].Value);
  804. Error += Data[i].Return == Result ? 0 : 1;
  805. }
  806. return Error;
  807. }
  808. int test_i32vec4()
  809. {
  810. type<glm::i32vec4> const Data[] =
  811. {
  812. {glm::i32vec4( 1), glm::i32vec4( 1)},
  813. {glm::i32vec4( 0), glm::i32vec4( 0)},
  814. {glm::i32vec4( 2), glm::i32vec4( 1)},
  815. {glm::i32vec4( 3), glm::i32vec4( 1)},
  816. {glm::i32vec4(-1), glm::i32vec4(-1)},
  817. {glm::i32vec4(-2), glm::i32vec4(-1)},
  818. {glm::i32vec4(-3), glm::i32vec4(-1)}
  819. };
  820. int Error = 0;
  821. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<glm::i32vec4>); ++i)
  822. {
  823. glm::i32vec4 Result = glm::sign(Data[i].Value);
  824. Error += glm::all(glm::equal(Data[i].Return, Result)) ? 0 : 1;
  825. }
  826. return Error;
  827. }
  828. int test()
  829. {
  830. int Error = 0;
  831. Error += test_int32();
  832. Error += test_i32vec4();
  833. return Error;
  834. }
  835. int perf_rand(std::size_t Samples)
  836. {
  837. int Error = 0;
  838. std::size_t const Count = Samples;
  839. std::vector<glm::int32> Input, Output;
  840. Input.resize(Count);
  841. Output.resize(Count);
  842. for(std::size_t i = 0; i < Count; ++i)
  843. Input[i] = static_cast<glm::int32>(glm::linearRand(-65536.f, 65536.f));
  844. std::clock_t Timestamp0 = std::clock();
  845. for(std::size_t i = 0; i < Count; ++i)
  846. Output[i] = sign_cmp(Input[i]);
  847. std::clock_t Timestamp1 = std::clock();
  848. for(std::size_t i = 0; i < Count; ++i)
  849. Output[i] = sign_if(Input[i]);
  850. std::clock_t Timestamp2 = std::clock();
  851. for(std::size_t i = 0; i < Count; ++i)
  852. Output[i] = sign_alu1(Input[i]);
  853. std::clock_t Timestamp3 = std::clock();
  854. for(std::size_t i = 0; i < Count; ++i)
  855. Output[i] = sign_alu2(Input[i]);
  856. std::clock_t Timestamp4 = std::clock();
  857. for(std::size_t i = 0; i < Count; ++i)
  858. Output[i] = sign_sub(Input[i]);
  859. std::clock_t Timestamp5 = std::clock();
  860. for(std::size_t i = 0; i < Count; ++i)
  861. Output[i] = glm::sign(Input[i]);
  862. std::clock_t Timestamp6 = std::clock();
  863. std::printf("sign_cmp(rand) Time %d clocks\n", static_cast<unsigned int>(Timestamp1 - Timestamp0));
  864. std::printf("sign_if(rand) Time %d clocks\n", static_cast<unsigned int>(Timestamp2 - Timestamp1));
  865. std::printf("sign_alu1(rand) Time %d clocks\n", static_cast<unsigned int>(Timestamp3 - Timestamp2));
  866. std::printf("sign_alu2(rand) Time %d clocks\n", static_cast<unsigned int>(Timestamp4 - Timestamp3));
  867. std::printf("sign_sub(rand) Time %d clocks\n", static_cast<unsigned int>(Timestamp5 - Timestamp4));
  868. std::printf("glm::sign(rand) Time %d clocks\n", static_cast<unsigned int>(Timestamp6 - Timestamp5));
  869. return Error;
  870. }
  871. int perf_linear(std::size_t Samples)
  872. {
  873. int Error = 0;
  874. std::size_t const Count = Samples;
  875. std::vector<glm::int32> Input, Output;
  876. Input.resize(Count);
  877. Output.resize(Count);
  878. for(std::size_t i = 0; i < Count; ++i)
  879. Input[i] = static_cast<glm::int32>(i);
  880. std::clock_t Timestamp0 = std::clock();
  881. for(std::size_t i = 0; i < Count; ++i)
  882. Output[i] = sign_cmp(Input[i]);
  883. std::clock_t Timestamp1 = std::clock();
  884. for(std::size_t i = 0; i < Count; ++i)
  885. Output[i] = sign_if(Input[i]);
  886. std::clock_t Timestamp2 = std::clock();
  887. for(std::size_t i = 0; i < Count; ++i)
  888. Output[i] = sign_alu1(Input[i]);
  889. std::clock_t Timestamp3 = std::clock();
  890. for(std::size_t i = 0; i < Count; ++i)
  891. Output[i] = sign_alu2(Input[i]);
  892. std::clock_t Timestamp4 = std::clock();
  893. for(std::size_t i = 0; i < Count; ++i)
  894. Output[i] = sign_sub(Input[i]);
  895. std::clock_t Timestamp5 = std::clock();
  896. std::printf("sign_cmp(linear) Time %d clocks\n", static_cast<unsigned int>(Timestamp1 - Timestamp0));
  897. std::printf("sign_if(linear) Time %d clocks\n", static_cast<unsigned int>(Timestamp2 - Timestamp1));
  898. std::printf("sign_alu1(linear) Time %d clocks\n", static_cast<unsigned int>(Timestamp3 - Timestamp2));
  899. std::printf("sign_alu2(linear) Time %d clocks\n", static_cast<unsigned int>(Timestamp4 - Timestamp3));
  900. std::printf("sign_sub(linear) Time %d clocks\n", static_cast<unsigned int>(Timestamp5 - Timestamp4));
  901. return Error;
  902. }
  903. int perf_linear_cal(std::size_t Samples)
  904. {
  905. int Error = 0;
  906. glm::int32 const Count = static_cast<glm::int32>(Samples);
  907. std::clock_t Timestamp0 = std::clock();
  908. glm::int32 Sum = 0;
  909. for(glm::int32 i = 1; i < Count; ++i)
  910. Sum += sign_cmp(i);
  911. std::clock_t Timestamp1 = std::clock();
  912. for(glm::int32 i = 1; i < Count; ++i)
  913. Sum += sign_if(i);
  914. std::clock_t Timestamp2 = std::clock();
  915. for(glm::int32 i = 1; i < Count; ++i)
  916. Sum += sign_alu1(i);
  917. std::clock_t Timestamp3 = std::clock();
  918. for(glm::int32 i = 1; i < Count; ++i)
  919. Sum += sign_alu2(i);
  920. std::clock_t Timestamp4 = std::clock();
  921. for(glm::int32 i = 1; i < Count; ++i)
  922. Sum += sign_sub(i);
  923. std::clock_t Timestamp5 = std::clock();
  924. std::printf("Sum %d\n", static_cast<unsigned int>(Sum));
  925. std::printf("sign_cmp(linear_cal) Time %d clocks\n", static_cast<unsigned int>(Timestamp1 - Timestamp0));
  926. std::printf("sign_if(linear_cal) Time %d clocks\n", static_cast<unsigned int>(Timestamp2 - Timestamp1));
  927. std::printf("sign_alu1(linear_cal) Time %d clocks\n", static_cast<unsigned int>(Timestamp3 - Timestamp2));
  928. std::printf("sign_alu2(linear_cal) Time %d clocks\n", static_cast<unsigned int>(Timestamp4 - Timestamp3));
  929. std::printf("sign_sub(linear_cal) Time %d clocks\n", static_cast<unsigned int>(Timestamp5 - Timestamp4));
  930. return Error;
  931. }
  932. int perf(std::size_t Samples)
  933. {
  934. int Error(0);
  935. Error += perf_linear_cal(Samples);
  936. Error += perf_linear(Samples);
  937. Error += perf_rand(Samples);
  938. return Error;
  939. }
  940. }//namespace sign
  941. namespace frexp_
  942. {
  943. int test()
  944. {
  945. int Error(0);
  946. {
  947. glm::vec1 x(1024);
  948. glm::ivec1 exp;
  949. glm::vec1 A = glm::frexp(x, exp);
  950. Error += glm::all(glm::epsilonEqual(A, glm::vec1(0.5), 0.00001f)) ? 0 : 1;
  951. Error += glm::all(glm::equal(exp, glm::ivec1(11))) ? 0 : 1;
  952. }
  953. {
  954. glm::vec2 x(1024, 0.24);
  955. glm::ivec2 exp;
  956. glm::vec2 A = glm::frexp(x, exp);
  957. Error += glm::all(glm::epsilonEqual(A, glm::vec2(0.5, 0.96), 0.00001f)) ? 0 : 1;
  958. Error += glm::all(glm::equal(exp, glm::ivec2(11, -2))) ? 0 : 1;
  959. }
  960. {
  961. glm::vec3 x(1024, 0.24, 0);
  962. glm::ivec3 exp;
  963. glm::vec3 A = glm::frexp(x, exp);
  964. Error += glm::all(glm::epsilonEqual(A, glm::vec3(0.5, 0.96, 0.0), 0.00001f)) ? 0 : 1;
  965. Error += glm::all(glm::equal(exp, glm::ivec3(11, -2, 0))) ? 0 : 1;
  966. }
  967. {
  968. glm::vec4 x(1024, 0.24, 0, -1.33);
  969. glm::ivec4 exp;
  970. glm::vec4 A = glm::frexp(x, exp);
  971. Error += glm::all(glm::epsilonEqual(A, glm::vec4(0.5, 0.96, 0.0, -0.665), 0.00001f)) ? 0 : 1;
  972. Error += glm::all(glm::equal(exp, glm::ivec4(11, -2, 0, 1))) ? 0 : 1;
  973. }
  974. return Error;
  975. }
  976. }//namespace frexp_
  977. namespace ldexp_
  978. {
  979. int test()
  980. {
  981. int Error(0);
  982. {
  983. glm::vec1 A = glm::vec1(0.5);
  984. glm::ivec1 exp = glm::ivec1(11);
  985. glm::vec1 x = glm::ldexp(A, exp);
  986. Error += glm::all(glm::epsilonEqual(x, glm::vec1(1024),0.00001f)) ? 0 : 1;
  987. }
  988. {
  989. glm::vec2 A = glm::vec2(0.5, 0.96);
  990. glm::ivec2 exp = glm::ivec2(11, -2);
  991. glm::vec2 x = glm::ldexp(A, exp);
  992. Error += glm::all(glm::epsilonEqual(x, glm::vec2(1024, .24),0.00001f)) ? 0 : 1;
  993. }
  994. {
  995. glm::vec3 A = glm::vec3(0.5, 0.96, 0.0);
  996. glm::ivec3 exp = glm::ivec3(11, -2, 0);
  997. glm::vec3 x = glm::ldexp(A, exp);
  998. Error += glm::all(glm::epsilonEqual(x, glm::vec3(1024, .24, 0),0.00001f)) ? 0 : 1;
  999. }
  1000. {
  1001. glm::vec4 A = glm::vec4(0.5, 0.96, 0.0, -0.665);
  1002. glm::ivec4 exp = glm::ivec4(11, -2, 0, 1);
  1003. glm::vec4 x = glm::ldexp(A, exp);
  1004. Error += glm::all(glm::epsilonEqual(x, glm::vec4(1024, .24, 0, -1.33),0.00001f)) ? 0 : 1;
  1005. }
  1006. return Error;
  1007. }
  1008. }//namespace ldexp_
  1009. int main()
  1010. {
  1011. int Error(0);
  1012. Error += sign::test();
  1013. Error += floor_::test();
  1014. Error += mod_::test();
  1015. Error += modf_::test();
  1016. Error += floatBitsToInt::test();
  1017. Error += floatBitsToUint::test();
  1018. Error += step_::test();
  1019. Error += max_::test();
  1020. Error += min_::test();
  1021. Error += mix_::test();
  1022. Error += round_::test();
  1023. Error += roundEven::test();
  1024. Error += isnan_::test();
  1025. Error += isinf_::test();
  1026. Error += frexp_::test();
  1027. Error += ldexp_::test();
  1028. # ifdef NDEBUG
  1029. std::size_t Samples = 1000;
  1030. Error += sign::perf(Samples);
  1031. # endif
  1032. return Error;
  1033. }