beejjorgensen / bgc

Beej's Guide to C Programming source
Other
467 stars 88 forks source link

Does atomic mulitplication exist? #134

Closed g-berthiaume closed 1 year ago

g-berthiaume commented 1 year ago

Question for the atomic chapter:

In his 2017 cppcon talk, Fedor Pikus mentionned that there was no atomic multiplier in C++ (a *= b won't compile). This seems to be in conflict with what is mentionned in the atomic chapter.

What do you think? Am I mistaken? Is there a difference between C and C++ ?

Thanks for your work.

beejjorgensen commented 1 year ago

I could see why it wouldn't have it (since a 64-bit mult would have a 128-bit result), but the C spec doesn't seem to have a restriction against it.

In the section on compound assignment, it says:

For the other operators [including *=], the left operand shall have atomic, qualified, or unqualified arithmetic type, and (considering the type the left operand would have after lvalue conversion) each operand shall have arithmetic type consistent with those allowed by the corresponding binary operator.

So a difference between languages, perhaps...?

g-berthiaume commented 1 year ago

After testing, this seems to be the case ! https://godbolt.org/z/fsoMPWacG

Thanks for your answer.

beejjorgensen commented 1 year ago

Thanks for taking the time to test it out!