Power-of-2 Boundaries
Prev: Basics
Next: Arithmetic Bounds
Sections
3-1Rounding Up/Down to a Multiple of a Known Power of 23-2Rounding Up/Down to the Next Power of 23-3Detecting a Power-of-2 Boundary Crossing
Problems
-
Show how to round an unsigned integer to the nearest multiple of 8, with the halfway case (a) rounding up, (b) rounding down, and (c) rounding up or down, whichever makes the next bit to the left a zero, unbiased rounding.
-
Show how to round an unsigned integer to the nearest multiple of 10, with the halfway case (a) rounding up, (b) rounding down, and (c) rounding up or down, whichever results in an even multiple of 10. Feel free to use division, remaindering, and multiplication instructions, and do not be concerned about values very close to the largest unsigned integer.
-
Code a function in C that does an unaligned load. The function is given an address , and it loads the four bytes from addresses through into a 32-bit GPR, as if those four bytes contained an integer. Parameter addresses the low-order byte, that is, the machine is little-endian. The function should be branch free, it should execute at most two load instructions and, if is fullword aligned, it must not attempt to load from address , because that may be in a read-protected block.