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