From: Mathieu Desnoyers Date: Mon, 26 Mar 2012 15:27:01 +0000 (-0400) Subject: Fix uatomic sign cast X-Git-Tag: v0.6.9~3 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=ecf2edf639926ad2a7842ebe1e6c456cc106e4c0;p=urcu.git Fix uatomic sign cast 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 --- diff --git a/tests/test_uatomic.c b/tests/test_uatomic.c index 692060b..5e72baf 100644 --- a/tests/test_uatomic.c +++ b/tests/test_uatomic.c @@ -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); \ diff --git a/urcu/compiler.h b/urcu/compiler.h index f977b95..9286786 100644 --- a/urcu/compiler.h +++ b/urcu/compiler.h @@ -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 */ diff --git a/urcu/uatomic/generic.h b/urcu/uatomic/generic.h index bf7cc6a..04f4afd 100644 --- a/urcu/uatomic/generic.h +++ b/urcu/uatomic/generic.h @@ -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) diff --git a/urcu/uatomic/ppc.h b/urcu/uatomic/ppc.h index 3180750..ee3c516 100644 --- a/urcu/uatomic/ppc.h +++ b/urcu/uatomic/ppc.h @@ -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 } diff --git a/urcu/uatomic/s390.h b/urcu/uatomic/s390.h index b274c1c..32c676e 100644 --- a/urcu/uatomic/s390.h +++ b/urcu/uatomic/s390.h @@ -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 diff --git a/urcu/uatomic/sparc64.h b/urcu/uatomic/sparc64.h index d9ecada..6294566 100644 --- a/urcu/uatomic/sparc64.h +++ b/urcu/uatomic/sparc64.h @@ -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 diff --git a/urcu/uatomic/x86.h b/urcu/uatomic/x86.h index 9b67f16..afe6e37 100644 --- a/urcu/uatomic/x86.h +++ b/urcu/uatomic/x86.h @@ -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)))