cfi.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Id$ */
  6. /*
  7. CONVERT FLOAT TO SIGNED (CFI m n)
  8. N.B. The caller must know what it is getting.
  9. A LONG is always returned. If it is an
  10. integer the high byte is cleared first.
  11. */
  12. #include "FP_trap.h"
  13. #include "FP_types.h"
  14. #include "FP_shift.h"
  15. long
  16. cfi(ds,ss,src)
  17. int ds; /* destination size (2 or 4) */
  18. int ss; /* source size (4 or 8) */
  19. DOUBLE src; /* assume worst case */
  20. {
  21. EXTEND buf;
  22. long new;
  23. short max_exp;
  24. extend(&src.d[0],&buf,ss); /* get extended format */
  25. if (buf.exp < 0) { /* no conversion needed */
  26. src.d[ss == 8] = 0L;
  27. return(0L);
  28. }
  29. max_exp = (ds << 3) - 2; /* signed numbers */
  30. /* have more limited max_exp */
  31. if (buf.exp > max_exp) {
  32. if (buf.exp == max_exp+1 && buf.sign && buf.m1 == NORMBIT &&
  33. buf.m2 == 0L) {
  34. }
  35. else {
  36. trap(EIOVFL); /* integer overflow */
  37. buf.exp %= max_exp; /* truncate */
  38. }
  39. }
  40. new = buf.m1 >> (31-buf.exp);
  41. if (buf.sign)
  42. new = -new;
  43. done:
  44. src.d[ss == 8] = new;
  45. return(new);
  46. }