FWIW, Zack, I see in your code that you were looking for a constant-time variant of Downey's algorithm.  I threw together a sketch of how you might do it in https://gist.github.com/nmathewson/8869955 .  Not terribly efficient, and compiler-dependent, but possibly interesting.