Hello Steve,
With both CW and CCW rotation, there are now four conditions that need to be differentiated -
- CW rotation, no wrap (n2 >= n1
- CW rotation, with wrap (n1 > n2)
- CCW rotation, no wrap (n1 >= n2)
- CCW rotation, with wrap (n2 > n1)
So a further test is required to differentiate between 1 and 4, and between 2 and 3. I will suggest to compare the difference with MOD/2, as follows -
#define MOD 8192
#define HALFMOD MOD/2
// n1, n2 must fall within range 0 to MOD - 1
word get_diff( word n1, word n2)
{
word diff;
if (n2 >= n1) {
diff = n2 - n1;
if (diff < HALFMOD) return diff; // CW rotation, no wrap
else return (MOD - diff); // CCW rotation, with wrap
}
else { // n2 < n1
diff = n1 - n2;
if (diff < HALFMOD) return diff; // CCW rotation, no wrap
else return (MOD - diff); // CW rotation, with wrap
}
}
Or perhaps the following simplified variation -
word get_diff( word n1, word n2)
{
word diff;
if (n2 >= n1)
diff = n2 - n1;
else
diff = n1 - n2;
if (diff < HALFMOD) return diff;
else return (MOD - diff);
}
Regards,
Mac