Rename log2 to lower_log2 and add upper_log2.
Other minor refinements.
Fix edge case bug in upper_log2 and add unit test for it.