## Monday, February 2, 2015

### Bit manipulation: finding a range of values

We previously looked at finding zero values in an array. A similar problem is to find a value larger than some target. The vanilla code for this is pretty simple:

```#include "timing.h"

int range(char * array, unsigned int length, unsigned char target)
{
for (unsigned int i=0; i<length; i++)
{
if (array[i]>target) { return i; }
}
return -1;
}
```

It's possible to recode this to use bit operations, but there is a small complication. We need two versions of the routine depending on whether the target value is >127 or not. Let's start with the target greater than 127. There are two rules to finding bytes greater than this target:

• The upper bit is set in the target value, this means that the upper bit must also be set in the bytes we examine. So we can AND the input value with 0x80, and this must be 0x80.
• We want a bit more precision than testing the upper bit. We need to know that the value is greater than the target value. So if we clear the upper bit we get a number between 0 and 127. This is equivalent to subtracting 128 off all the bytes that have a value greater than 127. So instead of doing a comparison of is 132 greater than 192 we can do an equivalent check of is (132-128) greater than (192-128), or is 4 greater than 64? However, we want bytes where this is true to end up with their upper bit set. So we can do an ADD operation where we add sufficient to each byte to cause the result to be greater than 128 for the bytes with a value greater than the target. The operation for this is `( byte & 0x7f ) + (255-target)`.

The second condition is hard to understand, so consider an example where we are searching for values greater than 192. We have an input of 132. So the first of the two conditions produces `132 & 0x80 = 0x80`. For the second condition we want to do `(132 & 0x7f) + (255-192) = 4+63 = 68` so the second condition does not produce a value with the upper bit set. Trying again with an input of 193 we get `65 + 63 = 128` so the upper bit is set, and we get a result of 0x80 indicating that the byte is selected.

The full operation is `(byte & ( (byte & 0x7f) + (255 - target) ) & 0x80)`.

If the target value is less than 128 we perform a similar set of operations. In this case if the upper bit is set then the byte is automatically greater than the target value. If the upper bit is not set we have to add sufficient on to cause the upper bit to be set by any value that meets the criteria.

The operation looks like `(byte | ( (byte & 0x7f) + (127 - target) ) & 0x80)`.

Putting all this together we get the following code:

```int range2( unsigned char* array, unsigned int length, unsigned char target )
{
unsigned int i = 0;
// Handle misalignment
while ( (length > 0) && ( (unsigned long long) & array[i] & 7) )
{
if ( array[i] > target ) { return i; }
i++;
length--;
}
// Optimised code
unsigned long long * p = (unsigned long long*) &array[i];
if (target < 128)
{
unsigned long long v8 = 127 - target;
v8 = v8 | (v8 << 8);
v8 = v8 | (v8 << 16);
v8 = v8 | (v8 << 32);

while (length > 8)
{
unsigned long long v = *p;
unsigned long long u;
u = v & 0x8080808080808080; // upper bit
v = v & 0x7f7f7f7f7f7f7f7f; // lower bits
v = v + v8;
unsigned long long r = (v | u) & 0x8080808080808080;
if (r) { break; }
length-=8;
p++;
i+=8;
}
}
else
{
unsigned long long v8 = 255 - target;
v8 = v8 | (v8 << 8);
v8 = v8 | (v8 << 16);
v8 = v8 | (v8 << 32);
while (length > 8)
{
unsigned long long v = *p;
unsigned long long u;
u = v & 0x8080808080808080; // upper bit
v = v & 0x7f7f7f7f7f7f7f7f; // lower bits
v = v + v8;
unsigned long long r = v & u;
if (r) { break; }
length-=8;
p++;
i+=8;
}
}

// Handle trailing values
while (length > 0)
{
if (array[i] > target) { return i; }
i++;
length--;
}
return -1;
}

```

The resulting code runs about 4x faster than the original version.