**由于http://csapp.cs.cmu.edu/并未完全开放实验,很多附加实验做不了,一些环境也没办法搭建,更没有标准答案。做了这个实验的朋友可以和我对对答案;)**

实验内容和要求可在http://csapp.cs.cmu.edu/3e/labs.html获得。

Data Lab [Updated 5/4/16]

bits.c

  1. /*
  2. * CS:APP Data Lab
  3. *
  4. * <李秋豪 Richard Li>
  5. *
  6. * bits.c - Source file with your solutions to the Lab.
  7. * This is the file you will hand in to your instructor.
  8. *
  9. * WARNING: Do not include the <stdio.h> header; it confuses the dlc
  10. * compiler. You can still use printf for debugging without including
  11. * <stdio.h>, although you might get a compiler warning. In general,
  12. * it's not good practice to ignore compiler warnings, but in this
  13. * case it's OK.
  14. */
  15. /*
  16. * Instructions to Students:
  17. *
  18. * STEP 1: Read the following instructions carefully.
  19. */
  20. /*
  21. You will provide your solution to the Data Lab by
  22. editing the collection of functions in this source file.
  23. INTEGER CODING RULES:
  24. Replace the "return" statement in each function with one
  25. or more lines of C code that implements the function. Your code
  26. must conform to the following style:
  27. int Funct(arg1, arg2, ...) {
  28. brief description of how your implementation works
  29. int var1 = Expr1;
  30. ...
  31. int varM = ExprM;
  32. varJ = ExprJ;
  33. ...
  34. varN = ExprN;
  35. return ExprR;
  36. }
  37. Each "Expr" is an expression using ONLY the following:
  38. 1. Integer constants 0 through 255 (0xFF), inclusive. You are
  39. not allowed to use big constants such as 0xffffffff.
  40. 2. Function arguments and local variables (no global variables).
  41. 3. Unary integer operations ! ~
  42. 4. Binary integer operations & ^ | + << >>
  43. Some of the problems restrict the set of allowed operators even further.
  44. Each "Expr" may consist of multiple operators. You are not restricted to
  45. one operator per line.
  46. You are expressly forbidden to:
  47. 1. Use any control constructs such as if, do, while, for, switch, etc.
  48. 2. Define or use any macros.
  49. 3. Define any additional functions in this file.
  50. 4. Call any functions.
  51. 5. Use any other operations, such as &&, ||, -, or ?:
  52. 6. Use any form of casting.
  53. 7. Use any data type other than int. This implies that you
  54. cannot use arrays, structs, or unions.
  55. You may assume that your machine:
  56. 1. Uses 2s complement, 32-bit representations of integers.
  57. 2. Performs right shifts arithmetically.
  58. 3. Has unpredictable behavior when shifting an integer by more
  59. than the word size.
  60. EXAMPLES OF ACCEPTABLE CODING STYLE:
  61. pow2plus1 - returns 2^x + 1, where 0 <= x <= 31
  62. int pow2plus1(int x) {
  63. exploit ability of shifts to compute powers of 2
  64. return (1 << x) + 1;
  65. }
  66. pow2plus4 - returns 2^x + 4, where 0 <= x <= 31
  67. int pow2plus4(int x) {
  68. exploit ability of shifts to compute powers of 2
  69. int result = (1 << x);
  70. result += 4;
  71. return result;
  72. }
  73. FLOATING POINT CODING RULES
  74. For the problems that require you to implent floating-point operations,
  75. the coding rules are less strict. You are allowed to use looping and
  76. conditional control. You are allowed to use both ints and unsigneds.
  77. You can use arbitrary integer and unsigned constants.
  78. You are expressly forbidden to:
  79. 1. Define or use any macros.
  80. 2. Define any additional functions in this file.
  81. 3. Call any functions.
  82. 4. Use any form of casting.
  83. 5. Use any data type other than int or unsigned. This means that you
  84. cannot use arrays, structs, or unions.
  85. 6. Use any floating point data types, operations, or constants.
  86. NOTES:
  87. 1. Use the dlc (data lab checker) compiler (described in the handout) to
  88. check the legality of your solutions.
  89. 2. Each function has a maximum number of operators (! ~ & ^ | + << >>)
  90. that you are allowed to use for your implementation of the function.
  91. The max operator count is checked by dlc. Note that '=' is not
  92. counted; you may use as many of these as you want without penalty.
  93. 3. Use the btest test harness to check your functions for correctness.
  94. 4. Use the BDD checker to formally verify your functions
  95. 5. The maximum number of ops for each function is given in the
  96. header comment for each function. If there are any inconsistencies
  97. between the maximum ops in the writeup and in this file, consider
  98. this file the authoritative source.
  99. * STEP 2: Modify the following functions according the coding rules.
  100. *
  101. * IMPORTANT. TO AVOID GRADING SURPRISES:
  102. * 1. Use the dlc compiler to check that your solutions conform
  103. * to the coding rules.
  104. * 2. Use the BDD checker to formally verify that your solutions produce
  105. * the correct answers.
  106. */
  107. /*----------------------------------------------------*/
  108. /*
  109. * bitAnd - x&y using only ~ and |
  110. * Example: bitAnd(6, 5) = 4
  111. * Legal ops: ~ |
  112. * Max ops: 8
  113. * Rating: 1
  114. */
  115. int bitAnd(int x, int y) {
  116. return ~(~x | ~y);
  117. }
  118. /*
  119. * getByte - Extract byte n from word x
  120. * Bytes numbered from 0 (LSB) to 3 (MSB)
  121. * Examples: getByte(0x12345678,1) = 0x56
  122. * Legal ops: ! ~ & ^ | + << >>
  123. * Max ops: 6
  124. * Rating: 2
  125. */
  126. int getByte(int x, int n) {
  127. n = n << 3; /* n = n*8 */
  128. return (x & (0xFF<<n)) >> n;
  129. }
  130. /*
  131. * logicalShift - shift x to the right by n, using a logical shift
  132. * Can assume that 0 <= n <= 31
  133. * Examples: logicalShift(0x87654321,4) = 0x08765432
  134. * Legal ops: ! ~ & ^ | + << >>
  135. * Max ops: 20
  136. * Rating: 3
  137. */
  138. int logicalShift(int x, int n) {
  139. int mask = ~0 << n;
  140. return (mask & x) >> n;
  141. }
  142. /*
  143. * bitCount - returns count of number of 1's in word
  144. * Examples: bitCount(5) = 2, bitCount(7) = 3
  145. * Legal ops: ! ~ & ^ | + << >>
  146. * Max ops: 40
  147. * Rating: 4
  148. */
  149. int bitCount(int x) {
  150. /*这个题我一开始的思路是逐个位的将x向右移动,每次移动后将和0x01与的
  151. 结果和sum相加。但是这样操作符的数量会超过40个。后来想到,其实没有必
  152. 要非要每次把加法放在起始位,其他位置也是符合加法规律的,所以可以在
  153. 多个位置同时进行加法,最后“汇总”到首位的地方*/
  154. /*同时要注意两个问题:1.第一次一位相加的时候可能会溢出到两位”10“,
  155. 第二次两位相加的时候可能会溢出到三位”100“,第三次相加的时候是不会
  156. 溢出超过四位的”1000“,所以会产生0x0y0z0a这种格式。将这四个四位相加
  157. 即可。2.四个四位相加时不能使用x+(x>>24)因为之前x = x + (x>>8)已经
  158. 将第三个和第四个4位相加了,最后只需要将新的第三位和第四位相加。*/
  159. int mask1 = (((((0x55 << 8) + 0x55) << 8) + 0x55) << 8) + 0x55;
  160. int mask2 = (((((0x33 << 8) + 0x33) << 8) + 0x33) << 8) + 0x33;
  161. int mask3 = (((((0x0f << 8) + 0x0f) << 8) + 0x0f) << 8) + 0x0f;
  162. x = (x & mask1) + ((x >>1) & mask1);
  163. x = (x & mask2) + ((x >>2) & mask2);
  164. x = (x & mask3) + ((x >>4) & mask3);
  165. x = x + (x>>8);
  166. x = x + (x>>16);
  167. return x & 0x3F;
  168. }
  169. /*
  170. * bang - Compute !x without using !
  171. * Examples: bang(3) = 0, bang(0) = 1
  172. * Legal ops: ~ & ^ | + << >>
  173. * Max ops: 12
  174. * Rating: 4
  175. */
  176. int bang(int x) {
  177. x = x | (x >> 16);
  178. x = x | (x >> 8);
  179. x = x | (x >> 4);
  180. x = x | (x >> 2);
  181. x = x | (x >> 1);
  182. return x & 1;
  183. }
  184. /*
  185. * tmin - return minimum two's complement integer
  186. * Legal ops: ! ~ & ^ | + << >>
  187. * Max ops: 4
  188. * Rating: 1
  189. */
  190. int tmin(void) {
  191. int x = 0x80 << 24;
  192. return x;
  193. }
  194. /*
  195. * fitsBits - return 1 if x can be represented as an
  196. * n-bit, two's complement integer.
  197. * 1 <= n <= 32
  198. * Examples: fitsBits(5,3) = 0, fitsBits(-4,3) = 1
  199. * Legal ops: ! ~ & ^ | + << >>
  200. * Max ops: 15
  201. * Rating: 2
  202. */
  203. int fitsBits(int x, int n) {
  204. n = ~n + 1; /* 取得-n */
  205. int y = x << (32 + n) >> (32 + n); /* 将缩短后的”符号位“扩展”*/
  206. return !(y ^ x);
  207. }
  208. /*
  209. * divpwr2 - Compute x/(2^n), for 0 <= n <= 30
  210. * Round toward zero
  211. * Examples: divpwr2(15,1) = 7, divpwr2(-33,4) = -2
  212. * Legal ops: ! ~ & ^ | + << >>
  213. * Max ops: 15
  214. * Rating: 2
  215. */
  216. int divpwr2(int x, int n) {
  217. int bias = (x >> 31) & ((1 << n) + ~0); /* 如果x为正数,与运算后bias为0 */
  218. x = x + bias;
  219. return x >> n;
  220. }
  221. /*
  222. * negate - return -x
  223. * Example: negate(1) = -1.
  224. * Legal ops: ! ~ & ^ | + << >>
  225. * Max ops: 5
  226. * Rating: 2
  227. */
  228. int negate(int x) {
  229. return ~x + 1;
  230. }
  231. /*
  232. * isPositive - return 1 if x > 0, return 0 otherwise
  233. * Example: isPositive(-1) = 0.
  234. * Legal ops: ! ~ & ^ | + << >>
  235. * Max ops: 8
  236. * Rating: 3
  237. */
  238. int isPositive(int x) {
  239. return (!((x >> 31) & 1)) & (!!x);
  240. }
  241. /*
  242. * isLessOrEqual - if x <= y then return 1, else return 0
  243. * Example: isLessOrEqual(4,5) = 1.
  244. * Legal ops: ! ~ & ^ | + << >>
  245. * Max ops: 24
  246. * Rating: 3
  247. */
  248. int isLessOrEqual(int x, int y) {
  249. int a = x >> 31;
  250. int b = y >> 31; /*正数的话为0,负数的话为-1*/
  251. int c = a ^ b; /*分同异号来处理,异号的话c为-1,同号的话c为0*/
  252. /*接下来对同号和异号两种情况分别处理,处理的结果不为零
  253. (准确讲是全f)代表x<=y,最后将两种情况或(实际只有一个不为零)*/
  254. int case1 = c & a; /*异号,只有当x为负数的时候返回1*/
  255. int case2 = ~c & ( ~((y + (~x + 1)) >> 31));/*同号,x-y的结果非负即返回1*/
  256. int result = case1 | case2;
  257. return !!result; /*由于result可能为全f,需要用!处理一下*/
  258. }
  259. /*
  260. * ilog2 - return floor(log base 2 of x), where x > 0
  261. * Example: ilog2(16) = 4
  262. * Legal ops: ! ~ & ^ | + << >>
  263. * Max ops: 90
  264. * Rating: 4
  265. */
  266. int ilog2(int x) {
  267. /*开始判断是在32位里的高16位还是低16位*/
  268. int a16 = !!(x >> 16); /*a16为1代表1在高16位,为0代表在低十六位*/
  269. int b16 = a16 << 4; /*1在高16位时b16 = 16*/
  270. /*开始判断是在16里的高8位还是低8位*/
  271. int a8 = !!(x >> (8 + b16));/*a8为1代表1在高8位,为0代表在低8位*/
  272. int b8 = a8 << 3;
  273. /*开始判断是在8里的高4位还是低4位*/
  274. int a4 = !!(x >> (4 + b8 + b16));/*a8为1代表1在高8位,为0代表在低8位*/
  275. int b4 = a4 << 2;
  276. /*开始判断是在4里的高2位还是低2位*/
  277. int a2 = !!(x >> (2 + b4 + b8 + b16));/*a8为1代表1在高8位,为0代表在低8位*/
  278. int b2 = a2 << 1;
  279. /*开始判断是在16里的高1位还是低1位*/
  280. int a1 = !!(x >> (1 + b2 + b4 + b8 + b16));/*a8为1代表1在高8位,为0代表在低8位*/
  281. int b1 = a1 << 0;
  282. return b1 + b2 + b4 + b8 + b16;
  283. }
  284. /*
  285. * float_neg - Return bit-level equivalent of expression -f for
  286. * floating point argument f.
  287. * Both the argument and result are passed as unsigned int's, but
  288. * they are to be interpreted as the bit-level representations of
  289. * single-precision floating point values.
  290. * When argument is NaN, return argument.
  291. * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
  292. * Max ops: 10
  293. * Rating: 2
  294. */
  295. unsigned float_neg(unsigned uf) {
  296. unsigned mask = ~0 << 31;
  297. return mask ^ x;
  298. }
  299. /*
  300. * float_i2f - Return bit-level equivalent of expression (float) x
  301. * Result is returned as unsigned int, but
  302. * it is to be interpreted as the bit-level representation of a
  303. * single-precision floating point values.
  304. * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
  305. * Max ops: 30
  306. * Rating: 4
  307. */
  308. unsigned float_i2f(int x) {
  309. int position = 0; /*计算最高有效位距离第0位的位置*/
  310. unsigned result = 0;
  311. unsigned bias = 127;
  312. int tmp = 0;
  313. if (x == 0)
  314. {
  315. return result;
  316. }
  317. if (x == 0x80000000) /*由于-x = x,此种情况单独处理*/
  318. {
  319. result = 0xcf000000;
  320. return result;
  321. }
  322. for(int i = 16; i >= 1; i /= 2) /*计算距离*/
  323. {
  324. tmp = i + positon;
  325. if (x << (tmp) >> (tmp) == x)
  326. {
  327. position += i;
  328. }
  329. }
  330. result += (((30 - position) + bias) << 23);/*放置exp位*/
  331. if (x < 0)
  332. {
  333. x = -x;
  334. result | 0x80000000; /*放置符号位*/
  335. }
  336. tmp = 7 - position;
  337. if (tmp > 0)
  338. {
  339. x >>= tmp;
  340. }
  341. else
  342. {
  343. x = (x <<-tmp) & 0x007fffff; /*注意移到第24位的数字要变为0*/
  344. }
  345. return result | x; /*放置frac位*/
  346. /*刚好30个,O(∩_∩)O哈哈~*/
  347. }
  348. /*
  349. * float_twice - Return bit-level equivalent of expression 2*f for
  350. * floating point argument f.
  351. * Both the argument and result are passed as unsigned int's, but
  352. * they are to be interpreted as the bit-level representation of
  353. * single-precision floating point values.
  354. * When argument is NaN, return argument
  355. * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
  356. * Max ops: 30
  357. * Rating: 4
  358. */
  359. unsigned float_twice(unsigned uf) {
  360. int tmp = 0x7f800000;
  361. int tmpp = uf & tmp; /*切割出exp位*/
  362. int tmppp = uf & 0x80000000; /*切割出符号位*/
  363. if (tmpp == tmp) /* infinity or NaN */
  364. {
  365. return uf;
  366. }
  367. if (tmpp == 0) /* Denormalized */
  368. {
  369. return (uf << 1) | tmppp; /*恢复符号位*/
  370. }
  371. return (uf & 0x807fffff) | (((tmpp >> 23) + 1) << 23); /* Noemalized */
  372. }

