core_func_common.cpp 36 KB

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