inline std::size_t float_hash_impl(long double v) { boost::uint32_t* ptr = (boost::uint32_t*)&v; std::size_t seed = *ptr++; hash_float_combine(seed, *ptr++); hash_float_combine(seed, *(boost::uint16_t*)ptr); return seed; }
inline std::size_t float_hash_impl2(T v) { boost::hash_detail::call_frexp<T> frexp; boost::hash_detail::call_ldexp<T> ldexp; int exp = 0; v = frexp(v, &exp); // A postive value is easier to hash, so combine the // sign with the exponent and use the absolute value. if(v < 0) { v = -v; exp += limits<T>::max_exponent - limits<T>::min_exponent; } // The result of frexp is always between 0.5 and 1, so its // top bit will always be 1. Subtract by 0.5 to remove that. v -= T(0.5); v = ldexp(v, limits<std::size_t>::digits + 1); std::size_t seed = static_cast<std::size_t>(v); v -= seed; // ceiling(digits(T) * log2(radix(T))/ digits(size_t)) - 1; std::size_t const length = (limits<T>::digits * boost::static_log2<limits<T>::radix>::value - 1) / limits<std::size_t>::digits; for(std::size_t i = 0; i != length; ++i) { v = ldexp(v, limits<std::size_t>::digits); std::size_t part = static_cast<std::size_t>(v); v -= part; hash_float_combine(seed, part); } hash_float_combine(seed, exp); return seed; }
inline std::size_t float_hash_impl2(T v) { boost::hash_detail::call_frexp<T> frexp; boost::hash_detail::call_ldexp<T> ldexp; int exp = 0; v = frexp(v, &exp); // A postive value is easier to hash, so combine the // sign with the exponent and use the absolute value. if(v < 0) { v = -v; exp += limits<T>::max_exponent - limits<T>::min_exponent; } v = ldexp(v, limits<std::size_t>::digits); std::size_t seed = static_cast<std::size_t>(v); v -= seed; // ceiling(digits(T) * log2(radix(T))/ digits(size_t)) - 1; std::size_t const length = (limits<T>::digits * boost::static_log2<limits<T>::radix>::value + limits<std::size_t>::digits - 1) / limits<std::size_t>::digits; for(std::size_t i = 0; i != length; ++i) { v = ldexp(v, limits<std::size_t>::digits); std::size_t part = static_cast<std::size_t>(v); v -= part; hash_float_combine(seed, part); } hash_float_combine(seed, exp); return seed; }