Quoting Tim Northover <t.p.northover at gmail.com>:> On 24 April 2015 at 13:53, Tyler Denniston <tyler at csail.mit.edu> wrote: >> I'm wondering how I can create an atomic load and add instruction for >> floating point values. If I use IRBuilder::CreateAtomicRMW() I get the >> error message: "atomicrmw operand must have integer type". > > I don't think x86 allows you to use a lock prefix on floating point > instructions, just basic arithmetic and cmpxchg. You'll probably need > to put a loop around cmpxchg instructions to emulate it. (and might > well need to cast the pointers to integer type for the cmpxchg, I know > we talked about allowing floats there but I'm not sure it's happened > yet).Hmm, ok. Can you briefly expand on how I should emulate this behavior? I don't understand what you mean by adding a loop around cmpxchg. Thanks, Tyler
> Hmm, ok. Can you briefly expand on how I should emulate this behavior? I > don't understand what you mean by adding a loop around cmpxchg.Probably easiest to write down in C: #include <stdatomic.h> void foo(_Atomic(float) *addr, float increment) { float oldval = *addr, newval; do { newval = oldval + increment; } while (__c11_atomic_compare_exchange_weak( addr, &oldval, newval, memory_order_seq_cst, memory_order_relaxed)); } Basically you keep trying to just swap in the new value until you've managed to do it atomically (or possibly with an intervening ABA change, but that doesn't matter usually). Tim.
Got it. Thanks! Quoting Tim Northover <t.p.northover at gmail.com>:>> Hmm, ok. Can you briefly expand on how I should emulate this behavior? I >> don't understand what you mean by adding a loop around cmpxchg. > > Probably easiest to write down in C: > > #include <stdatomic.h> > void foo(_Atomic(float) *addr, float increment) { > float oldval = *addr, newval; > do { > newval = oldval + increment; > } while (__c11_atomic_compare_exchange_weak( > addr, &oldval, newval, memory_order_seq_cst, memory_order_relaxed)); > } > > Basically you keep trying to just swap in the new value until you've > managed to do it atomically (or possibly with an intervening ABA > change, but that doesn't matter usually). > > Tim.
> } while (__c11_atomic_compare_exchange_weak( > addr, &oldval, newval, memory_order_seq_cst, memory_order_relaxed));Actually, I think this condition is inverted. Should be "while (!_c11...". Sorry about that. Tim.
Apparently Analagous Threads
- [LLVMdev] Floating point atomic load and add
- [LLVMdev] [RFC] Add second "failure" AtomicOrdering to cmpxchg instruction
- [LLVMdev] Proposal: "load linked" and "store conditional" atomic instructions
- [LLVMdev] Proposal: "load linked" and "store conditional" atomic instructions
- Server settings for BackgrounDRB?