Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

FLOATING POINT CODING RULES For the problems that require you to implent floatin

ID: 3650105 • Letter: F

Question

FLOATING POINT CODING RULES

For the problems that require you to implent 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 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.
/
float_neg - Return bit-level equivalent of expression -f 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 representations of
single-precision floating point values.
When argument is NaN, return argument.
Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
Max ops: 10
Rating: 2
/
unsigned float_neg(unsigned uf) {
return 2;

Explanation / Answer

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 /* * bitAnd - x&y using only ~ and | * Example: bitAnd(6, 5) = 4 * Legal ops: ~ | * Max ops: 8 * Rating: 1 */ int bitAnd(int x, int y) { return ~(~x|~y); } /* * TMax - return maximum two's complement integer * Legal ops: ! ~ & ^ | + > * Max ops: 4 * Rating: 1 */ int tmax(void) { //0x7fffffff return ~(1 * Max ops: 5 * Rating: 2 */ int copyLSB(int x) { //no exception return ((x > 31); } /* * leastBitPos - return a mask that marks the position of the * least significant 1 bit. If x == 0, return 0 * Example: leastBitPos(96) = 0x20 * Legal ops: ! ~ & ^ | + > * Max ops: 6 * Rating: 2 */ int leastBitPos(int x) { //no exception //modified on Feb 1st, for 3 steps return x & (~x + 1); } /* * divpwr2 - Compute x/(2^n), for 0 > 31); //positive number >> n //negtive number add (n-1) 1 first return (x + (sign & ((1 n; } /* * logicalShift - shift x to the right by n, using a logical shift * Can assume that 0 (n + neg_one)); //process mask1 for n-1 = -1 int mask2 = (n + neg_one) >> neg_one; return (x >> n) & (~(mask1 ^ mask2)); } /* * isNonNegative - return 1 if x >= 0, return 0 otherwise * Example: isNonNegative(-1) = 0. isNonNegative(0) = 1. * Legal ops: ! ~ & ^ | + > * Max ops: 6 * Rating: 3 */ int isNonNegative(int x) { return !(x >> 31); } /* * isGreater - if x > y then return 1, else return 0 * Example: isGreater(4,5) = 0, isGreater(5,4) = 1 * Legal ops: ! ~ & ^ | + > * Max ops: 24 * Rating: 3 */ int isGreater(int x, int y) { //-y int negY = ~y + 1; //-1 int negOne = ~0; //dif sign int dif = !!((x ^ y) >> 31); //dif, x is positive int gr = dif & !(x >> 31); //same, x-y-1>0 int agr = (!dif) & !((x + negY + negOne) >> 31); return (gr | agr); } /* * absVal - absolute value of x * Example: absVal(-1) = 1. * You may assume -TMax * Max ops: 40 * Rating: 4 */ int bitCount(int x) { //magic binary numbers int mask = 0x55 | (0x55 1) & mask); mask = 0x33 | (0x33 2) & mask); mask = 0x0f |(0x0f 4) & mask); mask = 0xff | (0xff > 8) & mask); mask = 0xff | (0xff > 16) & mask); return x; } /* * float_abs - Return bit-level equivalent of absolute value of f 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 representations of * single-precision floating point values. * When argument is NaN, return argument.. * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while * Max ops: 10 * Rating: 2 */ unsigned float_abs(unsigned uf) { //NaN if(!((0x7f800000 & uf) ^ 0x7f800000) && (uf >= 1; i++; } //exponent = i + bias E = (0x7f + i) (i - 23); frac = lost & frac_mask; prev = lost & 1; half_r = (frac_mask >> (46 - i)) & x; half = 1 half || (half_r == half && prev == 1)){ frac += 1; } } return sign + E + frac; }