/// @ref core /// @file glm/detail/func_common.inl #include "../vector_relational.hpp" #include "compute_common.hpp" #include "type_vec2.hpp" #include "type_vec3.hpp" #include "type_vec4.hpp" #include "_vectorize.hpp" #include namespace glm { // min template GLM_FUNC_QUALIFIER GLM_CONSTEXPR genType min(genType x, genType y) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer, "'min' only accept floating-point or integer inputs"); return (y < x) ? y : x; } // max template GLM_FUNC_QUALIFIER GLM_CONSTEXPR genType max(genType x, genType y) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer, "'max' only accept floating-point or integer inputs"); return (x < y) ? y : x; } // abs template<> GLM_FUNC_QUALIFIER GLM_CONSTEXPR int abs(int x) { int const y = x >> (sizeof(int) * 8 - 1); return (x ^ y) - y; } // round # if GLM_HAS_CXX11_STL using ::std::round; # else template GLM_FUNC_QUALIFIER genType round(genType x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'round' only accept floating-point inputs"); return x < static_cast(0) ? static_cast(int(x - static_cast(0.5))) : static_cast(int(x + static_cast(0.5))); } # endif // trunc # if GLM_HAS_CXX11_STL using ::std::trunc; # else template GLM_FUNC_QUALIFIER genType trunc(genType x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'trunc' only accept floating-point inputs"); return x < static_cast(0) ? -std::floor(-x) : std::floor(x); } # endif }//namespace glm namespace glm{ namespace detail { template struct compute_abs_vector { GLM_FUNC_QUALIFIER GLM_CONSTEXPR static vec call(vec const& x) { return detail::functor1::call(abs, x); } }; template struct compute_mix_vector { GLM_FUNC_QUALIFIER static vec call(vec const& x, vec const& y, vec const& a) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || GLM_CONFIG_UNRESTRICTED_GENTYPE, "'mix' only accept floating-point inputs for the interpolator a"); return vec(vec(x) + a * vec(y - x)); } }; template struct compute_mix_vector { GLM_FUNC_QUALIFIER static vec call(vec const& x, vec const& y, vec const& a) { vec Result; for(length_t i = 0; i < x.length(); ++i) Result[i] = a[i] ? y[i] : x[i]; return Result; } }; template struct compute_mix_scalar { GLM_FUNC_QUALIFIER static vec call(vec const& x, vec const& y, U const& a) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || GLM_CONFIG_UNRESTRICTED_GENTYPE, "'mix' only accept floating-point inputs for the interpolator a"); return vec(vec(x) + a * vec(y - x)); } }; template struct compute_mix_scalar { GLM_FUNC_QUALIFIER static vec call(vec const& x, vec const& y, bool const& a) { return a ? y : x; } }; template struct compute_mix { GLM_FUNC_QUALIFIER static T call(T const& x, T const& y, U const& a) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || GLM_CONFIG_UNRESTRICTED_GENTYPE, "'mix' only accept floating-point inputs for the interpolator a"); return static_cast(static_cast(x) + a * static_cast(y - x)); } }; template struct compute_mix { GLM_FUNC_QUALIFIER static T call(T const& x, T const& y, bool const& a) { return a ? y : x; } }; template struct compute_sign { GLM_FUNC_QUALIFIER static vec call(vec const& x) { return vec(glm::lessThan(vec(0), x)) - vec(glm::lessThan(x, vec(0))); } }; # if GLM_ARCH == GLM_ARCH_X86 template struct compute_sign { GLM_FUNC_QUALIFIER static vec call(vec const& x) { T const Shift(static_cast(sizeof(T) * 8 - 1)); vec const y(vec::type, Q>(-x) >> typename detail::make_unsigned::type(Shift)); return (x >> Shift) | y; } }; # endif template struct compute_floor { GLM_FUNC_QUALIFIER static vec call(vec const& x) { return detail::functor1::call(std::floor, x); } }; template struct compute_ceil { GLM_FUNC_QUALIFIER static vec call(vec const& x) { return detail::functor1::call(std::ceil, x); } }; template struct compute_fract { GLM_FUNC_QUALIFIER static vec call(vec const& x) { return x - floor(x); } }; template struct compute_trunc { GLM_FUNC_QUALIFIER static vec call(vec const& x) { return detail::functor1::call(trunc, x); } }; template struct compute_round { GLM_FUNC_QUALIFIER static vec call(vec const& x) { return detail::functor1::call(round, x); } }; template struct compute_mod { GLM_FUNC_QUALIFIER static vec call(vec const& a, vec const& b) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'mod' only accept floating-point inputs. Include for integer inputs."); return a - b * floor(a / b); } }; template struct compute_min_vector { GLM_FUNC_QUALIFIER static vec call(vec const& x, vec const& y) { return detail::functor2::call(min, x, y); } }; template struct compute_max_vector { GLM_FUNC_QUALIFIER static vec call(vec const& x, vec const& y) { return detail::functor2::call(max, x, y); } }; template struct compute_clamp_vector { GLM_FUNC_QUALIFIER static vec call(vec const& x, vec const& minVal, vec const& maxVal) { return min(max(x, minVal), maxVal); } }; template struct compute_step_vector { GLM_FUNC_QUALIFIER static vec call(vec const& edge, vec const& x) { return mix(vec(1), vec(0), glm::lessThan(x, edge)); } }; template struct compute_smoothstep_vector { GLM_FUNC_QUALIFIER static vec call(vec const& edge0, vec const& edge1, vec const& x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || GLM_CONFIG_UNRESTRICTED_GENTYPE, "'step' only accept floating-point inputs"); vec const tmp(clamp((x - edge0) / (edge1 - edge0), static_cast(0), static_cast(1))); return tmp * tmp * (static_cast(3) - static_cast(2) * tmp); } }; }//namespace detail template GLM_FUNC_QUALIFIER GLM_CONSTEXPR genFIType abs(genFIType x) { return detail::compute_abs::is_signed>::call(x); } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec abs(vec const& x) { return detail::compute_abs_vector::value>::call(x); } // sign // fast and works for any type template GLM_FUNC_QUALIFIER genFIType sign(genFIType x) { GLM_STATIC_ASSERT( std::numeric_limits::is_iec559 || (std::numeric_limits::is_signed && std::numeric_limits::is_integer), "'sign' only accept signed inputs"); return detail::compute_sign<1, genFIType, defaultp, std::numeric_limits::is_iec559, highp>::call(vec<1, genFIType>(x)).x; } template GLM_FUNC_QUALIFIER vec sign(vec const& x) { GLM_STATIC_ASSERT( std::numeric_limits::is_iec559 || (std::numeric_limits::is_signed && std::numeric_limits::is_integer), "'sign' only accept signed inputs"); return detail::compute_sign::is_iec559, detail::is_aligned::value>::call(x); } // floor using ::std::floor; template GLM_FUNC_QUALIFIER vec floor(vec const& x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'floor' only accept floating-point inputs."); return detail::compute_floor::value>::call(x); } template GLM_FUNC_QUALIFIER vec trunc(vec const& x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'trunc' only accept floating-point inputs"); return detail::compute_trunc::value>::call(x); } template GLM_FUNC_QUALIFIER vec round(vec const& x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'round' only accept floating-point inputs"); return detail::compute_round::value>::call(x); } /* // roundEven template GLM_FUNC_QUALIFIER genType roundEven(genType const& x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'roundEven' only accept floating-point inputs"); return genType(int(x + genType(int(x) % 2))); } */ // roundEven template GLM_FUNC_QUALIFIER genType roundEven(genType x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'roundEven' only accept floating-point inputs"); int Integer = static_cast(x); genType IntegerPart = static_cast(Integer); genType FractionalPart = fract(x); if(FractionalPart > static_cast(0.5) || FractionalPart < static_cast(0.5)) { return round(x); } else if((Integer % 2) == 0) { return IntegerPart; } else if(x <= static_cast(0)) // Work around... { return IntegerPart - static_cast(1); } else { return IntegerPart + static_cast(1); } //else // Bug on MinGW 4.5.2 //{ // return mix(IntegerPart + genType(-1), IntegerPart + genType(1), x <= genType(0)); //} } template GLM_FUNC_QUALIFIER vec roundEven(vec const& x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'roundEven' only accept floating-point inputs"); return detail::functor1::call(roundEven, x); } // ceil using ::std::ceil; template GLM_FUNC_QUALIFIER vec ceil(vec const& x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'ceil' only accept floating-point inputs"); return detail::compute_ceil::value>::call(x); } // fract template GLM_FUNC_QUALIFIER genType fract(genType x) { return fract(vec<1, genType>(x)).x; } template GLM_FUNC_QUALIFIER vec fract(vec const& x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'fract' only accept floating-point inputs"); return detail::compute_fract::value>::call(x); } // mod template GLM_FUNC_QUALIFIER genType mod(genType x, genType y) { # if GLM_COMPILER & GLM_COMPILER_CUDA // Another Cuda compiler bug https://github.com/g-truc/glm/issues/530 vec<1, genType, defaultp> Result(mod(vec<1, genType, defaultp>(x), y)); return Result.x; # else return mod(vec<1, genType, defaultp>(x), y).x; # endif } template GLM_FUNC_QUALIFIER vec mod(vec const& x, T y) { return detail::compute_mod::value>::call(x, vec(y)); } template GLM_FUNC_QUALIFIER vec mod(vec const& x, vec const& y) { return detail::compute_mod::value>::call(x, y); } // modf template GLM_FUNC_QUALIFIER genType modf(genType x, genType & i) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'modf' only accept floating-point inputs"); return std::modf(x, &i); } template GLM_FUNC_QUALIFIER vec<1, T, Q> modf(vec<1, T, Q> const& x, vec<1, T, Q> & i) { return vec<1, T, Q>( modf(x.x, i.x)); } template GLM_FUNC_QUALIFIER vec<2, T, Q> modf(vec<2, T, Q> const& x, vec<2, T, Q> & i) { return vec<2, T, Q>( modf(x.x, i.x), modf(x.y, i.y)); } template GLM_FUNC_QUALIFIER vec<3, T, Q> modf(vec<3, T, Q> const& x, vec<3, T, Q> & i) { return vec<3, T, Q>( modf(x.x, i.x), modf(x.y, i.y), modf(x.z, i.z)); } template GLM_FUNC_QUALIFIER vec<4, T, Q> modf(vec<4, T, Q> const& x, vec<4, T, Q> & i) { return vec<4, T, Q>( modf(x.x, i.x), modf(x.y, i.y), modf(x.z, i.z), modf(x.w, i.w)); } //// Only valid if (INT_MIN <= x-y <= INT_MAX) //// min(x,y) //r = y + ((x - y) & ((x - y) >> (sizeof(int) * //CHAR_BIT - 1))); //// max(x,y) //r = x - ((x - y) & ((x - y) >> (sizeof(int) * //CHAR_BIT - 1))); // min template GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec min(vec const& a, T b) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer, "'min' only accept floating-point or integer inputs"); return detail::compute_min_vector::value>::call(a, vec(b)); } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec min(vec const& a, vec const& b) { return detail::compute_min_vector::value>::call(a, b); } // max template GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec max(vec const& a, T b) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer, "'max' only accept floating-point or integer inputs"); return detail::compute_max_vector::value>::call(a, vec(b)); } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec max(vec const& a, vec const& b) { return detail::compute_max_vector::value>::call(a, b); } // clamp template GLM_FUNC_QUALIFIER GLM_CONSTEXPR genType clamp(genType x, genType minVal, genType maxVal) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer, "'clamp' only accept floating-point or integer inputs"); return min(max(x, minVal), maxVal); } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec clamp(vec const& x, T minVal, T maxVal) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer, "'clamp' only accept floating-point or integer inputs"); return detail::compute_clamp_vector::value>::call(x, vec(minVal), vec(maxVal)); } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec clamp(vec const& x, vec const& minVal, vec const& maxVal) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer, "'clamp' only accept floating-point or integer inputs"); return detail::compute_clamp_vector::value>::call(x, minVal, maxVal); } template GLM_FUNC_QUALIFIER genTypeT mix(genTypeT x, genTypeT y, genTypeU a) { return detail::compute_mix::call(x, y, a); } template GLM_FUNC_QUALIFIER vec mix(vec const& x, vec const& y, U a) { return detail::compute_mix_scalar::value>::call(x, y, a); } template GLM_FUNC_QUALIFIER vec mix(vec const& x, vec const& y, vec const& a) { return detail::compute_mix_vector::value>::call(x, y, a); } // step template GLM_FUNC_QUALIFIER genType step(genType edge, genType x) { return mix(static_cast(1), static_cast(0), x < edge); } template GLM_FUNC_QUALIFIER vec step(T edge, vec const& x) { return detail::compute_step_vector::value>::call(vec(edge), x); } template GLM_FUNC_QUALIFIER vec step(vec const& edge, vec const& x) { return detail::compute_step_vector::value>::call(edge, x); } // smoothstep template GLM_FUNC_QUALIFIER genType smoothstep(genType edge0, genType edge1, genType x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || GLM_CONFIG_UNRESTRICTED_GENTYPE, "'smoothstep' only accept floating-point inputs"); genType const tmp(clamp((x - edge0) / (edge1 - edge0), genType(0), genType(1))); return tmp * tmp * (genType(3) - genType(2) * tmp); } template GLM_FUNC_QUALIFIER vec smoothstep(T edge0, T edge1, vec const& x) { return detail::compute_smoothstep_vector::value>::call(vec(edge0), vec(edge1), x); } template GLM_FUNC_QUALIFIER vec smoothstep(vec const& edge0, vec const& edge1, vec const& x) { return detail::compute_smoothstep_vector::value>::call(edge0, edge1, x); } # if GLM_HAS_CXX11_STL using std::isnan; # else template GLM_FUNC_QUALIFIER bool isnan(genType x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'isnan' only accept floating-point inputs"); # if GLM_HAS_CXX11_STL return std::isnan(x); # elif GLM_COMPILER & GLM_COMPILER_VC return _isnan(x) != 0; # elif GLM_COMPILER & GLM_COMPILER_INTEL # if GLM_PLATFORM & GLM_PLATFORM_WINDOWS return _isnan(x) != 0; # else return ::isnan(x) != 0; # endif # elif (GLM_COMPILER & (GLM_COMPILER_GCC | GLM_COMPILER_CLANG)) && (GLM_PLATFORM & GLM_PLATFORM_ANDROID) && __cplusplus < 201103L return _isnan(x) != 0; # elif GLM_COMPILER & GLM_COMPILER_CUDA return ::isnan(x) != 0; # else return std::isnan(x); # endif } # endif template GLM_FUNC_QUALIFIER vec isnan(vec const& v) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'isnan' only accept floating-point inputs"); vec Result; for (length_t l = 0; l < v.length(); ++l) Result[l] = glm::isnan(v[l]); return Result; } # if GLM_HAS_CXX11_STL using std::isinf; # else template GLM_FUNC_QUALIFIER bool isinf(genType x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'isinf' only accept floating-point inputs"); # if GLM_HAS_CXX11_STL return std::isinf(x); # elif GLM_COMPILER & (GLM_COMPILER_INTEL | GLM_COMPILER_VC) # if(GLM_PLATFORM & GLM_PLATFORM_WINDOWS) return _fpclass(x) == _FPCLASS_NINF || _fpclass(x) == _FPCLASS_PINF; # else return ::isinf(x); # endif # elif GLM_COMPILER & (GLM_COMPILER_GCC | GLM_COMPILER_CLANG) # if(GLM_PLATFORM & GLM_PLATFORM_ANDROID && __cplusplus < 201103L) return _isinf(x) != 0; # else return std::isinf(x); # endif # elif GLM_COMPILER & GLM_COMPILER_CUDA // http://developer.download.nvidia.com/compute/cuda/4_2/rel/toolkit/docs/online/group__CUDA__MATH__DOUBLE_g13431dd2b40b51f9139cbb7f50c18fab.html#g13431dd2b40b51f9139cbb7f50c18fab return ::isinf(double(x)) != 0; # else return std::isinf(x); # endif } # endif template GLM_FUNC_QUALIFIER vec isinf(vec const& v) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'isinf' only accept floating-point inputs"); vec Result; for (length_t l = 0; l < v.length(); ++l) Result[l] = glm::isinf(v[l]); return Result; } GLM_FUNC_QUALIFIER int floatBitsToInt(float const& v) { union { float in; int out; } u; u.in = v; return u.out; } template GLM_FUNC_QUALIFIER vec floatBitsToInt(vec const& v) { return reinterpret_cast&>(const_cast&>(v)); } GLM_FUNC_QUALIFIER uint floatBitsToUint(float const& v) { union { float in; uint out; } u; u.in = v; return u.out; } template GLM_FUNC_QUALIFIER vec floatBitsToUint(vec const& v) { return reinterpret_cast&>(const_cast&>(v)); } GLM_FUNC_QUALIFIER float intBitsToFloat(int const& v) { union { int in; float out; } u; u.in = v; return u.out; } template GLM_FUNC_QUALIFIER vec intBitsToFloat(vec const& v) { return reinterpret_cast&>(const_cast&>(v)); } GLM_FUNC_QUALIFIER float uintBitsToFloat(uint const& v) { union { uint in; float out; } u; u.in = v; return u.out; } template GLM_FUNC_QUALIFIER vec uintBitsToFloat(vec const& v) { return reinterpret_cast&>(const_cast&>(v)); } template GLM_FUNC_QUALIFIER genType fma(genType const& a, genType const& b, genType const& c) { return a * b + c; } template GLM_FUNC_QUALIFIER genType frexp(genType x, int& exp) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'frexp' only accept floating-point inputs"); return std::frexp(x, &exp); } template GLM_FUNC_QUALIFIER vec frexp(vec const& v, vec& exp) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'frexp' only accept floating-point inputs"); vec Result; for (length_t l = 0; l < v.length(); ++l) Result[l] = std::frexp(v[l], &exp[l]); return Result; } template GLM_FUNC_QUALIFIER genType ldexp(genType const& x, int const& exp) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'ldexp' only accept floating-point inputs"); return std::ldexp(x, exp); } template GLM_FUNC_QUALIFIER vec ldexp(vec const& v, vec const& exp) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'ldexp' only accept floating-point inputs"); vec Result; for (length_t l = 0; l < v.length(); ++l) Result[l] = std::ldexp(v[l], exp[l]); return Result; } }//namespace glm #if GLM_CONFIG_SIMD == GLM_ENABLE # include "func_common_simd.inl" #endif