Assuming I have 4 ports that may be on and off, and are represented by the LSBs of a uint8_t mask, meaning 0b00001001 means ports 0, 3 are on.
Given a number between 0-3 (which represents the randomization, comes from mod4, or more specifically bitwise &), I’d like to choose a random port out of this mask based on the number received in a uniformly distributed manner, and with no if-statements/switch cases for the sake of efficiency – only bit twiddling/operations such that:
If __builtin_popcount(mask) = 1: The index of the only 1 will always be returned (__builtin_ctz(mask)).
If __builtin_popcount(mask) = 2: The numbers 0,1 would yield in the first port’s return, and 2,3 would result in the second one. So in the above example given 0 and 1, I’d want port 0 to be returned, and for 2 and 3 I’d want port 3.
If __builtin_popcount(mask) = 3: This is the main issue because we do not want to map two numbers to the same port always, because that would not be uniformly distributed anymore, assuming the number is random. I was thinking that 3 of the numbers could be mapped to 3 ports, and the 4th number would alternate based on some global/accessible variable in the function between all other 3 ports every call.
If __builtin_popcount(mask) = 4: The only way this happens is if the mask is 0b00001111 so then each number would simply map to its own port.
Implementing the above is not an issue with if-statements/switch cases, but that defeats the purpose as I must have this done in the most efficient way possible. Even modulo is not allowed as it’s too costly. So somehow I’d love help/direction to see how all this can be done using bitwise operations and that all those 4 things happen in it.
Thank you!
Everything I tried resulted in me having to use an if-statement at least somewhere and I couldn’t think of a way to generalize all the requirements into bit operations, which could very well be due to the lack of experience I have with bitwise twiddling.
I thought of tricks of having 1UL << (number – 1) and &’ing that and then finding the trailing zeros with __builtin_ctz but again it becomes an issue when I want all 4 to work. Perhaps a few commands like that could work.