]> git.lttng.org Git - urcu.git/commitdiff
Fix uatomic sign cast
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 26 Mar 2012 15:27:01 +0000 (11:27 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 9 Jan 2013 18:25:23 +0000 (13:25 -0500)
Passing an unsigned int to uatomic_sub does not honor sign extend to
long, as we should be allowed by assume.

Fix this by introducing caa_cast_long_keep_sign(), which casts either to
long or unsigned long depending on the signedness of the argument
received. It is used in uatomic_sub before applying the "-" operator,
since this operator needs to operate on the "long" type size (since sign
extension might not be performed if the argument received is unsigned).

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
tests/test_uatomic.c
urcu/compiler.h
urcu/uatomic/generic.h
urcu/uatomic/ppc.h
urcu/uatomic/s390.h
urcu/uatomic/sparc64.h
urcu/uatomic/x86.h

index 692060bcdf175763e2290c6c207d7d66366928c9..5e72baf842898dc3c29ec82df36ff5e97ed37e99 100644 (file)
@@ -40,6 +40,10 @@ do {                                         \
        assert(uatomic_read(ptr) == 122);       \
        v = uatomic_sub_return(ptr, 1);         \
        assert(v == 121);                       \
+       uatomic_sub(ptr, (unsigned int) 2);     \
+       assert(uatomic_read(ptr) == 119);       \
+       uatomic_inc(ptr);                       \
+       uatomic_inc(ptr);                       \
        assert(uatomic_read(ptr) == 121);       \
        uatomic_and(ptr, 129);                  \
        assert(uatomic_read(ptr) == 1);         \
index f977b95fdc60f73b6ef6c9ab2b96c4c30a884de2..92867865865f19e45029e0d65c1ee5944c404420 100644 (file)
@@ -85,4 +85,9 @@
 #define URCU_FORCE_CAST(type, arg)     ((type) (arg))
 #endif
 
+#define caa_is_signed_type(type)       (((type) (-1)) < 0)
+
+#define caa_cast_long_keep_sign(v)     \
+       (caa_is_signed_type(__typeof__(v)) ? (long) (v) : (unsigned long) (v))
+
 #endif /* _URCU_COMPILER_H */
index bf7cc6aa05172b5072368ae381d60fc1aa31a6d3..04f4afd715800dd02446183d183c4f896de142c1 100644 (file)
@@ -81,9 +81,10 @@ unsigned long _uatomic_cmpxchg(void *addr, unsigned long old,
 }
 
 
-#define uatomic_cmpxchg(addr, old, _new)                                   \
-       ((__typeof__(*(addr))) _uatomic_cmpxchg((addr), (unsigned long)(old),\
-                                               (unsigned long)(_new),      \
+#define uatomic_cmpxchg(addr, old, _new)                                     \
+       ((__typeof__(*(addr))) _uatomic_cmpxchg((addr),                       \
+                                               caa_cast_long_keep_sign(old), \
+                                               caa_cast_long_keep_sign(_new),\
                                                sizeof(*(addr))))
 
 
@@ -119,8 +120,8 @@ void _uatomic_and(void *addr, unsigned long val,
 
 #define uatomic_and(addr, v)                   \
        (_uatomic_and((addr),                   \
-                     (unsigned long)(v),       \
-                     sizeof(*(addr))))
+               caa_cast_long_keep_sign(v),     \
+               sizeof(*(addr))))
 #endif
 
 /* uatomic_or */
@@ -156,8 +157,8 @@ void _uatomic_or(void *addr, unsigned long val,
 
 #define uatomic_or(addr, v)                    \
        (_uatomic_or((addr),                    \
-                    (unsigned long)(v),        \
-                    sizeof(*(addr))))
+               caa_cast_long_keep_sign(v),     \
+               sizeof(*(addr))))
 #endif
 
 /* uatomic_add_return */
@@ -188,10 +189,10 @@ unsigned long _uatomic_add_return(void *addr, unsigned long val,
 }
 
 
-#define uatomic_add_return(addr, v)                                    \
-       ((__typeof__(*(addr))) _uatomic_add_return((addr),              \
-                                                 (unsigned long)(v),   \
-                                                 sizeof(*(addr))))
+#define uatomic_add_return(addr, v)                                        \
+       ((__typeof__(*(addr))) _uatomic_add_return((addr),                  \
+                                               caa_cast_long_keep_sign(v), \
+                                               sizeof(*(addr))))
 #endif /* #ifndef uatomic_add_return */
 
 #ifndef uatomic_xchg
@@ -253,7 +254,8 @@ unsigned long _uatomic_exchange(void *addr, unsigned long val, int len)
 }
 
 #define uatomic_xchg(addr, v)                                              \
-       ((__typeof__(*(addr))) _uatomic_exchange((addr), (unsigned long)(v), \
+       ((__typeof__(*(addr))) _uatomic_exchange((addr),                    \
+                                               caa_cast_long_keep_sign(v), \
                                                sizeof(*(addr))))
 #endif /* #ifndef uatomic_xchg */
 
@@ -322,10 +324,10 @@ void _uatomic_and(void *addr, unsigned long val, int len)
        _uatomic_link_error();
 }
 
-#define uatomic_and(addr, v)           \
-       (_uatomic_and((addr),           \
-                   (unsigned long)(v), \
-                   sizeof(*(addr))))
+#define uatomic_and(addr, v)                   \
+       (_uatomic_and((addr),                   \
+               caa_cast_long_keep_sign(v),     \
+               sizeof(*(addr))))
 #endif /* #ifndef uatomic_and */
 
 #ifndef uatomic_or
@@ -393,10 +395,10 @@ void _uatomic_or(void *addr, unsigned long val, int len)
        _uatomic_link_error();
 }
 
-#define uatomic_or(addr, v)            \
-       (_uatomic_or((addr),            \
-                    (unsigned long)(v),\
-                    sizeof(*(addr))))
+#define uatomic_or(addr, v)                    \
+       (_uatomic_or((addr),                    \
+               caa_cast_long_keep_sign(v),     \
+               sizeof(*(addr))))
 #endif /* #ifndef uatomic_or */
 
 #ifndef uatomic_add_return
@@ -469,10 +471,10 @@ unsigned long _uatomic_add_return(void *addr, unsigned long val, int len)
        return 0;
 }
 
