Skip to content
Snippets Groups Projects
Commit 449346aa authored by Matthieu Schaller's avatar Matthieu Schaller
Browse files

Added an atomic max for int

parent 85af978e
Branches
Tags
1 merge request!1048Atomic gravity and time-step limiter
...@@ -38,6 +38,29 @@ ...@@ -38,6 +38,29 @@
#define atomic_cas(v, o, n) __sync_val_compare_and_swap(v, o, n) #define atomic_cas(v, o, n) __sync_val_compare_and_swap(v, o, n)
#define atomic_swap(v, n) __sync_lock_test_and_set(v, n) #define atomic_swap(v, n) __sync_lock_test_and_set(v, n)
/**
* @brief Atomic min operation on ints.
*
* This is a text-book implementation based on an atomic CAS.
*
* @param address The address to update.
* @param y The value to update the address with.
*/
__attribute__((always_inline)) INLINE static void atomic_min(
volatile int *const address, const int y) {
int *int_ptr = (int *)address;
int test_val, old_val, new_val;
old_val = *address;
do {
test_val = old_val;
new_val = min(old_val, y);
old_val = atomic_cas(int_ptr, test_val, new_val);
} while (test_val != old_val);
}
/** /**
* @brief Atomic min operation on floats. * @brief Atomic min operation on floats.
* *
...@@ -69,29 +92,6 @@ __attribute__((always_inline)) INLINE static void atomic_min_f( ...@@ -69,29 +92,6 @@ __attribute__((always_inline)) INLINE static void atomic_min_f(
} while (test_val.as_int != old_val.as_int); } while (test_val.as_int != old_val.as_int);
} }
/**
* @brief Atomic min operation on ints.
*
* This is a text-book implementation based on an atomic CAS.
*
* @param address The address to update.
* @param y The value to update the address with.
*/
__attribute__((always_inline)) INLINE static void atomic_min(
volatile int *address, int y) {
int *int_ptr = (int *)address;
int test_val, old_val, new_val;
old_val = *address;
do {
test_val = old_val;
new_val = min(old_val, y);
old_val = atomic_cas(int_ptr, test_val, new_val);
} while (test_val != old_val);
}
/** /**
* @brief Atomic min operation on doubles. * @brief Atomic min operation on doubles.
* *
...@@ -145,6 +145,29 @@ __attribute__((always_inline)) INLINE static void atomic_max_c( ...@@ -145,6 +145,29 @@ __attribute__((always_inline)) INLINE static void atomic_max_c(
} while (test_val != old_val); } while (test_val != old_val);
} }
/**
* @brief Atomic max operation on ints.
*
* This is a text-book implementation based on an atomic CAS.
*
* @param address The address to update.
* @param y The value to update the address with.
*/
__attribute__((always_inline)) INLINE static void atomic_max(
volatile int *const address, const int y) {
int *int_ptr = (int *)address;
int test_val, old_val, new_val;
old_val = *address;
do {
test_val = old_val;
new_val = max(old_val, y);
old_val = atomic_cas(int_ptr, test_val, new_val);
} while (test_val != old_val);
}
/** /**
* @brief Atomic max operation on floats. * @brief Atomic max operation on floats.
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment