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.

308 lines
8.7 KiB

1 year ago
1 year ago
1 year ago
1 year ago
  1. /*
  2. * CS:APP Data Lab
  3. *
  4. * <Please put your name and userid here>
  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. int p1 = ~x;
  125. int q1 = ~y;
  126. int a1 = x&y;
  127. int b1 = ~a1;
  128. int a2 = p1&q1;
  129. int b2 = ~a2;
  130. int res = b1 & b2;
  131. return res;
  132. }
  133. /*
  134. * tmin - return minimum two's complement integer
  135. * Legal ops: ! ~ & ^ | + << >>
  136. * Max ops: 4
  137. * Rating: 1
  138. */
  139. int tmin(void) {
  140. int one = 1;
  141. int res = one << 31;
  142. return res;
  143. }
  144. //2
  145. /*
  146. * isTmax - returns 1 if x is the maximum, two's complement number,
  147. * and 0 otherwise
  148. * Legal ops: ! ~ & ^ | +
  149. * Max ops: 10
  150. * Rating: 1
  151. */
  152. int isTmax(int x) {
  153. int xplusone = x + 1;
  154. int all1 = xplusone ^ x;
  155. int allzero = all1 + 1;
  156. int notnegative1 = !(x ^ (-1));
  157. return !(allzero | notnegative1);
  158. }
  159. /*
  160. * allOddBits - return 1 if all odd-numbered bits in word set to 1
  161. * where bits are numbered from 0 (least significant) to 31 (most significant)
  162. * Examples allOddBits(0xFFFFFFFD) = 0, allOddBits(0xAAAAAAAA) = 1
  163. * Legal ops: ! ~ & ^ | + << >>
  164. * Max ops: 12
  165. * Rating: 2
  166. */
  167. int allOddBits(int x) {
  168. return 2;
  169. }
  170. /*
  171. * negate - return -x
  172. * Example: negate(1) = -1.
  173. * Legal ops: ! ~ & ^ | + << >>
  174. * Max ops: 5
  175. * Rating: 2
  176. */
  177. int negate(int x) {
  178. return 2;
  179. }
  180. //3
  181. /*
  182. * isAsciiDigit - return 1 if 0x30 <= x <= 0x39 (ASCII codes for characters '0' to '9')
  183. * Example: isAsciiDigit(0x35) = 1.
  184. * isAsciiDigit(0x3a) = 0.
  185. * isAsciiDigit(0x05) = 0.
  186. * Legal ops: ! ~ & ^ | + << >>
  187. * Max ops: 15
  188. * Rating: 3
  189. */
  190. int isAsciiDigit(int x) {
  191. return 2;
  192. }
  193. /*
  194. * conditional - same as x ? y : z
  195. * Example: conditional(2,4,5) = 4
  196. * Legal ops: ! ~ & ^ | + << >>
  197. * Max ops: 16
  198. * Rating: 3
  199. */
  200. int conditional(int x, int y, int z) {
  201. return 2;
  202. }
  203. /*
  204. * isLessOrEqual - if x <= y then return 1, else return 0
  205. * Example: isLessOrEqual(4,5) = 1.
  206. * Legal ops: ! ~ & ^ | + << >>
  207. * Max ops: 24
  208. * Rating: 3
  209. */
  210. int isLessOrEqual(int x, int y) {
  211. return 2;
  212. }
  213. //4
  214. /*
  215. * logicalNeg - implement the ! operator, using all of
  216. * the legal operators except !
  217. * Examples: logicalNeg(3) = 0, logicalNeg(0) = 1
  218. * Legal ops: ~ & ^ | + << >>
  219. * Max ops: 12
  220. * Rating: 4
  221. */
  222. int logicalNeg(int x) {
  223. return 2;
  224. }
  225. /* howManyBits - return the minimum number of bits required to represent x in
  226. * two's complement
  227. * Examples: howManyBits(12) = 5
  228. * howManyBits(298) = 10
  229. * howManyBits(-5) = 4
  230. * howManyBits(0) = 1
  231. * howManyBits(-1) = 1
  232. * howManyBits(0x80000000) = 32
  233. * Legal ops: ! ~ & ^ | + << >>
  234. * Max ops: 90
  235. * Rating: 4
  236. */
  237. int howManyBits(int x) {
  238. return 0;
  239. }
  240. //float
  241. /*
  242. * floatScale2 - Return bit-level equivalent of expression 2*f for
  243. * floating point argument f.
  244. * Both the argument and result are passed as unsigned int's, but
  245. * they are to be interpreted as the bit-level representation of
  246. * single-precision floating point values.
  247. * When argument is NaN, return argument
  248. * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
  249. * Max ops: 30
  250. * Rating: 4
  251. */
  252. unsigned floatScale2(unsigned uf) {
  253. return 2;
  254. }
  255. /*
  256. * floatFloat2Int - Return bit-level equivalent of expression (int) f
  257. * for floating point argument f.
  258. * Argument is passed as unsigned int, but
  259. * it is to be interpreted as the bit-level representation of a
  260. * single-precision floating point value.
  261. * Anything out of range (including NaN and infinity) should return
  262. * 0x80000000u.
  263. * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
  264. * Max ops: 30
  265. * Rating: 4
  266. */
  267. int floatFloat2Int(unsigned uf) {
  268. return 2;
  269. }
  270. /*
  271. * floatPower2 - Return bit-level equivalent of the expression 2.0^x
  272. * (2.0 raised to the power x) for any 32-bit integer x.
  273. *
  274. * The unsigned value that is returned should have the identical bit
  275. * representation as the single-precision floating-point number 2.0^x.
  276. * If the result is too small to be represented as a denorm, return
  277. * 0. If too large, return +INF.
  278. *
  279. * Legal ops: Any integer/unsigned operations incl. ||, &&. Also if, while
  280. * Max ops: 30
  281. * Rating: 4
  282. */
  283. unsigned floatPower2(int x) {
  284. return 2;
  285. }