core_func_common.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2. // OpenGL Mathematics Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net)
  3. ///////////////////////////////////////////////////////////////////////////////////////////////////
  4. // Created : 2011-01-15
  5. // Updated : 2011-09-13
  6. // Licence : This source is under MIT licence
  7. // File : test/core/func_common.cpp
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////
  9. #include <glm/gtc/constants.hpp>
  10. #include <glm/gtc/epsilon.hpp>
  11. #include <glm/gtc/vec1.hpp>
  12. #include <glm/gtc/random.hpp>
  13. #include <vector>
  14. #include <cstdio>
  15. #include <cmath>
  16. #include <ctime>
  17. int test_floor()
  18. {
  19. int Error(0);
  20. {
  21. float A(1.1f);
  22. float B = glm::floor(A);
  23. }
  24. {
  25. double A(1.1f);
  26. double B = glm::floor(A);
  27. }
  28. {
  29. glm::vec1 A(1.1f);
  30. glm::vec1 B = glm::floor(A);
  31. Error += glm::all(glm::epsilonEqual(B, glm::vec1(1.0), 0.0001f)) ? 0 : 1;
  32. }
  33. {
  34. glm::dvec1 A(1.1f);
  35. glm::dvec1 B = glm::floor(A);
  36. Error += glm::all(glm::epsilonEqual(B, glm::dvec1(1.0), 0.0001)) ? 0 : 1;
  37. }
  38. {
  39. glm::vec2 A(1.1f);
  40. glm::vec2 B = glm::floor(A);
  41. Error += glm::all(glm::epsilonEqual(B, glm::vec2(1.0), 0.0001f)) ? 0 : 1;
  42. }
  43. {
  44. glm::dvec2 A(1.1f);
  45. glm::dvec2 B = glm::floor(A);
  46. Error += glm::all(glm::epsilonEqual(B, glm::dvec2(1.0), 0.0001)) ? 0 : 1;
  47. }
  48. {
  49. glm::vec3 A(1.1f);
  50. glm::vec3 B = glm::floor(A);
  51. Error += glm::all(glm::epsilonEqual(B, glm::vec3(1.0), 0.0001f)) ? 0 : 1;
  52. }
  53. {
  54. glm::dvec3 A(1.1f);
  55. glm::dvec3 B = glm::floor(A);
  56. Error += glm::all(glm::epsilonEqual(B, glm::dvec3(1.0), 0.0001)) ? 0 : 1;
  57. }
  58. {
  59. glm::vec4 A(1.1f);
  60. glm::vec4 B = glm::floor(A);
  61. Error += glm::all(glm::epsilonEqual(B, glm::vec4(1.0), 0.0001f)) ? 0 : 1;
  62. }
  63. {
  64. glm::dvec4 A(1.1f);
  65. glm::dvec4 B = glm::floor(A);
  66. Error += glm::all(glm::epsilonEqual(B, glm::dvec4(1.0), 0.0001)) ? 0 : 1;
  67. }
  68. return Error;
  69. }
  70. int test_modf()
  71. {
  72. int Error(0);
  73. {
  74. float X(1.5f);
  75. float I(0.0f);
  76. float A = glm::modf(X, I);
  77. Error += I == 1.0f ? 0 : 1;
  78. Error += A == 0.5f ? 0 : 1;
  79. }
  80. {
  81. glm::vec4 X(1.1f, 1.2f, 1.5f, 1.7f);
  82. glm::vec4 I(0.0f);
  83. glm::vec4 A = glm::modf(X, I);
  84. Error += I == glm::vec4(1.0f) ? 0 : 1;
  85. Error += glm::all(glm::epsilonEqual(A, glm::vec4(0.1f, 0.2f, 0.5f, 0.7f), 0.00001f)) ? 0 : 1;
  86. }
  87. {
  88. glm::dvec4 X(1.1, 1.2, 1.5, 1.7);
  89. glm::dvec4 I(0.0);
  90. glm::dvec4 A = glm::modf(X, I);
  91. Error += I == glm::dvec4(1.0) ? 0 : 1;
  92. Error += glm::all(glm::epsilonEqual(A, glm::dvec4(0.1, 0.2, 0.5, 0.7), 0.000000001)) ? 0 : 1;
  93. }
  94. {
  95. double X(1.5);
  96. double I(0.0);
  97. double A = glm::modf(X, I);
  98. Error += I == 1.0 ? 0 : 1;
  99. Error += A == 0.5 ? 0 : 1;
  100. }
  101. return Error;
  102. }
  103. int test_floatBitsToInt()
  104. {
  105. int Error = 0;
  106. {
  107. float A = 1.0f;
  108. int B = glm::floatBitsToInt(A);
  109. float C = glm::intBitsToFloat(B);
  110. int D = *(int*)&A;
  111. Error += B == D ? 0 : 1;
  112. Error += A == C ? 0 : 1;
  113. }
  114. {
  115. glm::vec2 A(1.0f, 2.0f);
  116. glm::ivec2 B = glm::floatBitsToInt(A);
  117. glm::vec2 C = glm::intBitsToFloat(B);
  118. Error += B.x == *(int*)&(A.x) ? 0 : 1;
  119. Error += B.y == *(int*)&(A.y) ? 0 : 1;
  120. Error += A == C? 0 : 1;
  121. }
  122. {
  123. glm::vec3 A(1.0f, 2.0f, 3.0f);
  124. glm::ivec3 B = glm::floatBitsToInt(A);
  125. glm::vec3 C = glm::intBitsToFloat(B);
  126. Error += B.x == *(int*)&(A.x) ? 0 : 1;
  127. Error += B.y == *(int*)&(A.y) ? 0 : 1;
  128. Error += B.z == *(int*)&(A.z) ? 0 : 1;
  129. Error += A == C? 0 : 1;
  130. }
  131. {
  132. glm::vec4 A(1.0f, 2.0f, 3.0f, 4.0f);
  133. glm::ivec4 B = glm::floatBitsToInt(A);
  134. glm::vec4 C = glm::intBitsToFloat(B);
  135. Error += B.x == *(int*)&(A.x) ? 0 : 1;
  136. Error += B.y == *(int*)&(A.y) ? 0 : 1;
  137. Error += B.z == *(int*)&(A.z) ? 0 : 1;
  138. Error += B.w == *(int*)&(A.w) ? 0 : 1;
  139. Error += A == C? 0 : 1;
  140. }
  141. return Error;
  142. }
  143. int test_floatBitsToUint()
  144. {
  145. int Error = 0;
  146. {
  147. float A = 1.0f;
  148. glm::uint B = glm::floatBitsToUint(A);
  149. float C = glm::intBitsToFloat(B);
  150. Error += B == *(glm::uint*)&A ? 0 : 1;
  151. Error += A == C? 0 : 1;
  152. }
  153. {
  154. glm::vec2 A(1.0f, 2.0f);
  155. glm::uvec2 B = glm::floatBitsToUint(A);
  156. glm::vec2 C = glm::uintBitsToFloat(B);
  157. Error += B.x == *(glm::uint*)&(A.x) ? 0 : 1;
  158. Error += B.y == *(glm::uint*)&(A.y) ? 0 : 1;
  159. Error += A == C ? 0 : 1;
  160. }
  161. {
  162. glm::vec3 A(1.0f, 2.0f, 3.0f);
  163. glm::uvec3 B = glm::floatBitsToUint(A);
  164. glm::vec3 C = glm::uintBitsToFloat(B);
  165. Error += B.x == *(glm::uint*)&(A.x) ? 0 : 1;
  166. Error += B.y == *(glm::uint*)&(A.y) ? 0 : 1;
  167. Error += B.z == *(glm::uint*)&(A.z) ? 0 : 1;
  168. Error += A == C? 0 : 1;
  169. }
  170. {
  171. glm::vec4 A(1.0f, 2.0f, 3.0f, 4.0f);
  172. glm::uvec4 B = glm::floatBitsToUint(A);
  173. glm::vec4 C = glm::uintBitsToFloat(B);
  174. Error += B.x == *(glm::uint*)&(A.x) ? 0 : 1;
  175. Error += B.y == *(glm::uint*)&(A.y) ? 0 : 1;
  176. Error += B.z == *(glm::uint*)&(A.z) ? 0 : 1;
  177. Error += B.w == *(glm::uint*)&(A.w) ? 0 : 1;
  178. Error += A == C? 0 : 1;
  179. }
  180. return Error;
  181. }
  182. int test_min()
  183. {
  184. int Error = 0;
  185. glm::vec1 A0 = glm::min(glm::vec1(1), glm::vec1(1));
  186. glm::vec2 B0 = glm::min(glm::vec2(1), glm::vec2(1));
  187. glm::vec2 B1 = glm::min(glm::vec2(1), 1.0f);
  188. bool B2 = glm::all(glm::equal(B0, B1));
  189. Error += B2 ? 0 : 1;
  190. glm::vec3 C0 = glm::min(glm::vec3(1), glm::vec3(1));
  191. glm::vec3 C1 = glm::min(glm::vec3(1), 1.0f);
  192. bool C2 = glm::all(glm::equal(C0, C1));
  193. Error += C2 ? 0 : 1;
  194. glm::vec4 D0 = glm::min(glm::vec4(1), glm::vec4(1));
  195. glm::vec4 D1 = glm::min(glm::vec4(1), 1.0f);
  196. bool D2 = glm::all(glm::equal(D0, D1));
  197. Error += D2 ? 0 : 1;
  198. return Error;
  199. }
  200. int test_max()
  201. {
  202. int Error = 0;
  203. glm::vec1 A0 = glm::max(glm::vec1(1), glm::vec1(1));
  204. glm::vec2 B0 = glm::max(glm::vec2(1), glm::vec2(1));
  205. glm::vec2 B1 = glm::max(glm::vec2(1), 1.0f);
  206. bool B2 = glm::all(glm::equal(B0, B1));
  207. Error += B2 ? 0 : 1;
  208. glm::vec3 C0 = glm::max(glm::vec3(1), glm::vec3(1));
  209. glm::vec3 C1 = glm::max(glm::vec3(1), 1.0f);
  210. bool C2 = glm::all(glm::equal(C0, C1));
  211. Error += C2 ? 0 : 1;
  212. glm::vec4 D0 = glm::max(glm::vec4(1), glm::vec4(1));
  213. glm::vec4 D1 = glm::max(glm::vec4(1), 1.0f);
  214. bool D2 = glm::all(glm::equal(D0, D1));
  215. Error += D2 ? 0 : 1;
  216. return Error;
  217. }
  218. int test_clamp()
  219. {
  220. int Error = 0;
  221. return Error;
  222. }
  223. namespace test_mix
  224. {
  225. template <typename T, typename B>
  226. struct test
  227. {
  228. T x;
  229. T y;
  230. B a;
  231. T Result;
  232. };
  233. test<float, bool> TestBool[] =
  234. {
  235. {0.0f, 1.0f, false, 0.0f},
  236. {0.0f, 1.0f, true, 1.0f},
  237. {-1.0f, 1.0f, false, -1.0f},
  238. {-1.0f, 1.0f, true, 1.0f}
  239. };
  240. test<float, float> TestFloat[] =
  241. {
  242. {0.0f, 1.0f, 0.0f, 0.0f},
  243. {0.0f, 1.0f, 1.0f, 1.0f},
  244. {-1.0f, 1.0f, 0.0f, -1.0f},
  245. {-1.0f, 1.0f, 1.0f, 1.0f}
  246. };
  247. test<glm::vec2, bool> TestVec2Bool[] =
  248. {
  249. {glm::vec2(0.0f), glm::vec2(1.0f), false, glm::vec2(0.0f)},
  250. {glm::vec2(0.0f), glm::vec2(1.0f), true, glm::vec2(1.0f)},
  251. {glm::vec2(-1.0f), glm::vec2(1.0f), false, glm::vec2(-1.0f)},
  252. {glm::vec2(-1.0f), glm::vec2(1.0f), true, glm::vec2(1.0f)}
  253. };
  254. test<glm::vec2, glm::bvec2> TestBVec2[] =
  255. {
  256. {glm::vec2(0.0f), glm::vec2(1.0f), glm::bvec2(false), glm::vec2(0.0f)},
  257. {glm::vec2(0.0f), glm::vec2(1.0f), glm::bvec2(true), glm::vec2(1.0f)},
  258. {glm::vec2(-1.0f), glm::vec2(1.0f), glm::bvec2(false), glm::vec2(-1.0f)},
  259. {glm::vec2(-1.0f), glm::vec2(1.0f), glm::bvec2(true), glm::vec2(1.0f)},
  260. {glm::vec2(-1.0f), glm::vec2(1.0f), glm::bvec2(true, false), glm::vec2(1.0f, -1.0f)}
  261. };
  262. test<glm::vec3, bool> TestVec3Bool[] =
  263. {
  264. {glm::vec3(0.0f), glm::vec3(1.0f), false, glm::vec3(0.0f)},
  265. {glm::vec3(0.0f), glm::vec3(1.0f), true, glm::vec3(1.0f)},
  266. {glm::vec3(-1.0f), glm::vec3(1.0f), false, glm::vec3(-1.0f)},
  267. {glm::vec3(-1.0f), glm::vec3(1.0f), true, glm::vec3(1.0f)}
  268. };
  269. test<glm::vec3, glm::bvec3> TestBVec3[] =
  270. {
  271. {glm::vec3(0.0f), glm::vec3(1.0f), glm::bvec3(false), glm::vec3(0.0f)},
  272. {glm::vec3(0.0f), glm::vec3(1.0f), glm::bvec3(true), glm::vec3(1.0f)},
  273. {glm::vec3(-1.0f), glm::vec3(1.0f), glm::bvec3(false), glm::vec3(-1.0f)},
  274. {glm::vec3(-1.0f), glm::vec3(1.0f), glm::bvec3(true), glm::vec3(1.0f)},
  275. {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)}
  276. };
  277. test<glm::vec4, bool> TestVec4Bool[] =
  278. {
  279. {glm::vec4(0.0f), glm::vec4(1.0f), false, glm::vec4(0.0f)},
  280. {glm::vec4(0.0f), glm::vec4(1.0f), true, glm::vec4(1.0f)},
  281. {glm::vec4(-1.0f), glm::vec4(1.0f), false, glm::vec4(-1.0f)},
  282. {glm::vec4(-1.0f), glm::vec4(1.0f), true, glm::vec4(1.0f)}
  283. };
  284. test<glm::vec4, glm::bvec4> TestBVec4[] =
  285. {
  286. {glm::vec4(0.0f), glm::vec4(1.0f), glm::bvec4(false), glm::vec4(0.0f)},
  287. {glm::vec4(0.0f), glm::vec4(1.0f), glm::bvec4(true), glm::vec4(1.0f)},
  288. {glm::vec4(-1.0f), glm::vec4(1.0f), glm::bvec4(false), glm::vec4(-1.0f)},
  289. {glm::vec4(-1.0f), glm::vec4(1.0f), glm::bvec4(true), glm::vec4(1.0f)},
  290. {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)}
  291. };
  292. int run()
  293. {
  294. int Error = 0;
  295. // Float with bool
  296. {
  297. for(std::size_t i = 0; i < sizeof(TestBool) / sizeof(test<float, bool>); ++i)
  298. {
  299. float Result = glm::mix(TestBool[i].x, TestBool[i].y, TestBool[i].a);
  300. Error += glm::epsilonEqual(Result, TestBool[i].Result, glm::epsilon<float>()) ? 0 : 1;
  301. }
  302. }
  303. // Float with float
  304. {
  305. for(std::size_t i = 0; i < sizeof(TestFloat) / sizeof(test<float, float>); ++i)
  306. {
  307. float Result = glm::mix(TestFloat[i].x, TestFloat[i].y, TestFloat[i].a);
  308. Error += glm::epsilonEqual(Result, TestFloat[i].Result, glm::epsilon<float>()) ? 0 : 1;
  309. }
  310. }
  311. // vec2 with bool
  312. {
  313. for(std::size_t i = 0; i < sizeof(TestVec2Bool) / sizeof(test<glm::vec2, bool>); ++i)
  314. {
  315. glm::vec2 Result = glm::mix(TestVec2Bool[i].x, TestVec2Bool[i].y, TestVec2Bool[i].a);
  316. Error += glm::epsilonEqual(Result.x, TestVec2Bool[i].Result.x, glm::epsilon<float>()) ? 0 : 1;
  317. Error += glm::epsilonEqual(Result.y, TestVec2Bool[i].Result.y, glm::epsilon<float>()) ? 0 : 1;
  318. }
  319. }
  320. // vec2 with bvec2
  321. {
  322. for(std::size_t i = 0; i < sizeof(TestBVec2) / sizeof(test<glm::vec2, glm::bvec2>); ++i)
  323. {
  324. glm::vec2 Result = glm::mix(TestBVec2[i].x, TestBVec2[i].y, TestBVec2[i].a);
  325. Error += glm::epsilonEqual(Result.x, TestBVec2[i].Result.x, glm::epsilon<float>()) ? 0 : 1;
  326. Error += glm::epsilonEqual(Result.y, TestBVec2[i].Result.y, glm::epsilon<float>()) ? 0 : 1;
  327. }
  328. }
  329. // vec3 with bool
  330. {
  331. for(std::size_t i = 0; i < sizeof(TestVec3Bool) / sizeof(test<glm::vec3, bool>); ++i)
  332. {
  333. glm::vec3 Result = glm::mix(TestVec3Bool[i].x, TestVec3Bool[i].y, TestVec3Bool[i].a);
  334. Error += glm::epsilonEqual(Result.x, TestVec3Bool[i].Result.x, glm::epsilon<float>()) ? 0 : 1;
  335. Error += glm::epsilonEqual(Result.y, TestVec3Bool[i].Result.y, glm::epsilon<float>()) ? 0 : 1;
  336. Error += glm::epsilonEqual(Result.z, TestVec3Bool[i].Result.z, glm::epsilon<float>()) ? 0 : 1;
  337. }
  338. }
  339. // vec3 with bvec3
  340. {
  341. for(std::size_t i = 0; i < sizeof(TestBVec3) / sizeof(test<glm::vec3, glm::bvec3>); ++i)
  342. {
  343. glm::vec3 Result = glm::mix(TestBVec3[i].x, TestBVec3[i].y, TestBVec3[i].a);
  344. Error += glm::epsilonEqual(Result.x, TestBVec3[i].Result.x, glm::epsilon<float>()) ? 0 : 1;
  345. Error += glm::epsilonEqual(Result.y, TestBVec3[i].Result.y, glm::epsilon<float>()) ? 0 : 1;
  346. Error += glm::epsilonEqual(Result.z, TestBVec3[i].Result.z, glm::epsilon<float>()) ? 0 : 1;
  347. }
  348. }
  349. // vec4 with bool
  350. {
  351. for(std::size_t i = 0; i < sizeof(TestVec4Bool) / sizeof(test<glm::vec4, bool>); ++i)
  352. {
  353. glm::vec4 Result = glm::mix(TestVec4Bool[i].x, TestVec4Bool[i].y, TestVec4Bool[i].a);
  354. Error += glm::epsilonEqual(Result.x, TestVec4Bool[i].Result.x, glm::epsilon<float>()) ? 0 : 1;
  355. Error += glm::epsilonEqual(Result.y, TestVec4Bool[i].Result.y, glm::epsilon<float>()) ? 0 : 1;
  356. Error += glm::epsilonEqual(Result.z, TestVec4Bool[i].Result.z, glm::epsilon<float>()) ? 0 : 1;
  357. Error += glm::epsilonEqual(Result.w, TestVec4Bool[i].Result.w, glm::epsilon<float>()) ? 0 : 1;
  358. }
  359. }
  360. // vec4 with bvec4
  361. {
  362. for(std::size_t i = 0; i < sizeof(TestBVec4) / sizeof(test<glm::vec4, glm::bvec4>); ++i)
  363. {
  364. glm::vec4 Result = glm::mix(TestBVec4[i].x, TestBVec4[i].y, TestBVec4[i].a);
  365. Error += glm::epsilonEqual(Result.x, TestBVec4[i].Result.x, glm::epsilon<float>()) ? 0 : 1;
  366. Error += glm::epsilonEqual(Result.y, TestBVec4[i].Result.y, glm::epsilon<float>()) ? 0 : 1;
  367. Error += glm::epsilonEqual(Result.z, TestBVec4[i].Result.z, glm::epsilon<float>()) ? 0 : 1;
  368. Error += glm::epsilonEqual(Result.w, TestBVec4[i].Result.w, glm::epsilon<float>()) ? 0 : 1;
  369. }
  370. }
  371. return Error;
  372. }
  373. }//namespace test_mix
  374. namespace test_step
  375. {
  376. template <typename EDGE, typename VEC>
  377. struct test
  378. {
  379. EDGE edge;
  380. VEC x;
  381. VEC result;
  382. };
  383. test<float, glm::vec4> TestVec4Scalar [] =
  384. {
  385. { 0.0f, glm::vec4(1.0f, 2.0f, 3.0f, 4.0f), glm::vec4(1.0f) },
  386. { 1.0f, glm::vec4(1.0f, 2.0f, 3.0f, 4.0f), glm::vec4(1.0f) },
  387. { 0.0f, glm::vec4(-1.0f, -2.0f, -3.0f, -4.0f), glm::vec4(0.0f) }
  388. };
  389. test<glm::vec4, glm::vec4> TestVec4Vector [] =
  390. {
  391. { glm::vec4(-1.0f, -2.0f, -3.0f, -4.0f), glm::vec4(-2.0f, -3.0f, -4.0f, -5.0f), glm::vec4(0.0f) },
  392. { glm::vec4( 0.0f, 1.0f, 2.0f, 3.0f), glm::vec4( 1.0f, 2.0f, 3.0f, 4.0f), glm::vec4(1.0f) },
  393. { glm::vec4( 2.0f, 3.0f, 4.0f, 5.0f), glm::vec4( 1.0f, 2.0f, 3.0f, 4.0f), glm::vec4(0.0f) },
  394. { glm::vec4( 0.0f, 1.0f, 2.0f, 3.0f), glm::vec4(-1.0f,-2.0f,-3.0f,-4.0f), glm::vec4(0.0f) }
  395. };
  396. int run()
  397. {
  398. int Error = 0;
  399. // vec4 and float
  400. {
  401. for (std::size_t i = 0; i < sizeof(TestVec4Scalar) / sizeof(test<float, glm::vec4>); ++i)
  402. {
  403. glm::vec4 Result = glm::step(TestVec4Scalar[i].edge, TestVec4Scalar[i].x);
  404. Error += glm::all(glm::epsilonEqual(Result, TestVec4Scalar[i].result, glm::epsilon<float>())) ? 0 : 1;
  405. }
  406. }
  407. // vec4 and vec4
  408. {
  409. for (std::size_t i = 0; i < sizeof(TestVec4Vector) / sizeof(test<glm::vec4, glm::vec4>); ++i)
  410. {
  411. glm::vec4 Result = glm::step(TestVec4Vector[i].edge, TestVec4Vector[i].x);
  412. Error += glm::all(glm::epsilonEqual(Result, TestVec4Vector[i].result, glm::epsilon<float>())) ? 0 : 1;
  413. }
  414. }
  415. return Error;
  416. }
  417. }//namespace test_step
  418. int test_round()
  419. {
  420. int Error = 0;
  421. {
  422. float A = glm::round(0.0f);
  423. Error += A == 0.0f ? 0 : 1;
  424. float B = glm::round(0.5f);
  425. Error += B == 1.0f ? 0 : 1;
  426. float C = glm::round(1.0f);
  427. Error += C == 1.0f ? 0 : 1;
  428. float D = glm::round(0.1f);
  429. Error += D == 0.0f ? 0 : 1;
  430. float E = glm::round(0.9f);
  431. Error += E == 1.0f ? 0 : 1;
  432. float F = glm::round(1.5f);
  433. Error += F == 2.0f ? 0 : 1;
  434. float G = glm::round(1.9f);
  435. Error += G == 2.0f ? 0 : 1;
  436. }
  437. {
  438. float A = glm::round(-0.0f);
  439. Error += A == 0.0f ? 0 : 1;
  440. float B = glm::round(-0.5f);
  441. Error += B == -1.0f ? 0 : 1;
  442. float C = glm::round(-1.0f);
  443. Error += C == -1.0f ? 0 : 1;
  444. float D = glm::round(-0.1f);
  445. Error += D == 0.0f ? 0 : 1;
  446. float E = glm::round(-0.9f);
  447. Error += E == -1.0f ? 0 : 1;
  448. float F = glm::round(-1.5f);
  449. Error += F == -2.0f ? 0 : 1;
  450. float G = glm::round(-1.9f);
  451. Error += G == -2.0f ? 0 : 1;
  452. }
  453. return Error;
  454. }
  455. int test_roundEven()
  456. {
  457. int Error = 0;
  458. {
  459. float A1 = glm::roundEven(-1.5f);
  460. Error += glm::epsilonEqual(A1, -2.0f, 0.0001f) ? 0 : 1;
  461. float A2 = glm::roundEven(1.5f);
  462. Error += glm::epsilonEqual(A2, 2.0f, 0.0001f) ? 0 : 1;
  463. float A5 = glm::roundEven(-2.5f);
  464. Error += glm::epsilonEqual(A5, -2.0f, 0.0001f) ? 0 : 1;
  465. float A6 = glm::roundEven(2.5f);
  466. Error += glm::epsilonEqual(A6, 2.0f, 0.0001f) ? 0 : 1;
  467. float A3 = glm::roundEven(-3.5f);
  468. Error += glm::epsilonEqual(A3, -4.0f, 0.0001f) ? 0 : 1;
  469. float A4 = glm::roundEven(3.5f);
  470. Error += glm::epsilonEqual(A4, 4.0f, 0.0001f) ? 0 : 1;
  471. float C7 = glm::roundEven(-4.5f);
  472. Error += glm::epsilonEqual(C7, -4.0f, 0.0001f) ? 0 : 1;
  473. float C8 = glm::roundEven(4.5f);
  474. Error += glm::epsilonEqual(C8, 4.0f, 0.0001f) ? 0 : 1;
  475. float C1 = glm::roundEven(-5.5f);
  476. Error += glm::epsilonEqual(C1, -6.0f, 0.0001f) ? 0 : 1;
  477. float C2 = glm::roundEven(5.5f);
  478. Error += glm::epsilonEqual(C2, 6.0f, 0.0001f) ? 0 : 1;
  479. float C3 = glm::roundEven(-6.5f);
  480. Error += glm::epsilonEqual(C3, -6.0f, 0.0001f) ? 0 : 1;
  481. float C4 = glm::roundEven(6.5f);
  482. Error += glm::epsilonEqual(C4, 6.0f, 0.0001f) ? 0 : 1;
  483. float C5 = glm::roundEven(-7.5f);
  484. Error += glm::epsilonEqual(C5, -8.0f, 0.0001f) ? 0 : 1;
  485. float C6 = glm::roundEven(7.5f);
  486. Error += glm::epsilonEqual(C6, 8.0f, 0.0001f) ? 0 : 1;
  487. Error += 0;
  488. }
  489. {
  490. float A7 = glm::roundEven(-2.4f);
  491. Error += glm::epsilonEqual(A7, -2.0f, 0.0001f) ? 0 : 1;
  492. float A8 = glm::roundEven(2.4f);
  493. Error += glm::epsilonEqual(A8, 2.0f, 0.0001f) ? 0 : 1;
  494. float B1 = glm::roundEven(-2.6f);
  495. Error += glm::epsilonEqual(B1, -3.0f, 0.0001f) ? 0 : 1;
  496. float B2 = glm::roundEven(2.6f);
  497. Error += glm::epsilonEqual(B2, 3.0f, 0.0001f) ? 0 : 1;
  498. float B3 = glm::roundEven(-2.0f);
  499. Error += glm::epsilonEqual(B3, -2.0f, 0.0001f) ? 0 : 1;
  500. float B4 = glm::roundEven(2.0f);
  501. Error += glm::epsilonEqual(B4, 2.0f, 0.0001f) ? 0 : 1;
  502. Error += 0;
  503. }
  504. {
  505. float A = glm::roundEven(0.0f);
  506. Error += A == 0.0f ? 0 : 1;
  507. float B = glm::roundEven(0.5f);
  508. Error += B == 0.0f ? 0 : 1;
  509. float C = glm::roundEven(1.0f);
  510. Error += C == 1.0f ? 0 : 1;
  511. float D = glm::roundEven(0.1f);
  512. Error += D == 0.0f ? 0 : 1;
  513. float E = glm::roundEven(0.9f);
  514. Error += E == 1.0f ? 0 : 1;
  515. float F = glm::roundEven(1.5f);
  516. Error += F == 2.0f ? 0 : 1;
  517. float G = glm::roundEven(1.9f);
  518. Error += G == 2.0f ? 0 : 1;
  519. }
  520. {
  521. float A = glm::roundEven(-0.0f);
  522. Error += A == 0.0f ? 0 : 1;
  523. float B = glm::roundEven(-0.5f);
  524. Error += B == -0.0f ? 0 : 1;
  525. float C = glm::roundEven(-1.0f);
  526. Error += C == -1.0f ? 0 : 1;
  527. float D = glm::roundEven(-0.1f);
  528. Error += D == 0.0f ? 0 : 1;
  529. float E = glm::roundEven(-0.9f);
  530. Error += E == -1.0f ? 0 : 1;
  531. float F = glm::roundEven(-1.5f);
  532. Error += F == -2.0f ? 0 : 1;
  533. float G = glm::roundEven(-1.9f);
  534. Error += G == -2.0f ? 0 : 1;
  535. }
  536. {
  537. float A = glm::roundEven(1.5f);
  538. Error += A == 2.0f ? 0 : 1;
  539. float B = glm::roundEven(2.5f);
  540. Error += B == 2.0f ? 0 : 1;
  541. float C = glm::roundEven(3.5f);
  542. Error += C == 4.0f ? 0 : 1;
  543. float D = glm::roundEven(4.5f);
  544. Error += D == 4.0f ? 0 : 1;
  545. float E = glm::roundEven(5.5f);
  546. Error += E == 6.0f ? 0 : 1;
  547. float F = glm::roundEven(6.5f);
  548. Error += F == 6.0f ? 0 : 1;
  549. float G = glm::roundEven(7.5f);
  550. Error += G == 8.0f ? 0 : 1;
  551. }
  552. {
  553. float A = glm::roundEven(-1.5f);
  554. Error += A == -2.0f ? 0 : 1;
  555. float B = glm::roundEven(-2.5f);
  556. Error += B == -2.0f ? 0 : 1;
  557. float C = glm::roundEven(-3.5f);
  558. Error += C == -4.0f ? 0 : 1;
  559. float D = glm::roundEven(-4.5f);
  560. Error += D == -4.0f ? 0 : 1;
  561. float E = glm::roundEven(-5.5f);
  562. Error += E == -6.0f ? 0 : 1;
  563. float F = glm::roundEven(-6.5f);
  564. Error += F == -6.0f ? 0 : 1;
  565. float G = glm::roundEven(-7.5f);
  566. Error += G == -8.0f ? 0 : 1;
  567. }
  568. return Error;
  569. }
  570. int test_isnan()
  571. {
  572. int Error = 0;
  573. float Zero_f = 0.0;
  574. double Zero_d = 0.0;
  575. {
  576. Error += true == glm::isnan(0.0/Zero_d) ? 0 : 1;
  577. Error += true == glm::any(glm::isnan(glm::dvec2(0.0 / Zero_d))) ? 0 : 1;
  578. Error += true == glm::any(glm::isnan(glm::dvec3(0.0 / Zero_d))) ? 0 : 1;
  579. Error += true == glm::any(glm::isnan(glm::dvec4(0.0 / Zero_d))) ? 0 : 1;
  580. }
  581. {
  582. Error += true == glm::isnan(0.0f/Zero_f) ? 0 : 1;
  583. Error += true == glm::any(glm::isnan(glm::vec2(0.0f/Zero_f))) ? 0 : 1;
  584. Error += true == glm::any(glm::isnan(glm::vec3(0.0f/Zero_f))) ? 0 : 1;
  585. Error += true == glm::any(glm::isnan(glm::vec4(0.0f/Zero_f))) ? 0 : 1;
  586. }
  587. return Error;
  588. }
  589. int test_isinf()
  590. {
  591. int Error = 0;
  592. float Zero_f = 0.0;
  593. double Zero_d = 0.0;
  594. {
  595. Error += true == glm::isinf( 1.0/Zero_d) ? 0 : 1;
  596. Error += true == glm::isinf(-1.0/Zero_d) ? 0 : 1;
  597. Error += true == glm::any(glm::isinf(glm::dvec2( 1.0/Zero_d))) ? 0 : 1;
  598. Error += true == glm::any(glm::isinf(glm::dvec2(-1.0/Zero_d))) ? 0 : 1;
  599. Error += true == glm::any(glm::isinf(glm::dvec3( 1.0/Zero_d))) ? 0 : 1;
  600. Error += true == glm::any(glm::isinf(glm::dvec3(-1.0/Zero_d))) ? 0 : 1;
  601. Error += true == glm::any(glm::isinf(glm::dvec4( 1.0/Zero_d))) ? 0 : 1;
  602. Error += true == glm::any(glm::isinf(glm::dvec4(-1.0/Zero_d))) ? 0 : 1;
  603. }
  604. {
  605. Error += true == glm::isinf( 1.0f/Zero_f) ? 0 : 1;
  606. Error += true == glm::isinf(-1.0f/Zero_f) ? 0 : 1;
  607. Error += true == glm::any(glm::isinf(glm::vec2( 1.0f/Zero_f))) ? 0 : 1;
  608. Error += true == glm::any(glm::isinf(glm::vec2(-1.0f/Zero_f))) ? 0 : 1;
  609. Error += true == glm::any(glm::isinf(glm::vec3( 1.0f/Zero_f))) ? 0 : 1;
  610. Error += true == glm::any(glm::isinf(glm::vec3(-1.0f/Zero_f))) ? 0 : 1;
  611. Error += true == glm::any(glm::isinf(glm::vec4( 1.0f/Zero_f))) ? 0 : 1;
  612. Error += true == glm::any(glm::isinf(glm::vec4(-1.0f/Zero_f))) ? 0 : 1;
  613. }
  614. return Error;
  615. }
  616. namespace sign
  617. {
  618. template <typename genFIType>
  619. GLM_FUNC_QUALIFIER genFIType sign_if(genFIType x)
  620. {
  621. GLM_STATIC_ASSERT(
  622. std::numeric_limits<genFIType>::is_iec559 ||
  623. (std::numeric_limits<genFIType>::is_signed && std::numeric_limits<genFIType>::is_integer), "'sign' only accept signed inputs");
  624. genFIType result;
  625. if(x > genFIType(0))
  626. result = genFIType(1);
  627. else if(x < genFIType(0))
  628. result = genFIType(-1);
  629. else
  630. result = genFIType(0);
  631. return result;
  632. }
  633. template <typename genFIType>
  634. GLM_FUNC_QUALIFIER genFIType sign_alu1(genFIType x)
  635. {
  636. GLM_STATIC_ASSERT(
  637. std::numeric_limits<genFIType>::is_signed && std::numeric_limits<genFIType>::is_integer,
  638. "'sign' only accept integer inputs");
  639. return (x >> 31) | ((unsigned)-x >> 31);
  640. }
  641. template <typename genFIType>
  642. GLM_FUNC_QUALIFIER genFIType sign_alu2(genFIType x)
  643. {
  644. GLM_STATIC_ASSERT(
  645. std::numeric_limits<genFIType>::is_signed && std::numeric_limits<genFIType>::is_integer,
  646. "'sign' only accept integer inputs");
  647. return -((unsigned)x >> 31) | (-(unsigned)x >> 31);
  648. }
  649. template <typename genFIType>
  650. GLM_FUNC_QUALIFIER genFIType sign_sub(genFIType x)
  651. {
  652. GLM_STATIC_ASSERT(
  653. std::numeric_limits<genFIType>::is_signed && std::numeric_limits<genFIType>::is_integer,
  654. "'sign' only accept integer inputs");
  655. return ((unsigned)-x >> 31) - ((unsigned)x >> 31);
  656. }
  657. template <typename genFIType>
  658. GLM_FUNC_QUALIFIER genFIType sign_cmp(genFIType x)
  659. {
  660. GLM_STATIC_ASSERT(
  661. std::numeric_limits<genFIType>::is_signed && std::numeric_limits<genFIType>::is_integer,
  662. "'sign' only accept integer inputs");
  663. return (x > 0) - (x < 0);
  664. }
  665. template <typename genType>
  666. struct type
  667. {
  668. genType Value;
  669. genType Return;
  670. };
  671. int test_int32()
  672. {
  673. type<glm::int32> const Data[] =
  674. {
  675. { 0, 0},
  676. { 1, 1},
  677. { 2, 1},
  678. { 3, 1},
  679. {-1,-1},
  680. {-2,-1},
  681. {-3,-1}
  682. };
  683. int Error = 0;
  684. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<glm::int32>); ++i)
  685. {
  686. glm::int32 Result = sign_cmp(Data[i].Value);
  687. Error += Data[i].Return == Result ? 0 : 1;
  688. }
  689. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<glm::int32>); ++i)
  690. {
  691. glm::int32 Result = sign_if(Data[i].Value);
  692. Error += Data[i].Return == Result ? 0 : 1;
  693. }
  694. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<glm::int32>); ++i)
  695. {
  696. glm::int32 Result = sign_alu1(Data[i].Value);
  697. Error += Data[i].Return == Result ? 0 : 1;
  698. }
  699. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<glm::int32>); ++i)
  700. {
  701. glm::int32 Result = sign_alu2(Data[i].Value);
  702. Error += Data[i].Return == Result ? 0 : 1;
  703. }
  704. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<glm::int32>); ++i)
  705. {
  706. glm::int32 Result = sign_sub(Data[i].Value);
  707. Error += Data[i].Return == Result ? 0 : 1;
  708. }
  709. return Error;
  710. }
  711. int test()
  712. {
  713. int Error = 0;
  714. Error += test_int32();
  715. return Error;
  716. }
  717. int perf_rand()
  718. {
  719. int Error = 0;
  720. std::size_t const Count = 10000000;
  721. std::vector<glm::int32> Input, Output;
  722. Input.resize(Count);
  723. Output.resize(Count);
  724. for(std::size_t i = 0; i < Count; ++i)
  725. Input[i] = static_cast<glm::int32>(glm::linearRand(-65536.f, 65536.f));
  726. std::clock_t Timestamp0 = std::clock();
  727. for(std::size_t i = 0; i < Count; ++i)
  728. Output[i] = sign_cmp(Input[i]);
  729. std::clock_t Timestamp1 = std::clock();
  730. for(std::size_t i = 0; i < Count; ++i)
  731. Output[i] = sign_if(Input[i]);
  732. std::clock_t Timestamp2 = std::clock();
  733. for(std::size_t i = 0; i < Count; ++i)
  734. Output[i] = sign_alu1(Input[i]);
  735. std::clock_t Timestamp3 = std::clock();
  736. for(std::size_t i = 0; i < Count; ++i)
  737. Output[i] = sign_alu2(Input[i]);
  738. std::clock_t Timestamp4 = std::clock();
  739. for(std::size_t i = 0; i < Count; ++i)
  740. Output[i] = sign_sub(Input[i]);
  741. std::clock_t Timestamp5 = std::clock();
  742. std::printf("sign_cmp(rand) Time %d clocks\n", static_cast<unsigned int>(Timestamp1 - Timestamp0));
  743. std::printf("sign_if(rand) Time %d clocks\n", static_cast<unsigned int>(Timestamp2 - Timestamp1));
  744. std::printf("sign_alu1(rand) Time %d clocks\n", static_cast<unsigned int>(Timestamp3 - Timestamp2));
  745. std::printf("sign_alu2(rand) Time %d clocks\n", static_cast<unsigned int>(Timestamp4 - Timestamp3));
  746. std::printf("sign_sub(rand) Time %d clocks\n", static_cast<unsigned int>(Timestamp5 - Timestamp4));
  747. return Error;
  748. }
  749. int perf_linear()
  750. {
  751. int Error = 0;
  752. std::size_t const Count = 10000000;
  753. std::vector<glm::int32> Input, Output;
  754. Input.resize(Count);
  755. Output.resize(Count);
  756. for(std::size_t i = 0; i < Count; ++i)
  757. Input[i] = static_cast<glm::int32>(i);
  758. std::clock_t Timestamp0 = std::clock();
  759. for(std::size_t i = 0; i < Count; ++i)
  760. Output[i] = sign_cmp(Input[i]);
  761. std::clock_t Timestamp1 = std::clock();
  762. for(std::size_t i = 0; i < Count; ++i)
  763. Output[i] = sign_if(Input[i]);
  764. std::clock_t Timestamp2 = std::clock();
  765. for(std::size_t i = 0; i < Count; ++i)
  766. Output[i] = sign_alu1(Input[i]);
  767. std::clock_t Timestamp3 = std::clock();
  768. for(std::size_t i = 0; i < Count; ++i)
  769. Output[i] = sign_alu2(Input[i]);
  770. std::clock_t Timestamp4 = std::clock();
  771. for(std::size_t i = 0; i < Count; ++i)
  772. Output[i] = sign_sub(Input[i]);
  773. std::clock_t Timestamp5 = std::clock();
  774. std::printf("sign_cmp(linear) Time %d clocks\n", static_cast<unsigned int>(Timestamp1 - Timestamp0));
  775. std::printf("sign_if(linear) Time %d clocks\n", static_cast<unsigned int>(Timestamp2 - Timestamp1));
  776. std::printf("sign_alu1(linear) Time %d clocks\n", static_cast<unsigned int>(Timestamp3 - Timestamp2));
  777. std::printf("sign_alu2(linear) Time %d clocks\n", static_cast<unsigned int>(Timestamp4 - Timestamp3));
  778. std::printf("sign_sub(linear) Time %d clocks\n", static_cast<unsigned int>(Timestamp5 - Timestamp4));
  779. return Error;
  780. }
  781. int perf_linear_cal()
  782. {
  783. int Error = 0;
  784. glm::uint32 const Count = 10000000;
  785. std::clock_t Timestamp0 = std::clock();
  786. glm::int32 Sum = 0;
  787. for(glm::int32 i = 1; i < Count; ++i)
  788. Sum += sign_cmp(i);
  789. std::clock_t Timestamp1 = std::clock();
  790. for(glm::int32 i = 1; i < Count; ++i)
  791. Sum += sign_if(i);
  792. std::clock_t Timestamp2 = std::clock();
  793. for(glm::int32 i = 1; i < Count; ++i)
  794. Sum += sign_alu1(i);
  795. std::clock_t Timestamp3 = std::clock();
  796. for(glm::int32 i = 1; i < Count; ++i)
  797. Sum += sign_alu2(i);
  798. std::clock_t Timestamp4 = std::clock();
  799. for(glm::int32 i = 1; i < Count; ++i)
  800. Sum += sign_sub(i);
  801. std::clock_t Timestamp5 = std::clock();
  802. std::printf("Sum %d\n", static_cast<unsigned int>(Sum));
  803. std::printf("sign_cmp(linear_cal) Time %d clocks\n", static_cast<unsigned int>(Timestamp1 - Timestamp0));
  804. std::printf("sign_if(linear_cal) Time %d clocks\n", static_cast<unsigned int>(Timestamp2 - Timestamp1));
  805. std::printf("sign_alu1(linear_cal) Time %d clocks\n", static_cast<unsigned int>(Timestamp3 - Timestamp2));
  806. std::printf("sign_alu2(linear_cal) Time %d clocks\n", static_cast<unsigned int>(Timestamp4 - Timestamp3));
  807. std::printf("sign_sub(linear_cal) Time %d clocks\n", static_cast<unsigned int>(Timestamp5 - Timestamp4));
  808. return Error;
  809. }
  810. int perf()
  811. {
  812. int Error(0);
  813. Error += perf_linear_cal();
  814. Error += perf_linear();
  815. Error += perf_rand();
  816. return Error;
  817. }
  818. }//namespace sign
  819. int main()
  820. {
  821. int Error(0);
  822. Error += sign::test();
  823. Error += sign::perf();
  824. Error += test_floor();
  825. Error += test_modf();
  826. Error += test_floatBitsToInt();
  827. Error += test_floatBitsToUint();
  828. Error += test_step::run();
  829. Error += test_max();
  830. Error += test_min();
  831. Error += test_mix::run();
  832. Error += test_round();
  833. Error += test_roundEven();
  834. Error += test_isnan();
  835. Error += test_isinf();
  836. return Error;
  837. }