From 4bae47db4cd81345084eb098b886471ce45391be Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Thu, 19 Dec 2013 18:51:23 +0100 Subject: [PATCH] atomic.h: added atomicMaximum() helper function --- include/mitsuba/core/atomic.h | 39 +++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/include/mitsuba/core/atomic.h b/include/mitsuba/core/atomic.h index 096b9d47..a328ba55 100644 --- a/include/mitsuba/core/atomic.h +++ b/include/mitsuba/core/atomic.h @@ -189,6 +189,45 @@ inline int64_t atomicAdd(volatile int64_t *dst, int64_t delta) { #endif } +/** + * \brief Atomically set \c dst to the maximum of itself and \c value + * \return The maximum value now stored in \c dst + */ + +inline int64_t atomicMaximum(volatile int64_t *dst, int64_t value) { + int64_t current; + do { + current = *dst; + if (value <= current) + return current; + #if (defined(__i386__) || defined(__amd64__)) + __asm__ __volatile__ ("pause\n"); + #endif + } while (!atomicCompareAndExchange(dst, value, current)); + + return value; +} + +/* + * \brief Atomically set \c dst to the maximum of itself and \c value + * \return The maximum value now stored in \c dst + */ + +inline int32_t atomicMaximum(volatile int32_t *dst, int32_t value) { + int32_t current; + do { + current = *dst; + if (value <= current) + return current; + #if (defined(__i386__) || defined(__amd32__)) + __asm__ __volatile__ ("pause\n"); + #endif + } while (!atomicCompareAndExchange(dst, value, current)); + + return value; +} + + /*! }@ */ MTS_NAMESPACE_END