func_common.inl 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. /// @ref core
  2. /// @file glm/detail/func_common.inl
  3. #include "../vector_relational.hpp"
  4. #include "compute_common.hpp"
  5. #include "type_vec2.hpp"
  6. #include "type_vec3.hpp"
  7. #include "type_vec4.hpp"
  8. #include "_vectorize.hpp"
  9. #include <limits>
  10. namespace glm
  11. {
  12. // min
  13. template<typename genType>
  14. GLM_FUNC_QUALIFIER GLM_CONSTEXPR genType min(genType x, genType y)
  15. {
  16. GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559 || std::numeric_limits<genType>::is_integer, "'min' only accept floating-point or integer inputs");
  17. return (y < x) ? y : x;
  18. }
  19. // max
  20. template<typename genType>
  21. GLM_FUNC_QUALIFIER GLM_CONSTEXPR genType max(genType x, genType y)
  22. {
  23. GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559 || std::numeric_limits<genType>::is_integer, "'max' only accept floating-point or integer inputs");
  24. return (x < y) ? y : x;
  25. }
  26. // abs
  27. template<>
  28. GLM_FUNC_QUALIFIER GLM_CONSTEXPR int abs(int x)
  29. {
  30. int const y = x >> (sizeof(int) * 8 - 1);
  31. return (x ^ y) - y;
  32. }
  33. // round
  34. # if GLM_HAS_CXX11_STL
  35. using ::std::round;
  36. # else
  37. template<typename genType>
  38. GLM_FUNC_QUALIFIER genType round(genType x)
  39. {
  40. GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'round' only accept floating-point inputs");
  41. return x < static_cast<genType>(0) ? static_cast<genType>(int(x - static_cast<genType>(0.5))) : static_cast<genType>(int(x + static_cast<genType>(0.5)));
  42. }
  43. # endif
  44. // trunc
  45. # if GLM_HAS_CXX11_STL
  46. using ::std::trunc;
  47. # else
  48. template<typename genType>
  49. GLM_FUNC_QUALIFIER genType trunc(genType x)
  50. {
  51. GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'trunc' only accept floating-point inputs");
  52. return x < static_cast<genType>(0) ? -std::floor(-x) : std::floor(x);
  53. }
  54. # endif
  55. }//namespace glm
  56. namespace glm{
  57. namespace detail
  58. {
  59. template<length_t L, typename T, qualifier Q, bool Aligned>
  60. struct compute_abs_vector
  61. {
  62. GLM_FUNC_QUALIFIER GLM_CONSTEXPR static vec<L, T, Q> call(vec<L, T, Q> const& x)
  63. {
  64. return detail::functor1<vec, L, T, T, Q>::call(abs, x);
  65. }
  66. };
  67. template<length_t L, typename T, typename U, qualifier Q, bool Aligned>
  68. struct compute_mix_vector
  69. {
  70. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x, vec<L, T, Q> const& y, vec<L, U, Q> const& a)
  71. {
  72. GLM_STATIC_ASSERT(std::numeric_limits<U>::is_iec559 || GLM_CONFIG_UNRESTRICTED_GENTYPE, "'mix' only accept floating-point inputs for the interpolator a");
  73. return vec<L, T, Q>(vec<L, U, Q>(x) + a * vec<L, U, Q>(y - x));
  74. }
  75. };
  76. template<length_t L, typename T, qualifier Q, bool Aligned>
  77. struct compute_mix_vector<L, T, bool, Q, Aligned>
  78. {
  79. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x, vec<L, T, Q> const& y, vec<L, bool, Q> const& a)
  80. {
  81. vec<L, T, Q> Result;
  82. for(length_t i = 0; i < x.length(); ++i)
  83. Result[i] = a[i] ? y[i] : x[i];
  84. return Result;
  85. }
  86. };
  87. template<length_t L, typename T, typename U, qualifier Q, bool Aligned>
  88. struct compute_mix_scalar
  89. {
  90. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x, vec<L, T, Q> const& y, U const& a)
  91. {
  92. GLM_STATIC_ASSERT(std::numeric_limits<U>::is_iec559 || GLM_CONFIG_UNRESTRICTED_GENTYPE, "'mix' only accept floating-point inputs for the interpolator a");
  93. return vec<L, T, Q>(vec<L, U, Q>(x) + a * vec<L, U, Q>(y - x));
  94. }
  95. };
  96. template<length_t L, typename T, qualifier Q, bool Aligned>
  97. struct compute_mix_scalar<L, T, bool, Q, Aligned>
  98. {
  99. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x, vec<L, T, Q> const& y, bool const& a)
  100. {
  101. return a ? y : x;
  102. }
  103. };
  104. template<typename T, typename U>
  105. struct compute_mix
  106. {
  107. GLM_FUNC_QUALIFIER static T call(T const& x, T const& y, U const& a)
  108. {
  109. GLM_STATIC_ASSERT(std::numeric_limits<U>::is_iec559 || GLM_CONFIG_UNRESTRICTED_GENTYPE, "'mix' only accept floating-point inputs for the interpolator a");
  110. return static_cast<T>(static_cast<U>(x) + a * static_cast<U>(y - x));
  111. }
  112. };
  113. template<typename T>
  114. struct compute_mix<T, bool>
  115. {
  116. GLM_FUNC_QUALIFIER static T call(T const& x, T const& y, bool const& a)
  117. {
  118. return a ? y : x;
  119. }
  120. };
  121. template<length_t L, typename T, qualifier Q, bool isFloat, bool Aligned>
  122. struct compute_sign
  123. {
  124. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x)
  125. {
  126. return vec<L, T, Q>(glm::lessThan(vec<L, T, Q>(0), x)) - vec<L, T, Q>(glm::lessThan(x, vec<L, T, Q>(0)));
  127. }
  128. };
  129. # if GLM_ARCH == GLM_ARCH_X86
  130. template<length_t L, typename T, qualifier Q, bool Aligned>
  131. struct compute_sign<L, T, Q, false, Aligned>
  132. {
  133. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x)
  134. {
  135. T const Shift(static_cast<T>(sizeof(T) * 8 - 1));
  136. vec<L, T, Q> const y(vec<L, typename detail::make_unsigned<T>::type, Q>(-x) >> typename detail::make_unsigned<T>::type(Shift));
  137. return (x >> Shift) | y;
  138. }
  139. };
  140. # endif
  141. template<length_t L, typename T, qualifier Q, bool Aligned>
  142. struct compute_floor
  143. {
  144. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x)
  145. {
  146. return detail::functor1<vec, L, T, T, Q>::call(std::floor, x);
  147. }
  148. };
  149. template<length_t L, typename T, qualifier Q, bool Aligned>
  150. struct compute_ceil
  151. {
  152. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x)
  153. {
  154. return detail::functor1<vec, L, T, T, Q>::call(std::ceil, x);
  155. }
  156. };
  157. template<length_t L, typename T, qualifier Q, bool Aligned>
  158. struct compute_fract
  159. {
  160. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x)
  161. {
  162. return x - floor(x);
  163. }
  164. };
  165. template<length_t L, typename T, qualifier Q, bool Aligned>
  166. struct compute_trunc
  167. {
  168. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x)
  169. {
  170. return detail::functor1<vec, L, T, T, Q>::call(trunc, x);
  171. }
  172. };
  173. template<length_t L, typename T, qualifier Q, bool Aligned>
  174. struct compute_round
  175. {
  176. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x)
  177. {
  178. return detail::functor1<vec, L, T, T, Q>::call(round, x);
  179. }
  180. };
  181. template<length_t L, typename T, qualifier Q, bool Aligned>
  182. struct compute_mod
  183. {
  184. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& a, vec<L, T, Q> const& b)
  185. {
  186. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'mod' only accept floating-point inputs. Include <glm/gtc/integer.hpp> for integer inputs.");
  187. return a - b * floor(a / b);
  188. }
  189. };
  190. template<length_t L, typename T, qualifier Q, bool Aligned>
  191. struct compute_min_vector
  192. {
  193. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
  194. {
  195. return detail::functor2<vec, L, T, Q>::call(min, x, y);
  196. }
  197. };
  198. template<length_t L, typename T, qualifier Q, bool Aligned>
  199. struct compute_max_vector
  200. {
  201. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
  202. {
  203. return detail::functor2<vec, L, T, Q>::call(max, x, y);
  204. }
  205. };
  206. template<length_t L, typename T, qualifier Q, bool Aligned>
  207. struct compute_clamp_vector
  208. {
  209. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x, vec<L, T, Q> const& minVal, vec<L, T, Q> const& maxVal)
  210. {
  211. return min(max(x, minVal), maxVal);
  212. }
  213. };
  214. template<length_t L, typename T, qualifier Q, bool Aligned>
  215. struct compute_step_vector
  216. {
  217. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& edge, vec<L, T, Q> const& x)
  218. {
  219. return mix(vec<L, T, Q>(1), vec<L, T, Q>(0), glm::lessThan(x, edge));
  220. }
  221. };
  222. template<length_t L, typename T, qualifier Q, bool Aligned>
  223. struct compute_smoothstep_vector
  224. {
  225. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& edge0, vec<L, T, Q> const& edge1, vec<L, T, Q> const& x)
  226. {
  227. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559 || GLM_CONFIG_UNRESTRICTED_GENTYPE, "'step' only accept floating-point inputs");
  228. vec<L, T, Q> const tmp(clamp((x - edge0) / (edge1 - edge0), static_cast<T>(0), static_cast<T>(1)));
  229. return tmp * tmp * (static_cast<T>(3) - static_cast<T>(2) * tmp);
  230. }
  231. };
  232. }//namespace detail
  233. template<typename genFIType>
  234. GLM_FUNC_QUALIFIER GLM_CONSTEXPR genFIType abs(genFIType x)
  235. {
  236. return detail::compute_abs<genFIType, std::numeric_limits<genFIType>::is_signed>::call(x);
  237. }
  238. template<length_t L, typename T, qualifier Q>
  239. GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, T, Q> abs(vec<L, T, Q> const& x)
  240. {
  241. return detail::compute_abs_vector<L, T, Q, detail::is_aligned<Q>::value>::call(x);
  242. }
  243. // sign
  244. // fast and works for any type
  245. template<typename genFIType>
  246. GLM_FUNC_QUALIFIER genFIType sign(genFIType x)
  247. {
  248. GLM_STATIC_ASSERT(
  249. std::numeric_limits<genFIType>::is_iec559 || (std::numeric_limits<genFIType>::is_signed && std::numeric_limits<genFIType>::is_integer),
  250. "'sign' only accept signed inputs");
  251. return detail::compute_sign<1, genFIType, defaultp, std::numeric_limits<genFIType>::is_iec559, highp>::call(vec<1, genFIType>(x)).x;
  252. }
  253. template<length_t L, typename T, qualifier Q>
  254. GLM_FUNC_QUALIFIER vec<L, T, Q> sign(vec<L, T, Q> const& x)
  255. {
  256. GLM_STATIC_ASSERT(
  257. std::numeric_limits<T>::is_iec559 || (std::numeric_limits<T>::is_signed && std::numeric_limits<T>::is_integer),
  258. "'sign' only accept signed inputs");
  259. return detail::compute_sign<L, T, Q, std::numeric_limits<T>::is_iec559, detail::is_aligned<Q>::value>::call(x);
  260. }
  261. // floor
  262. using ::std::floor;
  263. template<length_t L, typename T, qualifier Q>
  264. GLM_FUNC_QUALIFIER vec<L, T, Q> floor(vec<L, T, Q> const& x)
  265. {
  266. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'floor' only accept floating-point inputs.");
  267. return detail::compute_floor<L, T, Q, detail::is_aligned<Q>::value>::call(x);
  268. }
  269. template<length_t L, typename T, qualifier Q>
  270. GLM_FUNC_QUALIFIER vec<L, T, Q> trunc(vec<L, T, Q> const& x)
  271. {
  272. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'trunc' only accept floating-point inputs");
  273. return detail::compute_trunc<L, T, Q, detail::is_aligned<Q>::value>::call(x);
  274. }
  275. template<length_t L, typename T, qualifier Q>
  276. GLM_FUNC_QUALIFIER vec<L, T, Q> round(vec<L, T, Q> const& x)
  277. {
  278. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'round' only accept floating-point inputs");
  279. return detail::compute_round<L, T, Q, detail::is_aligned<Q>::value>::call(x);
  280. }
  281. /*
  282. // roundEven
  283. template<typename genType>
  284. GLM_FUNC_QUALIFIER genType roundEven(genType const& x)
  285. {
  286. GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'roundEven' only accept floating-point inputs");
  287. return genType(int(x + genType(int(x) % 2)));
  288. }
  289. */
  290. // roundEven
  291. template<typename genType>
  292. GLM_FUNC_QUALIFIER genType roundEven(genType x)
  293. {
  294. GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'roundEven' only accept floating-point inputs");
  295. int Integer = static_cast<int>(x);
  296. genType IntegerPart = static_cast<genType>(Integer);
  297. genType FractionalPart = fract(x);
  298. if(FractionalPart > static_cast<genType>(0.5) || FractionalPart < static_cast<genType>(0.5))
  299. {
  300. return round(x);
  301. }
  302. else if((Integer % 2) == 0)
  303. {
  304. return IntegerPart;
  305. }
  306. else if(x <= static_cast<genType>(0)) // Work around...
  307. {
  308. return IntegerPart - static_cast<genType>(1);
  309. }
  310. else
  311. {
  312. return IntegerPart + static_cast<genType>(1);
  313. }
  314. //else // Bug on MinGW 4.5.2
  315. //{
  316. // return mix(IntegerPart + genType(-1), IntegerPart + genType(1), x <= genType(0));
  317. //}
  318. }
  319. template<length_t L, typename T, qualifier Q>
  320. GLM_FUNC_QUALIFIER vec<L, T, Q> roundEven(vec<L, T, Q> const& x)
  321. {
  322. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'roundEven' only accept floating-point inputs");
  323. return detail::functor1<vec, L, T, T, Q>::call(roundEven, x);
  324. }
  325. // ceil
  326. using ::std::ceil;
  327. template<length_t L, typename T, qualifier Q>
  328. GLM_FUNC_QUALIFIER vec<L, T, Q> ceil(vec<L, T, Q> const& x)
  329. {
  330. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'ceil' only accept floating-point inputs");
  331. return detail::compute_ceil<L, T, Q, detail::is_aligned<Q>::value>::call(x);
  332. }
  333. // fract
  334. template<typename genType>
  335. GLM_FUNC_QUALIFIER genType fract(genType x)
  336. {
  337. return fract(vec<1, genType>(x)).x;
  338. }
  339. template<length_t L, typename T, qualifier Q>
  340. GLM_FUNC_QUALIFIER vec<L, T, Q> fract(vec<L, T, Q> const& x)
  341. {
  342. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'fract' only accept floating-point inputs");
  343. return detail::compute_fract<L, T, Q, detail::is_aligned<Q>::value>::call(x);
  344. }
  345. // mod
  346. template<typename genType>
  347. GLM_FUNC_QUALIFIER genType mod(genType x, genType y)
  348. {
  349. # if GLM_COMPILER & GLM_COMPILER_CUDA
  350. // Another Cuda compiler bug https://github.com/g-truc/glm/issues/530
  351. vec<1, genType, defaultp> Result(mod(vec<1, genType, defaultp>(x), y));
  352. return Result.x;
  353. # else
  354. return mod(vec<1, genType, defaultp>(x), y).x;
  355. # endif
  356. }
  357. template<length_t L, typename T, qualifier Q>
  358. GLM_FUNC_QUALIFIER vec<L, T, Q> mod(vec<L, T, Q> const& x, T y)
  359. {
  360. return detail::compute_mod<L, T, Q, detail::is_aligned<Q>::value>::call(x, vec<L, T, Q>(y));
  361. }
  362. template<length_t L, typename T, qualifier Q>
  363. GLM_FUNC_QUALIFIER vec<L, T, Q> mod(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
  364. {
  365. return detail::compute_mod<L, T, Q, detail::is_aligned<Q>::value>::call(x, y);
  366. }
  367. // modf
  368. template<typename genType>
  369. GLM_FUNC_QUALIFIER genType modf(genType x, genType & i)
  370. {
  371. GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'modf' only accept floating-point inputs");
  372. return std::modf(x, &i);
  373. }
  374. template<typename T, qualifier Q>
  375. GLM_FUNC_QUALIFIER vec<1, T, Q> modf(vec<1, T, Q> const& x, vec<1, T, Q> & i)
  376. {
  377. return vec<1, T, Q>(
  378. modf(x.x, i.x));
  379. }
  380. template<typename T, qualifier Q>
  381. GLM_FUNC_QUALIFIER vec<2, T, Q> modf(vec<2, T, Q> const& x, vec<2, T, Q> & i)
  382. {
  383. return vec<2, T, Q>(
  384. modf(x.x, i.x),
  385. modf(x.y, i.y));
  386. }
  387. template<typename T, qualifier Q>
  388. GLM_FUNC_QUALIFIER vec<3, T, Q> modf(vec<3, T, Q> const& x, vec<3, T, Q> & i)
  389. {
  390. return vec<3, T, Q>(
  391. modf(x.x, i.x),
  392. modf(x.y, i.y),
  393. modf(x.z, i.z));
  394. }
  395. template<typename T, qualifier Q>
  396. GLM_FUNC_QUALIFIER vec<4, T, Q> modf(vec<4, T, Q> const& x, vec<4, T, Q> & i)
  397. {
  398. return vec<4, T, Q>(
  399. modf(x.x, i.x),
  400. modf(x.y, i.y),
  401. modf(x.z, i.z),
  402. modf(x.w, i.w));
  403. }
  404. //// Only valid if (INT_MIN <= x-y <= INT_MAX)
  405. //// min(x,y)
  406. //r = y + ((x - y) & ((x - y) >> (sizeof(int) *
  407. //CHAR_BIT - 1)));
  408. //// max(x,y)
  409. //r = x - ((x - y) & ((x - y) >> (sizeof(int) *
  410. //CHAR_BIT - 1)));
  411. // min
  412. template<length_t L, typename T, qualifier Q>
  413. GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, T, Q> min(vec<L, T, Q> const& a, T b)
  414. {
  415. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559 || std::numeric_limits<T>::is_integer, "'min' only accept floating-point or integer inputs");
  416. return detail::compute_min_vector<L, T, Q, detail::is_aligned<Q>::value>::call(a, vec<L, T, Q>(b));
  417. }
  418. template<length_t L, typename T, qualifier Q>
  419. GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, T, Q> min(vec<L, T, Q> const& a, vec<L, T, Q> const& b)
  420. {
  421. return detail::compute_min_vector<L, T, Q, detail::is_aligned<Q>::value>::call(a, b);
  422. }
  423. // max
  424. template<length_t L, typename T, qualifier Q>
  425. GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, T, Q> max(vec<L, T, Q> const& a, T b)
  426. {
  427. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559 || std::numeric_limits<T>::is_integer, "'max' only accept floating-point or integer inputs");
  428. return detail::compute_max_vector<L, T, Q, detail::is_aligned<Q>::value>::call(a, vec<L, T, Q>(b));
  429. }
  430. template<length_t L, typename T, qualifier Q>
  431. GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, T, Q> max(vec<L, T, Q> const& a, vec<L, T, Q> const& b)
  432. {
  433. return detail::compute_max_vector<L, T, Q, detail::is_aligned<Q>::value>::call(a, b);
  434. }
  435. // clamp
  436. template<typename genType>
  437. GLM_FUNC_QUALIFIER GLM_CONSTEXPR genType clamp(genType x, genType minVal, genType maxVal)
  438. {
  439. GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559 || std::numeric_limits<genType>::is_integer, "'clamp' only accept floating-point or integer inputs");
  440. return min(max(x, minVal), maxVal);
  441. }
  442. template<length_t L, typename T, qualifier Q>
  443. GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, T, Q> clamp(vec<L, T, Q> const& x, T minVal, T maxVal)
  444. {
  445. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559 || std::numeric_limits<T>::is_integer, "'clamp' only accept floating-point or integer inputs");
  446. return detail::compute_clamp_vector<L, T, Q, detail::is_aligned<Q>::value>::call(x, vec<L, T, Q>(minVal), vec<L, T, Q>(maxVal));
  447. }
  448. template<length_t L, typename T, qualifier Q>
  449. GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, T, Q> clamp(vec<L, T, Q> const& x, vec<L, T, Q> const& minVal, vec<L, T, Q> const& maxVal)
  450. {
  451. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559 || std::numeric_limits<T>::is_integer, "'clamp' only accept floating-point or integer inputs");
  452. return detail::compute_clamp_vector<L, T, Q, detail::is_aligned<Q>::value>::call(x, minVal, maxVal);
  453. }
  454. template<typename genTypeT, typename genTypeU>
  455. GLM_FUNC_QUALIFIER genTypeT mix(genTypeT x, genTypeT y, genTypeU a)
  456. {
  457. return detail::compute_mix<genTypeT, genTypeU>::call(x, y, a);
  458. }
  459. template<length_t L, typename T, typename U, qualifier Q>
  460. GLM_FUNC_QUALIFIER vec<L, T, Q> mix(vec<L, T, Q> const& x, vec<L, T, Q> const& y, U a)
  461. {
  462. return detail::compute_mix_scalar<L, T, U, Q, detail::is_aligned<Q>::value>::call(x, y, a);
  463. }
  464. template<length_t L, typename T, typename U, qualifier Q>
  465. GLM_FUNC_QUALIFIER vec<L, T, Q> mix(vec<L, T, Q> const& x, vec<L, T, Q> const& y, vec<L, U, Q> const& a)
  466. {
  467. return detail::compute_mix_vector<L, T, U, Q, detail::is_aligned<Q>::value>::call(x, y, a);
  468. }
  469. // step
  470. template<typename genType>
  471. GLM_FUNC_QUALIFIER genType step(genType edge, genType x)
  472. {
  473. return mix(static_cast<genType>(1), static_cast<genType>(0), x < edge);
  474. }
  475. template<length_t L, typename T, qualifier Q>
  476. GLM_FUNC_QUALIFIER vec<L, T, Q> step(T edge, vec<L, T, Q> const& x)
  477. {
  478. return detail::compute_step_vector<L, T, Q, detail::is_aligned<Q>::value>::call(vec<L, T, Q>(edge), x);
  479. }
  480. template<length_t L, typename T, qualifier Q>
  481. GLM_FUNC_QUALIFIER vec<L, T, Q> step(vec<L, T, Q> const& edge, vec<L, T, Q> const& x)
  482. {
  483. return detail::compute_step_vector<L, T, Q, detail::is_aligned<Q>::value>::call(edge, x);
  484. }
  485. // smoothstep
  486. template<typename genType>
  487. GLM_FUNC_QUALIFIER genType smoothstep(genType edge0, genType edge1, genType x)
  488. {
  489. GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559 || GLM_CONFIG_UNRESTRICTED_GENTYPE, "'smoothstep' only accept floating-point inputs");
  490. genType const tmp(clamp((x - edge0) / (edge1 - edge0), genType(0), genType(1)));
  491. return tmp * tmp * (genType(3) - genType(2) * tmp);
  492. }
  493. template<length_t L, typename T, qualifier Q>
  494. GLM_FUNC_QUALIFIER vec<L, T, Q> smoothstep(T edge0, T edge1, vec<L, T, Q> const& x)
  495. {
  496. return detail::compute_smoothstep_vector<L, T, Q, detail::is_aligned<Q>::value>::call(vec<L, T, Q>(edge0), vec<L, T, Q>(edge1), x);
  497. }
  498. template<length_t L, typename T, qualifier Q>
  499. GLM_FUNC_QUALIFIER vec<L, T, Q> smoothstep(vec<L, T, Q> const& edge0, vec<L, T, Q> const& edge1, vec<L, T, Q> const& x)
  500. {
  501. return detail::compute_smoothstep_vector<L, T, Q, detail::is_aligned<Q>::value>::call(edge0, edge1, x);
  502. }
  503. # if GLM_HAS_CXX11_STL
  504. using std::isnan;
  505. # else
  506. template<typename genType>
  507. GLM_FUNC_QUALIFIER bool isnan(genType x)
  508. {
  509. GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'isnan' only accept floating-point inputs");
  510. # if GLM_HAS_CXX11_STL
  511. return std::isnan(x);
  512. # elif GLM_COMPILER & GLM_COMPILER_VC
  513. return _isnan(x) != 0;
  514. # elif GLM_COMPILER & GLM_COMPILER_INTEL
  515. # if GLM_PLATFORM & GLM_PLATFORM_WINDOWS
  516. return _isnan(x) != 0;
  517. # else
  518. return ::isnan(x) != 0;
  519. # endif
  520. # elif (GLM_COMPILER & (GLM_COMPILER_GCC | GLM_COMPILER_CLANG)) && (GLM_PLATFORM & GLM_PLATFORM_ANDROID) && __cplusplus < 201103L
  521. return _isnan(x) != 0;
  522. # elif GLM_COMPILER & GLM_COMPILER_CUDA
  523. return ::isnan(x) != 0;
  524. # else
  525. return std::isnan(x);
  526. # endif
  527. }
  528. # endif
  529. template<length_t L, typename T, qualifier Q>
  530. GLM_FUNC_QUALIFIER vec<L, bool, Q> isnan(vec<L, T, Q> const& v)
  531. {
  532. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'isnan' only accept floating-point inputs");
  533. vec<L, bool, Q> Result;
  534. for (length_t l = 0; l < v.length(); ++l)
  535. Result[l] = glm::isnan(v[l]);
  536. return Result;
  537. }
  538. # if GLM_HAS_CXX11_STL
  539. using std::isinf;
  540. # else
  541. template<typename genType>
  542. GLM_FUNC_QUALIFIER bool isinf(genType x)
  543. {
  544. GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'isinf' only accept floating-point inputs");
  545. # if GLM_HAS_CXX11_STL
  546. return std::isinf(x);
  547. # elif GLM_COMPILER & (GLM_COMPILER_INTEL | GLM_COMPILER_VC)
  548. # if(GLM_PLATFORM & GLM_PLATFORM_WINDOWS)
  549. return _fpclass(x) == _FPCLASS_NINF || _fpclass(x) == _FPCLASS_PINF;
  550. # else
  551. return ::isinf(x);
  552. # endif
  553. # elif GLM_COMPILER & (GLM_COMPILER_GCC | GLM_COMPILER_CLANG)
  554. # if(GLM_PLATFORM & GLM_PLATFORM_ANDROID && __cplusplus < 201103L)
  555. return _isinf(x) != 0;
  556. # else
  557. return std::isinf(x);
  558. # endif
  559. # elif GLM_COMPILER & GLM_COMPILER_CUDA
  560. // http://developer.download.nvidia.com/compute/cuda/4_2/rel/toolkit/docs/online/group__CUDA__MATH__DOUBLE_g13431dd2b40b51f9139cbb7f50c18fab.html#g13431dd2b40b51f9139cbb7f50c18fab
  561. return ::isinf(double(x)) != 0;
  562. # else
  563. return std::isinf(x);
  564. # endif
  565. }
  566. # endif
  567. template<length_t L, typename T, qualifier Q>
  568. GLM_FUNC_QUALIFIER vec<L, bool, Q> isinf(vec<L, T, Q> const& v)
  569. {
  570. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'isinf' only accept floating-point inputs");
  571. vec<L, bool, Q> Result;
  572. for (length_t l = 0; l < v.length(); ++l)
  573. Result[l] = glm::isinf(v[l]);
  574. return Result;
  575. }
  576. GLM_FUNC_QUALIFIER int floatBitsToInt(float const& v)
  577. {
  578. union
  579. {
  580. float in;
  581. int out;
  582. } u;
  583. u.in = v;
  584. return u.out;
  585. }
  586. template<length_t L, qualifier Q>
  587. GLM_FUNC_QUALIFIER vec<L, int, Q> floatBitsToInt(vec<L, float, Q> const& v)
  588. {
  589. return reinterpret_cast<vec<L, int, Q>&>(const_cast<vec<L, float, Q>&>(v));
  590. }
  591. GLM_FUNC_QUALIFIER uint floatBitsToUint(float const& v)
  592. {
  593. union
  594. {
  595. float in;
  596. uint out;
  597. } u;
  598. u.in = v;
  599. return u.out;
  600. }
  601. template<length_t L, qualifier Q>
  602. GLM_FUNC_QUALIFIER vec<L, uint, Q> floatBitsToUint(vec<L, float, Q> const& v)
  603. {
  604. return reinterpret_cast<vec<L, uint, Q>&>(const_cast<vec<L, float, Q>&>(v));
  605. }
  606. GLM_FUNC_QUALIFIER float intBitsToFloat(int const& v)
  607. {
  608. union
  609. {
  610. int in;
  611. float out;
  612. } u;
  613. u.in = v;
  614. return u.out;
  615. }
  616. template<length_t L, qualifier Q>
  617. GLM_FUNC_QUALIFIER vec<L, float, Q> intBitsToFloat(vec<L, int, Q> const& v)
  618. {
  619. return reinterpret_cast<vec<L, float, Q>&>(const_cast<vec<L, int, Q>&>(v));
  620. }
  621. GLM_FUNC_QUALIFIER float uintBitsToFloat(uint const& v)
  622. {
  623. union
  624. {
  625. uint in;
  626. float out;
  627. } u;
  628. u.in = v;
  629. return u.out;
  630. }
  631. template<length_t L, qualifier Q>
  632. GLM_FUNC_QUALIFIER vec<L, float, Q> uintBitsToFloat(vec<L, uint, Q> const& v)
  633. {
  634. return reinterpret_cast<vec<L, float, Q>&>(const_cast<vec<L, uint, Q>&>(v));
  635. }
  636. template<typename genType>
  637. GLM_FUNC_QUALIFIER genType fma(genType const& a, genType const& b, genType const& c)
  638. {
  639. return a * b + c;
  640. }
  641. template<typename genType>
  642. GLM_FUNC_QUALIFIER genType frexp(genType x, int& exp)
  643. {
  644. GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'frexp' only accept floating-point inputs");
  645. return std::frexp(x, &exp);
  646. }
  647. template<length_t L, typename T, qualifier Q>
  648. GLM_FUNC_QUALIFIER vec<L, T, Q> frexp(vec<L, T, Q> const& v, vec<L, int, Q>& exp)
  649. {
  650. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'frexp' only accept floating-point inputs");
  651. vec<L, T, Q> Result;
  652. for (length_t l = 0; l < v.length(); ++l)
  653. Result[l] = std::frexp(v[l], &exp[l]);
  654. return Result;
  655. }
  656. template<typename genType>
  657. GLM_FUNC_QUALIFIER genType ldexp(genType const& x, int const& exp)
  658. {
  659. GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'ldexp' only accept floating-point inputs");
  660. return std::ldexp(x, exp);
  661. }
  662. template<length_t L, typename T, qualifier Q>
  663. GLM_FUNC_QUALIFIER vec<L, T, Q> ldexp(vec<L, T, Q> const& v, vec<L, int, Q> const& exp)
  664. {
  665. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'ldexp' only accept floating-point inputs");
  666. vec<L, T, Q> Result;
  667. for (length_t l = 0; l < v.length(); ++l)
  668. Result[l] = std::ldexp(v[l], exp[l]);
  669. return Result;
  670. }
  671. }//namespace glm
  672. #if GLM_CONFIG_SIMD == GLM_ENABLE
  673. # include "func_common_simd.inl"
  674. #endif