Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

343 rindas
10 KiB

pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
  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. int base = 42;
  169. int neg = 1 << 7;
  170. int unit8 = base + neg;
  171. int unit16 = (unit8 << 8) + unit8;
  172. int unit32 = (unit16 << 16) + unit16;
  173. int shouldunit32 = x & unit32;
  174. int allzero = shouldunit32^unit32;
  175. int res = !allzero;
  176. return res;
  177. }
  178. /*
  179. * negate - return -x
  180. * Example: negate(1) = -1.
  181. * Legal ops: ! ~ & ^ | + << >>
  182. * Max ops: 5
  183. * Rating: 2
  184. */
  185. int negate(int x) {
  186. int notx = ~x;
  187. int res = notx + 1;// since ~x + x = 1111...111 = -1, ~x + x + 1 = 0
  188. return res;
  189. }
  190. //3
  191. /*
  192. * isAsciiDigit - return 1 if 0x30 <= x <= 0x39 (ASCII codes for characters '0' to '9')
  193. * Example: isAsciiDigit(0x35) = 1.
  194. * isAsciiDigit(0x3a) = 0.
  195. * isAsciiDigit(0x05) = 0.
  196. * Legal ops: ! ~ & ^ | + << >>
  197. * Max ops: 15
  198. * Rating: 3
  199. */
  200. int isAsciiDigit(int x) {
  201. int neg0x3a = ~(0x3a) + 1;
  202. int neg0x30 = ~(0x30) + 1;
  203. int signofminus0x3a = (x + neg0x3a) >> 31; // should be all 1
  204. int signofminus0x30 = (x + neg0x30) >> 31; // should be all 0
  205. int res = !(signofminus0x30 | (~signofminus0x3a));
  206. return res;
  207. }
  208. /*
  209. * conditional - same as x ? y : z
  210. * Example: conditional(2,4,5) = 4
  211. * Legal ops: ! ~ & ^ | + << >>
  212. * Max ops: 16
  213. * Rating: 3
  214. */
  215. int conditional(int x, int y, int z) {
  216. int signx = (!x << 31) >> 31; // if x == 0 signx 111, if x != 0 signx 00000
  217. int ywhenx = (~signx) & y; // if signx 111 is 0
  218. int zwhenx = (signx) & z;
  219. int result = ywhenx | zwhenx;
  220. return result;
  221. }
  222. /*
  223. * isLessOrEqual - if x <= y then return 1, else return 0
  224. * Example: isLessOrEqual(4,5) = 1.
  225. * Legal ops: ! ~ & ^ | + << >>
  226. * Max ops: 24
  227. * Rating: 3
  228. */
  229. int isLessOrEqual(int x, int y) {
  230. int signx = x >> 31;
  231. int signy = y >> 31;
  232. int isnegpos = (signx ^ signy) & (signx); // 0xFFFFF if signx = 1 sign y = 0
  233. int isposneg = (signx ^ signy) & (signy);
  234. int sign1 = ~(1 << 31); // 01111111...111
  235. int absx = x & sign1;
  236. int absy = y & sign1;
  237. int diff = absy + (~absx) + 1;
  238. int signdiff = diff >> 31;
  239. int res = (!(!isnegpos)) | (!isnegpos & !signdiff & !isposneg);
  240. return res;
  241. }
  242. //4
  243. /*
  244. * logicalNeg - implement the ! operator, using all of
  245. * the legal operators except !
  246. * Examples: logicalNeg(3) = 0, logicalNeg(0) = 1
  247. * Legal ops: ~ & ^ | + << >>
  248. * Max ops: 12
  249. * Rating: 4
  250. */
  251. int logicalNeg(int x) {
  252. int sign = (x ^ (~x + 1)) >> 31;
  253. int one = 1;
  254. int singlesign = sign & one;
  255. int isnotmin = (~x) >> 31; // if x is 00000, get 1111 and if x is 1000 get 00000
  256. int notmin = isnotmin & one;
  257. int res = (singlesign ^ one) & notmin;
  258. return res;
  259. }
  260. /* howManyBits - return the minimum number of bits required to represent x in
  261. * two's complement
  262. * Examples: howManyBits(12) = 5
  263. * howManyBits(298) = 10
  264. * howManyBits(-5) = 4
  265. * howManyBits(0) = 1
  266. * howManyBits(-1) = 1
  267. * howManyBits(0x80000000) = 32
  268. * Legal ops: ! ~ & ^ | + << >>
  269. * Max ops: 90
  270. * Rating: 4
  271. */
  272. int howManyBits(int x) {
  273. return 0;
  274. }
  275. //float
  276. /*
  277. * floatScale2 - Return bit-level equivalent of expression 2*f for
  278. * floating point argument f.
  279. * Both the argument and result are passed as unsigned int's, but
  280. * they are to be interpreted as the bit-level representation of
  281. * single-precision floating point values.
  282. * When argument is NaN, return argument
  283. * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
  284. * Max ops: 30
  285. * Rating: 4
  286. */
  287. unsigned floatScale2(unsigned uf) {
  288. return 2;
  289. }
  290. /*
  291. * floatFloat2Int - Return bit-level equivalent of expression (int) f
  292. * for floating point argument f.
  293. * Argument is passed as unsigned int, but
  294. * it is to be interpreted as the bit-level representation of a
  295. * single-precision floating point value.
  296. * Anything out of range (including NaN and infinity) should return
  297. * 0x80000000u.
  298. * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
  299. * Max ops: 30
  300. * Rating: 4
  301. */
  302. int floatFloat2Int(unsigned uf) {
  303. return 2;
  304. }
  305. /*
  306. * floatPower2 - Return bit-level equivalent of the expression 2.0^x
  307. * (2.0 raised to the power x) for any 32-bit integer x.
  308. *
  309. * The unsigned value that is returned should have the identical bit
  310. * representation as the single-precision floating-point number 2.0^x.
  311. * If the result is too small to be represented as a denorm, return
  312. * 0. If too large, return +INF.
  313. *
  314. * Legal ops: Any integer/unsigned operations incl. ||, &&. Also if, while
  315. * Max ops: 30
  316. * Rating: 4
  317. */
  318. unsigned floatPower2(int x) {
  319. return 2;
  320. }