soxluvr23

HW for computer systems. can someone point me what to do?

Feb 25th, 2013
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.47 KB | None | 0 0
  1. /*-------------------------------------------------------------------------*
  2.  *---                                   ---*
  3.  *---       floatAdder.c                        ---*
  4.  *---                                   ---*
  5.  *---       This file adds 2 32-bit IEEE floating point numbers with    ---*
  6.  *---   integer operations.  Doesn't handle 'NaN' properly, nor does    ---*
  7.  *---   does it round properly.  Those are the only 2 bugs of which ---*
  8.  *---   I'm aware.                          ---*
  9.  *---                                   ---*
  10.  *---   ----    ----    ----    ----    ----    ----    ----    ----    ---*
  11.  *---                                   ---*
  12.  *---   Version 1.0     2013 February 8     Joseph Phillips ---*
  13.  *---                                   ---*
  14.  *-------------------------------------------------------------------------*/
  15.  
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18.  
  19. //--            Sign related constants              --//
  20.  
  21. //  PURPOSE:  To tell how many bits to shift the sign bit from the least
  22. //  signficant position to where the sign bit belongs.
  23. #define  SIGN_SHIFT             31
  24.  
  25. //  PURPOSE:  To be the mask to only keep the sign bit.
  26. #define  SIGN_MASK              0x80000000
  27.  
  28. //  PURPOSE:  To be the mask to keep everything but the sign bit.
  29. #define  EVERYTHING_BUT_SIGN_MASK       (~SIGN_MASK)
  30.  
  31.  
  32.  
  33. //--            Exponent related constants          --//
  34.  
  35. //  PURPOSE:  To tell how many bits to shift the exponent bit field from the
  36. //  least signficant position to where the exponent bit field belongs.
  37. #define  EXPONENT_SHIFT             23
  38.  
  39. //  PURPOSE:  To be the mask to only keep the exponent bit field.
  40. #define  EXPONENT_MASK              0x7F800000
  41.  
  42. //  PURPOSE:  To tell the exponent bit pattern for 'infinity' and
  43. //  'not-a-number'.
  44. #define  EXPONENT_INFINITE_BIT_PATTERN      0xFF
  45.  
  46. //  PURPOSE:  To tell the exponent bit pattern for denormalized numbers
  47. //  (including 0.0).
  48. #define  EXPONENT_DENORMALIZED_BIT_PATTERN  0x00
  49.  
  50. //  PURPOSE:  To tell the 'bias' of the exponent bit field:
  51. //  (powerOf2) = (exponentBitPattern) - EXPONENT_BIAS
  52. #define  EXPONENT_BIAS              0x7F
  53.  
  54. //  PURPOSE:  To tell the power of 2 for 'infinity' and 'not-a-number'.
  55. #define  INFINITE_POWER_OF_2            +128
  56.  
  57. //  PURPOSE:  To tell the power of 2 for denormalized numbers (including 0.0):
  58. #define  DENORMALIZED_POWER_OF_2        -127
  59.  
  60. #define  INDISTINGUISHABLE_FROM_0_POWER_OF_2    (DENORMALIZED_POWER_OF_2-23)
  61.  
  62.  
  63. //--            Mantissa related constants          --//
  64.  
  65. //  PURPOSE:  To tell the mask to only keep the mantissa bit field.
  66. #define  MANTISSA_MASK              0x007FFFFF
  67.  
  68. //  PURPOSE:  To tell give the hidden bit in its proper position.
  69. #define  MANTISSA_HIDDEN_BIT            0x00800000
  70.  
  71. //  PURPOSE:  To tell how many bits to shift the mantissa bit field from the
  72. //  least signficant position to where the mantissa bit field belongs.
  73. #define  MANTISSA_SHIFT             0
  74.  
  75. //  PURPOSE:  To tell how many mantissa bits there are (including hidden bit)
  76. #define  NUM_MANTISSA_BITS          24
  77.  
  78.  
  79.  
  80. //--            Miscellaneous related constants         --//
  81.  
  82. //  PURPOSE:  To give the maximum length of C-strings.
  83. #define  TEXT_LEN               64
  84.  
  85.  
  86.  
  87. //  PURPOSE:  To return 1 if 'f' is 0.0 or -0.0.  Returns 0 otherwise.
  88. int             isZero  (float f)
  89. {
  90.   unsigned int  u = *(unsigned int*)&f;
  91.  
  92.   //  Your code here
  93.  
  94.   return(0 /* Perhaps change this */);
  95. }
  96.  
  97.  
  98. //  PURPOSE:  To return the +1 if the sign of 'f' is positive, or -1 otherwise.
  99. int     getSign     (float f)
  100. {
  101.   unsigned int  u = *(unsigned int*)&f;
  102.  
  103.   //  Your code here
  104.  
  105.   return(0 /* Perhaps change this */);
  106. }
  107.  
  108.  
  109. //  PURPOSE:  To return the exponent (the X of 2^X) of the floating point
  110. //  'f' from 'DENORMALIZED_POWER_OF_2' to 'INFINITE_POWER_OF_2'.
  111. //  (Does _not_ return the bit pattern.)
  112. int     getPowerOf2 (float f)
  113. {
  114.   unsigned int  u   = *(unsigned int*)&f;
  115.   unsigned int  i   = 0; /* Perhaps change this */
  116.  
  117.   //  Your code here
  118.  
  119.   return(0 /* Perhaps change this */);
  120. }
  121.  
  122.  
  123.  
  124. //  PURPOSE:  To return the mantissa of 'f', with the HIDDEN_BIT or-ed in if
  125. //   'f' is not denormalized.
  126. unsigned int    getMantissa (float f)
  127. {
  128.   unsigned int  mantissa    = *(unsigned int*)&f;
  129.  
  130.   //  Your code here
  131.  
  132.   return(0 /* Perhaps change this */);
  133. }
  134.  
  135.  
  136.  
  137. //  PURPOSE:  To return the 0x0 when given +1, or 0x1 when given -1.
  138. unsigned char   signToSignBit   (int    sign)
  139. {
  140.   //  Your code here
  141.  
  142.   return(0 /* Perhaps change this */);
  143. }
  144.  
  145.  
  146. //  PURPOSE:  To return the exponent field's bit pattern for power of 2
  147. //  'powerOf2'.  If 'powerOf2' is greater or equal to 'INFINITE_POWER_OF_2'
  148. //  then it returns 'EXPONENT_INFINITE_BIT_PATTERN'.  If 'powerOf2' is
  149. //  less than or equal to 'EXPONENT_DENORMALIZED_BIT_PATTERN' then it
  150. //  returns 'EXPONENT_DENORMALIZED_BIT_PATTERN'.  Otherwise it returns the
  151. //  corresponding bit pattern for 'powerOf2' given bias 'EXPONENT_BIAS'.
  152. unsigned char   pwrOf2ToExpBits (int    powerOf2)
  153. {
  154.   //  Your code here
  155.  
  156.   return(0 /* Perhaps change this */);
  157. }
  158.  
  159.  
  160. //  PURPOSE:  To return the mantissa _field_, 'mantissa' with its hidden
  161. //  bit turned off.
  162. unsigned int  mantissaField (unsigned int   mantissa
  163.                 )
  164. {
  165.   //  Your code here
  166.  
  167.   return(0 /* Perhaps change this */);
  168. }
  169.  
  170.  
  171. //  PURPOSE:  To return the floating point number constructed from sign bit
  172. //  'signBit'
  173. float   buildFloat  (int        sign,
  174.              int        exp,
  175.              unsigned int   mantissaBits
  176.             )
  177. {
  178.   //  Leave this code alone!
  179.   unsigned int  u   = (signToSignBit(sign)      << SIGN_SHIFT)     |
  180.               (pwrOf2ToExpBits(exp)     << EXPONENT_SHIFT) |
  181.               (mantissaField(mantissaBits)  << MANTISSA_SHIFT);
  182.   float     f   = *(float*)&u;
  183.   return(f);
  184. }
  185.  
  186.  
  187. //  PURPOSE:  To return 'f' added with 'g'.
  188. float   add (float  f,
  189.          float  g
  190.         )
  191. {
  192.   //  I.  Handle when either 'f' or 'g' is 0.0:
  193.   if  ( isZero(f) )
  194.     return(g);
  195.  
  196.   if  ( isZero(g) )
  197.     return(f);
  198.  
  199.   //  II.  Do operation:
  200.   int       signF       = getSign(f);
  201.   int       signG       = getSign(g);
  202.   int       powerOf2F   = getPowerOf2(f);
  203.   int       powerOf2G   = getPowerOf2(g);
  204.   unsigned int  mantissaF   = getMantissa(f);
  205.   unsigned int  mantissaG   = getMantissa(g);
  206.   unsigned int  mantissa;
  207.   int       powerOf2;
  208.   int       sign;
  209.  
  210.   if  (signF == signG)
  211.   {
  212.     //  II.A.  Do addition:
  213.  
  214.     //  (This is required.)
  215.     //
  216.     //  See which has the bigger power-of-2: 'f' or 'g'
  217.     //  Shift the smaller of the two by the difference in power of 2.
  218.     //  Then add the mantissas.
  219.     //
  220.     //  What is the value of 'powerOf2'?  What is the value of 'sign'?
  221.     //
  222.     //  How do you detect when the mantissa overflows?
  223.     //  What do you do when the mantissa does overflow?
  224.  
  225.   }
  226.   else
  227.   {
  228.     //  II.B.  Do subtraction:
  229.     //  II.B.1.  Handle canceling to 0:
  230.     if  ( (powerOf2F == powerOf2G) && (mantissaF == mantissaG) )
  231.       return(buildFloat(+1,EXPONENT_DENORMALIZED_BIT_PATTERN,0x0));
  232.  
  233.     //  II.B.2.  Do subtraction:
  234.     //  (This is +5 extra credit.)
  235.     //
  236.     //  Subtract the smaller from the bigger.
  237.     //  How do you tell which is bigger from 'powerOf2F', 'powerOf2G', 'mantissaF' and 'mantissaG'?
  238.     //  Do the same mantissa shifting as with addition.
  239.     //
  240.     //  What is the value of 'powerOf2'?  What is the value of 'sign'?
  241.     //
  242.     //  With addition you may be left with too many bits in the mantissa,
  243.     //  with subtraction you may be left with too few.
  244.     //  If that's the case, then keeping shifting the most significant bit
  245.     //  in the mantissa until either it gets to the mantissa's most
  246.     //  significant bit position (the hidden bit's position) or until
  247.     //  'powerOf2' gets down to 'DENORMALIZED_POWER_OF_2'.
  248.     //
  249.     //  Each time you shift 'mantissa' what should you do to 'powerOf2'?
  250.  
  251.   }
  252.  
  253.   //  III.  Return built float:
  254.   //  Leave this code alone!
  255.   return(buildFloat(sign,powerOf2,mantissa));
  256. }
  257.  
  258.  
  259. //  PURPOSE:  To first test your 'getSign()', 'getPowerOf2()' and
  260. //  'getMantissa()' functions, and then your 'add()' function.  Ignores
  261. //  arguments from OS.  Returns 'EXIT_SUCCESS' to OS.
  262. int main    ()
  263. {
  264.   //  Leave this code alone!
  265.   float f;
  266.   float g;
  267.   char  text[TEXT_LEN];
  268.  
  269.   do
  270.   {
  271.     printf("Please enter a floating point number or 0 to quit testing: ");
  272.     fgets(text,TEXT_LEN,stdin);
  273.     f = atof(text);
  274.  
  275.     printf("The sign     of %g is %+d\n",f,getSign(f));
  276.     printf("The exponent of %g is 2^%d\n",f,getPowerOf2(f));
  277.     printf("The mantissa of %g is 0x%06X\n",f,getMantissa(f));
  278.   }
  279.   while  ( !isZero(f) );
  280.  
  281.   printf("\n\n");
  282.  
  283.   do
  284.   {
  285.     printf("Please enter the 1st floating point number to add: ");
  286.     fgets(text,TEXT_LEN,stdin);
  287.     f = atof(text);
  288.  
  289.     printf("Please enter the 2nd floating point number to add: ");
  290.     fgets(text,TEXT_LEN,stdin);
  291.     g = atof(text);
  292.  
  293.     printf("         You say  %g + %g == %g\n",f,g,add(f,g));
  294.     printf("The hardware says %g + %g == %g\n",f,g,f+g);
  295.   }
  296.   while  ( !isZero(f) && !isZero(g) );
  297.  
  298.   return(EXIT_SUCCESS);
  299. }
Advertisement
Add Comment
Please, Sign In to add comment