-#define uatomic_add_return(addr, v)                                    \
-       ((__typeof__(*(addr))) _uatomic_add_return((addr),              \
-                                                 (unsigned long)(v),   \
-                                                 sizeof(*(addr))))
+#define uatomic_add_return(addr, v)                                        \
+       ((__typeof__(*(addr))) _uatomic_add_return((addr),                  \
+                                               caa_cast_long_keep_sign(v), \
+                                               sizeof(*(addr))))
 #endif /* #ifndef uatomic_add_return */
 
 #ifndef uatomic_xchg
@@ -546,7 +548,8 @@ unsigned long _uatomic_exchange(void *addr, unsigned long val, int len)
 }
 
 #define uatomic_xchg(addr, v)                                              \
-       ((__typeof__(*(addr))) _uatomic_exchange((addr), (unsigned long)(v), \
+       ((__typeof__(*(addr))) _uatomic_exchange((addr),                    \
+                                               caa_cast_long_keep_sign(v), \
                                                sizeof(*(addr))))
 #endif /* #ifndef uatomic_xchg */
 
@@ -558,8 +561,10 @@ unsigned long _uatomic_exchange(void *addr, unsigned long val, int len)
 #define uatomic_add(addr, v)           (void)uatomic_add_return((addr), (v))
 #endif
 
-#define uatomic_sub_return(addr, v)    uatomic_add_return((addr), -(v))
-#define uatomic_sub(addr, v)           uatomic_add((addr), -(v))
+#define uatomic_sub_return(addr, v)    \
+       uatomic_add_return((addr), -(caa_cast_long_keep_sign(v)))
+#define uatomic_sub(addr, v)           \
+       uatomic_add((addr), -(caa_cast_long_keep_sign(v)))
 
 #ifndef uatomic_inc
 #define uatomic_inc(addr)              uatomic_add((addr), 1)
index 31807505c271fc0a1e6eea5400a529585ea8935a..ee3c516645cbe3899fab919bbcf208c5229ffa0b 100644 (file)
@@ -100,7 +100,8 @@ unsigned long _uatomic_exchange(void *addr, unsigned long val, int len)
 }
 
 #define uatomic_xchg(addr, v)                                              \
-       ((__typeof__(*(addr))) _uatomic_exchange((addr), (unsigned long)(v), \
+       ((__typeof__(*(addr))) _uatomic_exchange((addr),                    \
+                                               caa_cast_long_keep_sign(v), \
                                                sizeof(*(addr))))
 /* cmpxchg */
 
@@ -159,9 +160,10 @@ unsigned long _uatomic_cmpxchg(void *addr, unsigned long old,
 }
 
 
-#define uatomic_cmpxchg(addr, old, _new)                                   \
-       ((__typeof__(*(addr))) _uatomic_cmpxchg((addr), (unsigned long)(old),\
-                                               (unsigned long)(_new),      \
+#define uatomic_cmpxchg(addr, old, _new)                                     \
+       ((__typeof__(*(addr))) _uatomic_cmpxchg((addr),                       \
+                                               caa_cast_long_keep_sign(old), \
+                                               caa_cast_long_keep_sign(_new),\
                                                sizeof(*(addr))))
 
 /* uatomic_add_return */
@@ -215,10 +217,10 @@ unsigned long _uatomic_add_return(void *addr, unsigned long val,
 }
 
 
-#define uatomic_add_return(addr, v)                                    \
-       ((__typeof__(*(addr))) _uatomic_add_return((addr),              \
-                                                 (unsigned long)(v),   \
-                                                 sizeof(*(addr))))
+#define uatomic_add_return(addr, v)                                        \
+       ((__typeof__(*(addr))) _uatomic_add_return((addr),                  \
+                                               caa_cast_long_keep_sign(v), \
+                                               sizeof(*(addr))))
 
 #ifdef __cplusplus 
 }
index b274c1cb372d22be165aa6dd01bf5da08430d21b..32c676e0af590af4e983d61074ca4b8b11ed5441 100644 (file)
@@ -106,8 +106,9 @@ unsigned long _uatomic_exchange(volatile void *addr, unsigned long val, int len)
 }
 
 #define uatomic_xchg(addr, v)                                              \
-       (__typeof__(*(addr))) _uatomic_exchange((addr), (unsigned long)(v), \
-                                              sizeof(*(addr)))
+       (__typeof__(*(addr))) _uatomic_exchange((addr),                     \
+                                               caa_cast_long_keep_sign(v), \
+                                               sizeof(*(addr)))
 
 /* cmpxchg */
 
@@ -145,10 +146,10 @@ unsigned long _uatomic_cmpxchg(void *addr, unsigned long old,
        return 0;
 }
 
-#define uatomic_cmpxchg(addr, old, _new)                               \
-       (__typeof__(*(addr))) _uatomic_cmpxchg((addr),                  \
-                                              (unsigned long)(old),    \
-                                              (unsigned long)(_new),   \
+#define uatomic_cmpxchg(addr, old, _new)                                    \
+       (__typeof__(*(addr))) _uatomic_cmpxchg((addr),                       \
+                                              caa_cast_long_keep_sign(old), \
+                                              caa_cast_long_keep_sign(_new),\
                                               sizeof(*(addr)))
 
 #ifdef __cplusplus 
index d9ecada5aa56c3891c2f34624a60247f02c5168e..62945668fa6612734ca26bbffb965951bda394c4 100644 (file)
@@ -66,9 +66,10 @@ unsigned long _uatomic_cmpxchg(void *addr, unsigned long old,
 }
 
 
-#define uatomic_cmpxchg(addr, old, _new)                                   \
-       ((__typeof__(*(addr))) _uatomic_cmpxchg((addr), (unsigned long)(old),\
-                                               (unsigned long)(_new),      \
+#define uatomic_cmpxchg(addr, old, _new)                                      \
+       ((__typeof__(*(addr))) _uatomic_cmpxchg((addr),                        \
+                                               caa_cast_long_keep_sign(old),  \
+                                               caa_cast_long_keep_sign(_new), \
                                                sizeof(*(addr))))
 
 #ifdef __cplusplus 
index 9b67f1608d365830bd951382781ada35f91b6150..afe6e37a9d7bf455694077fcbbe3cb2c2dd3a8cb 100644 (file)
@@ -102,8 +102,9 @@ unsigned long __uatomic_cmpxchg(void *addr, unsigned long old,
 }
 
 #define _uatomic_cmpxchg(addr, old, _new)                                    \
-       ((__typeof__(*(addr))) __uatomic_cmpxchg((addr), (unsigned long)(old),\
-                                               (unsigned long)(_new),        \
+       ((__typeof__(*(addr))) __uatomic_cmpxchg((addr),                      \
+                                               caa_cast_long_keep_sign(old), \
+                                               caa_cast_long_keep_sign(_new),\
                                                sizeof(*(addr))))
 
 /* xchg */
@@ -163,7 +164,8 @@ unsigned long __uatomic_exchange(void *addr, unsigned long val, int len)
 }
 
 #define _uatomic_xchg(addr, v)                                               \
-       ((__typeof__(*(addr))) __uatomic_exchange((addr), (unsigned long)(v), \
+       ((__typeof__(*(addr))) __uatomic_exchange((addr),                     \
+                                               caa_cast_long_keep_sign(v),   \
                                                sizeof(*(addr))))
 
 /* uatomic_add_return */
@@ -226,10 +228,10 @@ unsigned long __uatomic_add_return(void *addr, unsigned long val,
        return 0;
 }
 
-#define _uatomic_add_return(addr, v)                                   \
-       ((__typeof__(*(addr))) __uatomic_add_return((addr),             \
-                                                 (unsigned long)(v),   \
-                                                 sizeof(*(addr))))
+#define _uatomic_add_return(addr, v)                                       \
+       ((__typeof__(*(addr))) __uatomic_add_return((addr),                 \
+                                               caa_cast_long_keep_sign(v), \
+                                               sizeof(*(addr))))
 
 /* uatomic_and */
 
@@ -283,7 +285,7 @@ void __uatomic_and(void *addr, unsigned long val, int len)
 }
 
 #define _uatomic_and(addr, v)                                             \
-       (__uatomic_and((addr), (unsigned long)(v), sizeof(*(addr))))
+       (__uatomic_and((addr), caa_cast_long_keep_sign(v), sizeof(*(addr))))
 
 /* uatomic_or */
 
@@ -337,7 +339,7 @@ void __uatomic_or(void *addr, unsigned long val, int len)
 }
 
 #define _uatomic_or(addr, v)                                              \
-       (__uatomic_or((addr), (unsigned long)(v), sizeof(*(addr))))
+       (__uatomic_or((addr), caa_cast_long_keep_sign(v), sizeof(*(addr))))
 
 /* uatomic_add */
 
@@ -391,7 +393,7 @@ void __uatomic_add(void *addr, unsigned long val, int len)
 }
 
 #define _uatomic_add(addr, v)                                             \
-       (__uatomic_add((addr), (unsigned long)(v), sizeof(*(addr))))
+       (__uatomic_add((addr), caa_cast_long_keep_sign(v), sizeof(*(addr))))
 
 
 /* uatomic_inc */
@@ -517,7 +519,7 @@ extern unsigned long _compat_uatomic_set(void *addr,
                                         unsigned long _new, int len);
 #define compat_uatomic_set(addr, _new)                                        \
        ((__typeof__(*(addr))) _compat_uatomic_set((addr),                     \
-                                               (unsigned long)(_new),         \
+                                               caa_cast_long_keep_sign(_new), \
                                                sizeof(*(addr))))
 
 
@@ -525,35 +527,35 @@ extern unsigned long _compat_uatomic_xchg(void *addr,
                                          unsigned long _new, int len);
 #define compat_uatomic_xchg(addr, _new)                                               \
        ((__typeof__(*(addr))) _compat_uatomic_xchg((addr),                    \
-                                               (unsigned long)(_new),         \
+                                               caa_cast_long_keep_sign(_new), \
                                                sizeof(*(addr))))
 
 extern unsigned long _compat_uatomic_cmpxchg(void *addr, unsigned long old,
                                             unsigned long _new, int len);
 #define compat_uatomic_cmpxchg(addr, old, _new)                                       \
        ((__typeof__(*(addr))) _compat_uatomic_cmpxchg((addr),                 \
-                                               (unsigned long)(old),          \
-                                               (unsigned long)(_new),         \
+                                               caa_cast_long_keep_sign(old),  \
+                                               caa_cast_long_keep_sign(_new), \
                                                sizeof(*(addr))))
 
 extern void _compat_uatomic_and(void *addr, unsigned long _new, int len);
 #define compat_uatomic_and(addr, v)                                   \
        (_compat_uatomic_and((addr),                                   \
-                       (unsigned long)(v),                            \
+                       caa_cast_long_keep_sign(v),                    \
                        sizeof(*(addr))))
 
 extern void _compat_uatomic_or(void *addr, unsigned long _new, int len);
 #define compat_uatomic_or(addr, v)                                    \
        (_compat_uatomic_or((addr),                                    \
-                         (unsigned long)(v),                          \
+                         caa_cast_long_keep_sign(v),                  \
                          sizeof(*(addr))))
 
 extern unsigned long _compat_uatomic_add_return(void *addr,
                                                unsigned long _new, int len);
-#define compat_uatomic_add_return(addr, v)                            \
-       ((__typeof__(*(addr))) _compat_uatomic_add_return((addr),      \
-                                                  (unsigned long)(v), \
-                                                  sizeof(*(addr))))
+#define compat_uatomic_add_return(addr, v)                                 \
+       ((__typeof__(*(addr))) _compat_uatomic_add_return((addr),           \
+                                               caa_cast_long_keep_sign(v), \
+                                               sizeof(*(addr))))
 
 #define compat_uatomic_add(addr, v)                                           \
                ((void)compat_uatomic_add_return((addr), (v)))
This page took 0.040515 seconds and 4 git commands to generate.