-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbits.c
More file actions
405 lines (359 loc) · 11.8 KB
/
bits.c
File metadata and controls
405 lines (359 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* CS:APP Data Lab
*
* <Please put your name and userid here>
*
* bits.c - Source file with your solutions to the Lab.
* This is the file you will hand in to your instructor.
*
* WARNING: Do not include the <stdio.h> header; it confuses the dlc
* compiler. You can still use printf for debugging without including
* <stdio.h>, although you might get a compiler warning. In general,
* it's not good practice to ignore compiler warnings, but in this
* case it's OK.
*/
#if 0
/*
* Instructions to Students:
*
* STEP 1: Read the following instructions carefully.
*/
You will provide your solution to the Data Lab by
editing the collection of functions in this source file.
INTEGER CODING RULES:
Replace the "return" statement in each function with one
or more lines of C code that implements the function. Your code
must conform to the following style:
int Funct(arg1, arg2, ...) {
/* brief description of how your implementation works */
int var1 = Expr1;
...
int varM = ExprM;
varJ = ExprJ;
...
varN = ExprN;
return ExprR;
}
Each "Expr" is an expression using ONLY the following:
1. Integer constants 0 through 255 (0xFF), inclusive. You are
not allowed to use big constants such as 0xffffffff.
2. Function arguments and local variables (no global variables).
3. Unary integer operations ! ~
4. Binary integer operations & ^ | + << >>
Some of the problems restrict the set of allowed operators even further.
Each "Expr" may consist of multiple operators. You are not restricted to
one operator per line.
You are expressly forbidden to:
1. Use any control constructs such as if, do, while, for, switch, etc.
2. Define or use any macros.
3. Define any additional functions in this file.
4. Call any functions.
5. Use any other operations, such as &&, ||, -, or ?:
6. Use any form of casting.
7. Use any data type other than int. This implies that you
cannot use arrays, structs, or unions.
You may assume that your machine:
1. Uses 2s complement, 32-bit representations of integers.
2. Performs right shifts arithmetically.
3. Has unpredictable behavior when shifting if the shift amount
is less than 0 or greater than 31.
EXAMPLES OF ACCEPTABLE CODING STYLE:
/*
* pow2plus1 - returns 2^x + 1, where 0 <= x <= 31
*/
int pow2plus1(int x) {
/* exploit ability of shifts to compute powers of 2 */
return (1 << x) + 1;
}
/*
* pow2plus4 - returns 2^x + 4, where 0 <= x <= 31
*/
int pow2plus4(int x) {
/* exploit ability of shifts to compute powers of 2 */
int result = (1 << x);
result += 4;
return result;
}
FLOATING POINT CODING RULES
For the problems that require you to implement floating-point operations,
the coding rules are less strict. You are allowed to use looping and
conditional control. You are allowed to use both ints and unsigneds.
You can use arbitrary integer and unsigned constants. You can use any arithmetic,
logical, or comparison operations on int or unsigned data.
You are expressly forbidden to:
1. Define or use any macros.
2. Define any additional functions in this file.
3. Call any functions.
4. Use any form of casting.
5. Use any data type other than int or unsigned. This means that you
cannot use arrays, structs, or unions.
6. Use any floating point data types, operations, or constants.
NOTES:
1. Use the dlc (data lab checker) compiler (described in the handout) to
check the legality of your solutions.
2. Each function has a maximum number of operations (integer, logical,
or comparison) that you are allowed to use for your implementation
of the function. The max operator count is checked by dlc.
Note that assignment ('=') is not counted; you may use as many of
these as you want without penalty.
3. Use the btest test harness to check your functions for correctness.
4. Use the BDD checker to formally verify your functions
5. The maximum number of ops for each function is given in the
header comment for each function. If there are any inconsistencies
between the maximum ops in the writeup and in this file, consider
this file the authoritative source.
/*
* STEP 2: Modify the following functions according the coding rules.
*
* IMPORTANT. TO AVOID GRADING SURPRISES:
* 1. Use the dlc compiler to check that your solutions conform
* to the coding rules.
* 2. Use the BDD checker to formally verify that your solutions produce
* the correct answers.
*/
#endif
#include "bits.h"
// P1
/*
* signMask - return a mask with only the most significant bit set (0x80000000)
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 2
* Rating: 1
*/
int signMask(void) {
return 1;
}
// P2
/*
* bitXor - x^y using only ~ and &
* Example: bitXor(4, 5) = 1, bitXor(7, 7) = 0
* Legal ops: ~ &
* Max ops: 8
* Rating: 2
*/
int bitXor(int x, int y) {
return 2;
}
// P3
/*
* negativePart - return -x if x < 0, otherwise return 0
* Examples: negativePart(-10) = 10, negativePart(5) = 0
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 6
* Rating: 3
*/
int negativePart(int x){
return 3;
}
// P4
/*
* clearByte - return x with the nth byte cleared to 0
* You can assume 0 <= n <= 3
* Example: clearByte(0x01020304, 2) = 0x01000304
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 6
* Rating: 4
*/
int clearByte(int x,int n) {
return 4;
}
// P5
/*
* roundUp - round up x to the nearest multiple of 256 that is bigger than x
* Example: roundUp(0x117f) = 0x1200
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 8
* Rating: 4
*/
int roundUp(int x) {
return 5;
}
// P6
/*
* isLargerOrEqual - return 1 if x >= y, else return 0
* Example: isLargerOrEqual(5,4) = 1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 24
* Rating: 4
*/
int isLargerOrEqual(int x, int y) {
return 6;
}
// P7
/*
* logicalShift - shift x to the right by n bits, using a logical shift
* Can assume that 0 <= n <= 31
* Examples: logicalShift(0x87654321,4) = 0x08765432
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 4
*/
int logicalShift(int x, int n) {
return 7;
}
// P8
/*
* swapNibblePairs - swap the low and high 4 bits within each byte of x
* Examples: swapNibblePairs(0xAB) = 0xBA
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 24
* Rating: 5
*/
int swapNibblePairs(int x) {
return 8;
}
// P9
/*
* secondLowestZeroBit - return a mask that marks the position of the second least significant 0 bit
* Examples: secondLowestZeroBit(0xFFFFFFFA) = 0x4, secondLowestZeroBit(0x7FFFFFFF) = 0
* secondLowestZeroBit(-1) = 0
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 8
* Rating: 5
*/
int secondLowestZeroBit(int x) {
return 9;
}
// P10
/*
* rotateRightBits - rotate x to right by n bits
* you can assume n >= 0
* Examples: rotateRightBits(0x12345678, 8) = 0x78123456
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 16
* Rating: 5
*/
int rotateRightBits(int x, int n) {
return 10;
}
// P11
/*
* fractions - return floor((x*5 + 8)/16) for 0 <= x <= (1 << 28), x is an integer
* Example: fractions(20) = 6
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 8
* Rating: 5
*/
int fractions(int x) {
return 11;
}
// P12
/*
* overflowCalc - given binary representations of three 32-bit positive numbers and add them together,
* return the binary representation of the part where bits are higher than 32.
* Examples: overflowCalc(0xffffffff, 0xffffffff, 0xffffffff) = 2
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 30
* Rating: 7
*/
int overflowCalc(int x, int y, int z) {
return 12;
}
// P13
/*
* mul5Sat - return x*5, and if x*5 overflow, change the result to
* INT_MAX(0x7fffffff) or INT_MIN(0x80000000) correspondingly
* Examples: mul5Sat(1) = 0x5, mul5Sat(0x40000000) = 0x7fffffff
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 36
* Rating: 7
*/
int mul5Sat(int x) {
return 13;
}
// P14
/*
* float_half - Return bit-level equivalent of expression f/2 for
* floating point argument f.
* Both the argument and result are passed as unsigned int's, but
* they are to be interpreted as the bit-level representation of
* single-precision floating point values.
* When argument is NaN, return argument
* Legal ops: Any integer / unsigned operations incl. ||, &&. also if, while
* Max ops: 32
* Rating: 7
*/
unsigned float_half(unsigned f) {
return 14;
}
// P15
/*
* float_i2f - Return bit-level equivalent of expression (float) x.
* Result is returned as unsigned int, but
* it is to be interpreted as the bit-level representation of a
* single-precision floating point values.
* Legal ops: Any integer / unsigned operations incl. ||, &&. also if, while
* Max ops: 40
* Rating: 7
*/
unsigned float_i2f(int x) {
return 15;
}
#ifdef NOT_SUPPOSED_TO_BE_DEFINED
# __ __ _
# \ \ / / | |
# \ \ /\ / /__| | ___ ___ _ __ ___ ___
# \ \/ \/ / _ \ |/ __/ _ \| '_ ' _ \ / _ \
# \ /\ / __/ | (_| (_) | | | | | | __/
# \/ \/ \___|_|\___\___/|_| |_| |_|\___|
#
# ██╗ ██╗ ██████╗ ███╗ ██╗ ██████╗ ██████╗ ██████╗ █████╗ ██████╗ ████████╗
# ██║ ██║██╔═══██╗████╗ ██║██╔═══██╗██╔══██╗ ██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝
# ███████║██║ ██║██╔██╗ ██║██║ ██║██████╔╝ ██████╔╝███████║██████╔╝ ██║
# ██╔══██║██║ ██║██║╚██╗██║██║ ██║██╔══██╗ ██╔═══╝ ██╔══██║██╔══██╗ ██║
# ██║ ██║╚██████╔╝██║ ╚████║╚██████╔╝██║ ██║ ██║ ██║ ██║██║ ██║ ██║
# ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝
#
#endif
// P16
/*
* float_inv - Return bit-level equivalent of expression 1/x (x is an integer) for
* Result is returned as unsigned int, but
* it is to be interpreted as the bit-level representation of a
* single-precision floating point values.
* When x is 0, return NaN.
* Legal ops: Any integer / unsigned operations incl. ||, &&. also if, while
* Max ops: 120
* Rating: 10
* For mercy, x is between -16777216 and 16777216, meaning that you don't have
* to handle denormalized numbers.
*/
unsigned float_inv(int x) {
return 16;
}
// P17
/*
* oddParity - return the odd parity bit of x, that is,
* when the number of 1s in the binary representation of x is even, then the return 1, otherwise return 0.
* Examples: oddParity(5) = 1, oddParity(7) = 0
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 56
* Rating: 10
*/
int oddParity(int x) {
return 17;
}
// P18
/*
* bitCount - return count of number of 1's in the binary representation of x
* Examples: bitCount(5) = 2, bitCount(7) = 3
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 40
* Rating: 10
*/
int bitCount(int x) {
return 18;
}
// P19
/*
* bitReverse - Reverse bits in an 32-bit integer
* Examples: bitReverse(0x80000004) = 0x20000001
* bitReverse(0x7FFFFFFF) = 0xFFFFFFFE
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 34
* Rating: 10
*/
int bitReverse(int x)
{
return 19;
}