fpimem.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "fpi.h"
  2. /*
  3. * the following routines depend on memory format, not the machine
  4. */
  5. void
  6. fpis2i(Internal *i, void *v)
  7. {
  8. Single *s = v;
  9. i->s = (*s & 0x80000000) ? 1: 0;
  10. if((*s & ~0x80000000) == 0){
  11. SetZero(i);
  12. return;
  13. }
  14. i->e = ((*s>>23) & 0x00FF) - SingleExpBias + ExpBias;
  15. i->h = (*s & 0x007FFFFF)<<(1+NGuardBits);
  16. i->l = 0;
  17. if(i->e)
  18. i->h |= HiddenBit;
  19. else
  20. i->e++;
  21. }
  22. void
  23. fpid2i(Internal *i, void *v)
  24. {
  25. Double *d = v;
  26. i->s = (d->h & 0x80000000) ? 1: 0;
  27. i->e = (d->h>>20) & 0x07FF;
  28. i->h = ((d->h & 0x000FFFFF)<<(4+NGuardBits))|((d->l>>25) & 0x7F);
  29. i->l = (d->l & 0x01FFFFFF)<<NGuardBits;
  30. if(i->e)
  31. i->h |= HiddenBit;
  32. else
  33. i->e++;
  34. }
  35. void
  36. fpiw2i(Internal *i, void *v)
  37. {
  38. Word w, word = *(Word*)v;
  39. short e;
  40. if(word < 0){
  41. i->s = 1;
  42. word = -word;
  43. }
  44. else
  45. i->s = 0;
  46. if(word == 0){
  47. SetZero(i);
  48. return;
  49. }
  50. if(word > 0){
  51. for (e = 0, w = word; w; w >>= 1, e++)
  52. ;
  53. } else
  54. e = 32;
  55. if(e > FractBits){
  56. i->h = word>>(e - FractBits);
  57. i->l = (word & ((1<<(e - FractBits)) - 1))<<(2*FractBits - e);
  58. }
  59. else {
  60. i->h = word<<(FractBits - e);
  61. i->l = 0;
  62. }
  63. i->e = (e - 1) + ExpBias;
  64. }
  65. void
  66. fpii2s(void *v, Internal *i)
  67. {
  68. short e;
  69. Single *s = (Single*)v;
  70. fpiround(i);
  71. if(i->h & HiddenBit)
  72. i->h &= ~HiddenBit;
  73. else
  74. i->e--;
  75. *s = i->s ? 0x80000000: 0;
  76. e = i->e;
  77. if(e < ExpBias){
  78. if(e <= (ExpBias - SingleExpBias))
  79. return;
  80. e = SingleExpBias - (ExpBias - e);
  81. }
  82. else if(e >= (ExpBias + (SingleExpMax-SingleExpBias))){
  83. *s |= SingleExpMax<<23;
  84. return;
  85. }
  86. else
  87. e = SingleExpBias + (e - ExpBias);
  88. *s |= (e<<23)|(i->h>>(1+NGuardBits));
  89. }
  90. void
  91. fpii2d(void *v, Internal *i)
  92. {
  93. Double *d = (Double*)v;
  94. fpiround(i);
  95. if(i->h & HiddenBit)
  96. i->h &= ~HiddenBit;
  97. else
  98. i->e--;
  99. i->l = ((i->h & GuardMask)<<25)|(i->l>>NGuardBits);
  100. i->h >>= NGuardBits;
  101. d->h = i->s ? 0x80000000: 0;
  102. d->h |= (i->e<<20)|((i->h & 0x00FFFFFF)>>4);
  103. d->l = (i->h<<28)|i->l;
  104. }
  105. void
  106. fpii2w(Word *word, Internal *i)
  107. {
  108. Word w;
  109. short e;
  110. fpiround(i);
  111. e = (i->e - ExpBias) + 1;
  112. if(e <= 0)
  113. w = 0;
  114. else if(e > 31)
  115. w = 0x7FFFFFFF;
  116. else if(e > FractBits)
  117. w = (i->h<<(e - FractBits))|(i->l>>(2*FractBits - e));
  118. else
  119. w = i->h>>(FractBits-e);
  120. if(i->s)
  121. w = -w;
  122. *word = w;
  123. }