TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 229 subscribers
Post #24735 23
Why doesn't std::atomic support multiplication, division, and mod?

I looked online, and the only answer I could find was that no architectures support them. Ok, I guess that makes sense. However, I noticed that clang targeting x86_64 lowers std::atomic<float>::fetch_add as this as copied from Compiler Explorer,source:'%23include+%3Catomic%3E%0A%0Aauto+fetchaddtest(std::atomic%3Cfloat%3E%26+atomic,+float+rhs)+-%3E+void+%7B%0A++++atomic.fetchadd(rhs)%3B%0A%7D%0A'),l:'5',n:'0',o:'C%2B%2B+source+%231',t:'0')),k:37.75456919060052,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((g:!((h:ir,i:('-fno-discard-value-names':'0',compilerName:'x86-64+clang+(trunk)',demangle-symbols:'0',editorid:1,filter-attributes:'0',filter-comments:'0',filter-debug-info:'0',filter-instruction-metadata:'0',fontScale:12,fontUsePx:'0',j:1,selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),show-optimized:'0',treeid:0,wrap:'1'),l:'5',n:'0',o:'LLVM+IR+Viewer+x86-64+clang+(trunk)+(Editor+%231,+Compiler+%231)',t:'0')),header:(),k:58.110236220472444,l:'4',m:83.92484342379957,n:'0',o:'',s:0,t:'0'),(g:!((g:!((h:compiler,i:(compiler:clangtrunk,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'1',trim:'0',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:12,fontUsePx:'0',j:1,lang:c%2B%2B,libs:!(),options:'-O3+-std%3Dc%2B%2B26',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+x86-64+clang+(trunk)+(Editor+%231)',t:'0')),header:(),k:46.736824930657534,l:'4',m:74.47698744769873,n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compilerName:'x64+msvc+v19.latest',editorid:1,fontScale:12,fontUsePx:'0',j:1,wrap:'1'),l:'5',n:'0',o:'Output+of+x86-64+clang+(trunk)+(Compiler+%231)',t:'0')),header:(),l:'4',m:25.52301255230126,n:'0',o:'',s:0,t:'0')),k:41.889763779527556,l:'3',n:'0',o:'',t:'0')),k:62.24543080939948,l:'2',m:100,n:'0',o:'',t:'0')),l:'2',n:'0',o:'',t:'0')),version:4):

fetchaddtest(std::atomic<float>&, float):
  movd xmm1, dword ptr rdi
.LBB01:
  movd eax, xmm1
  addss xmm1, xmm0
  movd ecx, xmm1
  lock cmpxchg dword ptr [rdi], ecx
  movd xmm1, eax
  jne .LBB0
1
  ret

It's my understanding that this is something like the following:

auto std::atomic<float>::fetchadd(float arg) -> float {
float old
value = this->load();
while(this->compareexchangeweak(oldvalue, expected + arg) == false){}
return old
value;
}

I checked GCC and MSVC too, and they all do the same. So my question is this: assuming there isn't something I'm misunderstanding, if the standard already has methods that do the operation not wait-free on x86, why not add the rest of the operations?

I also found that apparently Microsoft added them for their implementation of C11_Atomic according to this 2022 blog post.

https://redd.it/1qsf6pa
@r_cpp
godbolt.org Compiler Explorer Compiler Explorer is an interactive online compiler which shows the assembly output of compiled C++, Rust, Go (and many more) code.
More from @r_cpp
  1. Oct 8, 2026Henrik Fransson: From Prompt to Probe - Closing the Agentic AI Loop on Bare Metal https://…
  2. Oct 7, 2026(Imperfect) Emulation of Rust's borrow checker in C++23 with Clang Extensions https://comp…
  3. Oct 7, 2026Escaping `CreateProcess()` arguments on Windows https://holyblackcat.github.io/blog/2026/0…
  4. Oct 7, 2026flat_map internals https://quantdev.blog/posts/flat-map-internals/index.html https://redd.…
  5. Oct 7, 2026New C++ Conference Videos Released This Month - September 2026 C++Now 2026-08-31 - 2026-09…
  6. Oct 7, 2026Jean Frantz René: C++ Memory safety for safety-critical and non-safety critical applicatio…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →