Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.55 KB | None | 0 0
  1. /*
  2. d2s.c
  3. Trevor Lounsbury Fall 2017
  4.  
  5. This Program:
  6. - convert double-precision floating point to single-precision floating point
  7. by only using integer operations
  8. - there should not be any declarations of float or double in this file!
  9. - 90 / 100... No
  10. */
  11. #include <stdio.h>
  12. #include <stdint.h>
  13.  
  14. /**
  15. * just checks for a negitive value and returns the output to be used
  16. * for the final value
  17. * @param sign [The input sign 1 = neg , 0 = pos]
  18. * @return [single presision float to be used in the output]
  19. */
  20. static uint32_t negitiveCheck(uint64_t sign)
  21. {
  22. uint32_t o;
  23. if(sign == 1)
  24. {
  25. o = 0x80000000;
  26. }
  27. else
  28. {
  29. o = 0x00000000;
  30. }
  31. return o;
  32. }
  33.  
  34. /**
  35. * Takes in the exponent hex and determine's edge cases...
  36. * : -/+ Zero
  37. * : -/+ Infinites
  38. * : NaN's
  39. * @param exp = Local variable of the exponent
  40. * @param sign = sign of the current value.
  41. * @param fraction = the mantisa of the input number.
  42. */
  43. static uint32_t checkExp(uint64_t sign , uint64_t exp , uint64_t fraction)
  44. {
  45. // Zero
  46. if ( exp == 0x000 && fraction == 0x0 )
  47. {
  48. return negitiveCheck(sign);
  49. }
  50. else if( exp == 0x7FF && fraction == 0x0)
  51. {
  52. //negitive infinity
  53. if ( sign == 1 )
  54. {
  55. return 0xFF800000;
  56. }
  57. //Positive infinity
  58. else
  59. {
  60. return 0x7F800000;
  61. }
  62. }
  63. else
  64. return 0x0;
  65. }
  66.  
  67. /**
  68. * checks input for a NaN value
  69. * @param exp input exponent
  70. * @param fraction input fraction
  71. * @param sign sign of input (1|0)
  72. * @return returns either final value | 0xa if input is not a nan
  73. */
  74. static uint64_t nanCheck(uint64_t exp, uint64_t fraction, uint64_t sign)
  75. {
  76. //If true NaN with singling non-zero payload
  77. if ( exp == 0x7FF && fraction != 0x0 )
  78. {
  79. uint64_t output = 0x7FC00000;
  80. output |= (fraction >> 29);
  81. return output;
  82. }
  83. else if ( exp == 0x0 && fraction != 0x0)
  84. {
  85. int i = 23;
  86. //normilizing the number
  87. uint64_t newExp = 1023 - (126 + i);
  88.  
  89. //'or'ing proper sign to the new exponent;
  90. //printf("nan case 2\n");
  91. return negitiveCheck(sign) | (newExp<<52);
  92.  
  93. }
  94. else
  95. {
  96. //0xa represents that the value is not a nan
  97. return 0xa;
  98. }
  99. }
  100.  
  101. /**
  102. * Gets the final output value using converison techniques from class
  103. * @param sign [the sign of the original input value]
  104. * @param exp [the exponent of the original input value]
  105. * @param fraction [the fraction of the original input value]
  106. * @return [returns a single precision floating point number]
  107. */
  108. static uint32_t finalOutput(uint64_t sign , uint64_t exp , uint64_t fraction)
  109. {
  110. //Checking if the input is a NaN
  111. if ( nanCheck(exp,fraction,sign) != 0xa)
  112. {
  113. return nanCheck(exp,fraction,sign);
  114. }
  115.  
  116. uint32_t output = negitiveCheck(sign);
  117.  
  118. uint32_t expChop = exp >> 3;
  119. output = output | (expChop) << 23;
  120.  
  121. return output;
  122. }
  123.  
  124. // this is just a stub!
  125. uint32_t d2s(uint64_t in)
  126. {
  127. uint64_t sign = in >> 63;
  128. uint64_t exponent = (in >> 52) & 0x7FF;
  129. uint64_t fraction = in & 0xFFFFFFFFFFFFF;
  130. //uint64_t exponentActual = exponent - 0x3FF;
  131.  
  132. // printf( "Input : %lu\n", in );
  133. // printf( "Sign : %lu\n", sign );
  134. // printf( "Exponent : %x\n", exponent );
  135. // printf( "exponentActual : %lu\n", exponentActual );
  136. // printf( "Fraction : %lu\n", fraction );
  137.  
  138. if(checkExp(sign, exponent, fraction) != 0)
  139. {
  140. //checking for edge cases and returning the proper cases
  141. return checkExp(sign, exponent, fraction);
  142. }
  143. else
  144. {
  145. //If all cases have been delt with then go through and convert
  146. return finalOutput(sign, exponent, fraction);
  147. }
  148.  
  149. return 0;
  150. }
  151. =================================================----------------------
  152.  
  153. /*
  154. s2d.c
  155. Trevor Lounsbury Fall 2017
  156.  
  157. This Program:
  158. - convert single-precision floating point to double-precision floating point
  159. by only using integer operations
  160. - there should not be any declarations of float or double in this file!
  161. */
  162. #include <stdio.h>
  163. #include <stdint.h>
  164.  
  165. static uint64_t negitiveCheck(uint64_t sign);
  166. static int amountToShift( uint64_t fraction);
  167. static uint64_t nanCheck(uint64_t exp, uint64_t fraction, uint64_t sign);
  168. static uint64_t checkExp(uint64_t sign , uint64_t exp , uint64_t fraction);
  169. static uint64_t finalOutput( uint64_t sign, uint64_t exp, uint64_t fraction);
  170.  
  171.  
  172. /**
  173. * just checks for a negitive value and returns the output to be used
  174. * for the final value
  175. * @param sign [The input sign 1 = neg , 0 = pos]
  176. * @return [single presision float to be used in the output]
  177. */
  178. static uint64_t negitiveCheck(uint64_t sign)
  179. {
  180. uint64_t o;
  181. if(sign == 1)
  182. {
  183. o = 0x8000000000000000;
  184. }
  185. else
  186. {
  187. o = 0x0000000000000000;
  188. }
  189. return o;
  190. }
  191.  
  192. /**
  193. * Finds the location of first bit coming from left to right
  194. * @return The amount needed to shift to normilze the input
  195. */
  196. static int amountToShift( uint64_t fraction)
  197. {
  198. // i =23 due to it being the max size of the fraction
  199. int i = 23;
  200. while(fraction >= 0x1)
  201. {
  202. if (fraction == 0x1)
  203. {
  204. //return amount of shifts needed
  205. return i;
  206. }
  207. else
  208. {
  209. //shift everything to the right
  210. fraction /= 2;
  211. i--;
  212. }
  213. }
  214. //Should never reach but if an error occurs will return -1;
  215. return -1;
  216. }
  217.  
  218. /**
  219. * checks input for a NaN value
  220. * @param exp input exponent
  221. * @param fraction input fraction
  222. * @param sign sign of input (1|0)
  223. * @return returns either final value | 0xa if input is not a nan
  224. */
  225. static uint64_t nanCheck(uint64_t exp, uint64_t fraction, uint64_t sign)
  226. {
  227. //If true NaN with non-zero payload
  228. if ( exp == 0xFF && fraction != 0x0 )
  229. {
  230. uint64_t output = 0x7ff8000000000000;
  231. output |= (fraction << 29);
  232. return output;
  233. }
  234. //Denormalzed
  235. else if ( exp == 0x0 && fraction != 0x0)
  236. {
  237. //printf("%lu\n", fraction);
  238. int i = amountToShift(fraction);
  239. //normilizing the number
  240. uint64_t newExp = 1023 - (126 + i);
  241.  
  242. //'or'ing proper sign to the new exponent;
  243. return negitiveCheck(sign) | (newExp<<52);
  244. }
  245. else
  246. {
  247. //0xa represents that the value is not a nan
  248. return 0xa;
  249. }
  250. }
  251.  
  252. /**
  253. * Takes in the exponent hex and determine's edge cases...
  254. * : -/+ Zero
  255. * : -/+ Infinites
  256. * : NaN's
  257. * @param exp = Local variable of the exponent
  258. * @param sign = sign of the current value.
  259. * @param fraction = the mantisa of the input number.
  260. */
  261. static uint64_t checkExp(uint64_t sign , uint64_t exp , uint64_t fraction)
  262. {
  263. // Zero
  264. if ( exp == 0x0 && fraction == 0x0 )
  265. {
  266. return negitiveCheck(sign);
  267. }
  268. else if( exp == 0xFF && fraction == 0x0)
  269. {
  270. //negitive infinity
  271. if ( sign == 1 )
  272. {
  273. return 0xfff0000000000000;
  274. }
  275. //Positive infinity
  276. else
  277. {
  278. return 0x7ff0000000000000;
  279. }
  280. }
  281. else
  282. {
  283. //0xa represents that the value is not a nan
  284. return 0xa;
  285. }
  286. }
  287.  
  288.  
  289. /**
  290. * Gets the final output value using converison techniques from class
  291. * @param sign [the sign of the original input value]
  292. * @param exp [the exponent of the original input value]
  293. * @param fraction [the fraction of the original input value]
  294. * @return [returns a single precision floating point number]
  295. */
  296. static uint64_t finalOutput( uint64_t sign, uint64_t exp, uint64_t fraction)
  297. {
  298. uint64_t output = negitiveCheck(sign);
  299.  
  300. //Checking if the input is a NaN
  301. if ( nanCheck(exp,fraction,sign) != 0xa)
  302. {
  303. return output | nanCheck(exp,fraction,sign);
  304. }
  305. //dealing with 1's case
  306. //shifting is differnet for this value
  307. if (exp == 0x7F)
  308. {
  309. exp = 0x3FF;
  310. output = output | ((exp)<<52);
  311. output |= (fraction <<29);
  312. }
  313. else
  314. {
  315. //adding the 10 at the front of the exponent;
  316. exp = exp - 127;
  317. exp += 1023;
  318. exp = exp << 52;
  319. output |= exp;
  320. output |= (fraction <<29);
  321. }
  322. return output;
  323. }
  324.  
  325.  
  326. // this is just a stub!
  327. int s2d(uint32_t in)
  328. {
  329. // Alright so whats the plan of attack?
  330. // first turn it into a actual int
  331. uint64_t sign = in >> 31;
  332. uint64_t exponent = (in >> 23) & 0xff;
  333. uint64_t fraction = in & 0x7FFFFF;
  334.  
  335. // printf("sign = %lu\n", sign );
  336. // printf("exponent = %lu\n", exponent );
  337. // printf("fraction = %lu\n", fraction );
  338.  
  339. //if checkExp != 0 then the value is ( +/- 0 | +/- infinity )
  340. if( checkExp(sign, exponent, fraction) != 0xa )
  341. {
  342. return checkExp(sign, exponent, fraction);
  343. }
  344. else
  345. {
  346. return finalOutput(sign,exponent, fraction);
  347. }
  348.  
  349. return 0;
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement