0

struct node* address; //format <addr,flagBit1,flagBit2>

I want to use BTS to atomically set the flagBit1 bit.

EDIT

I want to blindly set this bit without caring about if it was previously set or not

I can use assembly code to get this done as suggested in Using bts assembly instruction with gcc compiler

But is there an intrinsic I can use which is portable across different architectures?

For instance, gcc Atomic Builtins has __sync_fetch_and_or. Does this have the same property as BTS?

Johan
  • 74,508
  • 24
  • 191
  • 319
arunmoezhi
  • 3,082
  • 6
  • 35
  • 54
  • Yes, but see this [gcc bug report](http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49244). – Jester Apr 09 '14 at 21:46
  • @Jester: Thanks for the reference. I'm not interested in the previous value of `flagBit`. I just want to blindly set it without caring if it was previously set or not – arunmoezhi Apr 09 '14 at 21:50
  • If you don't need atomicity, the comments in gcc seem to say that using bts is likely to slow your code down... – Marc Glisse Apr 09 '14 at 21:53
  • I need atomicity. Else I would rather use bitwise operation. Updated the question accordingly – arunmoezhi Apr 09 '14 at 21:55
  • Can you use C11? Support is pretty thin for now, but it should provide a suitable atomic operation. Otherwise, the __sync builtins should work (though they won't use bts currently). Note that they were replaced: http://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html (though the __sync versions have the advantage that several compilers implement them). – Marc Glisse Apr 09 '14 at 22:04
  • @MarcGlisse: Are you suggesting me to use `__atomic_test_and_set`? – arunmoezhi Apr 09 '14 at 22:08
  • More like __atomic_or_fetch or __atomic_fetch_or, no? – Marc Glisse Apr 09 '14 at 22:11
  • What is the difference between `atomic_or_fetch` and `atomic_fetch_or` – arunmoezhi Apr 09 '14 at 22:15
  • The `atomic_or_fetch` does the `or` first and returns the result, the other returns the original value. – Jester Apr 09 '14 at 22:17
  • The final result of both should be the same, right? If yes, then since I don't care what was stored previously I can use both safely. – arunmoezhi Apr 09 '14 at 22:18

1 Answers1

3

For a blind setting of a bit, sync_fetch_and_or or sync_or_and_fetch seem to be both equally good, with the result discarded the compiler knows to optimize it out. On x86 gcc won't use bts, instead it will simply do a lock or which should be fine.

Jester
  • 56,577
  • 4
  • 81
  • 125