core_func_common.cpp 34 KB

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