Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2011
4,573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.33 KB | None | 0 0
  1. /*
  2. * CS:APP Data Lab
  3. *
  4. * bits.c - Source file with your solutions to the Lab.
  5. * This is the file you will hand in to your instructor.
  6. *
  7. * WARNING: Do not include the <stdio.h> header; it confuses the dlc
  8. * compiler. You can still use printf for debugging without including
  9. * <stdio.h>, although you might get a compiler warning. In general,
  10. * it's not good practice to ignore compiler warnings, but in this
  11. * case it's OK.
  12. */
  13.  
  14. #include "btest.h"
  15. #include <limits.h>
  16.  
  17. /*
  18. * Instructions to Students:
  19. *
  20. * STEP 1: Fill in the following struct with your identifying info.
  21. */
  22. team_struct team =
  23. {
  24. /* Team name: Replace with either:
  25. Your login ID if working as a one person team
  26. or, ID1+ID2 where ID1 is the login ID of the first team member
  27. and ID2 is the login ID of the second team member */
  28. "apa259",
  29. /* Student name 1: Replace with the full name of first team member */
  30. "Andre Azzolini",
  31. /* Login ID 1: Replace with the login ID of first team member */
  32. "apa259",
  33.  
  34. /* The following should only be changed if there are two team members */
  35. /* Student name 2: Full name of the second team member */
  36. "",
  37. /* Login ID 2: Login ID of the second team member */
  38. ""
  39. };
  40.  
  41. #if 0
  42. /*
  43. * STEP 2: Read the following instructions carefully.
  44. */
  45.  
  46. You will provide your solution to the Data Lab by
  47. editing the collection of functions in this source file.
  48.  
  49. CODING RULES:
  50.  
  51. Replace the "return" statement in each function with one
  52. or more lines of C code that implements the function. Your code
  53. must conform to the following style:
  54.  
  55. int Funct(arg1, arg2, ...) {
  56. /* brief description of how your implementation works */
  57. int var1 = Expr1;
  58. ...
  59. int varM = ExprM;
  60.  
  61. varJ = ExprJ;
  62. ...
  63. varN = ExprN;
  64. return ExprR;
  65. }
  66.  
  67. Each "Expr" is an expression using ONLY the following:
  68. 1. Integer constants 0 through 255 (0xFF), inclusive. You are
  69. not allowed to use big constants such as 0xffffffff.
  70. 2. Function arguments and local variables (no global variables).
  71. 3. Unary integer operations ! ~
  72. 4. Binary integer operations & ^ | + << >>
  73.  
  74. Some of the problems restrict the set of allowed operators even further.
  75. Each "Expr" may consist of multiple operators. You are not restricted to
  76. one operator per line.
  77.  
  78. You are expressly forbidden to:
  79. 1. Use any control constructs such as if, do, while, for, switch, etc.
  80. 2. Define or use any macros.
  81. 3. Define any additional functions in this file.
  82. 4. Call any functions.
  83. 5. Use any other operations, such as &&, ||, -, or ?:
  84. 6. Use any form of casting.
  85.  
  86. You may assume that your machine:
  87. 1. Uses 2s complement, 32-bit representations of integers.
  88. 2. Performs right shifts arithmetically.
  89. 3. Has unpredictable behavior when shifting an integer by more
  90. than the word size.
  91.  
  92. EXAMPLES OF ACCEPTABLE CODING STYLE:
  93. /*
  94. * pow2plus1 - returns 2^x + 1, where 0 <= x <= 31
  95. */
  96. int pow2plus1(int x) {
  97. /* exploit ability of shifts to compute powers of 2 */
  98. return (1 << x) + 1;
  99. }
  100.  
  101. /*
  102. * pow2plus4 - returns 2^x + 4, where 0 <= x <= 31
  103. */
  104. int pow2plus4(int x) {
  105. /* exploit ability of shifts to compute powers of 2 */
  106. int result = (1 << x);
  107. result += 4;
  108. return result;
  109. }
  110.  
  111.  
  112. NOTES:
  113. 1. Use the dlc (data lab checker) compiler (described in the handout) to
  114. check the legality of your solutions.
  115. 2. Each function has a maximum number of operators (! ~ & ^ | + << >>)
  116. that you are allowed to use for your implementation of the function.
  117. The max operator count is checked by dlc. Note that '=' is not
  118. counted; you may use as many of these as you want without penalty.
  119. 3. Use the btest test harness to check your functions for correctness.
  120. 4. The maximum number of ops for each function is given in the
  121. header comment for each function. If there are any inconsistencies
  122. between the maximum ops in the writeup and in this file, consider
  123. this file the authoritative source.
  124. #endif
  125.  
  126. /*
  127. * STEP 3: Modify the following functions according the coding rules.
  128. *
  129. * IMPORTANT. TO AVOID GRADING SURPRISES:
  130. * 1. Use the dlc compiler to check that your solutions conform
  131. * to the coding rules.
  132. * 2. Use the btest test harness to check that your solutions produce
  133. * the correct answers. Watch out for corner cases around Tmin and Tmax.
  134. */
  135. /*
  136. * bitAnd - x&y using only ~ and |
  137. * Example: bitAnd(6, 5) = 4
  138. * Legal ops: ~ |
  139. * Max ops: 8
  140. * Rating: 1
  141. */
  142. int bitAnd(int x, int y) {
  143. // Utilizes DeMorgan's law involving negation of an "or"
  144. return ~(~x | ~y);
  145. }
  146.  
  147. /*
  148. * bitXor - x^y using only ~ and &
  149. * Example: bitXor(4, 5) = 1
  150. * Legal ops: ~ &
  151. * Max ops: 14
  152. * Rating: 2
  153. */
  154. int bitXor(int x, int y) {
  155. // Uses laws of XOR and DeMorgan's
  156. int left = (x & ~y);
  157. int right = (~x & y);
  158. return ~(~left & ~right);
  159. }
  160. /*
  161. * isEqual - return 1 if x == y, and 0 otherwise
  162. * Examples: isEqual(5,5) = 1, isEqual(4,5) = 0
  163. * Legal ops: ! ~ & ^ | + << >>
  164. * Max ops: 5
  165. * Rating: 2
  166. */
  167. int isEqual(int x, int y) {
  168. // Exclusive Or of two equal numbers always equals 0
  169. return !(x^y);
  170. }
  171. /*
  172. * evenBits - return word with all even-numbered bits set to 1
  173. * Legal ops: ! ~ & ^ | + << >>
  174. * Max ops: 8
  175. * Rating: 2
  176. */
  177. int evenBits(void) {
  178. // Per the allowed assumptions and the fact this function returns an int,
  179. // I'm assuming this word size is 32 bits.
  180. int res = 0x55;
  181. res = res | (res << 4);
  182. res = res | (res << 8);
  183. res = res | (res << 16);
  184. return res;
  185. }
  186. /*
  187. * fitsBits - return 1 if x can be represented as an
  188. * n-bit, two's complement integer.
  189. * 1 <= n <= 32
  190. * Examples: fitsBits(5,3) = 0, fitsBits(-4,3) = 1
  191. * Legal ops: ! ~ & ^ | + << >>
  192. * Max ops: 15
  193. * Rating: 2
  194. */
  195. int fitsBits(int x, int n) {
  196. int neg = x >> 31;
  197. int abs = (neg & ~x) | (~neg & x);
  198. return !(abs >> (n+(~1+1)));
  199. }
  200. /*
  201. * bitMask - Generate a mask consisting of all 1's
  202. * lowbit and highbit
  203. * Examples: bitMask(5,3) = 0x38
  204. * Assume 0 <= lowbit <= 31, and 0 <= highbit <= 31
  205. * If lowbit > highbit, then mask should be all 0's
  206. * Legal ops: ! ~ & ^ | + << >>
  207. * Max ops: 16
  208. * Rating: 3
  209. */
  210. int bitMask(int highbit, int lowbit) {
  211. int lo = ~0 << lowbit;
  212. int hi = ~0 << highbit;
  213. hi = ~(hi << 1);
  214. return lo & hi;
  215. }
  216. /*
  217. * conditional - same as x ? y : z
  218. * Example: conditional(2,4,5) = 4
  219. * Legal ops: ! ~ & ^ | + << >>
  220. * Max ops: 16
  221. * Rating: 3
  222. */
  223. int conditional(int x, int y, int z) {
  224. x = ~(!!x) + 1;
  225. return (x & y) | (~x & z);
  226. }
  227. /*
  228. * reverseBytes - reverse the bytes of x
  229. * Example: reverseBytes(0x01020304) = 0x04030201
  230. * Legal ops: ! ~ & ^ | + << >>
  231. * Max ops: 25
  232. * Rating: 3
  233. */
  234. int reverseBytes(int x) {
  235. int mask = 0xFF;
  236. int a = 0;
  237. a = (x & mask);
  238. a <<= 8;
  239. x >>= 8;
  240. a |= (x & mask);
  241. a <<= 8;
  242. x >>= 8;
  243. a |= (x & mask);
  244. a <<= 8;
  245. x >>= 8;
  246. a |= (x & mask);
  247. return a;
  248. }
  249. /*
  250. * bang - Compute !x without using !
  251. * Examples: bang(3) = 0, bang(0) = 1
  252. * Legal ops: ~ & ^ | + << >>
  253. * Max ops: 12
  254. * Rating: 4
  255. */
  256. int bang(int x) {
  257. int y = x >> 31;
  258. int neg = (y & x) | (~y & (~x+1));
  259. neg >>= 31;
  260. neg = ~neg;
  261. return neg & 0x01;
  262. }
  263. /*
  264. * bitCount - returns count of number of 1's in word
  265. * Examples: bitCount(5) = 2, bitCount(7) = 3
  266. * Legal ops: ! ~ & ^ | + << >>
  267. * Max ops: 40
  268. * Rating: 4
  269. */
  270. int bitCount(int x) {
  271. int mask1, mask2, mask3, mask4, mask5;
  272. int a;
  273.  
  274. mask1 = 0x55;
  275. mask1 = (mask1 << 8) + mask1;
  276. mask1 = (mask1 << 16) + mask1;
  277.  
  278. mask2 = 0x33;
  279. mask2 = (mask2 << 8) + mask2;
  280. mask2 = (mask2 << 16) + mask2;
  281.  
  282. mask3 = 0x0F;
  283. mask3 = (mask3 << 8) + mask3;
  284. mask3 = (mask3 << 16) + mask3;
  285.  
  286. mask4 = 0xFF;
  287. mask4 = (mask4 << 16) + mask4;
  288.  
  289. mask5 = 0xFF;
  290. mask5 = (mask5 << 8) + mask5;
  291.  
  292. a = x + ~((x >> 1) & mask1) + 1;
  293. a = ((a >> 2) & mask2) + (a & mask2);
  294. a = ((a >> 4) + a) & mask3;
  295. a = ((a >> 8) + a) & mask4;
  296. a = ((a >> 16) + a) & mask5;
  297.  
  298. return a;
  299. }
  300. /*
  301. * tmin - return minimum two's complement integer
  302. * Legal ops: ! ~ & ^ | + << >>
  303. * Max ops: 4
  304. * Rating: 1
  305. */
  306. int tmin(void) {
  307. int x = 0x1;
  308. x = x << 31;
  309. return x;
  310. }
  311. /*
  312. * isNegative - return 1 if x < 0, return 0 otherwise
  313. * Example: isNegative(-1) = 1.
  314. * Legal ops: ! ~ & ^ | + << >>
  315. * Max ops: 6
  316. * Rating: 3
  317. */
  318. int isNegative(int x) {
  319. x >>= 31;
  320. return !!x;
  321. }
  322. /*
  323. * multFiveEights - multiplies by 5/8 rounding toward 0.
  324. * Examples: multFiveEights(77) = 48
  325. * multFiveEights(-22) = -13
  326. * You can assume |x| < (1 << 29)
  327. * Legal ops: ! ~ & ^ | + << >>
  328. * Max ops: 12
  329. * Rating: 3
  330. */
  331. int multFiveEights(int x) {
  332. int y = (x << 2) + x;
  333. int a = y >> 3;
  334. y >>= 31;
  335. return (~y & a) | (y & (a+1));
  336. }
  337. /*
  338. * sum3 - x+y+z using only a single '+'
  339. * Example: sum3(3, 4, 5) = 12
  340. * Legal ops: ! ~ & ^ | << >>
  341. * Max ops: 16
  342. * Rating: 3
  343. */
  344. /* A helper routine to perform the addition. Don't change this code */
  345. static int sum(int x, int y) {
  346. return x+y;
  347. }
  348. int sum3(int x, int y, int z) {
  349. int word1 = 0;
  350. int word2 = 0;
  351. /**************************************************************
  352. Fill in code below that computes values for word1 and word2
  353. without using any '+' operations
  354. ***************************************************************/
  355. /**************************************************************
  356. Don't change anything below here
  357. ***************************************************************/
  358. return sum(word1,word2);
  359. }
  360. /*
  361. * addOK - Determine if can compute x+y without overflow
  362. * Example: addOK(0x80000000,0x80000000) = 0,
  363. * addOK(0x80000000,0x70000000) = 1,
  364. * Legal ops: ! ~ & ^ | + << >>
  365. * Max ops: 20
  366. * Rating: 3
  367. */
  368. int addOK(int x, int y) {
  369. int s = x + y;
  370. int a;
  371. x >>= 31;
  372. y >>= 31;
  373. s >>= 31;
  374. a = (x^y);
  375. a = (a & 0) | (~a & (x^s));
  376. return (a & 0) | (~a & 1);
  377. }
  378. /*
  379. * isLess - if x < y then return 1, else return 0
  380. * Example: isLess(4,5) = 1.
  381. * Legal ops: ! ~ & ^ | + << >>
  382. * Max ops: 24
  383. * Rating: 3
  384. */
  385. int isLess(int x, int y) {
  386. // If signs are the same, d should be neg.
  387. int d = (x + (~y + 1)) >> 31;
  388. int a;
  389. x = x >> 31;
  390. y = y >> 31;
  391. a = (x^y);
  392. a = (a & x) | (~a & d);
  393. return (a & 1) | (~a & 0);
  394. }
  395. /*
  396. * abs - absolute value of x (except returns TMin for TMin)
  397. * Example: abs(-1) = 1.
  398. * Legal ops: ! ~ & ^ | + << >>
  399. * Max ops: 10
  400. * Rating: 4
  401. */
  402. int abs(int x) {
  403. int y = x >> 31;
  404. return (~y & x) | (y & (~x+1));
  405. }
  406. /*
  407. * isNonZero - Check whether x is nonzero using
  408. * the legal operators except !
  409. * Examples: isNonZero(3) = 1, isNonZero(0) = 0
  410. * Legal ops: ~ & ^ | + << >>
  411. * Max ops: 10
  412. * Rating: 4
  413. */
  414. int isNonZero(int x) {
  415. int y = x >> 31;
  416. int neg = (y & x) | (~y & (~x+1));
  417. neg >>= 31;
  418. return neg & 0x01;
  419. }
  420. /*
  421. * tc2sm - Convert from two's complement to sign-magnitude
  422. * where the MSB is the sign bit
  423. * You can assume that x > TMin
  424. * Example: tc2sm(-5) = 0x80000005.
  425. * Legal ops: ! ~ & ^ | + << >>
  426. * Max ops: 15
  427. * Rating: 4
  428. */
  429. int tc2sm(int x) {
  430. int y = x >> 31;
  431. int neg = (y & x) | (~y & (~x+1));
  432. int pos = ~neg + 1;
  433. return (~y & pos) | (y & ((1<<31) | pos));
  434. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement