## Thursday, 29 November 2012

### Tesseral arithmetic - useful snippets

This post doesn't introduce anything new, and is, in my opinion, boring. Feel free to skip.

My previous post didn't have too many useful snippets in it (mainly useful techniques to make your own snippets), and I thought I could improve on that. This post is not a good read in isolation - it's probably a good idea to read my previous post first, if you haven't already.

Tesseral addition (see previous post) was nice, but very often you only need to increment/decrement one dimension of a coordinate (for example when iterating over a portion of a Z-ordered grid in reading order), equivalent to adding/subtracting `(1, 0)` or `(0, 1)` to/from a coordinate. Since only one part of the coordinate changes, only about half as much code is necessary. Also, since the thing being added to the coordinate is a constant, one of the masking operations can be merged with it.
```static uint IncX(uint z)
{
uint xsum = (z | 0xAAAAAAAA) + 1;
return (xsum & 0x55555555) | (z & 0xAAAAAAAA);
}

static uint IncY(uint z)
{
uint ysum = (z | 0x55555555) + 2;
return (ysum & 0xAAAAAAAA) | (z & 0x55555555);
}

static uint DecX(uint z)
{
uint xsum = (z & 0x55555555) - 1;
return (xsum & 0x55555555) | (z & 0xAAAAAAAA);
}

static uint DecY(uint z)
{
uint ysum = (z & 0xAAAAAAAA) - 2;
return (ysum & 0xAAAAAAAA) | (z & 0x55555555);
}```

My previous post only had `TesseralMin`, not the corresponding `TesseralMax`, so here you go:
```public static uint TesseralMax(uint z, uint w)
{
uint xdiff = (z & 0x55555555) - (w & 0x55555555);
uint ydiff = (z >> 1 & 0x55555555) - (w >> 1 & 0x55555555);
uint maskx = (uint)((int)xdiff >> 31);
uint masky = (uint)((int)ydiff >> 31);
return new T((xmin & 0x55555555) | (ymin & 0xAAAAAAAA));
}```
Note that the only difference is that the mask and the complemented mask have switched places.

This `TesseralMax` and the `TesseralMin` from the previous post can be combined with the increments and decrements (and with full tesseral addition, but that's less frequently useful) to form saturating increments and decrements, useful for sampling around a position on a Z-ordered grid without getting out of bounds.
```static uint IncXSat(uint z, uint xmax)
{
uint xsum = ((z | 0xAAAAAAAA) + 1) & 0x55555555;
uint xdiff = xsum - xmax;
uint maskx = (uint)((int)xdiff << 1 >> 31);
return xsat | (z & 0xAAAAAAAA);
}

static uint IncYSat(uint z, uint ymax)
{
uint ysum = ((z | 0x55555555) + 2) & 0xAAAAAAAA;
uint ydiff = ysum - ymax;
uint masky = (uint)((int)ydiff >> 31);
return ysat | (z & 0x55555555);
}

static uint DecXSat(uint z, uint xmin)
{
uint xsum = ((z & 0x55555555) - 1) & 0x55555555;
uint xdiff = xsum - xmin;
uint maskx = (uint)((int)xdiff << 1 >> 31);
return xsat | (z & 0xAAAAAAAA);
}

static uint DecYSat(uint z, uint ymin)
{
uint ysum = ((z & 0xAAAAAAAA) - 2) & 0xAAAAAAAA;
uint ydiff = ysum - ymin;
uint masky = (uint)((int)ydiff >> 31);
Merging them this way is nice, because only "half" of a `TesseralMin` or `TesseralMax` is necessary that way. On the other hand, they do have the overflow problem again, though that usually won't be a problem.