Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- d2s.c
- Trevor Lounsbury Fall 2017
- This Program:
- - convert double-precision floating point to single-precision floating point
- by only using integer operations
- - there should not be any declarations of float or double in this file!
- - 90 / 100... No
- */
- #include <stdio.h>
- #include <stdint.h>
- /**
- * just checks for a negitive value and returns the output to be used
- * for the final value
- * @param sign [The input sign 1 = neg , 0 = pos]
- * @return [single presision float to be used in the output]
- */
- static uint32_t negitiveCheck(uint64_t sign)
- {
- uint32_t o;
- if(sign == 1)
- {
- o = 0x80000000;
- }
- else
- {
- o = 0x00000000;
- }
- return o;
- }
- /**
- * Takes in the exponent hex and determine's edge cases...
- * : -/+ Zero
- * : -/+ Infinites
- * : NaN's
- * @param exp = Local variable of the exponent
- * @param sign = sign of the current value.
- * @param fraction = the mantisa of the input number.
- */
- static uint32_t checkExp(uint64_t sign , uint64_t exp , uint64_t fraction)
- {
- // Zero
- if ( exp == 0x000 && fraction == 0x0 )
- {
- return negitiveCheck(sign);
- }
- else if( exp == 0x7FF && fraction == 0x0)
- {
- //negitive infinity
- if ( sign == 1 )
- {
- return 0xFF800000;
- }
- //Positive infinity
- else
- {
- return 0x7F800000;
- }
- }
- else
- return 0x0;
- }
- /**
- * checks input for a NaN value
- * @param exp input exponent
- * @param fraction input fraction
- * @param sign sign of input (1|0)
- * @return returns either final value | 0xa if input is not a nan
- */
- static uint64_t nanCheck(uint64_t exp, uint64_t fraction, uint64_t sign)
- {
- //If true NaN with singling non-zero payload
- if ( exp == 0x7FF && fraction != 0x0 )
- {
- uint64_t output = 0x7FC00000;
- output |= (fraction >> 29);
- return output;
- }
- else if ( exp == 0x0 && fraction != 0x0)
- {
- int i = 23;
- //normilizing the number
- uint64_t newExp = 1023 - (126 + i);
- //'or'ing proper sign to the new exponent;
- //printf("nan case 2\n");
- return negitiveCheck(sign) | (newExp<<52);
- }
- else
- {
- //0xa represents that the value is not a nan
- return 0xa;
- }
- }
- /**
- * Gets the final output value using converison techniques from class
- * @param sign [the sign of the original input value]
- * @param exp [the exponent of the original input value]
- * @param fraction [the fraction of the original input value]
- * @return [returns a single precision floating point number]
- */
- static uint32_t finalOutput(uint64_t sign , uint64_t exp , uint64_t fraction)
- {
- //Checking if the input is a NaN
- if ( nanCheck(exp,fraction,sign) != 0xa)
- {
- return nanCheck(exp,fraction,sign);
- }
- uint32_t output = negitiveCheck(sign);
- uint32_t expChop = exp >> 3;
- output = output | (expChop) << 23;
- return output;
- }
- // this is just a stub!
- uint32_t d2s(uint64_t in)
- {
- uint64_t sign = in >> 63;
- uint64_t exponent = (in >> 52) & 0x7FF;
- uint64_t fraction = in & 0xFFFFFFFFFFFFF;
- //uint64_t exponentActual = exponent - 0x3FF;
- // printf( "Input : %lu\n", in );
- // printf( "Sign : %lu\n", sign );
- // printf( "Exponent : %x\n", exponent );
- // printf( "exponentActual : %lu\n", exponentActual );
- // printf( "Fraction : %lu\n", fraction );
- if(checkExp(sign, exponent, fraction) != 0)
- {
- //checking for edge cases and returning the proper cases
- return checkExp(sign, exponent, fraction);
- }
- else
- {
- //If all cases have been delt with then go through and convert
- return finalOutput(sign, exponent, fraction);
- }
- return 0;
- }
- =================================================----------------------
- /*
- s2d.c
- Trevor Lounsbury Fall 2017
- This Program:
- - convert single-precision floating point to double-precision floating point
- by only using integer operations
- - there should not be any declarations of float or double in this file!
- */
- #include <stdio.h>
- #include <stdint.h>
- static uint64_t negitiveCheck(uint64_t sign);
- static int amountToShift( uint64_t fraction);
- static uint64_t nanCheck(uint64_t exp, uint64_t fraction, uint64_t sign);
- static uint64_t checkExp(uint64_t sign , uint64_t exp , uint64_t fraction);
- static uint64_t finalOutput( uint64_t sign, uint64_t exp, uint64_t fraction);
- /**
- * just checks for a negitive value and returns the output to be used
- * for the final value
- * @param sign [The input sign 1 = neg , 0 = pos]
- * @return [single presision float to be used in the output]
- */
- static uint64_t negitiveCheck(uint64_t sign)
- {
- uint64_t o;
- if(sign == 1)
- {
- o = 0x8000000000000000;
- }
- else
- {
- o = 0x0000000000000000;
- }
- return o;
- }
- /**
- * Finds the location of first bit coming from left to right
- * @return The amount needed to shift to normilze the input
- */
- static int amountToShift( uint64_t fraction)
- {
- // i =23 due to it being the max size of the fraction
- int i = 23;
- while(fraction >= 0x1)
- {
- if (fraction == 0x1)
- {
- //return amount of shifts needed
- return i;
- }
- else
- {
- //shift everything to the right
- fraction /= 2;
- i--;
- }
- }
- //Should never reach but if an error occurs will return -1;
- return -1;
- }
- /**
- * checks input for a NaN value
- * @param exp input exponent
- * @param fraction input fraction
- * @param sign sign of input (1|0)
- * @return returns either final value | 0xa if input is not a nan
- */
- static uint64_t nanCheck(uint64_t exp, uint64_t fraction, uint64_t sign)
- {
- //If true NaN with non-zero payload
- if ( exp == 0xFF && fraction != 0x0 )
- {
- uint64_t output = 0x7ff8000000000000;
- output |= (fraction << 29);
- return output;
- }
- //Denormalzed
- else if ( exp == 0x0 && fraction != 0x0)
- {
- //printf("%lu\n", fraction);
- int i = amountToShift(fraction);
- //normilizing the number
- uint64_t newExp = 1023 - (126 + i);
- //'or'ing proper sign to the new exponent;
- return negitiveCheck(sign) | (newExp<<52);
- }
- else
- {
- //0xa represents that the value is not a nan
- return 0xa;
- }
- }
- /**
- * Takes in the exponent hex and determine's edge cases...
- * : -/+ Zero
- * : -/+ Infinites
- * : NaN's
- * @param exp = Local variable of the exponent
- * @param sign = sign of the current value.
- * @param fraction = the mantisa of the input number.
- */
- static uint64_t checkExp(uint64_t sign , uint64_t exp , uint64_t fraction)
- {
- // Zero
- if ( exp == 0x0 && fraction == 0x0 )
- {
- return negitiveCheck(sign);
- }
- else if( exp == 0xFF && fraction == 0x0)
- {
- //negitive infinity
- if ( sign == 1 )
- {
- return 0xfff0000000000000;
- }
- //Positive infinity
- else
- {
- return 0x7ff0000000000000;
- }
- }
- else
- {
- //0xa represents that the value is not a nan
- return 0xa;
- }
- }
- /**
- * Gets the final output value using converison techniques from class
- * @param sign [the sign of the original input value]
- * @param exp [the exponent of the original input value]
- * @param fraction [the fraction of the original input value]
- * @return [returns a single precision floating point number]
- */
- static uint64_t finalOutput( uint64_t sign, uint64_t exp, uint64_t fraction)
- {
- uint64_t output = negitiveCheck(sign);
- //Checking if the input is a NaN
- if ( nanCheck(exp,fraction,sign) != 0xa)
- {
- return output | nanCheck(exp,fraction,sign);
- }
- //dealing with 1's case
- //shifting is differnet for this value
- if (exp == 0x7F)
- {
- exp = 0x3FF;
- output = output | ((exp)<<52);
- output |= (fraction <<29);
- }
- else
- {
- //adding the 10 at the front of the exponent;
- exp = exp - 127;
- exp += 1023;
- exp = exp << 52;
- output |= exp;
- output |= (fraction <<29);
- }
- return output;
- }
- // this is just a stub!
- int s2d(uint32_t in)
- {
- // Alright so whats the plan of attack?
- // first turn it into a actual int
- uint64_t sign = in >> 31;
- uint64_t exponent = (in >> 23) & 0xff;
- uint64_t fraction = in & 0x7FFFFF;
- // printf("sign = %lu\n", sign );
- // printf("exponent = %lu\n", exponent );
- // printf("fraction = %lu\n", fraction );
- //if checkExp != 0 then the value is ( +/- 0 | +/- infinity )
- if( checkExp(sign, exponent, fraction) != 0xa )
- {
- return checkExp(sign, exponent, fraction);
- }
- else
- {
- return finalOutput(sign,exponent, fraction);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement