Data Lab for the Csapp experiment

Source: Internet
Author: User

0. Guide to Getting Started

There are 12 functions that need to be replenished, all of which require only modification of the bits.c file, and three ways to test it: btest, DLC, and BDD checker.

Some tips:

Declare all variables at the beginning of the function
} should be in the first column
Note the precedence of the operation symbols, using parentheses to ensure that the order is correct
Attention!, 0, TMin, etc.

The task guide is still relatively clear, there are some of the following explanations:

The range of integers is 0 to 255 (0xFF) and is not allowed for larger
Only parameters and local variables can be included
Unary operator! ~
Binary Operators & | + << >>

The operations that are not allowed are:

Use any conditional control statement
Defining and Using Macros
To define other functions
Calling functions
Use a different operator
Using Type conversions
Use a type other than int (for integral type)
Use a type other than int, unsigned (for floating-point numbers)

can be considered as machine:

Use 2 ' s complent,32 bit
Perform arithmetic right shift
Moving more digits than word length can cause problems.

Other things to be aware of are:

Use DLC (Data Lab checker) to detect the legality of the Code (there is no use of the symbolic syntax, etc.)
Each function has the upper value of the operand, note = does not count
Use Btest to test the correctness of the results
Use BDD Checker to test your functions formally

Show method showing the 32-bit int integer
void show(int x){    /*    Show 32 Bits Integer By Binary    */    for (int032; ++i)    {        if040)            " ";        311);        1;    }    cout << endl;}

This method is used to verify that the function is written correctly and the convenience of debugging

1.thirdBits

Topic requirements: Return word with every third bit (starting from the LSB) set to 1
allows operation:! ~ & ^ | + << >>
operand Limit: 8
Score: 1

int thirdBits(){    /*    Desired output: 0100 1001 0010 0100 1001 0010 0100 1001     step 1:         0000 0000 0000 0000 0000 0000 0100 1001  0x49    step 2:         0000 0000 0000 0000 1001 0010 0100 1001    step 3:         0000 0000 1001 0100 1001 0010 0100 1001    step 4:         0100 1001 0010 0100 1001 0010 0100 1001     */    int0x49;    int9);    int c = a + b;    return18) + c;}
2.isTmin

Title: Returns 1 if X is the minimum, and the complement number, and 0 otherwise
Allow Operation:! ~ & ^ | +
Operand limit: 10
Score: 1

This problem is used to test the int_min of the problem with the int_min+int_min overflow 0, but also note that 0+0 is also 0

int isTmin(int x){    /*    Return 1 if x is the minimum, two’s complement number, and 0 otherwise.    */    return !(x + x) & !!(x);}
3.isNotEqual

Title Requirements: return 0 if x = = y, and 1 otherwise
Example: Isnotequal (5,5) = 0, isnotequal (4,5) = 1
Allow Operation:! ~ & ^ | + << >>
Operand limit: 6
Score: 2

The problem with an XOR can, the difference is not 0, the same 0, then used!! Tips for converting an int value to a bool value

int isNotEqual(intint y){    /*    Return 0 if x == y, and 1 otherwise    */    return !!(x^y);}
4.anyOddBit

Title Requirement: Return 1 if any odd-numbered bit in Word set to 1
Example: Anyoddbit (0x5) = 0, anyoddbit (0x7) = 1
Allow Operation:! ~ & ^ | + << >>
Operand limit: 12
Score: 2

The idea of the problem is to declare a variable with a value of 0XAA and then extend it to 32 bits, and finally do and operate with X to get the answer

int anyOddBit(int x){    /*    Return 1 if any odd-numbered bit in word set to 1    1010 1010    */    int0xAA;    int8) + a;    int8) + a;    int8) + a;    return !!(x&d);}
5.negate

Title Requirement: Return-x
For example: Negate (1) =-1.
Allow Operation:! ~ & ^ | + << >>
Operand limit: 5
Score: 2

The problem using the complement of the reverse after adding a can meet the requirements, but at first did not expect to take the counter-operation but to create a 0xffffffff this number to operate

int negate(int x){    /*    Return negate     eg: if x==1 return -1    Or return ~x+1    */    int mask = (1 ^ (13131;    return1;}
6.conditional

Topic Requirements: Same as x? Y:z
Example: Conditional (2,4,5) = 4
Allow Operation:! ~ & ^ | + << >>
Operand limit: 16
Score: 3

The title is three mesh operator

int Conditional(intintint z){    /*    x?y:z    Better : int mask= ~!x+1;    */    int31;    31;    return (mask&y) | ((~mask)&z);}
7.subOK

Title Requirements: Determine if can compute X-y without overflow
For example:
Subok (0x80000000,0x80000000) = 1
Subok (0x80000000,0x70000000) = 0
Allow Operation:! ~ & ^ | + << >>
Operand limit: 20
Score: 3

This question to determine whether the subtraction overflow, subtraction is overflow, the first depends on the two number of symbols, we can find

Subtract from the same number is not overflow, and the subtraction of the overflow when the result of the symbol will be the same as the symbol of the meiosis, so there is the following code

int subOK(intint y){    /*    Determine if can compute x-y without overflow    */    int311;    int311;    int1;    int311;    return !(a^b) | !(a^c);}
8.isGreater

Title Requirements: If x > y then return 1, else return 0
Example: Isgreater (4,5) = 0, isgreater (5,4) = 1
Allow Operation:! ~ & ^ | + << >>
Operand limit: 24
Score: 3

The problem compares the size of two numbers, first we make clear positive numbers are larger than negative numbers, and negative numbers are smaller than the positive number

Using this property we can first judge by the nature, and then use two to subtract to a positive number when returning 1 properties write an expression

What we need to note is that the place does not need to be considered for overflow, because overflow is the act of subtracting the number of different

int isGreater(intint y){    /*    if x > y then return 1, else return 0      */    int a = (x>>311, b = (y>>311;    int1;    int c = (res>>311;    return ((!a&b) | !c)&(!a | b);}
9.bitParity

Title Requirements: Returns 1 if X contains an odd number of 0 ' s
Example: bitparity (5) = 0, bitparity (7) = 1
Allow Operation:! ~ & ^ | + << >>
Operand limit: 20
Score: 4

The question asked us to count out whether an int shaped binary expression has an odd number of 0, and we can use XOR or not change the parity to write

int bitParity(int x){    /*    returns 1 if x contains an odd number of 0’s    bitParity(5) = 0, bitParity(7) = 1    */    16) ^ x;    8) ^ x;    4) ^ x;    2) ^ x;    1) ^ x;    return1);}
10.howManyBits

