extend.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. CONVERTS FLOATING POINT TO EXTENDED FORMAT
  8. Two sizes of FLOATING Point are known:
  9. SINGLE and DOUBLE
  10. */
  11. /********************************************************/
  12. /*
  13. It is not required to normalize in extended
  14. format, but it has been chosen to do so.
  15. Extended Format is as follows (at exit):
  16. ->sign S000 0000 | 0000 0000 <SIGN>
  17. ->exp 0EEE EEEE | EEEE EEEE <EXPONENT>
  18. ->m1 LFFF FFFF | FFFF FFFF <L.Fraction>
  19. FFFF FFFF | FFFF FFFF <Fraction>
  20. ->m2 FFFF FFFF | FFFF FFFF <Fraction>
  21. FFFF F000 | 0000 0000 <Fraction>
  22. */
  23. /********************************************************/
  24. #include "FP_bias.h"
  25. #include "FP_shift.h"
  26. #include "FP_types.h"
  27. #include "get_put.h"
  28. /********************************************************/
  29. void
  30. extend(from,to,size)
  31. unsigned long *from;
  32. EXTEND *to;
  33. int size;
  34. {
  35. register char *cpt1;
  36. unsigned long tmp;
  37. int leadbit = 0;
  38. cpt1 = (char *) from;
  39. #if FL_MSL_AT_LOW_ADDRESS
  40. #if FL_MSW_AT_LOW_ADDRESS
  41. to->exp = uget2(cpt1);
  42. #else
  43. to->exp = uget2(cpt1+2);
  44. #endif
  45. #else
  46. #if FL_MSW_AT_LOW_ADDRESS
  47. to->exp = uget2(cpt1+(size == sizeof(DOUBLE) ? 4 : 0));
  48. #else
  49. to->exp = uget2(cpt1+(size == sizeof(DOUBLE) ? 6 : 2));
  50. #endif
  51. #endif
  52. to->sign = (to->exp & 0x8000); /* set sign bit */
  53. to->exp ^= to->sign;
  54. if (size == sizeof(DOUBLE))
  55. to->exp >>= DBL_EXPSHIFT;
  56. else
  57. to->exp >>= SGL_EXPSHIFT;
  58. if (to->exp > 0)
  59. leadbit++; /* will set Lead bit later */
  60. else to->exp++;
  61. if (size == sizeof(DOUBLE)) {
  62. #if FL_MSL_AT_LOW_ADDRESS
  63. to->m1 = get4(cpt1);
  64. cpt1 += 4;
  65. tmp = get4(cpt1);
  66. #else
  67. tmp = get4(cpt1);
  68. cpt1 += 4;
  69. to->m1 = get4(cpt1);
  70. #endif
  71. if (to->exp == 1 && to->m1 == 0 && tmp == 0) {
  72. to->exp = 0;
  73. to->sign = 0;
  74. to->m1 = 0;
  75. to->m2 = 0;
  76. return;
  77. }
  78. to->m1 <<= DBL_M1LEFT; /* shift */
  79. to->exp -= DBL_BIAS; /* remove bias */
  80. to->m1 |= (tmp>>DBL_RPACK); /* plus 10 == 32 */
  81. to->m2 = (tmp<<DBL_LPACK); /* plus 22 == 32 */
  82. }
  83. else { /* size == sizeof(SINGLE) */
  84. to->m1 = get4(cpt1);
  85. to->m1 <<= SGL_M1LEFT; /* shift */
  86. if (to->exp == 1 && to->m1 == 0) {
  87. to->exp = 0;
  88. to->sign = 0;
  89. to->m1 = 0;
  90. to->m2 = 0;
  91. return;
  92. }
  93. to->exp -= SGL_BIAS; /* remove bias */
  94. to->m2 = 0L;
  95. }
  96. to->m1 |= NORMBIT; /* set bit L */
  97. if (leadbit == 0) { /* set or clear Leading Bit */
  98. to->m1 &= ~NORMBIT; /* clear bit L */
  99. nrm_ext(to); /* and normalize */
  100. }
  101. }