这是一个本人学习 csapp 的 learning 库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

363 lines
10 KiB

  1. /*
  2. * CS:APP Data Lab
  3. *
  4. * <GentleCold>
  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. #if 0
  16. /*
  17. * Instructions to Students:
  18. *
  19. * STEP 1: Read the following instructions carefully.
  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 if the shift amount
  59. is less than 0 or greater than 31.
  60. EXAMPLES OF ACCEPTABLE CODING STYLE:
  61. /*
  62. * pow2plus1 - returns 2^x + 1, where 0 <= x <= 31
  63. */
  64. int pow2plus1(int x) {
  65. /* exploit ability of shifts to compute powers of 2 */
  66. return (1 << x) + 1;
  67. }
  68. /*
  69. * pow2plus4 - returns 2^x + 4, where 0 <= x <= 31
  70. */
  71. int pow2plus4(int x) {
  72. /* exploit ability of shifts to compute powers of 2 */
  73. int result = (1 << x);
  74. result += 4;
  75. return result;
  76. }
  77. FLOATING POINT CODING RULES
  78. For the problems that require you to implement floating-point operations,
  79. the coding rules are less strict. You are allowed to use looping and
  80. conditional control. You are allowed to use both ints and unsigneds.
  81. You can use arbitrary integer and unsigned constants. You can use any arithmetic,
  82. logical, or comparison operations on int or unsigned data.
  83. You are expressly forbidden to:
  84. 1. Define or use any macros.
  85. 2. Define any additional functions in this file.
  86. 3. Call any functions.
  87. 4. Use any form of casting.
  88. 5. Use any data type other than int or unsigned. This means that you
  89. cannot use arrays, structs, or unions.
  90. 6. Use any floating point data types, operations, or constants.
  91. NOTES:
  92. 1. Use the dlc (data lab checker) compiler (described in the handout) to
  93. check the legality of your solutions.
  94. 2. Each function has a maximum number of operations (integer, logical,
  95. or comparison) that you are allowed to use for your implementation
  96. of the function. The max operator count is checked by dlc.
  97. Note that assignment ('=') is not counted; you may use as many of
  98. these as you want without penalty.
  99. 3. Use the btest test harness to check your functions for correctness.
  100. 4. Use the BDD checker to formally verify your functions
  101. 5. The maximum number of ops for each function is given in the
  102. header comment for each function. If there are any inconsistencies
  103. between the maximum ops in the writeup and in this file, consider
  104. this file the authoritative source.
  105. /*
  106. * STEP 2: Modify the following functions according the coding rules.
  107. *
  108. * IMPORTANT. TO AVOID GRADING SURPRISES:
  109. * 1. Use the dlc compiler to check that your solutions conform
  110. * to the coding rules.
  111. * 2. Use the BDD checker to formally verify that your solutions produce
  112. * the correct answers.
  113. */
  114. #endif
  115. //1
  116. /*
  117. * bitXor - x^y using only ~ and &
  118. * Example: bitXor(4, 5) = 1
  119. * Legal ops: ~ &
  120. * Max ops: 14
  121. * Rating: 1
  122. */
  123. int bitXor(int x, int y) {
  124. // make 0 ^ 1 to 1 & 1 and 0 & 1 equal to 1 0
  125. // make 0 ^ 0 to 1 & 0 and 0 & 1 equal to 0 0
  126. // make 1 0 to ~(0 & 1) equal to 1
  127. // make 0 0 to ~(1 & 1) equal to 0
  128. return ~(~(~x & y) & ~(x & ~y));
  129. }
  130. /*
  131. * tmin - return minimum two's complement integer
  132. * Legal ops: ! ~ & ^ | + << >>
  133. * Max ops: 4
  134. * Rating: 1
  135. */
  136. int tmin(void) {
  137. return 1 << 31;
  138. }
  139. //2
  140. /*
  141. * isTmax - returns 1 if x is the maximum, two's complement number,
  142. * and 0 otherwise
  143. * Legal ops: ! ~ & ^ | +
  144. * Max ops: 10
  145. * Rating: 1
  146. */
  147. int isTmax(int x) {
  148. x += 1;
  149. return !!(!!x & !((~x + 1) ^ x)); // attention to zero case
  150. }
  151. /*
  152. * allOddBits - return 1 if all odd-numbered bits in word set to 1
  153. * where bits are numbered from 0 (least significant) to 31 (most significant)
  154. * Examples allOddBits(0xFFFFFFFD) = 0, allOddBits(0xAAAAAAAA) = 1
  155. * Legal ops: ! ~ & ^ | + << >>
  156. * Max ops: 12
  157. * Rating: 2
  158. */
  159. int allOddBits(int x) {
  160. // 0101 is stored as 1010
  161. int mask = 0xAA + (0xAA << 8) + (0xAA << 16) + (0xAA << 24);
  162. return !((mask & x) ^ mask);
  163. }
  164. /*
  165. * negate - return -x
  166. * Example: negate(1) = -1.
  167. * Legal ops: ! ~ & ^ | + << >>
  168. * Max ops: 5
  169. * Rating: 2
  170. */
  171. int negate(int x) {
  172. return ~x + 1;
  173. }
  174. //3
  175. /*
  176. * isAsciiDigit - return 1 if 0x30 <= x <= 0x39 (ASCII codes for characters '0' to '9')
  177. * Example: isAsciiDigit(0x35) = 1.
  178. * isAsciiDigit(0x3a) = 0.
  179. * isAsciiDigit(0x05) = 0.
  180. * Legal ops: ! ~ & ^ | + << >>
  181. * Max ops: 15
  182. * Rating: 3
  183. */
  184. int isAsciiDigit(int x) {
  185. int i = !((1 << 31) & ((~0x30 + 1) + x));
  186. int j = !!((1 << 31) & ((~0x39) + x));
  187. return i & j;
  188. }
  189. /*
  190. * conditional - same as x ? y : z
  191. * Example: conditional(2,4,5) = 4
  192. * Legal ops: ! ~ & ^ | + << >>
  193. * Max ops: 16
  194. * Rating: 3
  195. */
  196. int conditional(int x, int y, int z) {
  197. int mask = ~(!x) + 1;
  198. y &= ~mask;
  199. z &= mask;
  200. return y + z;
  201. }
  202. /*
  203. * isLessOrEqual - if x <= y then return 1, else return 0
  204. * Example: isLessOrEqual(4,5) = 1.
  205. * Legal ops: ! ~ & ^ | + << >>
  206. * Max ops: 24
  207. * Rating: 3
  208. */
  209. int isLessOrEqual(int x, int y) {
  210. // judge if positive or negative
  211. int i = x >> 31 & 1;
  212. int j = y >> 31 & 1;
  213. // three cases
  214. return !(!i & j) & ((i ^ j) | !(((~x + 1) + y) & (1 << 31)));
  215. }
  216. //4
  217. /*
  218. * logicalNeg - implement the ! operator, using all of
  219. * the legal operators except !
  220. * Examples: logicalNeg(3) = 0, logicalNeg(0) = 1
  221. * Legal ops: ~ & ^ | + << >>
  222. * Max ops: 12
  223. * Rating: 4
  224. */
  225. int logicalNeg(int x) {
  226. int i = (x >> 31) & 1;
  227. int j = ((~x + 1) >> 31) & 1;
  228. return ~(i | j) + 2;
  229. }
  230. /* howManyBits - return the minimum number of bits required to represent x in
  231. * two's complement
  232. * Examples: howManyBits(12) = 5
  233. * howManyBits(298) = 10
  234. * howManyBits(-5) = 4
  235. * howManyBits(0) = 1
  236. * howManyBits(-1) = 1
  237. * howManyBits(0x80000000) = 32
  238. * Legal ops: ! ~ & ^ | + << >>
  239. * Max ops: 90
  240. * Rating: 4
  241. */
  242. int howManyBits(int x) {
  243. // TODO reference from https://zhuanlan.zhihu.com/p/59534845
  244. int s = x >> 31, a1, a2, a3, a4, a5; // c99
  245. x = ((~s) & x) + (s & (~x));
  246. a1 = !!(x >> 16) << 4;
  247. x >>= a1;
  248. a2 = !!(x >> 8) << 3;
  249. x >>= a2;
  250. a3 = !!(x >> 4) << 2;
  251. x >>= a3;
  252. a4 = !!(x >> 2) << 1;
  253. x >>= a4;
  254. a5 = !!(x >> 1);
  255. x >>= a5;
  256. return a1 + a2 + a3 + a4 + a5 + x + 1;
  257. }
  258. //float
  259. /*
  260. * floatScale2 - Return bit-level equivalent of expression 2*f for
  261. * floating point argument f.
  262. * Both the argument and result are passed as unsigned int's, but
  263. * they are to be interpreted as the bit-level representation of
  264. * single-precision floating point values.
  265. * When argument is NaN, return argument
  266. * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
  267. * Max ops: 30
  268. * Rating: 4
  269. */
  270. unsigned floatScale2(unsigned uf) {
  271. int s = (uf >> 31) & 1;
  272. int e = ((uf >> 23) & 0x000000FF);
  273. int f = uf & 0x007FFFFF;
  274. if (e == 0) {
  275. if (f == 0) return uf; // 0.0 * 2
  276. return s << 31 | f << 1; // still non-formatted
  277. }
  278. e += 1; // formatted
  279. if ((e == 0x000000FF && f != 0) || e > 0x000000FF) return uf; // nan
  280. return (s << 31) | (e << 23) | f;
  281. }
  282. /*
  283. * floatFloat2Int - Return bit-level equivalent of expression (int) f
  284. * for floating point argument f.
  285. * Argument is passed as unsigned int, but
  286. * it is to be interpreted as the bit-level representation of a
  287. * single-precision floating point value.
  288. * Anything out of range (including NaN and infinity) should return
  289. * 0x80000000u.
  290. * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
  291. * Max ops: 30
  292. * Rating: 4
  293. */
  294. int floatFloat2Int(unsigned uf) {
  295. unsigned s = (uf >> 31) & 1;
  296. unsigned e = ((uf >> 23) & 0x000000FF);
  297. unsigned f = uf & 0x007FFFFF;
  298. if (e < 0x7F) return 0;
  299. if (e > 0x7F + 31) return 0x80000000u; // nan
  300. e -= 0x7F;
  301. if (s)
  302. return ~((((f << e) & (~0x007FFFFF)) >> 23) + (1 << e)) + 1;
  303. else
  304. return ((((f << e) & (~0x007FFFFF)) >> 23) + (1 << e));
  305. }
  306. /*
  307. * floatPower2 - Return bit-level equivalent of the expression 2.0^x
  308. * (2.0 raised to the power x) for any 32-bit integer x.
  309. *
  310. * The unsigned value that is returned should have the identical bit
  311. * representation as the single-precision floating-point number 2.0^x.
  312. * If the result is too small to be represented as a denorm, return
  313. * 0. If too large, return +INF.
  314. *
  315. * Legal ops: Any integer/unsigned operations incl. ||, &&. Also if, while
  316. * Max ops: 30
  317. * Rating: 4
  318. */
  319. unsigned floatPower2(int x) {
  320. unsigned s = 0;
  321. unsigned e = 0x7F;
  322. unsigned f = 0;
  323. if (x >= 0x80) return 0x7F800000; // nan
  324. if (x < 0) {
  325. if (x == 0x80000000) return 0;
  326. x = ~x + 1;
  327. if (x >= 0x7F) return 0;
  328. e -= x;
  329. } else {
  330. e += x;
  331. }
  332. return (s << 31) | (e << 23) | f;
  333. }