core_func_common.cpp 34 KB

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