This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH][libatomic] Avoid misaligned atomic operations


When using word-wide CAS to emulate atomic fetch-and-op, addresses should
be word-aligned to avoid exceptions on some targets.

The problem manifested in a new port I'm working on as a failure in test
gcc.dg/atomic/stdatomic-op-1.c, and I've confirmed that this patch
fixes it.  x86_64-unknown-linux still bootstraps, but that is admittedly
of little significance, since that target doesn't use these routines.

FYI, I don't have commit access.

2015-01-09  Andrew Waterman <waterman@cs.berkeley.edu>

	* fop_n.c (libat_fetch_op): Align address to word boundary.
---
 libatomic/fop_n.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libatomic/fop_n.c b/libatomic/fop_n.c
index 307184d..c2556eb 100644
--- a/libatomic/fop_n.c
+++ b/libatomic/fop_n.c
@@ -112,9 +112,9 @@ SIZE(C2(libat_fetch_,NAME)) (UTYPE *mptr, UTYPE opval, int smodel)
 
   pre_barrier (smodel);
 
-  wptr = (UWORD *)mptr;
-  shift = 0;
-  mask = -1;
+  wptr = (UWORD *)((uintptr_t)mptr & -WORDSIZE);
+  shift = (((uintptr_t)mptr % WORDSIZE) * CHAR_BIT) ^ SIZE(INVERT_MASK);
+  mask = SIZE(MASK) << shift;
 
   wopval = (UWORD)opval << shift;
   woldval = __atomic_load_n (wptr, __ATOMIC_RELAXED);
-- 
2.2.1


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]