From b4c6cb9a86789f8cbb49e62d1042df00f6e142ef Mon Sep 17 00:00:00 2001
From: Pedro Gonnet <gonnet@google.com>
Date: Fri, 26 Apr 2019 22:13:58 +0200
Subject: [PATCH] switch to using a 32-bit flag field and atomics, replace '|='
 with '= ( ... || ... )', which should do short-circuit evaluation, and is
 easier to read.

---
 src/atomic.h |  1 +
 src/cell.c   |  8 ++++----
 src/cell.h   | 12 ++++++------
 src/runner.c |  2 +-
 4 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/src/atomic.h b/src/atomic.h
index 97e0935f5b..43da982346 100644
--- a/src/atomic.h
+++ b/src/atomic.h
@@ -29,6 +29,7 @@
 #define atomic_add(v, i) __sync_fetch_and_add(v, i)
 #define atomic_sub(v, i) __sync_fetch_and_sub(v, i)
 #define atomic_or(v, i) __sync_fetch_and_or(v, i)
+#define atomic_and(v, i) __sync_fetch_and_and(v, i)
 #define atomic_inc(v) atomic_add(v, 1)
 #define atomic_dec(v) atomic_sub(v, 1)
 #define atomic_cas(v, o, n) __sync_val_compare_and_swap(v, o, n)
diff --git a/src/cell.c b/src/cell.c
index 392e5bb447..f14fcb711f 100644
--- a/src/cell.c
+++ b/src/cell.c
@@ -4097,7 +4097,7 @@ void cell_drift_part(struct cell *c, const struct engine *e, int force) {
   float cell_h_max = 0.f;
 
   /* Drift irrespective of cell flags? */
-  force |= cell_get_flag(c, cell_flag_do_hydro_drift);
+  force = (force || cell_get_flag(c, cell_flag_do_hydro_drift));
 
 #ifdef SWIFT_DEBUG_CHECKS
   /* Check that we only drift local cells. */
@@ -4279,7 +4279,7 @@ void cell_drift_gpart(struct cell *c, const struct engine *e, int force) {
   struct gpart *const gparts = c->grav.parts;
 
   /* Drift irrespective of cell flags? */
-  force |= cell_get_flag(c, cell_flag_do_grav_drift);
+  force = (force || cell_get_flag(c, cell_flag_do_grav_drift));
 
 #ifdef SWIFT_DEBUG_CHECKS
   /* Check that we only drift local cells. */
@@ -4407,7 +4407,7 @@ void cell_drift_spart(struct cell *c, const struct engine *e, int force) {
   float cell_h_max = 0.f;
 
   /* Drift irrespective of cell flags? */
-  force |= cell_get_flag(c, cell_flag_do_stars_drift);
+  force = (force || cell_get_flag(c, cell_flag_do_stars_drift));
 
 #ifdef SWIFT_DEBUG_CHECKS
   /* Check that we only drift local cells. */
@@ -4574,7 +4574,7 @@ void cell_drift_bpart(struct cell *c, const struct engine *e, int force) {
   float cell_h_max = 0.f;
 
   /* Drift irrespective of cell flags? */
-  force |= cell_get_flag(c, cell_flag_do_bh_drift);
+  force = (force || cell_get_flag(c, cell_flag_do_bh_drift));
 
 #ifdef SWIFT_DEBUG_CHECKS
   /* Check that we only drift local cells. */
diff --git a/src/cell.h b/src/cell.h
index e53b02036d..117eb708f2 100644
--- a/src/cell.h
+++ b/src/cell.h
@@ -269,7 +269,7 @@ struct cell {
   struct cell *super;
 
   /*! Cell flags bit-mask. */
-  uint16_t flags;
+  uint32_t flags;
 
   /*! Hydro variables */
   struct {
@@ -1292,19 +1292,19 @@ __attribute__((always_inline)) INLINE static void cell_free_stars_sorts(
 
 /** Set the given flag for the given cell. */
 __attribute__((always_inline)) INLINE static void cell_set_flag(struct cell *c,
-                                                                uint16_t flag) {
-  c->flags |= flag;
+                                                                uint32_t flag) {
+  atomic_or(&c->flags, flag);
 }
 
 /** Clear the given flag for the given cell. */
 __attribute__((always_inline)) INLINE static void cell_clear_flag(
-    struct cell *c, uint16_t flag) {
-  c->flags &= ~flag;
+    struct cell *c, uint32_t flag) {
+  atomic_and(&c->flags, ~flag);
 }
 
 /** Get the given flag for the given cell. */
 __attribute__((always_inline)) INLINE static int cell_get_flag(
-    const struct cell *c, uint16_t flag) {
+    const struct cell *c, uint32_t flag) {
   return (c->flags & flag) > 0;
 }
 
diff --git a/src/runner.c b/src/runner.c
index b6878935b3..1a15ce5371 100644
--- a/src/runner.c
+++ b/src/runner.c
@@ -2938,7 +2938,7 @@ void runner_do_limiter(struct runner *r, struct cell *c, int force, int timer) {
                 ti_gravity_beg_max = 0;
 
   /* Limit irrespective of cell flags? */
-  force |= cell_get_flag(c, cell_flag_do_hydro_limiter);
+  force = (force || cell_get_flag(c, cell_flag_do_hydro_limiter));
 
   /* Early abort? */
   if (c->hydro.count == 0) {
-- 
GitLab