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

Count poll and run with potentially one less branch #1525

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

Conversation

Andersama
Copy link

Note: The same condition can be expressed with a single statement.

n += (~n > 0);
or
n += (~n != 0);

However this introduces an additional ~ operation for every loop.

An equivalent but more convoluted loop counts down:

  size_t inverted_n = (std::numeric_limits<size_t>::max)();
  while (do_one(INFINITE, this_thread, ec))
    inverted_n -= (inverted_n != 0); //count "down" backwards from the max
  return ~inverted_n; //so we can flip the result without doing ~'s at each step

Ultimately, each method should be checking the non-zero flag, so outside additional few nanoseconds worth of instructions they should be equivalent, main point is to remove an unnecessary branch. I've only modified the related the windows file as an example, and I'm not sure where the other implementation exists.

Note: The same condition can be expressed by

n += (~n > 0);
or
n += (~n != 0);
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