/// @ref core /// @file glm/detail/func_integer.inl #include "../ext/vec1.hpp" #include "type_vec2.hpp" #include "type_vec3.hpp" #include "type_vec4.hpp" #include "type_int.hpp" #include "_vectorize.hpp" #if(GLM_ARCH & GLM_ARCH_X86 && GLM_COMPILER & GLM_COMPILER_VC) # include # pragma intrinsic(_BitScanReverse) #endif//(GLM_ARCH & GLM_ARCH_X86 && GLM_COMPILER & GLM_COMPILER_VC) #include #if !GLM_HAS_EXTENDED_INTEGER_TYPE # if GLM_COMPILER & GLM_COMPILER_GCC # pragma GCC diagnostic ignored "-Wlong-long" # endif # if (GLM_COMPILER & GLM_COMPILER_CLANG) # pragma clang diagnostic ignored "-Wc++11-long-long" # endif #endif namespace glm{ namespace detail { template GLM_FUNC_QUALIFIER T mask(T Bits) { return Bits >= static_cast(sizeof(T) * 8) ? ~static_cast(0) : (static_cast(1) << Bits) - static_cast(1); } template struct compute_bitfieldReverseStep { GLM_FUNC_QUALIFIER static vec call(vec const& v, T, T) { return v; } }; template struct compute_bitfieldReverseStep { GLM_FUNC_QUALIFIER static vec call(vec const& v, T Mask, T Shift) { return (v & Mask) << Shift | (v & (~Mask)) >> Shift; } }; template struct compute_bitfieldBitCountStep { GLM_FUNC_QUALIFIER static vec call(vec const& v, T, T) { return v; } }; template struct compute_bitfieldBitCountStep { GLM_FUNC_QUALIFIER static vec call(vec const& v, T Mask, T Shift) { return (v & Mask) + ((v >> Shift) & Mask); } }; template struct compute_findLSB { GLM_FUNC_QUALIFIER static int call(genIUType Value) { if(Value == 0) return -1; return glm::bitCount(~Value & (Value - static_cast(1))); } }; # if GLM_HAS_BITSCAN_WINDOWS template struct compute_findLSB { GLM_FUNC_QUALIFIER static int call(genIUType Value) { unsigned long Result(0); unsigned char IsNotNull = _BitScanForward(&Result, *reinterpret_cast(&Value)); return IsNotNull ? int(Result) : -1; } }; # if !((GLM_COMPILER & GLM_COMPILER_VC) && (GLM_MODEL == GLM_MODEL_32)) template struct compute_findLSB { GLM_FUNC_QUALIFIER static int call(genIUType Value) { unsigned long Result(0); unsigned char IsNotNull = _BitScanForward64(&Result, *reinterpret_cast(&Value)); return IsNotNull ? int(Result) : -1; } }; # endif # endif//GLM_HAS_BITSCAN_WINDOWS template struct compute_findMSB_step_vec { GLM_FUNC_QUALIFIER static vec call(vec const& x, T Shift) { return x | (x >> Shift); } }; template struct compute_findMSB_step_vec { GLM_FUNC_QUALIFIER static vec call(vec const& x, T) { return x; } }; template struct compute_findMSB_vec { GLM_FUNC_QUALIFIER static vec call(vec const& v) { vec x(v); x = compute_findMSB_step_vec= 8>::call(x, static_cast( 1)); x = compute_findMSB_step_vec= 8>::call(x, static_cast( 2)); x = compute_findMSB_step_vec= 8>::call(x, static_cast( 4)); x = compute_findMSB_step_vec= 16>::call(x, static_cast( 8)); x = compute_findMSB_step_vec= 32>::call(x, static_cast(16)); x = compute_findMSB_step_vec= 64>::call(x, static_cast(32)); return vec(sizeof(T) * 8 - 1) - glm::bitCount(~x); } }; # if GLM_HAS_BITSCAN_WINDOWS template GLM_FUNC_QUALIFIER int compute_findMSB_32(genIUType Value) { unsigned long Result(0); unsigned char IsNotNull = _BitScanReverse(&Result, *reinterpret_cast(&Value)); return IsNotNull ? int(Result) : -1; } template struct compute_findMSB_vec { GLM_FUNC_QUALIFIER static vec call(vec const& x) { return detail::functor1::call(compute_findMSB_32, x); } }; # if !((GLM_COMPILER & GLM_COMPILER_VC) && (GLM_MODEL == GLM_MODEL_32)) template GLM_FUNC_QUALIFIER int compute_findMSB_64(genIUType Value) { unsigned long Result(0); unsigned char IsNotNull = _BitScanReverse64(&Result, *reinterpret_cast(&Value)); return IsNotNull ? int(Result) : -1; } template struct compute_findMSB_vec { GLM_FUNC_QUALIFIER static vec call(vec const& x) { return detail::functor1::call(compute_findMSB_64, x); } }; # endif # endif//GLM_HAS_BITSCAN_WINDOWS }//namespace detail // uaddCarry GLM_FUNC_QUALIFIER uint uaddCarry(uint const& x, uint const& y, uint & Carry) { uint64 const Value64(static_cast(x) + static_cast(y)); uint64 const Max32((static_cast(1) << static_cast(32)) - static_cast(1)); Carry = Value64 > Max32 ? 1u : 0u; return static_cast(Value64 % (Max32 + static_cast(1))); } template GLM_FUNC_QUALIFIER vec uaddCarry(vec const& x, vec const& y, vec& Carry) { vec Value64(vec(x) + vec(y)); vec Max32((static_cast(1) << static_cast(32)) - static_cast(1)); Carry = mix(vec(0), vec(1), greaterThan(Value64, Max32)); return vec(Value64 % (Max32 + static_cast(1))); } // usubBorrow GLM_FUNC_QUALIFIER uint usubBorrow(uint const& x, uint const& y, uint & Borrow) { GLM_STATIC_ASSERT(sizeof(uint) == sizeof(uint32), "uint and uint32 size mismatch"); Borrow = x >= y ? static_cast(0) : static_cast(1); if(y >= x) return y - x; else return static_cast((static_cast(1) << static_cast(32)) + (static_cast(y) - static_cast(x))); } template GLM_FUNC_QUALIFIER vec usubBorrow(vec const& x, vec const& y, vec& Borrow) { Borrow = mix(vec(1), vec(0), greaterThanEqual(x, y)); vec const YgeX(y - x); vec const XgeY(vec((static_cast(1) << static_cast(32)) + (vec(y) - vec(x)))); return mix(XgeY, YgeX, greaterThanEqual(y, x)); } // umulExtended GLM_FUNC_QUALIFIER void umulExtended(uint const& x, uint const& y, uint & msb, uint & lsb) { GLM_STATIC_ASSERT(sizeof(uint) == sizeof(uint32), "uint and uint32 size mismatch"); uint64 Value64 = static_cast(x) * static_cast(y); msb = static_cast(Value64 >> static_cast(32)); lsb = static_cast(Value64); } template GLM_FUNC_QUALIFIER void umulExtended(vec const& x, vec const& y, vec& msb, vec& lsb) { GLM_STATIC_ASSERT(sizeof(uint) == sizeof(uint32), "uint and uint32 size mismatch"); vec Value64(vec(x) * vec(y)); msb = vec(Value64 >> static_cast(32)); lsb = vec(Value64); } // imulExtended GLM_FUNC_QUALIFIER void imulExtended(int x, int y, int& msb, int& lsb) { GLM_STATIC_ASSERT(sizeof(int) == sizeof(int32), "int and int32 size mismatch"); int64 Value64 = static_cast(x) * static_cast(y); msb = static_cast(Value64 >> static_cast(32)); lsb = static_cast(Value64); } template GLM_FUNC_QUALIFIER void imulExtended(vec const& x, vec const& y, vec& msb, vec& lsb) { GLM_STATIC_ASSERT(sizeof(int) == sizeof(int32), "int and int32 size mismatch"); vec Value64(vec(x) * vec(y)); lsb = vec(Value64 & static_cast(0xFFFFFFFF)); msb = vec((Value64 >> static_cast(32)) & static_cast(0xFFFFFFFF)); } // bitfieldExtract template GLM_FUNC_QUALIFIER genIUType bitfieldExtract(genIUType Value, int Offset, int Bits) { return bitfieldExtract(vec<1, genIUType>(Value), Offset, Bits).x; } template GLM_FUNC_QUALIFIER vec bitfieldExtract(vec const& Value, int Offset, int Bits) { GLM_STATIC_ASSERT(std::numeric_limits::is_integer, "'bitfieldExtract' only accept integer inputs"); return (Value >> static_cast(Offset)) & static_cast(detail::mask(Bits)); } // bitfieldInsert template GLM_FUNC_QUALIFIER genIUType bitfieldInsert(genIUType const& Base, genIUType const& Insert, int Offset, int Bits) { return bitfieldInsert(vec<1, genIUType>(Base), vec<1, genIUType>(Insert), Offset, Bits).x; } template GLM_FUNC_QUALIFIER vec bitfieldInsert(vec const& Base, vec const& Insert, int Offset, int Bits) { GLM_STATIC_ASSERT(std::numeric_limits::is_integer, "'bitfieldInsert' only accept integer values"); T const Mask = static_cast(detail::mask(Bits) << Offset); return (Base & ~Mask) | (Insert & Mask); } // bitfieldReverse template GLM_FUNC_QUALIFIER genType bitfieldReverse(genType x) { return bitfieldReverse(glm::vec<1, genType, glm::defaultp>(x)).x; } template GLM_FUNC_QUALIFIER vec bitfieldReverse(vec const& v) { vec x(v); x = detail::compute_bitfieldReverseStep::value, sizeof(T) * 8>= 2>::call(x, static_cast(0x5555555555555555ull), static_cast( 1)); x = detail::compute_bitfieldReverseStep::value, sizeof(T) * 8>= 4>::call(x, static_cast(0x3333333333333333ull), static_cast( 2)); x = detail::compute_bitfieldReverseStep::value, sizeof(T) * 8>= 8>::call(x, static_cast(0x0F0F0F0F0F0F0F0Full), static_cast( 4)); x = detail::compute_bitfieldReverseStep::value, sizeof(T) * 8>= 16>::call(x, static_cast(0x00FF00FF00FF00FFull), static_cast( 8)); x = detail::compute_bitfieldReverseStep::value, sizeof(T) * 8>= 32>::call(x, static_cast(0x0000FFFF0000FFFFull), static_cast(16)); x = detail::compute_bitfieldReverseStep::value, sizeof(T) * 8>= 64>::call(x, static_cast(0x00000000FFFFFFFFull), static_cast(32)); return x; } // bitCount template GLM_FUNC_QUALIFIER int bitCount(genType x) { return bitCount(glm::vec<1, genType, glm::defaultp>(x)).x; } template GLM_FUNC_QUALIFIER vec bitCount(vec const& v) { # if GLM_COMPILER & GLM_COMPILER_VC # pragma warning(push) # pragma warning(disable : 4310) //cast truncates constant value # endif vec::type, Q> x(*reinterpret_cast::type, Q> const *>(&v)); x = detail::compute_bitfieldBitCountStep::type, Q, detail::is_aligned::value, sizeof(T) * 8>= 2>::call(x, typename detail::make_unsigned::type(0x5555555555555555ull), typename detail::make_unsigned::type( 1)); x = detail::compute_bitfieldBitCountStep::type, Q, detail::is_aligned::value, sizeof(T) * 8>= 4>::call(x, typename detail::make_unsigned::type(0x3333333333333333ull), typename detail::make_unsigned::type( 2)); x = detail::compute_bitfieldBitCountStep::type, Q, detail::is_aligned::value, sizeof(T) * 8>= 8>::call(x, typename detail::make_unsigned::type(0x0F0F0F0F0F0F0F0Full), typename detail::make_unsigned::type( 4)); x = detail::compute_bitfieldBitCountStep::type, Q, detail::is_aligned::value, sizeof(T) * 8>= 16>::call(x, typename detail::make_unsigned::type(0x00FF00FF00FF00FFull), typename detail::make_unsigned::type( 8)); x = detail::compute_bitfieldBitCountStep::type, Q, detail::is_aligned::value, sizeof(T) * 8>= 32>::call(x, typename detail::make_unsigned::type(0x0000FFFF0000FFFFull), typename detail::make_unsigned::type(16)); x = detail::compute_bitfieldBitCountStep::type, Q, detail::is_aligned::value, sizeof(T) * 8>= 64>::call(x, typename detail::make_unsigned::type(0x00000000FFFFFFFFull), typename detail::make_unsigned::type(32)); return vec(x); # if GLM_COMPILER & GLM_COMPILER_VC # pragma warning(pop) # endif } // findLSB template GLM_FUNC_QUALIFIER int findLSB(genIUType Value) { GLM_STATIC_ASSERT(std::numeric_limits::is_integer, "'findLSB' only accept integer values"); return detail::compute_findLSB::call(Value); } template GLM_FUNC_QUALIFIER vec findLSB(vec const& x) { GLM_STATIC_ASSERT(std::numeric_limits::is_integer, "'findLSB' only accept integer values"); return detail::functor1::call(findLSB, x); } // findMSB template GLM_FUNC_QUALIFIER int findMSB(genIUType v) { GLM_STATIC_ASSERT(std::numeric_limits::is_integer, "'findMSB' only accept integer values"); return findMSB(vec<1, genIUType>(v)).x; } template GLM_FUNC_QUALIFIER vec findMSB(vec const& v) { GLM_STATIC_ASSERT(std::numeric_limits::is_integer, "'findMSB' only accept integer values"); return detail::compute_findMSB_vec::call(v); } }//namespace glm #if GLM_ARCH != GLM_ARCH_PURE && GLM_HAS_UNRESTRICTED_UNIONS # include "func_integer_simd.inl" #endif