In-depth understanding of computer systems (CSAPP) experiment two Datalab-handout

Source: Internet
Author: User

The purpose of the experiment is to fill in the functions of the BITS.C in accordance with the required requirements (such as only limited and specified operators and data types, not the use of control statements, etc.) to implement functions.

At the same time, the DLC file is used to detect whether the functions inside the BITS.C are written as required, and there are no illegal data types. How to use:./DLC bits.c

Once the detection is successful, use Btest to test the functionality of each function correctly. How to use:./btest, if a function is wrong, the wrong data is displayed, along with the correct data.

The complete bits.c is as follows. It is also a reference to the great gods on the Internet.

/* * Cs:app Data Lab * * <please put your name and userid here> * * Bits.c-source file with your solutions to The Lab. * This is the file you'll hand in to your instructor. * * Warning:do not include the <stdio.h> header; It confuses the DLC * compiler. You can still the use of printf for debugging without including * <stdio.h&gt, and although you might get a compiler warning.   In general, * It's not good the practice to ignore compiler warnings, but the this * case it's OK. */#if 0/* * instructions to Students: * * STEP 1:read The following instructions carefully. */you would provide your solution to the Data Lab byediting the collection of functions in this source file. INTEGER CODING rules:replace The "return" statement in each function with one or more lines of C code that implements The function. Your code must conform to the following Style:int funct (arg1, arg2, ...) {/* Brief description of how your implementation works */int var1 = EXPR1;     ... int varm = EXPRM;      Varj = EXPRJ;      ... varn = ExprN;  return EXPRR; Each of the "Expr" is a expression using only the following:1. Integer Constants 0 through 255 (0xFF), inclusive.  You aren't allowed to use big constants such as 0xFFFFFFFF.  2. Function arguments and local variables (no global variables). 3. Unary integer Operations! ~ 4. Binary integer Operations & ^ |  + << >> Some of the problems restrict the set of allowed operators even further. Each of the "Expr" may consist of multiple operators.  You is not a restricted to one operator per line.  You is expressly forbidden to:1. Use any control constructs such as if, does, while, for, switch, etc.  2. Define or use any macros.  3. Define any additional functions in the this file.  4. Call any functions.  5. Use any other operations, such as &&, | |,-, or?: 6. Use any form of casting.  7. Use any data type, other than int. This implies the cannot use arrays, StRucts, or unions.  Assume that your machine:1. Uses 2s complement, 32-bit representations of integers.  2. Performs right shifts arithmetically. 3. Have unpredictable behavior when shifting a integer by more than the word size.  EXAMPLES of acceptable CODING STYLE:/* * Pow2plus1-returns 2^x + 1, where 0 <= x <= */int Pow2plus1 (int  x) {/* Exploit ability of shifts to compute powers of 2 */return (1 << x) + 1; }/* * Pow2plus4-returns 2^x + 4, where 0 <= x <= */int POW2PLUS4 (int x) {/* Exploit ability of Shi     FTS to compute powers of 2 */int result = (1 << x);     result + = 4;  return result; }floating Point CODING rulesfor The problems this require you to implent floating-point operations,the CODING rules is le  SS Strict.  You is allowed to use looping andconditional control. You is allowed to use both ints and unsigneds. You can use arbitrary integer and unsigned constants. Using loops and control statements, using INT and unsignedYou is expressly forbidden to:1. Define or use any macros. Define or use Macro 2. Define any additional functions in the This file defines the additional function 3. Call any functions. Call function 4. Use any form of casting. Use conversion 5.  Use any data type, other than int or unsigned.     This means the cannot use arrays, structs, or unions. Use array, struct, shared 6.  Use any floating point data types, operations, or constants. Use any floating-point number type, operation, normally lit. Notes:1. Use the DLC (Data Lab checker) compiler (described in the handout) to check the legality of your Solutions      . Check legality with DLC 2. Each function have a maximum number of operators (! ~ & ^ | + << >>) that's allowed to use for yo      ur implementation of the function. The max operator count is checked by DLC. Note that ' = ' was not counted;     Many of these as you want without penalty.  Each function has a limited number of operators to use, ' = ' does not count.  3. Use the Btest test harness to check your functions for correctness. 4. Use the BDD Checker to formally verify your functions  5. The maximum number of OPS for each function are given in the header comment for each function. If there is any inconsistencies between the maximum ops in the writeup and in this file, consider this file the Authoritative source./* * STEP 2:modify The following functions according the coding rules. * * IMPORTANT. To AVOID GRADING surprises: * 1. Use the DLC compiler to check for that your solutions conform * to the coding rules. * 2. Use the BDD Checker to formally verify that your solutions produce * the correct answers.  * * #endif/* * Bitand-x&y using only ~ and | * Example:bitand (6, 5) = 4 * Legal Ops: ~ | * Max Ops:8 * rating:1 * Method: Use Morgan's Law */int bitand (int x, int y) {return ~ ((~x) | ( ~y)); // ~~, ~| = &}/* * getbyte-extract byte n from Word X * extract nth byte from Word X * Bytes numbered from 0 (LSB) to 3 (MSB) * Lowest effective Bit is 0, in turn, the most significant bit is 3 * examples:getbyte (0x12345678,1) = 0x56 * Legal OPS:! ~ & ^ | + << >> * MaX Ops:6 * Rating:2 * method: */int getbyte (int x, int n) {int tmp = x >> ((n) << 3);//Shift right to head 0xff123456, arithmetic right shift TMP = tmp & 0xFF; Preserves the most right byte. return tmp;;}  /* * Logicalshift-shift X to the right by N, using a logical shift * logical SHIFT, logic shift * Can assume that 0 <= N <= 31 * Examples:logicalshift (0x87654321,4) = 0x08765432 * Legal OPS:! ~ & ^ |  + << >> * Max ops:20 * rating:3 */int logicalshift (int x, int n) {int tmp = ~ (1 <<);//0x7f FF FF fftmp = ((tmp >> N) << 1) + 1; Because n >= 0, implement TMP >> (n-1) function TMP = TMP & (x >> n);//return TMP;}    /* * Bitcount-returns Count of number of 1 ' in Word * returns 1 in binary number * Examples:bitcount (5) = 2, Bitcount (7) = 3 * Legal OPS:! ~ & ^ | + << >> * Max ops:40 * rating:4 * Reference on-line solution. Using the dichotomy method, the number of x per two digits is calculated first, and the number is stored with the corresponding two teams (1). It then calculates the number of 1 per 4 bits, which is stored with the corresponding 4 bits. In turn. * Finally, the number of 1 in 16 digits, that is, the number of 1 in X is obtained.      */int bitcount (int x) {int count; int tmpMask1 = (0x55) | (0x55&LT;&LT;8); int mask1 = (TMPMASK1) |      (TMPMASK1&LT;&LT;16); int tmpMask2 = (0x33) |      (0X33&LT;&LT;8); int mask2 = (TMPMASK2) |      (TMPMASK2&LT;&LT;16); int tmpMask3 = (0x0f) |      (0X0F&LT;&LT;8); int mask3 = (TMPMASK3) |      (TMPMASK3&LT;&LT;16); int mask4 = (0xff) |      (0XFF&LT;&LT;16); int mask5 = (0xff) |      (0XFF&LT;&LT;8);      Count = (X&mask1) + ((x>>1) &mask1);      Count = (Count&mask2) + ((count>>2) &mask2);      Count = (count + (Count >> 4)) & Mask3;      Count = (count + (Count >> 8)) & Mask4;      Count = (count + (Count >>)) & Mask5;  return count; }/* * Bang-compute!x without using! * 0 for 0 * Examples:bang (3) = 0, bang (0) = 1 * Legal Ops: ~ & ^ | + << >> * Max ops:12 * rating:4 */int Bang (int x) {int tmp = ~x + 1;//tmp =-X;TMP = x | tmp;//Non 0 most The high level must be 1 because positive and negative numbers or TMP = tmp >> 31; Non 0 is 0xff FF FF ff,0 0x (tmp+1);} /* * Tmin-return Minimum-Scomplement integer * int minimum number * Legal OPS:! ~ & ^ | + << >> * Max ops:4 * rating:1 */int tmin (void) {return 1<<31;//easy to understand}/* * Fitsbits-return 1 If X can be represented as an * n-bit, and both ' s complement integer. * If x can be represented as n-bit twos complement form * 1 <= N <= * examples:fitsbits (5,3) = 0, fitsbits ( -4,3) = 1 * Legal OPS:! ~ & ^ |  + << >> * Max ops:15 * rating:2 */int fitsbits (int x, int n) {int shiftnumber= + (~n + 1);//32- N Return! (x^ ((x<<shiftnumber) >>shiftnumber)); /* First move the 32-n bit to the left and move the 32-n bit to the right, that is, the last n digits are retained. The x can be represented as an n-bit integer in the same way as X or if the two are the same! 0 is 1 */}/* * Divpwr2-compute x/(2^n), for 0 <= n <= 30 * calculation X/(2^n) * Round toward Zero * EXAMPLES:DIVPWR2 ( 15,1) = 7, DIVPWR2 ( -33,4) =-2 * Legal OPS:! ~ & ^ |     + << >> * Max ops:15 * rating:2 */int divpwr2 (int x, int n) {//Full 0 or all 1 int signx = x >> 31; int mask = (1 << N) + (~0);//Get 2^n-1 int bias = SIGNX & mask;If x is a positive number, then the bias is 0, i.e. no addition, direct shift//If x is negative, plus offset after shift return (x + bias) >> N;} /* * Negate-return-x * example:negate (1) =-1. * Ask the opposite number * Legal OPS:! ~ & ^ |  + << >> * Max ops:5 * rating:2 */int negate (int x) {return (~x) + 1;//take reverse plus 1}/* * Ispositive-return 1 if x > 0, return 0 otherwise * x > 0 return 1, x <= 0 return 0 * example:ispositive (-1) = 0. * Legal OPS:! ~ & ^ | + << >> * Max ops:8 * rating:3 */int ispositive (int x) {return! (x >> 31) | (!x)); Look at the sign bit to}/* * islessorequal-if x <= y then return 1, else return 0 * example:islessorequal (4,5) = 1. * x-y <= 0 returns 1, > 0 returns 1 * Legal OPS:! ~ & ^ | + << >> * Max ops:24 * rating:3 */int islessorequal (int x, int y) {int singx = (x >> +) & 1;i NT Singy = (y >> x) & 1;//comparison sign bit 1 0 = 1, 0 1 = 0;int sing = (singx ^ singy) & Singx; Guaranteed SINGX and Singy int. = x + ((~y) + 1); X-y, in the case of the same number, under different circumstancesWill cross 0 0 =, 1 1 = TMP = ((tmp>>31) &1) & (! ( Singx ^ singy));//Guaranteed SINGX and singy//int t = (! x ^ y)); Judging equal//printf ("Sing =%d, tmp =%d\n", sing, TMP); return (Sing | tmp | ((! (x ^ y))); }/* * Ilog2-return Floor (log base 2 of x), where x > 0 * EXAMPLE:ILOG2 (16) = 4 is the number of bits that are represented by the binary. * Legal OPS:! ~ & ^ | + << >> * Max ops:90 * Rating:4 * method: Reference. Dichotomy method. If you move the 16-bit to the right, then you get (10000) 2=16 * Otherwise 0, the highest bit is 0, if not 0, it contains 2 16. That is, get the highest log * number, the same as other */int ilog2 (int x) {int bitsnumber=0;bitsnumber= (!! (x>>16)) <<4;//bitsnumber=bitsnumber+ ((!! (X>> (bitsnumber+8))) &LT;&LT;3); bitsnumber=bitsnumber+ ((!! (X>> (bitsnumber+4))) &LT;&LT;2); bitsnumber=bitsnumber+ ((!! (X>> (bitsnumber+2))) &LT;&LT;1); bitsnumber=bitsnumber+ (!! (X>> (bitsnumber+1))); /for Non Zero Bitsnumber, it should add 0//for zero bitsnumber, it should subtract 1bitsnumber=bitsnumber+ (!! Bitsnumber) + (~0) + (! ( 1^X));//When x is 0 o'clock, you need to subtract one to get the correct value. return bitsnumber;} /* * Float_neg-return Bit-level equivalent of expression-f for * floating point argument F. Return and float parameter-f equals binary * Both the argument and Resul T is passed as unsigned int ' s, but * they is to be interpreted as the bit-level representations of * Single-precisio n floating point values. * Parameters and return results are unsigned integers, but can be interpreted as a binary representation of a single-precision floating-point number * When argument is NaN, return argument. * Legal Ops:any integer/unsigned operations incl. | |, &&.  Also if, while * Max ops:10 * rating:2 */unsigned Float_neg (unsigned uf) {unsigned result;unsigned tmp; Result=uf ^ 0x80000000;  Change the symbol position to reverse Tmp=uf & (0X7FFFFFFF); if (tmp > 0x7f800000)//At this time nan result = Uf;return result;} /* * Float_i2f-return bit-level equivalent of expression (float) x * Returns the binary form of the floating-point number of int x. * Result is returned as unsigned int, but * it was to be interpreted as the bit-level representation of a * SINGLE-PR Ecision floating point values. * Returns the unsigned type but represents the time of the binary single precision form * Legal Ops:any integer/unsigned operations incl. | |, &AMP;&AMP;. Also if, while * Max ops:30 * rating:4 */unsigned float_i2f (int x) {unsigned shiftleft=0;unsigned aftershift, TMP, F lag;unsigned absx=x;unsigned sign=0;//special Caseif (x==0) return 0;//if x < 0, sign = 1000...,abs_x =-xif (x<0) {s Ign=0x80000000;absx=-x;}    Aftershift=absx;//count Shift_left and After_shiftwhile (1) {tmp=aftershift;    aftershift<<=1;    shiftleft++; if (TMP & 0x80000000) break;} if ((Aftershift & 0x01ff) >0x0100) Flag=1;else if ((Aftershift & 0x03ff) ==0x0300) Flag=1;else Flag=0;re Turn sign + (AFTERSHIFT&GT;&GT;9) + ((159-shiftleft) <<23) + flag;} /* * Float_twice-return bit-level equivalent of expression 2*f for * floating point argument f.x returns a float expressed in unsinged Binary unsigned twice-fold binary type * Both the argument and result is passed as unsigned int ' s, but * they is to be interpreted as The bit-level representation of * single-precision floating point values. * When argument is NaN, return argument * Legal Ops:any integer/unsigned operations incl. | |, &&.    Also if, while * Max ops:30 * rating:4 */unsigned float_twice (unsigned uf) {unsigned f = uf; if ((f & 0x7f800000) = = 0)//{//shift left one F = ((F & 0x007fffff) << 1) |    (0x80000000 & F); } else if ((F & 0x7f800000)! = 0x7f800000) {f =f + 0x00800000;} return F;}

  

In-depth understanding of computer systems (CSAPP) experiment two Datalab-handout

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.