Verify the correctness of tmult_ OK

Source: Internet
Author: User

Csapp page124. practice problem 2.35

/* Determine whether arguments can be multiplied without overflow */int tmult_ok(int x, int y){    int p = x*y;    /* Either x is zero, or dividing p by x gives y */    return !x || p/x == y;}

The function of tmult_ OK is to check whether the multiplication of the two complement numbers overflows. The logic is, either X is zero, or the product P is divided by X is equal to the other multiplier y, which means no overflow occurs.

This requires a mathematical proof of the correctness of the function. First, it proves the Correctness When x = 0. Then consider that the number of W digits (W is not equal to 0) include y, P, and Q, where p is X, and y executes the result of the 2nd complement multiplication, Q is the value of P/X.

The book provides three clues to prove the correctness of the function.

  1. Proof: the result of integer multiplication of X and Y can be written in the form of x * Y = P + T * 2 ^ W. if and only if T! = 0, P overflows.
  2. Proof: P can be written as P = x * q + R, where | r | <| x |.
  3. Proof: q = y only when r = T = 0.

The answer is as follows:

1. Because X and Y are all numbers represented by W-bit 2's complement Code, their product must be expressed by a maximum of 2 W bits, because:

-2 ^ (W-1) <= X, Y <= 2 ^ (W-1)-1 Note: This is the value range of the second complement code
So x * y must be greater than or equal to-2 ^ (W-1) * (2 ^ (W-1)-1) is equal to-2 ^ (2 * w-1) + 2 ^ (W-1) Note: Tmax * tmin
Less than or equal to (-2 ^ (W-1) ^ 2 is equal to 2 ^ (2 * w-2) Note: tmin * tmin, it can be proved that Tmax ^ 2 is not as big as tmin ^ 2

When we see that the power of 2 is 2 w, it must be 2 W bits. Therefore, x * y should be represented by 2 W bits (but 2 W bits overflow, no matter for the time being). Now, we set u to the unsigned form with the lower W bits of the product, V is equal to the second complement of W-bit of the product.
Then, according to formula 2.3, that is, the conversion formula from binary to binary complement code, we can get the formula X * Y = V * 2 ^ W + U, which is incredible at first glance, how can we split a single-bit vector into two halves? The first w-bit is calculated by Two-complement codes and then multiplied by 2 ^ W, the last W Bit is calculated by the unsigned number, and then how does the two parts add up to the product of X and Y ??
In fact, as long as the binary-to-binary complement formula is carefully pushed down, this conclusion can be proved immediately:

 

 

We also know that u = t2u (p) Here P is the product of the binary complement code, because no matter whether the product result overflows, the product of the unsigned form and the tow's complement form is exactly the same as that of the W Bit, which is proved by the number 2.18.
So according to the formula t2u u = pw-1 * 2 ^ W + P, put U into the previous x * Y = V * 2 ^ W + u here, simplification can be obtained: x * Y = 2 ^ w * (V + pw-1) + P, now we set T = V + pw-1, the formula becomes: x * Y = T * 2 ^ W + P

Here we can see that when T = 0, x * Y is equal to P, and T is not equal to 0, the product overflows. The above 1st items are proved here.

2. no matter whether the value of X * Y has exceeded P or not, P is an integer. If P is an integer, dividing it by a non-zero integer x will inevitably produce a quotient (q) with a remainder (R), P = x * q + R | r | <| x |
Here, we take the absolute value because the symbols of R and X may be different. For example,-7/2 requires-3 remainder-1, |-1 | <| 2 |. 2nd entries are proved here.

3. if q = y is obtained based on P = x * q + R, P = x * Y + R. x * Y = T * 2 ^ W + P, so P = T * 2 ^ W + P + R, after p is eliminated on both sides, T * 2 ^ W =-R is obtained. To make this equation true, there are two conditions: either equal signs and equal signs have equal values, or both sides have zero values.
In the proof of article 2nd, we obtain | r | <| x |, and X is a binary complement, so the maximum absolute value is 2 ^ W. So | r | <2 ^ W, back here, 2 ^ W appears on the left of the equation, and R is smaller than 2 ^ W. It can be seen that the values must be zero if they are not equal. So it must be r = T = 0

According to article 3, we know that T must be 0 if the product is not overflow. According to article 3, when T is 0, P/x = y is represented by a program (! X | P/x = q) the logic is proven to be correct.

This 3 also proves that in the case of x = 0 again, when X is equal to zero, the product zero will certainly not overflow, so this program is correct and reliable. Pass.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.