1
0

float.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <u.h>
  2. #include <lib9.h>
  3. #define DPRECSTR "0.0000004000000000125"
  4. #define DPREC 0.0000004000000000125
  5. #define DIEEELO 0x9ac0499f
  6. #define DIEEEHI 0x3e9ad7f2
  7. jmp_buf errj;
  8. char *err;
  9. void
  10. catcher(void *u, char *s)
  11. {
  12. err = 0;
  13. if(strncmp(s, "sys: fp:", 8) == 0){
  14. err = s;
  15. notejmp(u, errj, 0);
  16. }
  17. sys_noted(NDFLT);
  18. }
  19. void
  20. tstdiv(double p)
  21. {
  22. double r = 1.0;
  23. r /= p;
  24. fprint(2, "1/%0.20g = %0.20g\n", p, r);
  25. }
  26. typedef union I2UL
  27. {
  28. int d[2];
  29. unsigned long l;
  30. }I2UL;
  31. typedef union D2UL
  32. {
  33. double d;
  34. unsigned long l;
  35. }D2UL;
  36. void
  37. main(void)
  38. {
  39. double p = DPREC;
  40. int d[2] = { DIEEELO, DIEEEHI };
  41. I2UL i2l;
  42. D2UL d2l;
  43. uint64_t dieee, q;
  44. i2l.d[0] = d[0];
  45. i2l.d[1] = d[1];
  46. dieee = i2l.l;
  47. d2l.d = p;
  48. q = d2l.l;
  49. err = 0;
  50. sys_notify(catcher);
  51. setjmp(errj);
  52. if(err){
  53. fprint(2, "FAIL: %s\n", err);
  54. exits("FAIL");
  55. }
  56. fprint(2, "Double-precision test number: %s\n", DPRECSTR);
  57. fprint(2, "Expected internal representation: %ullx\n", dieee);
  58. fprint(2, "Actual internal representation: %ullx\n", q);
  59. if(q != dieee) {
  60. print("FAIL\n");
  61. exits("FAIL");
  62. }
  63. tstdiv(p);
  64. print("PASS\n");
  65. exits("PASS");
  66. }