Advertisement
Guest User

Untitled

a guest
May 5th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /* double, double -> double
  4. * function fpu_add adds 2 double FP numbers
  5. */
  6. double fpu_add(double a, double b);
  7. double fpu_div(double a, double b);
  8.  
  9. /* -> unsigned short (2B)
  10. * function get_fpu reads the FPU control word and returns it's value
  11. */
  12. unsigned short get_fpu();
  13.  
  14. void set_fpu(unsigned short int);
  15.  
  16. /*
  17. *
  18. */
  19. int main(int argc, char * argv[])
  20. {
  21. unsigned short int fpucw = 0x1234;
  22. double dbls[3] = {123.45, 0.123, 0.0};
  23. double dblx[3] = {123.45, 0.0, 0.0};
  24. /* generate double constants */
  25. unsigned long dbl = 0x4000000000000000;
  26. double * wsk = &dbl;
  27. //dbls[1] = *wsk;
  28. printf("unsigned long: %lx\n", dbl);
  29. printf("unsigned long: %lf\n", *wsk);
  30.  
  31. /* add two numbers */
  32. dbls[2] = fpu_add(dbls[0], dbls[1]);
  33. printf("default: %lf + %lf = %lf\n", dbls[0], dbls[1], dbls[2]);
  34. fpucw = get_fpu();
  35.  
  36. set_fpu(0xc7f); //changing precission
  37. /* add the same numbers as earlier to see difference */
  38. dbls[2] = fpu_add(dbls[0], dbls[1]);
  39. printf("default: %lf + %lf = %lf\n", dbls[0], dbls[1], dbls[2]);
  40. fpucw = get_fpu();
  41.  
  42. set_fpu(0x7f); //changing exceptions
  43. /* add the same numbers as earlier to see difference */
  44. dblx[2] = fpu_div(dblx[0], dblx[1]);
  45. printf("dividing: %lf / %lf = %lf\n", dblx[0], dblx[1], dblx[2]);
  46.  
  47. /* print FPU control word */
  48. fpucw = get_fpu();
  49. printf("FPU CW: %04hx\n", fpucw);
  50.  
  51. /* Return 0 if exiting normally.
  52. */
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement