现在的位置: 首页 > 综合 > 正文

CSAPP lab. data

2017年10月17日 ⁄ 综合 ⁄ 共 6942字 ⁄ 字号 评论关闭
/* 
 * CS:APP Data Lab 
 * 
 * bits.c - Source file with your solutions to the Lab.
 *          This is the file you will hand in to your instructor.
 *
 * WARNING: Do not include the <stdio.h> header; it confuses the dlc
 * compiler. You can still use printf for debugging without including
 * <stdio.h>, although you might get a compiler warning. In general,
 * it's not good practice to ignore compiler warnings, but in this
 * case it's OK.  
 */

#include "bits.h"
#include <limits.h>

/*
 * Instructions to Students:
 *
 * STEP 1: Fill in the following struct with your identifying info.
 */

/*
 * STEP 2: Read the following instructions carefully.
 */

/*
You will provide your solution to the Data Lab by
editing the collection of functions in this source file.

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 "Expr" is an expression using ONLY the following:
  1. Integer constants 0 through 255 (0xFF), inclusive. You are
      not 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 "Expr" may consist of multiple operators. You are not restricted to
  one operator per line.

  You are expressly forbidden to:
  1. Use any control constructs such as if, do, while, for, switch, etc.
  2. Define or use any macros.
  3. Define any additional functions in this file.
  4. Call any functions.
  5. Use any other operations, such as &&, ||, -, or ?:
  6. Use any form of casting.
 
  You may assume that your machine:
  1. Uses 2s complement, 32-bit representations of integers.
  2. Performs right shifts arithmetically.
  3. Has unpredictable behavior when shifting an integer by more
     than the word size.

EXAMPLES OF ACCEPTABLE CODING STYLE:
*/
  /*
   * pow2plus1 - returns 2^x + 1, where 0 <= x <= 31
   */
  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 <= 31
   */
  int pow2plus4(int x) {
     /* exploit ability of shifts to compute powers of 2 */
     int result = (1 << x);
     result += 4;
     return result;
  }

/*
NOTES:
  1. Use the dlc (data lab checker) compiler (described in the handout) to 
     check the legality of your solutions.
  2. Each function has a maximum number of operators (! ~ & ^ | + << >>)
     that you are allowed to use for your implementation of the function. 
     The max operator count is checked by dlc. Note that '=' is not 
     counted; you may use as many of these as you want without penalty.
  3. Use the btest test harness to check your functions for correctness.
  4. The maximum number of ops for each function is given in the
     header comment for each function. If there are any inconsistencies 
     between the maximum ops in the writeup and in this file, consider
     this file the authoritative source.
#endif
*/
/*
 * STEP 3: Modify the following functions according the coding rules.
 * 
 *   IMPORTANT. TO AVOID GRADING SURPRISES:
 *   1. Use the dlc compiler to check that your solutions conform
 *      to the coding rules.
 *   2. Use the btest test harness to check that your solutions produce 
 *      the correct answers. Watch out for corner cases around Tmin and Tmax.
 */
/* 
 * bang - Compute !x without using !
 *   Examples: bang(3) = 0, bang(0) = 1
 *   Legal ops: ~ & ^ | + << >>
 *   Max ops: 12
 *   Rating: 4 
 */
int bang(int x) {
	int var1 = (~x) + 1;   /* 如果x为0或-TMAX,则运算结束最高位依旧为0,否则最高位为1 */
	int var2 = (var1 >> 31) & 1; /* 正数 */
	int var3 = (x >> 31) & 1; /* 负数 */
	return 	~(var2 | var3) & 1;
  //comp
}
/*
 * bitCount - returns count of number of 1's in word
 *   Examples: bitCount(5) = 2, bitCount(7) = 3
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 40
 *   Rating: 4
 */
int bitCount(int x) {
	int tmp1 = (0x55 << 8) + 0x55;
	int var1 = (tmp1 << 16) + tmp1;   /* var1 = 0x55555555 */
	int tmp2 = (0x33 << 8) + 0x33;
	int var2 = (tmp2 << 16) + tmp2;   /* var2 = 0x33333333 */
	int tmp3 = (0x0f << 8) + 0x0f;
	int var3 = (tmp3 << 16) + tmp3;   /* var3 = 0x0f0f0f0f */
	int var4 = (0xff << 16) + 0xff;   /* var4 = 0x00ff00ff */
	int var5 = (0xff << 8) + 0xff;    /* var5 = 0x0000ffff */
	x = (x & var1) + ((x >> 1) & var1);
	x = (x & var2) + ((x >> 2) & var2);
	x = (x & var3) + ((x >> 4) & var3);
	x = (x & var4) + ((x >> 8) & var4);
	x = (x & var5) + ((x >> 16) & var5);
	return x;
  //comp
}
/* 
 * copyLSB - set all bits of result to least significant bit of x
 *   Example: copyLSB(5) = 0xFFFFFFFF, copyLSB(6) = 0x00000000
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 5
 *   Rating: 2
 */
