The two mapping algorithms work in similar ways. The entire square is viewed as composed of 4 regions, arranged 2 by 2. Each region is composed of 4 smaller regions, and so on, for a number of levels. At level "s", each region is "s" by "s" cells. There is a single FOR loop that iterates through levels. On each iteration, an amount is added to "d" or to "x" and "y", determined by which of the 4 regions it is in at the current level. The current region out of the 4 is ("rx","ry"), where "rx" and "ry" are each 0 or 1. So it consumes 2 input bits, (either 2 from "d" or 1 each from "x" and "y"), and generates two output bits. It also calls the rotation function so that ("x","y") will be appropriate for the next level, on the next iteration. For xy2d, it starts at the top level of the entire square, and works its way down to the lowest level of individual cells. For d2xy, it starts at the bottom with cells, and works up to include the entire square.