I was wonder if there is a simpler (single) way to calculate the remaining space in a circular buffer than this?
int remaining = (end > start)
? end-start
: bufferSize - start + end;
If you're worried about poorly-predicted conditionals slowing down your CPU's pipeline, you could use this:
int remaining = (end - start) + (-((int) (end <= start)) & bufferSize);
But that's likely to be premature optimisation (unless you have really identified this as a hotspot). Stick with your current technique, which is much more readable.
Hmmm....
int remaining = (end - start + bufferSize) % bufferSize;
13 tokens, do I win?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With