CS:APP3e 深入理解计算机系统_3e Datalab实验的更多相关文章

  1. CS:APP3e 深入理解计算机系统_3e MallocLab实验

    详细的题目要求和资源可以到 http://csapp.cs.cmu.edu/3e/labs.html 或者 http://www.cs.cmu.edu/~./213/schedule.html 获取. ...

  2. CS:APP3e 深入理解计算机系统_3e bomblab实验

    bomb.c /*************************************************************************** * Dr. Evil's Ins ...

  3. CS:APP3e 深入理解计算机系统_3e Attacklab 实验

    详细的题目要求和资源可以到 http://csapp.cs.cmu.edu/3e/labs.html 或者 http://www.cs.cmu.edu/~./213/schedule.html 获取. ...

  4. CS:APP3e 深入理解计算机系统_3e CacheLab实验

    详细的题目要求和资源可以到 http://csapp.cs.cmu.edu/3e/labs.html 或者 http://www.cs.cmu.edu/~./213/schedule.html 获取. ...

  5. CS:APP3e 深入理解计算机系统_3e ShellLab(tsh)实验

    详细的题目要求和资源可以到 http://csapp.cs.cmu.edu/3e/labs.html 或者 http://www.cs.cmu.edu/~./213/schedule.html 获取. ...

  6. CS:APP3e 深入理解计算机系统_3e C Programming Lab实验

    queue.h: /* * Code for basic C skills diagnostic. * Developed for courses 15-213/18-213/15-513 by R. ...

  7. CS:APP3e 深入理解计算机系统_3e Y86-64模拟器指南

    详细的题目要求和资源可以到 http://csapp.cs.cmu.edu/3e/labs.html 或者 http://www.cs.cmu.edu/~./213/schedule.html 获取. ...

  8. 深入理解计算机系统_3e 第九章家庭作业 CS:APP3e chapter 9 homework

    9.11 A. 00001001 111100 B. +----------------------------+ | Parameter Value | +--------------------- ...

  9. 深入理解计算机系统_3e 第八章家庭作业 CS:APP3e chapter 8 homework

    8.9 关于并行的定义我之前写过一篇文章,参考: 并发与并行的区别 The differences between Concurrency and Parallel +---------------- ...

随机推荐

  1. [转载] MapReduce工作原理讲解

    转载自http://www.aboutyun.com/thread-6723-1-1.html 有时候我们在用,但是却不知道为什么.就像苹果砸到我们头上,这或许已经是很自然的事情了,但是牛顿却发现了地 ...

  2. 记PHP面向对象编程

    访问控制 public(公开的):可以在类中.子类中.类外访问. protected(受保护的):只能在类本身及子类中访问. private(私有的):只能在声明他们的类中进行访问,私有的类成员不能被 ...

  3. sqoop: mysql to hive

    sqoop import --connect 数据库连接 --username 数据库用户名--password 数据库密码 --table 导入的表 -m 1 --hive-import --tar ...

  4. 通过hadoop + hive搭建离线式的分析系统之快速搭建一览

    最近有个需求,需要整合所有店铺的数据做一个离线式分析系统,曾经都是按照店铺分库分表来给各自商家通过highchart多维度展示自家的店铺经营 数据,我们知道这是一个以店铺为维度的切分数据,非常适合目前 ...

  5. web-故道白云&Clound的错误

    web-故道白云 题目: 解题思路: 0x01 首先看到题目说html里有秘密,就看了下源代码如图, 重点在红圈那里,表示输入的变量是id,当然上一行的method="get"同时 ...

  6. 3721:和数-poj

    总时间限制:  1000ms 内存限制:  65536kB 描述 给定一个正整数序列,判断其中有多少个数,等于数列中其他两个数的和. 比如,对于数列1 2 3 4, 这个问题的答案就是2, 因为3 = ...

  7. git使用(上)-----基本的方法

    git应该是一项必须要掌握的工具.先简述它和SVN的区别 SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以首先要从中央服务器哪里得到最新的版本,然后干活 ...

  8. python网络编程之单线程之间的并发

    单线程之间的并发就是利用一个线程实现并发的效果,也就是利用了cup遇到阻塞的那段时间去做别的事情,从而提高了cup的利用率,使之在单个线程中就实现了并发的效果. 下面就是一个简单的服务端单个线程实现并 ...

  9. 批量抓取cisco设备配置脚本编写(expect/sed/awk/shell)

    应同事需求自行编写了第一个脚本,中间遇到一些坑. 需求,要求抓取设备"show ip interface brief"信息和"show interface des&quo ...

  10. Postgresql中的explain

    PostgreSQL为每个收到的查询设计一个查询规划.选择正确的匹配查询结构和数据属性的规划对执行效率是至关重要要的,所以系统包含一个复杂的规划器来试图选择好的规划.你可以使用EXPLAIN命令查看查 ...