ATOMIC_CMPXCHG(3clc)
π ATOMIC_CMPXCHG π
okay bestie, welcome to ATOMIC_CMPXCHG(3clc) from the OpenCL Manual β yeah we're in the 3clc section, come on in, the vibes are atomic π¦
β¨ NAME β¨
atomic_cmpxchg β the atomic_cmpxchg functions. plural. yes there's more than one. keep up π
int atomic_cmpxchg(volatile global(3clc) int *p, int cmp, int val);
unsigned int atomic_cmpxchg(volatile global(3clc) unsigned int *p,
unsigned int cmp, unsigned int val);
int atomic_cmpxchg(volatile local(3clc) int *p, int cmp, int val);
unsigned int atomic_cmpxchg(volatile local(3clc) unsigned int *p,
unsigned int cmp, unsigned int val);
four glow-ups of the same function, depending on whether you're kicking it in global or local memory and whether your int is signed or unsigned. pick your fighter π
πΈ DESCRIPTION πΈ
okay so here's the tea. this function:
- Reads the 32-bit value (the manual calls it
old, iconic name, no notes) that lives at the location pointed to byp. - Computes
(old == cmp) ? val : oldβ like, "does the thing match what I expected? if yes, swap in the new thing, if no, keep the old thing" π - Stores that result at the location pointed to by
p.
and then β the moment we've all been waiting for β the function returns old. yes, the original value, not the new one. it's giving "I remember who you were before" π
there's also a 64-bit version of this whole situation,atom_cmpxchg(3clc), but it's not just sitting there unlocked. it's enabled bycl_khr_int64_base_atomics(3clc). no extension, no 64-bit. periodt π₯
π SPECIFICATION π
OpenCL Specification[1]
(the footnote is real, we'll get to it, stay with me π«Ά)
π SEE ALSO π
go read those too, bestie, they're the rest of the friend group π
π AUTHORS π
The Khronos Group β the actual icons. respect. π
β COPYRIGHT β
Copyright Β© 2007-2011 The Khronos Group Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and/or associated documentation files (the "Materials"), to deal in the Materials without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Materials, and to permit persons to whom the Materials are furnished to do so, subject to the following condition: the above copyright notice and this permission notice shall be included in all copies or substantial portions of the Materials.
(yeah, the whole legal paragraph, every word, because we do not skip the fine print in this house π)
π NOTES π
OpenCL Specificationβ page 279, section 6.12.11 - Atomic Functions for 32-bit integers
and that's the whole page!! every signature, every default-free option-free detail, every reference, all of it, untouched, just with better energy β¨
this little masterpiece came from The Khronos Group, dated 01/14/2021, and it lives under the name ATOMIC_CMPXCHG(3clc) β mwah, iconic, no cap π
π¦ where this page comes from
- ubuntu jammy: opencl-1.2-man-doc 1.0~svn33624-5 Β· other
- ubuntu noble: opencl-1.2-man-doc 1.0~svn33624-5 Β· other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms