If it is "and" with constant, for some special pattern, we could use the rotate and instructions instead of the "andi." which requires us to generate the mask using several instructions. i.e. If the mask looks like this. That is, it has two groups of ones, and either the number of leading zeroes or trailing zeroes is zero.
// MB MB2 ME2 ME // +----------------------+ // |0001111100000011111111| // +----------------------+ // 0 32 64
We could optimize it as follows:
- Rotate left MB2 + 1 bits and then, clear the bits (MB2, ME2)
MB MB2 ME2 ME MB MB2 ME2 ME +----------------------+ +----------------------+ |1111111111111111111111| -> |0000001111111111111111+ +----------------------+ +----------------------+ 0 32 64 0 32 64
- Rotate back and then, clear the bits [0, MB)
MB MB2 ME2 ME MB MB2 ME2 ME +----------------------+ +----------------------+ |0000001111111111111111| -> |0001111100000011111111+ +----------------------+ +----------------------+ 0 32 64 0 32 64