Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix atomic fetch operations #101

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Fix atomic fetch operations #101

wants to merge 1 commit into from

Conversation

chjj
Copy link

@chjj chjj commented Aug 6, 2022

The atomic_fetch ops are supposed to return the old value of the object. chibicc is currently returning the new values.

stdatomic.h:

#define atomic_fetch_add(obj, val) (*(obj) += (val))
#define atomic_fetch_sub(obj, val) (*(obj) -= (val))
#define atomic_fetch_or(obj, val) (*(obj) |= (val))
#define atomic_fetch_xor(obj, val) (*(obj) ^= (val))
#define atomic_fetch_and(obj, val) (*(obj) &= (val))

I was originally thinking about how to fix this in a simple way. Three of them (add/sub/xor) are fixable with macros. Something like:

#define atomic_fetch_add(obj, val) ({ \
  typeof(val) _val = (val);           \
  ((*(obj) += _val) - _val);          \
})

#define atomic_fetch_sub(obj, val) ({ \
  typeof(val) _val = (val);           \
  ((*(obj) -= _val) + _val);          \
})

#define atomic_fetch_xor(obj, val) ({ \
  typeof(val) _val = (val);           \
  ((*(obj) ^= _val) ^ _val);          \
})

However, atomic_fetch_and and atomic_fetch_or are not fixable via macro as they are not reversible.

This PR adds a single new builtin (__builtin_atomic_fetch_op) which reuses the existing atomic operation code.

fuhsnn added a commit to fuhsnn/slimcc that referenced this pull request Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant