Advertisement
Guest User

IEEE Binary operations

a guest
Sep 9th, 2018
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.44 KB | None | 0 0
  1.     #include "Debug.h"
  2.     #include "iFloat.h"
  3.    
  4.     /** @file iFloat.c
  5.      *  @brief You will modify this file and implement nine functions
  6.      *  @details Your implementation of the functions defined in iFloat.h.
  7.      *  You may add other function if you find it helpful. Added function
  8.      *  should be declared <b>static</b> to indicate they are only used
  9.      *  within this file.
  10.      *  <p>
  11.      *  @author <b>Casey Key</b> goes here
  12.      */
  13.    
  14.     /* declaration for useful function contained in testFloat.c */
  15.     const char* getBinary (iFloat_t value);
  16.    
  17.     /** @todo Implement based on documentation contained in iFloat.h */
  18.     iFloat_t floatGetSign (iFloat_t x) {
  19.       return (x >> (BITS - 1)) & 1;
  20.     }
  21.    
  22.     /** @todo Implement based on documentation contained in iFloat.h */
  23.     iFloat_t floatGetExp (iFloat_t x) {
  24.       return (BITS == 16) ? (x & 0x7C00) >> 10 : (x & 0x7F800000) >> 23;
  25.     }
  26.    
  27.     /** @todo Implement based on documentation contained in iFloat.h */
  28.     iFloat_t floatGetVal (iFloat_t x) {
  29.       iFloat_t mantissa = (BITS == 16) ? (x & 0x03FF)
  30.                           : (x & 0x007FFFFF);
  31.         debug("%s: getVal before implicit 1", getBinary(mantissa));
  32.         //mantissa = (BITS == 16) ? (mantissa | 0000010000000000)
  33.         //                : (mantissa | 0x008);
  34.         mantissa = x | 0000010000000000;
  35.         debug("%s: getVal after implicit 1", getBinary(mantissa));
  36.         mantissa = (BITS == 16) ? (mantissa & 0x07FF)
  37.                           : (mantissa & 0x00FFFFFF);
  38.         if(floatGetSign(x) == 1) {
  39.             mantissa = ~mantissa + 1;
  40.         }
  41.         return mantissa;
  42.     }
  43.    
  44.     /** @todo Implement based on documentation contained in iFloat.h */
  45.     void floatGetAll(iFloat_t x, iFloat_t* sign, iFloat_t*exp, iFloat_t* val) {
  46.         * sign = floatGetSign(x);
  47.         * exp = floatGetExp(x);
  48.         * val = floatGetVal(x);
  49.     }
  50.    
  51.     /** @todo Implement based on documentation contained in iFloat.h */
  52.     iFloat_t floatLeftMost1 (iFloat_t bits) {
  53.         for(int pos = BITS-1; pos >= 0; pos--, bits = bits << 1)
  54.             if((BITS == 16) ? bits & 0x8000 : bits & 0x8000000) return pos;
  55.         return -1;
  56.     }
  57.    
  58.     /** @todo Implement based on documentation contained in iFloat.h */
  59.     iFloat_t floatAbs (iFloat_t x) {
  60.       return (BITS == 16) ? ((!floatGetSign(x)) ? x : x & 0x7FFF)
  61.                           : ((!floatGetSign(x)) ? x : x & 0x7FFFFFFFF);
  62.     }
  63.    
  64.     /** @todo Implement based on documentation contained in iFloat.h */
  65.     iFloat_t floatNegate (iFloat_t x) {
  66.       return (BITS == 16) ? x ^ 0x8000 : x ^ 0x8000000;
  67.     }
  68.    
  69.     /** @todo Implement based on documentation contained in iFloat.h */
  70.     iFloat_t floatAdd (iFloat_t x, iFloat_t y) {
  71.       int exp_x = floatGetExp(x);
  72.       debug("%s: exponent of x", getBinary(exp_x));
  73.       debug("%s: natissa with explicit 1, optionally 2s compliment", getBinary(floatGetVal(x)));
  74.       int val_x = floatGetVal(x);
  75.    
  76.       //(floatGetSign(x) == 1) ? ~floatGetVal(x) : floatGetVal(x);
  77.       debug("%s: bits of val_x after assignment", getBinary(val_x));
  78.      
  79.       int exp_y = floatGetExp(y);
  80.       int val_y = floatGetVal(y);
  81.       debug("%s: bits of val_y after assignment", getBinary(val_y));
  82.       // exponent c is the smaller of the two, zero if equal
  83.       int exp_c = 0;
  84.       exp_c = (exp_x > exp_y && exp_y != exp_x) ? exp_y : exp_x;
  85.      
  86.      
  87.       // Set the exponents equal to one another, shift values
  88.       if(exp_x > exp_y && exp_c != 0) {
  89.           int exp_r = exp_x;
  90.           exp_y = exp_x;
  91.           debug("%d: common exponent, difference of %d", exp_y, exp_y-exp_c);
  92.           debug("%d: bits of val_y before equalizing", val_y);
  93.           for(int i = 0; i < exp_y - exp_c; i++)  {
  94.             val_y = val_y >> 1;
  95.         }
  96.         debug("%s: bits of val_y after equalizing (IEEE 754)", getBinary(val_y)); // example only
  97.       }
  98.       else {
  99.           int exp_r = exp_y;
  100.           exp_x = exp_y;
  101.           debug("%d: common exponent, difference of %d", exp_x, exp_x-exp_c);
  102.           for(int i = 0; i < exp_x - exp_c; i++)  {
  103.             val_x = val_x >> 1;
  104.         }
  105.         debug("%s: bits of val_x after equalizing (IEEE 754)", getBinary(val_x));
  106.       }
  107.      
  108.       int val_r = val_x + val_y;
  109.       int sign_r = floatGetSign(val_r);
  110.       if(sign_r == 1) {
  111.           int mag_r = ~val_r;
  112.       }
  113.       // At Step 5
  114.       return 0;
  115.     }
  116.    
  117.     /** @todo Implement based on documentation contained in iFloat.h */
  118.     iFloat_t floatSub (iFloat_t x, iFloat_t y) {
  119.       return 0;
  120.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement