I need to construct a functor that iterates over the linear representation of a sub-lattice of size \$d_x,d_y,d_z,d_q\$ embedded in a lattice of size \$n_x,n_y,n_z,n_q\$. The sub-lattice corner is shifted by \$(l_x,l_y,l_z,l_q)\$. Since the functor can be called million of times, the goal is to produce the most efficient code with the least integer pressure on the CPU.
That is, given ten 16-bit unsigned integers \$d_x,d_y,d_z, n_x,n_y,n_z, l_x,l_y,l_z,l_q\$ with \$d_x\leq n_x\$, \$d_y\leq n_y\$, and \$d_z\leq n_z\$, construct the most efficient function that takes a 64-bit unsigned integer \$0\leq i\lt d_x*d_y*d_z\$ and returns a 64-bit unsigned integer \$j\$ such that if
$$i=i_q (d_z d_y d_x) + i_z (d_y d_x) + i_y d_x + i_x$$
$$j=(i_q+l_q)(n_z n_y n_x) + (i_z+l_z)(n_y n_x) + (i_y+l_y)n_x + (i_x+l_x)$$
Winner solution is the one that achieves the stated goal using the least amount of cycles. Solution preferred in x86_64 assembly (pseudo-code is ok). It is ok to use any instruction that belongs to the instruction set available on Intel/AMD cpus of latest generation (SSE4,SSE3,...).