int copyLSB(int x) {
	int last = x & 1;
	return (~last) + 1;
  //comp
}
/* 
 * divpwr2 - Compute x/(2^n), for 0 <= n <= 30
 *  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) {
	int flag = (x >> 31) & 1; /* 判断是否是负数 */
	int tmp = (1 << n) + (~1 + 1); /* 偏移量 */
	return ( x + ((~flag + 1) & tmp) ) >> n;	
  //comp
}
/* 
 * evenBits - return word with all even-numbered bits set to 1
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 8
 *   Rating: 2
 */
int evenBits(void) {
	int var1 = (0xaa << 8) + var1;
	int var2 = (var1 << 16) + var1;
	return var2;
  //comp
}
/* 
 * fitsBits - return 1 if x can be represented as an 
 *  n-bit, two's complement integer.
 *   1 <= n <= 32
 *   Examples: fitsBits(5,3) = 0, fitsBits(-4,3) = 1
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 15
 *   Rating: 2
 */
int fitsBits(int x, int n) {
	int var1 = (x>> (n + (~1) + 1));
	return (!(var1+1)) | (!var1);                       /* 全1或者全0 */
  //comp
}
/* 
 * getByte - Extract byte n from word x
 *   Bytes numbered from 0 (LSB) to 3 (MSB)
 *   Examples: getByte(0x12345678,1) = 0x56
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 6
 *   Rating: 2
 */
int getByte(int x, int n) {
	return ((x >> (n<<3)) & 0xff);
  //comp
}
/* 
 * isGreater - if x > y  then return 1, else return 0 
 *   Example: isGreater(4,5) = 0, isGreater(5,4) = 1
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 24
 *   Rating: 3
 */
int isGreater(int x, int y) {
	int var1 = x + (~y) + 1;
	return !((!var1) | (var1 >>31));                       /* var1为0或者最高位为1,返回0 */
  //comp
}
/* 
 * isNonNegative - return 1 if x >= 0, return 0 otherwise 
 *   Example: isNonNegative(-1) = 0.  isNonNegative(0) = 1.
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 6
 *   Rating: 3
 */
int isNonNegative(int x) {
	return !((x >> 31) & 1);
  //comp
}
/* 
 * isNotEqual - return 0 if x == y, and 1 otherwise 
 *   Examples: isNotEqual(5,5) = 0, isNotEqual(4,5) = 1
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 6
 *   Rating: 2
 */
int isNotEqual(int x, int y) {
	return !!(x^y); 
  //comp
}
/*
 * isPower2 - returns 1 if x is a power of 2, and 0 otherwise
 *   Examples: isPower2(5) = 0, isPower2(8) = 1, isPower2(0) = 0
 *   Note that no negative number is a power of 2.
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 60
 *   Rating: 4
 */
int isPower2(int x) {    /* 如果x中只有一个1,那么(x-1)&x 必然为0 */
	return  !(x>>31) & !((x + ~0) & x);
  //comp
}
/* 
 * leastBitPos - return a mask that marks the position of the
 *               least significant 1 bit. If x == 0, return 0
 *   Example: leastBitPos(96) = 0x20
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 6
 *   Rating: 4 
 */
int leastBitPos(int x) {
	return ((x ^ (x + ~0)) >> 1) + 1;
  //comp
}
/* 
 * logicalShift - shift x to the right by n, using a logical shift
 *   Can assume that 1 <= n <= 31
 *   Examples: logicalShift(0x87654321,4) = 0x08765432
 *   Legal ops: ~ & ^ | + << >>
 *   Max ops: 16
 *   Rating: 3 
 */
int logicalShift(int x, int n) {
	return (x>>n)&((1<<(32 + (~n) + 1)) + ~0);
  //comp
}
/*
 * satAdd - adds two numbers but when positive overflow occurs, returns
 *          maximum possible value, and when negative overflow occurs,
 *          it returns minimum positive value.
 *   Examples: satAdd(0x40000000,0x40000000) = 0x7fffffff
 *             satAdd(0x80000000,0xffffffff) = 0x80000000
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 30
 *   Rating: 4
 */
int satAdd(int x, int y) {
	int var1 = x + y;
	int sigx = x>>31;
	int sigy = y>>31;
	int sigs = var1>>31;
	int max = (1<<31)-1; /*最大值*/
	int min = (1<<31);   /*最小值*/
	int overflow = (!(sigx^sigy))&(sigx^sigs);  /*是否溢出(最高为同号)*/
	int posover = overflow&(!sigx); /* 正溢出 */
	int negover = overflow&(sigx); /* 负溢出 */     
	return (((~posover)+1)&max) + (((~negover)+1)&min) + ((~(!(posover|negover))+1)&var1);
  //comp
}
/* 
 * tc2sm - Convert from two's complement to sign-magnitude (原码)
 *   where the MSB is the sign bit
 *   You can assume that x > TMin
 *   Example: tc2sm(-5) = 0x80000005.
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 15
 *   Rating: 4
 */
int tc2sm(int x) {
	int sign = (x>>31)&1;
	int var = (~sign) + 1;   /* 正数为0,负数为11111...*/
	return ((var^x)+sign) | (sign<<31);
  //comp
}
开始把lab一个个补起来。。虽然大三了。。但是被上交和复旦的大二学生完爆还是绝逼不能接受的。。

【上篇】
【下篇】

抱歉!评论已关闭.