[cfe-commits] [libcxx] r145721 - /libcxx/trunk/include/functional

Howard Hinnant hhinnant at apple.com
Fri Dec 2 14:52:09 PST 2011


Author: hhinnant
Date: Fri Dec  2 16:52:09 2011
New Revision: 145721

URL: http://llvm.org/viewvc/llvm-project?rev=145721&view=rev
Log:
Fixes to hash for long long, unsigned long long, float, double and long double.  Credit Dave Zarzycki

Modified:
    libcxx/trunk/include/functional

Modified: libcxx/trunk/include/functional
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/functional?rev=145721&r1=145720&r2=145721&view=diff
==============================================================================
--- libcxx/trunk/include/functional (original)
+++ libcxx/trunk/include/functional Fri Dec  2 16:52:09 2011
@@ -1921,11 +1921,19 @@
     _LIBCPP_INLINE_VISIBILITY
     size_t operator()(long long __v) const _NOEXCEPT
     {
-        size_t __r = 0;
-        const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
-        for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
-            __r ^= __p[__i];
-        return __r;
+#ifdef __LP64__
+        return __v;
+#else
+        union {
+            long long __l;
+            struct {
+                int __a;
+                int __b;
+            } __s;
+        } __u;
+        __u.__l = __v;
+        return __u.__s.__a ^ __u.__s.__b;
+#endif
     }
 };
 
@@ -1936,11 +1944,19 @@
     _LIBCPP_INLINE_VISIBILITY
     size_t operator()(unsigned long long __v) const _NOEXCEPT
     {
-        size_t __r = 0;
-        const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
-        for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
-            __r ^= __p[__i];
-        return __r;
+#ifdef __LP64__
+        return __v;
+#else
+        union {
+            unsigned long long __ul;
+            struct {
+                int __a;
+                int __b;
+            } __s;
+        } __u;
+        __u.__ul = __v;
+        return __u.__s.__a ^ __u.__s.__b;
+#endif
     }
 };
 
@@ -1951,10 +1967,18 @@
     _LIBCPP_INLINE_VISIBILITY
     size_t operator()(float __v) const _NOEXCEPT
     {
+        union {
+#ifdef __LP64__
+            double __f;
+#else
+            float __f;
+#endif
+            size_t __d;
+        } __u;
         if (__v == 0)
             return 0;
-        const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
-        return *__p;
+        __u.__f = __v;
+        return __u.__d;
     }
 };
 
@@ -1965,13 +1989,18 @@
     _LIBCPP_INLINE_VISIBILITY
     size_t operator()(double __v) const _NOEXCEPT
     {
+        union {
+#ifdef __LP64__
+            double __f;
+#else
+            float __f;
+#endif
+            size_t __d;
+        } __u;
         if (__v == 0)
             return 0;
-        size_t __r = 0;
-        const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
-        for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
-            __r ^= __p[__i];
-        return __r;
+        __u.__f = __v;
+        return __u.__d;
     }
 };
 
@@ -1982,13 +2011,18 @@
     _LIBCPP_INLINE_VISIBILITY
     size_t operator()(long double __v) const _NOEXCEPT
     {
+        union {
+#ifdef __LP64__
+            double __f;
+#else
+            float __f;
+#endif
+            size_t __d;
+        } __u;
         if (__v == 0)
             return 0;
-        size_t __r = 0;
-        const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
-        for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
-            __r ^= __p[__i];
-        return __r;
+        __u.__f = __v;
+        return __u.__d;
     }
 };
 





More information about the cfe-commits mailing list