Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Debug.h"
- #include "iFloat.h"
- /** @file iFloat.c
- * @brief You will modify this file and implement nine functions
- * @details Your implementation of the functions defined in iFloat.h.
- * You may add other function if you find it helpful. Added function
- * should be declared <b>static</b> to indicate they are only used
- * within this file.
- * <p>
- * @author <b>Casey Key</b> goes here
- */
- /* declaration for useful function contained in testFloat.c */
- const char* getBinary (iFloat_t value);
- /** @todo Implement based on documentation contained in iFloat.h */
- iFloat_t floatGetSign (iFloat_t x) {
- return (x >> (BITS - 1)) & 1;
- }
- /** @todo Implement based on documentation contained in iFloat.h */
- iFloat_t floatGetExp (iFloat_t x) {
- return (BITS == 16) ? (x & 0x7C00) >> 10 : (x & 0x7F800000) >> 23;
- }
- /** @todo Implement based on documentation contained in iFloat.h */
- iFloat_t floatGetVal (iFloat_t x) {
- iFloat_t mantissa = (BITS == 16) ? (x & 0x03FF)
- : (x & 0x007FFFFF);
- debug("%s: getVal before implicit 1", getBinary(mantissa));
- //mantissa = (BITS == 16) ? (mantissa | 0000010000000000)
- // : (mantissa | 0x008);
- mantissa = x | 0000010000000000;
- debug("%s: getVal after implicit 1", getBinary(mantissa));
- mantissa = (BITS == 16) ? (mantissa & 0x07FF)
- : (mantissa & 0x00FFFFFF);
- if(floatGetSign(x) == 1) {
- mantissa = ~mantissa + 1;
- }
- return mantissa;
- }
- /** @todo Implement based on documentation contained in iFloat.h */
- void floatGetAll(iFloat_t x, iFloat_t* sign, iFloat_t*exp, iFloat_t* val) {
- * sign = floatGetSign(x);
- * exp = floatGetExp(x);
- * val = floatGetVal(x);
- }
- /** @todo Implement based on documentation contained in iFloat.h */
- iFloat_t floatLeftMost1 (iFloat_t bits) {
- for(int pos = BITS-1; pos >= 0; pos--, bits = bits << 1)
- if((BITS == 16) ? bits & 0x8000 : bits & 0x8000000) return pos;
- return -1;
- }
- /** @todo Implement based on documentation contained in iFloat.h */
- iFloat_t floatAbs (iFloat_t x) {
- return (BITS == 16) ? ((!floatGetSign(x)) ? x : x & 0x7FFF)
- : ((!floatGetSign(x)) ? x : x & 0x7FFFFFFFF);
- }
- /** @todo Implement based on documentation contained in iFloat.h */
- iFloat_t floatNegate (iFloat_t x) {
- return (BITS == 16) ? x ^ 0x8000 : x ^ 0x8000000;
- }
- /** @todo Implement based on documentation contained in iFloat.h */
- iFloat_t floatAdd (iFloat_t x, iFloat_t y) {
- int exp_x = floatGetExp(x);
- debug("%s: exponent of x", getBinary(exp_x));
- debug("%s: natissa with explicit 1, optionally 2s compliment", getBinary(floatGetVal(x)));
- int val_x = floatGetVal(x);
- //(floatGetSign(x) == 1) ? ~floatGetVal(x) : floatGetVal(x);
- debug("%s: bits of val_x after assignment", getBinary(val_x));
- int exp_y = floatGetExp(y);
- int val_y = floatGetVal(y);
- debug("%s: bits of val_y after assignment", getBinary(val_y));
- // exponent c is the smaller of the two, zero if equal
- int exp_c = 0;
- exp_c = (exp_x > exp_y && exp_y != exp_x) ? exp_y : exp_x;
- // Set the exponents equal to one another, shift values
- if(exp_x > exp_y && exp_c != 0) {
- int exp_r = exp_x;
- exp_y = exp_x;
- debug("%d: common exponent, difference of %d", exp_y, exp_y-exp_c);
- debug("%d: bits of val_y before equalizing", val_y);
- for(int i = 0; i < exp_y - exp_c; i++) {
- val_y = val_y >> 1;
- }
- debug("%s: bits of val_y after equalizing (IEEE 754)", getBinary(val_y)); // example only
- }
- else {
- int exp_r = exp_y;
- exp_x = exp_y;
- debug("%d: common exponent, difference of %d", exp_x, exp_x-exp_c);
- for(int i = 0; i < exp_x - exp_c; i++) {
- val_x = val_x >> 1;
- }
- debug("%s: bits of val_x after equalizing (IEEE 754)", getBinary(val_x));
- }
- int val_r = val_x + val_y;
- int sign_r = floatGetSign(val_r);
- if(sign_r == 1) {
- int mag_r = ~val_r;
- }
- // At Step 5
- return 0;
- }
- /** @todo Implement based on documentation contained in iFloat.h */
- iFloat_t floatSub (iFloat_t x, iFloat_t y) {
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement