Objective:
I was looking at the data conditions in the assembly to send instructions, doing exercises when looking at such a thing about the power of 2 division of the topic. The results are dumbfounded, and the No ... The second chapter looked at the time of the confused, saw a few times also did not see too understand, this time again involved, found to come back to see or easy point. So write this blog, easy to review later.
The following are the problems I have encountered today:
Problem:
Division, in our usual arithmetic operations, the result is always rounded in the direction of 0, but in the computer, rounding is different. In most machines, division is much slower than multiplication and addition, and computers are sensitive to the number of powers of 2, because the instructions and binaries used in the computer are the binary forms, and the computer is the quickest to do so. So, if the divisor is a power of 2 when doing division, then we can do some optimizations to make the operation faster.
Workaround:
Because the left-shift operation is equivalent to multiplying by 2, the right-shift operation is equivalent to dividing by 2. So when the divisor of our division is a power of 2, it is much faster to use a simple shift operation than a common method.
For unsigned numbers, if the divisor is 2 k power, then we will be dividend ah corresponding to do arithmetic right shift operation, the right shift K-bit. The result is the same as the result of our division in peacetime.
For unsigned numbers, suppose X is a binary number with W bits, X ' is a sequence from the W bit to K bit, and X ' is a sequence of 0-k bits. Then X ' =floor (x/2^k), this expression is provable, the process is very easy I will not say more.
For a non-negative number with a signed number of unsigned numbers, divide by 2 by the power of K and the result of moving the dividend to the right K-bit is the same.
But for the number of signed numbers, there will be some errors.
In the decimal column is the computer calculation results after rounding results, the right column is computed by the computer results, we can see that the division is not required to round the time, the result and the right shift K-bit operation is the same, but if there is no division of the need for rounding, we will find out the problem, The shift causes it to round instead of rounding to 0,-771.25 is supposed to be rounded to-771, but he rounded to-772.
In peacetime hand division, our results are rounded to 0, but in the computer is actually rounded down, because the non-negative number is rounded down to 0 rounding so there is no effect, but negative numbers will not be. Because after the shift, negative numbers are added to the left of the highest sign bit instead of 0. Negative numbers should be rounded up in order to match the rule that the hand is rounded to 0 when dividing.
Workaround for negative number division rounding to 0:
To ensure proper rounding, we decided to use a bias value to solve the problem.
For any integer x and y>0, there is ceil (x/y) = Floor ((x+y-1)/y)
Suppose X=ky + R (r>=0 && r < y span>
We can be clear (x+y-1)/y = (ky+ r-1)/y = k + (r + y-1)/y
If x can be divisible by y at this point, i.e. R = 0, then ceil (x/y) = Floor ((x+y-1)/y) = k
otherwise equal to K + 1 then that is, in the calculation of dividend is negative, the divisor is a power division of 2, you can give x plus the divisor-1 (y-1) This offset, it is possible to ensure that the result of division is correct, because the divisor is the power of 2, can be sufficient to move the right to replace the operation, When the divisor is a negative number, you should add 2^k-1 to the offset, you can get the correct results.
So in the computer if the divisor is a power of 2, it will be used to the form of the following expression to calculate the division of the
(x < 0 x k-x >> k
How to achieve divisor in a computer is a power division of 2 "reprinted from Csdn"