Title Requirement: Return the minimum number of bits required to represent x in and the complement
For example:
Howmanybits (12) = 5
Howmanybits (298) = 10
Howmanybits (-5) = 4
Howmanybits (0) = 1
Howmanybits (-1) = 1
Howmanybits (0x80000000) = 32
Allow Operation:! ~ & ^ | + << >>
Operand limit: 90
Score: 4

The question requires us to return to the number with a minimum of a few to indicate that we can use the dichotomy to determine how many bits

And then we have to differentiate between special cases 0 and 1.

It is worth mentioning that we have to reverse the negative, but do not have to add one, because the complement indicates that the negative range is larger than the positive number

and the smallest negative number is just the biggest positive after the reverse.

intHowmanybits (intx) {/*return the minimum number of bits required to represent x in the other ' s complement    */    intA = ((!x) << to) >> to;//When x is 0 o'clock, a is all 1, otherwise all 0    intB = ((!~x) << to) >> to;//When x is-1, B is all 1, otherwise all 0    intBit_16, Bit_8, Bit_4, bit_2, bit_1;intSumintop = x ^ (x >> to); Bit_16 = (!! (Op >> -)) <<4;    Op = op >> bit_16; Bit_8 = (!! (Op >>8)) <<3;    Op = op >> bit_8; Bit_4 = (!! (Op >>4)) <<2;    Op = op >> bit_4; Bit_2 = (!! (Op >>2)) <<1;    Op = op >> bit_2; Bit_1 = (!! (Op >>1));    Op = op >> bit_1; sum =2+ bit_16 + bit_8 + bit_4 + bit_2 + bit_1;return(A &1) | (B &1) | (~a & ~b&sum);}
11.float_i2f

Title requirement: Return bit-level equivalent of expression (float) x. Result is returned as unsigned int, but it's to be interpreted As the bit-level representation of a single-precision floating point values.
Allowed actions: any integer/unsigned operations incl. | |, &&. Also if, while
Operand limit: 30
Score: 4

This problem can be written as long as you know the expression of floating point

unsignedFLOAT_I2F (intx) {intSign = (x >> to) &1;intExponent, fraction, fraction_mask =0x7fffff;if(x = =0)return 0;Else if(x = =0x8000000) exponent =158;Else{if(Sign = =1) x = ~x +1;intBit_16, Bit_8, Bit_4, bit_2, bit_1;intSumintop = x ^ (x >> to); Bit_16 = (!! (Op >> -)) <<4;        Op = op >> bit_16; Bit_8 = (!! (Op >>8)) <<3;        Op = op >> bit_8; Bit_4 = (!! (Op >>4)) <<2;        Op = op >> bit_4; Bit_2 = (!! (Op >>2)) <<1;        Op = op >> bit_2; Bit_1 = (!! (Op >>1));        Op = op >> bit_1; sum =1+ bit_16 + bit_8 + bit_4 + bit_2 + bit_1;if(Sum > -) fraction = x >> (Sum- -);Else if(Sum < -) fraction = x << ( --sum);        Fraction &= Fraction_mask; exponent =127+ Sum-1; } show (fraction);unsignedUX = (sign << to) + (exponent << at) + fraction;returnUX;}
12.float_f2i

Title requirements: Return Bit-level equivalent of expression (int) F for floating point argument F. argument is passed as unsigned int, But it was to being interpreted as the bit-level representation of a single-precision floating point value. Anything out of range (including NaN and infinity) should return 0X80000000U.
Allowed actions: any integer/unsigned operations incl. | |, &&. Also if, while
Operand limit: 30
Score: 4

The problem is the inverse process of 11 questions, it is worth noting that we have to cut the number of parts

intFloat_f2i (floatf) {int*PF = (int*) &f;intx = *PF; Show (x);intSign = x >> to;intX_abs = x &0x7fffffff;intexponent = X_abs >> at;if(Exponent <127)return 0; Show (exponent);intFraction_mask =0x7fffff;intfraction = x&fraction_mask; Show (fraction);inti =0; while(fraction) {if(Fraction &1) Break; Fraction >>=1;    ++i;    } cout << i << Endl;    Show (fraction); Fraction |= (1<< ( at-i)); Show (fraction);intLen = exponent-127- ( at-i); cout << len << Endl;intResif(Len >0) res = fraction << len;Elseres = fraction >>-len;    cout << exponent << Endl; cout << Res << Endl;if(Sign = =-1) res = ~res +1;returnRes;}
Conclusion

The first data experiment was finished, and through this experiment I deepened my understanding of the data, especially the design of various masks, with a deeper understanding

Data Lab for the Csapp experiment

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.