Skip to content

Commit 8b8aae3

Browse files
committed
opal/asm: add atomic min/max convenience functions
This commit adds atomic functions for min/max. Signed-off-by: Nathan Hjelm <[email protected]>
1 parent e2808c9 commit 8b8aae3

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

opal/include/opal/sys/atomic.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,10 @@ static inline int32_t opal_atomic_xor_fetch_32(volatile int32_t *addr, int32_t v
409409
static inline int32_t opal_atomic_fetch_xor_32(volatile int32_t *addr, int32_t value);
410410
static inline int32_t opal_atomic_sub_fetch_32(volatile int32_t *addr, int delta);
411411
static inline int32_t opal_atomic_fetch_sub_32(volatile int32_t *addr, int delta);
412+
static inline int32_t opal_atomic_min_fetch_32 (volatile int32_t *addr, int32_t value);
413+
static inline int32_t opal_atomic_fetch_min_32 (volatile int32_t *addr, int32_t value);
414+
static inline int32_t opal_atomic_max_fetch_32 (volatile int32_t *addr, int32_t value);
415+
static inline int32_t opal_atomic_fetch_max_32 (volatile int32_t *addr, int32_t value);
412416

413417
#endif /* OPAL_HAVE_ATOMIC_MATH_32 */
414418

@@ -434,8 +438,12 @@ static inline int64_t opal_atomic_fetch_or_64(volatile int64_t *addr, int64_t va
434438
static inline int64_t opal_atomic_fetch_xor_64(volatile int64_t *addr, int64_t value);
435439
static inline int64_t opal_atomic_sub_fetch_64(volatile int64_t *addr, int64_t delta);
436440
static inline int64_t opal_atomic_fetch_sub_64(volatile int64_t *addr, int64_t delta);
441+
static inline int64_t opal_atomic_min_fetch_64 (volatile int64_t *addr, int64_t value);
442+
static inline int64_t opal_atomic_fetch_min_64 (volatile int64_t *addr, int64_t value);
443+
static inline int64_t opal_atomic_max_fetch_64 (volatile int64_t *addr, int64_t value);
444+
static inline int64_t opal_atomic_fetch_max_64 (volatile int64_t *addr, int64_t value);
437445

438-
#endif /* OPAL_HAVE_ATOMIC_MATH_32 */
446+
#endif /* OPAL_HAVE_ATOMIC_MATH_64 */
439447

440448
#if ! OPAL_HAVE_ATOMIC_MATH_64
441449
/* fix up the value of opal_have_atomic_math_64 to allow for C versions */

opal/include/opal/sys/atomic_impl.h

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,39 @@
3939
*********************************************************************/
4040
#if OPAL_HAVE_ATOMIC_COMPARE_EXCHANGE_32
4141

42+
#if !defined(OPAL_HAVE_ATOMIC_MIN_32)
43+
static inline int32_t opal_atomic_fetch_min_32 (volatile int32_t *addr, int32_t value)
44+
{
45+
int32_t old = *addr;
46+
do {
47+
if (old <= value) {
48+
break;
49+
}
50+
} while (!opal_atomic_compare_exchange_strong_32 (addr, &old, value));
51+
52+
return old;
53+
}
54+
55+
#define OPAL_HAVE_ATOMIC_MIN_32 1
56+
57+
#endif /* OPAL_HAVE_ATOMIC_MIN_32 */
58+
59+
#if !defined(OPAL_HAVE_ATOMIC_MAX_32)
60+
static inline int32_t opal_atomic_fetch_max_32 (volatile int32_t *addr, int32_t value)
61+
{
62+
int32_t old = *addr;
63+
do {
64+
if (old >= value) {
65+
break;
66+
}
67+
} while (!opal_atomic_compare_exchange_strong_32 (addr, &old, value));
68+
69+
return old;
70+
}
71+
72+
#define OPAL_HAVE_ATOMIC_MAX_32 1
73+
#endif /* OPAL_HAVE_ATOMIC_MAX_32 */
74+
4275
#define OPAL_ATOMIC_DEFINE_CMPXCG_OP(type, bits, operation, name) \
4376
static inline type opal_atomic_fetch_ ## name ## _ ## bits (volatile type *addr, type value) \
4477
{ \
@@ -104,6 +137,39 @@ OPAL_ATOMIC_DEFINE_CMPXCG_OP(int32_t, 32, -, sub)
104137

105138
#if OPAL_HAVE_ATOMIC_COMPARE_EXCHANGE_64
106139

140+
#if !defined(OPAL_HAVE_ATOMIC_MIN_64)
141+
static inline int64_t opal_atomic_fetch_min_64 (volatile int64_t *addr, int64_t value)
142+
{
143+
int64_t old = *addr;
144+
do {
145+
if (old <= value) {
146+
break;
147+
}
148+
} while (!opal_atomic_compare_exchange_strong_64 (addr, &old, value));
149+
150+
return old;
151+
}
152+
153+
#define OPAL_HAVE_ATOMIC_MIN_64 1
154+
155+
#endif /* OPAL_HAVE_ATOMIC_MIN_64 */
156+
157+
#if !defined(OPAL_HAVE_ATOMIC_MAX_64)
158+
static inline int64_t opal_atomic_fetch_max_64 (volatile int64_t *addr, int64_t value)
159+
{
160+
int64_t old = *addr;
161+
do {
162+
if (old >= value) {
163+
break;
164+
}
165+
} while (!opal_atomic_compare_exchange_strong_64 (addr, &old, value));
166+
167+
return old;
168+
}
169+
170+
#define OPAL_HAVE_ATOMIC_MAX_64 1
171+
#endif /* OPAL_HAVE_ATOMIC_MAX_64 */
172+
107173
#if !defined(OPAL_HAVE_ATOMIC_SWAP_64)
108174
#define OPAL_HAVE_ATOMIC_SWAP_64 1
109175
static inline int64_t opal_atomic_swap_64(volatile int64_t *addr,
@@ -115,7 +181,7 @@ static inline int64_t opal_atomic_swap_64(volatile int64_t *addr,
115181

116182
return old;
117183
}
118-
#endif /* OPAL_HAVE_ATOMIC_SWAP_32 */
184+
#endif /* OPAL_HAVE_ATOMIC_SWAP_64 */
119185

120186
#if !defined(OPAL_HAVE_ATOMIC_ADD_64)
121187
#define OPAL_HAVE_ATOMIC_ADD_64 1
@@ -321,12 +387,37 @@ OPAL_ATOMIC_DEFINE_OP_FETCH(or, |, int32_t, int32_t, 32)
321387
OPAL_ATOMIC_DEFINE_OP_FETCH(xor, ^, int32_t, int32_t, 32)
322388
OPAL_ATOMIC_DEFINE_OP_FETCH(sub, -, int32_t, int32_t, 32)
323389

390+
static inline int32_t opal_atomic_min_fetch_32 (volatile int32_t *addr, int32_t value)
391+
{
392+
int32_t old = opal_atomic_fetch_min_32 (addr, value);
393+
return old <= value ? old : value;
394+
}
395+
396+
static inline int32_t opal_atomic_max_fetch_32 (volatile int32_t *addr, int32_t value)
397+
{
398+
int32_t old = opal_atomic_fetch_max_32 (addr, value);
399+
return old >= value ? old : value;
400+
}
401+
324402
#if OPAL_HAVE_ATOMIC_MATH_64
325403
OPAL_ATOMIC_DEFINE_OP_FETCH(add, +, int64_t, int64_t, 64)
326404
OPAL_ATOMIC_DEFINE_OP_FETCH(and, &, int64_t, int64_t, 64)
327405
OPAL_ATOMIC_DEFINE_OP_FETCH(or, |, int64_t, int64_t, 64)
328406
OPAL_ATOMIC_DEFINE_OP_FETCH(xor, ^, int64_t, int64_t, 64)
329407
OPAL_ATOMIC_DEFINE_OP_FETCH(sub, -, int64_t, int64_t, 64)
408+
409+
static inline int64_t opal_atomic_min_fetch_64 (volatile int64_t *addr, int64_t value)
410+
{
411+
int64_t old = opal_atomic_fetch_min_64 (addr, value);
412+
return old <= value ? old : value;
413+
}
414+
415+
static inline int64_t opal_atomic_max_fetch_64 (volatile int64_t *addr, int64_t value)
416+
{
417+
int64_t old = opal_atomic_fetch_max_64 (addr, value);
418+
return old >= value ? old : value;
419+
}
420+
330421
#endif
331422

332423
static inline intptr_t opal_atomic_fetch_add_ptr( volatile void* addr,

0 commit comments

Comments
 (0)