/// @ref core /// @file glm/detail/func_common.inl #include "func_vector_relational.hpp" #include "type_vec2.hpp" #include "type_vec3.hpp" #include "type_vec4.hpp" #include "_vectorize.hpp" #include namespace glm { // min template GLM_FUNC_QUALIFIER genType min(genType x, genType y) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer || GLM_UNRESTRICTED_GENTYPE, "'min' only accept floating-point or integer inputs"); return x < y ? x : y; } // max template GLM_FUNC_QUALIFIER genType max(genType x, genType y) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer || GLM_UNRESTRICTED_GENTYPE, "'max' only accept floating-point or integer inputs"); return x > y ? x : y; } // abs template <> GLM_FUNC_QUALIFIER int32 abs(int32 x) { int32 const y = x >> 31; 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 {}; template struct compute_abs { GLM_FUNC_QUALIFIER static genFIType call(genFIType x) { GLM_STATIC_ASSERT( std::numeric_limits::is_iec559 || std::numeric_limits::is_signed || GLM_UNRESTRICTED_GENTYPE, "'abs' only accept floating-point and integer scalar or vector inputs"); return x >= genFIType(0) ? x : -x; // TODO, perf comp with: *(((int *) &x) + 1) &= 0x7fffffff; } }; #if GLM_COMPILER & GLM_COMPILER_CUDA template <> struct compute_abs { GLM_FUNC_QUALIFIER static float call(float x) { return fabsf(x); } }; #endif template struct compute_abs { GLM_FUNC_QUALIFIER static genFIType call(genFIType x) { GLM_STATIC_ASSERT( (!std::numeric_limits::is_signed && std::numeric_limits::is_integer) || GLM_UNRESTRICTED_GENTYPE, "'abs' only accept floating-point and integer scalar or vector inputs"); return x; } }; template class vecType, bool Aligned> struct compute_abs_vector { GLM_FUNC_QUALIFIER static vecType call(vecType const & x) { return detail::functor1::call(abs, x); } }; template class vecType, bool Aligned> struct compute_mix_vector { GLM_FUNC_QUALIFIER static vecType call(vecType const & x, vecType const & y, vecType const & a) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || GLM_UNRESTRICTED_GENTYPE, "'mix' only accept floating-point inputs for the interpolator a"); return vecType(vecType(x) + a * vecType(y - x)); } }; template class vecType, bool Aligned> struct compute_mix_vector { GLM_FUNC_QUALIFIER static vecType call(vecType const & x, vecType const & y, vecType const & a) { vecType Result(uninitialize); for(length_t i = 0; i < x.length(); ++i) Result[i] = a[i] ? y[i] : x[i]; return Result; } }; template class vecType, bool Aligned> struct compute_mix_scalar { GLM_FUNC_QUALIFIER static vecType call(vecType const & x, vecType const & y, U const & a) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || GLM_UNRESTRICTED_GENTYPE, "'mix' only accept floating-point inputs for the interpolator a"); return vecType(vecType(x) + a * vecType(y - x)); } }; template class vecType, bool Aligned> struct compute_mix_scalar { GLM_FUNC_QUALIFIER static vecType call(vecType const & x, vecType 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_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 class vecType, bool isFloat, bool Aligned> struct compute_sign { GLM_FUNC_QUALIFIER static vecType call(vecType const & x) { return vecType(glm::lessThan(vecType(0), x)) - vecType(glm::lessThan(x, vecType(0))); } }; # if GLM_ARCH == GLM_ARCH_X86 template class vecType, bool Aligned> struct compute_sign { GLM_FUNC_QUALIFIER static vecType call(vecType const & x) { T const Shift(static_cast(sizeof(T) * 8 - 1)); vecType const y(vecType::type, P>(-x) >> typename make_unsigned::type(Shift)); return (x >> Shift) | y; } }; # endif template class vecType, bool Aligned> struct compute_floor { GLM_FUNC_QUALIFIER static vecType call(vecType const & x) { return detail::functor1::call(std::floor, x); } }; template class vecType, bool Aligned> struct compute_ceil { GLM_FUNC_QUALIFIER static vecType call(vecType const & x) { return detail::functor1::call(std::ceil, x); } }; template class vecType, bool Aligned> struct compute_fract { GLM_FUNC_QUALIFIER static vecType call(vecType const & x) { return x - floor(x); } }; template class vecType, bool Aligned> struct compute_trunc { GLM_FUNC_QUALIFIER static vecType call(vecType const & x) { return detail::functor1::call(trunc, x); } }; template class vecType, bool Aligned> struct compute_round { GLM_FUNC_QUALIFIER static vecType call(vecType const & x) { return detail::functor1::call(round, x); } }; template class vecType, bool Aligned> struct compute_mod { GLM_FUNC_QUALIFIER static vecType call(vecType const & a, vecType 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 class vecType, bool Aligned> struct compute_min_vector { GLM_FUNC_QUALIFIER static vecType call(vecType const & x, vecType const & y) { return detail::functor2::call(min, x, y); } }; template class vecType, bool Aligned> struct compute_max_vector { GLM_FUNC_QUALIFIER static vecType call(vecType const & x, vecType const & y) { return detail::functor2::call(max, x, y); } }; template class vecType, bool Aligned> struct compute_clamp_vector { GLM_FUNC_QUALIFIER static vecType call(vecType const & x, vecType const & minVal, vecType const & maxVal) { return min(max(x, minVal), maxVal); } }; template class vecType, bool Aligned> struct compute_step_vector { GLM_FUNC_QUALIFIER static vecType call(vecType const & edge, vecType const & x) { return mix(vecType(1), vecType(0), glm::lessThan(x, edge)); } }; template class vecType, bool Aligned> struct compute_smoothstep_vector { GLM_FUNC_QUALIFIER static vecType call(vecType const & edge0, vecType const & edge1, vecType const & x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || GLM_UNRESTRICTED_GENTYPE, "'step' only accept floating-point inputs"); vecType 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 genFIType abs(genFIType x) { return detail::compute_abs::is_signed>::call(x); } template class vecType> GLM_FUNC_QUALIFIER vecType abs(vecType 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::is_iec559, highp>::call(tvec1(x)).x; } template class vecType> GLM_FUNC_QUALIFIER vecType sign(vecType 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 class vecType> GLM_FUNC_QUALIFIER vecType floor(vecType const & x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'floor' only accept floating-point inputs."); return detail::compute_floor::value>::call(x); } template class vecType> GLM_FUNC_QUALIFIER vecType trunc(vecType const & x) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'trunc' only accept floating-point inputs"); return detail::compute_trunc::value>::call(x); } template class vecType> GLM_FUNC_QUALIFIER vecType round(vecType 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 class vecType> GLM_FUNC_QUALIFIER vecType roundEven(vecType 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 class vecType> GLM_FUNC_QUALIFIER vecType ceil(vecType 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(tvec1(x)).x; } template class vecType> GLM_FUNC_QUALIFIER vecType fract(vecType 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 tvec1 Result(mod(tvec1(x), y)); return Result.x; # else return mod(tvec1(x), y).x; # endif } template class vecType> GLM_FUNC_QUALIFIER vecType mod(vecType const & x, T y) { return detail::compute_mod::value>::call(x, vecType(y)); } template class vecType> GLM_FUNC_QUALIFIER vecType mod(vecType const & x, vecType 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 tvec1 modf(tvec1 const & x, tvec1 & i) { return tvec1( modf(x.x, i.x)); } template GLM_FUNC_QUALIFIER tvec2 modf(tvec2 const & x, tvec2 & i) { return tvec2( modf(x.x, i.x), modf(x.y, i.y)); } template GLM_FUNC_QUALIFIER tvec3 modf(tvec3 const & x, tvec3 & i) { return tvec3( modf(x.x, i.x), modf(x.y, i.y), modf(x.z, i.z)); } template GLM_FUNC_QUALIFIER tvec4 modf(tvec4 const & x, tvec4 & i) { return tvec4( 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 class vecType> GLM_FUNC_QUALIFIER vecType min(vecType const & a, T b) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer || GLM_UNRESTRICTED_GENTYPE, "'min' only accept floating-point or integer inputs"); return detail::compute_min_vector::value>::call(a, vecType(b)); } template class vecType> GLM_FUNC_QUALIFIER vecType min(vecType const & a, vecType const & b) { return detail::compute_min_vector::value>::call(a, b); } // max template class vecType> GLM_FUNC_QUALIFIER vecType max(vecType const & a, T b) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer || GLM_UNRESTRICTED_GENTYPE, "'max' only accept floating-point or integer inputs"); return detail::compute_max_vector::value>::call(a, vecType(b)); } template class vecType> GLM_FUNC_QUALIFIER vecType max(vecType const & a, vecType const & b) { return detail::compute_max_vector::value>::call(a, b); } // clamp template GLM_FUNC_QUALIFIER genType clamp(genType x, genType minVal, genType maxVal) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer || GLM_UNRESTRICTED_GENTYPE, "'clamp' only accept floating-point or integer inputs"); return min(max(x, minVal), maxVal); } template class vecType> GLM_FUNC_QUALIFIER vecType clamp(vecType const & x, T minVal, T maxVal) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer || GLM_UNRESTRICTED_GENTYPE, "'clamp' only accept floating-point or integer inputs"); return detail::compute_clamp_vector::value>::call(x, vecType(minVal), vecType(maxVal)); } template class vecType> GLM_FUNC_QUALIFIER vecType clamp(vecType const & x, vecType const & minVal, vecType const & maxVal) { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559 || std::numeric_limits::is_integer || GLM_UNRESTRICTED_GENTYPE, "'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 class vecType> GLM_FUNC_QUALIFIER vecType mix(vecType const & x, vecType const & y, U a) { return detail::compute_mix_scalar::value>::call(x, y, a); } template class vecType> GLM_FUNC_QUALIFIER vecType mix(vecType const & x, vecType const & y, vecType 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), glm::lessThan(x, edge)); } template