The "mod" of a negative number is another negative number. If you
cross the International Date Line from right to left, the tile number
will be negative.
Try:
ll.x ^= (ll.x>>z<<z);
to clip off the overflow bits. It will always produce a positive tile
number.