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 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
|
#if 0
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, ...) { 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:
int pow2plus1(int x) { return (1 << x) + 1; }
int pow2plus4(int x) { 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.
#endif
int bitXor(int x, int y) {
return ~(~(~x & y) & ~(x & ~y)); }
int tmin(void) {
return (1 << 31);
}
int isTmax(int x) {
return !( ((x+1) + (x+1)) ^ 0) & !!(x+1); }
int allOddBits(int x) {
int mask = 0xAA + (0xAA << 8 ) + (0xAA << 16) + (0xAA << 24) ; return !((x & mask) ^ mask); }
int negate(int x) { return (~x + 1); }
int isAsciiDigit(int x) { int one = !((x & ~0xF) ^ 0x30); int bit4 = (x & 0x8) ; int two = ( (bit4>> 3)& !(x & 0x6))|(!bit4); return one & two ; }
int conditional(int x, int y, int z) {
int flag = !!x; int mask = ~(flag + ~0) ; return (mask & y) + (~mask & z); }
int isLessOrEqual(int x, int y) {
return ((x >> 31) & !(y >> 31)) | !(((x ^ y) >> 31) | ((y + ~x + 1) >> 31));
}
int logicalNeg(int x) {
int inverse = ~x + 1 ; int mask = (1 << 31) ; int flag = ~( ( (x & mask) | (inverse & mask) ) >> 31 ) & 1 ; return flag ;
}
int howManyBits(int x) {
int MSB = (x & (1 << 31)) >> 31 ; int absx = ((~x) & MSB) + (x & ~MSB); int bit16 = 0 ; int bit8 = 0; int bit4 = 0 ; int bit2 = 0; int bit1 = 0 ; int bit0 = 0 ; bit16 = !!(absx >> 16) << 4; absx = absx >> bit16 ; bit8 = !!(absx >> 8) << 3 ; absx = absx >> bit8 ; bit4 = !!(absx >> 4) << 2 ; absx = absx >> bit4 ; bit2 = !!(absx >> 2) << 1 ; absx = absx >> bit2 ; bit1 = !!(absx >> 1) ; absx = absx >> bit1 ; bit0 = absx ;
return bit16 + bit8 + bit4 + bit2 + bit1 + bit0 + 1;
}
unsigned floatScale2(unsigned uf) {
int MSBsaver = uf & (1<<31) ; int exp = ((0xFF << 23) & uf )>>23 ; int fracmask = ~((1 << 31) + (0xFF << 23)); int frac = uf & fracmask; int res = 0;
if ( exp == 0xFF ){ return uf ; } if (exp == 0){ res = (uf << 2)>>1 | MSBsaver ; } else{ exp = exp + 1; res = MSBsaver | ((exp << 23) + frac ); } if ( exp == 0xFF && frac != 0 ){ res = uf; } return res; }
int floatFloat2Int(unsigned uf) {
int MSB = (uf & (1<<31)) >> 31 ; int exp = ((0xFF << 23) & uf )>>23 ; int fracmask = ~((1 << 31) + (0xFF << 23)); int frac = uf & fracmask; int res = 0x80000000u ; int e = 0;
if (exp != 0xff ){ if (exp == 0 ) return 0 ; e = exp - 127 ; if ( e <= 30 && 0 <= e) { if (e >= 23 ) res = (frac + (1<<23)) << e; else { res = (frac + (1<<23)) >> (23 - e ); }
if (MSB == 1) { res = (~res) + 1; } } else if (exp - 127 == 31 && frac == 0){ res = (1 << 31); } else if (e < 0) res = 0; } return res ; }
unsigned floatPower2(int x) {
int exp = 0; int frac = 0;
if (x < -149) return 0; if (x > 128 ) return (0xFF << 23) ; if ( x >= -126 ){ exp = x + 127 ; } else{ frac = (1<<23) >> (-(x + 126)); } return (exp << 23) + frac ;
}
|