hash: Prevent warnings about strict aliasing rules.
authorJesse Gross <jesse@nicira.com>
Wed, 20 Jan 2010 18:50:48 +0000 (13:50 -0500)
committerJesse Gross <jesse@nicira.com>
Wed, 20 Jan 2010 19:02:50 +0000 (14:02 -0500)
Some versions of GCC complain about violations of strict aliasing
rules due to a cast between different pointer types.  This avoids
any aliasing by copying the value first.

lib/hash.h

index 3f14038..5f6409c 100644 (file)
@@ -83,8 +83,11 @@ static inline uint32_t hash_boolean(bool x, uint32_t basis)
 
 static inline uint32_t hash_double(double x, uint32_t basis)
 {
-    BUILD_ASSERT_DECL(sizeof x == 8);
-    return hash_2words((const uint32_t *) &x, basis);
+    uint32_t value[2];
+    BUILD_ASSERT_DECL(sizeof x == sizeof value);
+
+    memcpy(value, &x, sizeof value);
+    return hash_2words(value, basis);
 }
 
 static inline uint32_t hash_pointer(const void *p, uint32_t basis)