con_float 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. #define CODE_GENERATOR for code generator
  8. #define CODE_EXPANDER for code expander
  9. #define IEEEFLOAT for machines using IEEE floating point format
  10. #define PDPFLOAT for machines using the PDP-11 floating point format
  11. If none of these are defined, the format of the machine on which the
  12. code generator runs is used.
  13. Returns 1 if sz has an illegal value, 2 in case of overflow,
  14. and 0 if all went well.
  15. If neither IEEEFLOAT nor PDPFLOAT are defined, the return value is not
  16. trustworthy.
  17. Unfortunately, the IEEE standard does not define the byte-order.
  18. depends on the #defines
  19. FL_MSL_AT_LOW_ADDRESS 1 if most significant long is at low address
  20. FL_MSW_AT_LOW_ADDRESS 1 if most significant word is at low address
  21. FL_MSB_AT_LOW_ADDRESS 1 if most significant byte is at low address
  22. */
  23. #ifdef IEEEFLOAT
  24. #define USE_FLT
  25. #endif
  26. #ifdef PDPFLOAT
  27. #define USE_FLT
  28. #undef FL_MSL_AT_LOW_ADDRESS
  29. #define FL_MSL_AT_LOW_ADDRESS 1
  30. #undef FL_MSW_AT_LOW_ADDRESS
  31. #define FL_MSW_AT_LOW_ADDRESS 1
  32. #undef FL_MSB_AT_LOW_ADDRESS
  33. #define FL_MSB_AT_LOW_ADDRESS 0
  34. #endif
  35. #define I0 ((FL_MSL_AT_LOW_ADDRESS ? 0 : 4) + (FL_MSW_AT_LOW_ADDRESS ? 0 : 2) \
  36. + (FL_MSB_AT_LOW_ADDRESS ? 0 : 1))
  37. #define I1 ((FL_MSL_AT_LOW_ADDRESS ? 0 : 4) + (FL_MSW_AT_LOW_ADDRESS ? 0 : 2) \
  38. + (FL_MSB_AT_LOW_ADDRESS ? 1 : 0))
  39. #define I2 ((FL_MSL_AT_LOW_ADDRESS ? 0 : 4) + (FL_MSW_AT_LOW_ADDRESS ? 2 : 0) \
  40. + (FL_MSB_AT_LOW_ADDRESS ? 0 : 1))
  41. #define I3 ((FL_MSL_AT_LOW_ADDRESS ? 0 : 4) + (FL_MSW_AT_LOW_ADDRESS ? 2 : 0) \
  42. + (FL_MSB_AT_LOW_ADDRESS ? 1 : 0))
  43. #define I4 ((FL_MSL_AT_LOW_ADDRESS ? 4 : 0) + (FL_MSW_AT_LOW_ADDRESS ? 0 : 2) \
  44. + (FL_MSB_AT_LOW_ADDRESS ? 0 : 1))
  45. #define I5 ((FL_MSL_AT_LOW_ADDRESS ? 4 : 0) + (FL_MSW_AT_LOW_ADDRESS ? 0 : 2) \
  46. + (FL_MSB_AT_LOW_ADDRESS ? 1 : 0))
  47. #define I6 ((FL_MSL_AT_LOW_ADDRESS ? 4 : 0) + (FL_MSW_AT_LOW_ADDRESS ? 2 : 0) \
  48. + (FL_MSB_AT_LOW_ADDRESS ? 0 : 1))
  49. #define I7 ((FL_MSL_AT_LOW_ADDRESS ? 4 : 0) + (FL_MSW_AT_LOW_ADDRESS ? 2 : 0) \
  50. + (FL_MSB_AT_LOW_ADDRESS ? 1 : 0))
  51. #ifndef USE_FLT
  52. static int
  53. float_cst(str, sz, buf)
  54. char *str, *buf;
  55. int sz;
  56. {
  57. int i;
  58. char *p;
  59. float fl;
  60. double f;
  61. double atof();
  62. if (sz!= 4 && sz!= 8) {
  63. return 1;
  64. }
  65. f = atof(str);
  66. if (sz == 4) {
  67. fl = f;
  68. p = (char *) &fl;
  69. }
  70. else {
  71. p = (char *) &f;
  72. }
  73. for (i = sz; i; i--) {
  74. *buf++ = *p++;
  75. }
  76. return 0;
  77. }
  78. #else /* USE_FLT */
  79. #include <ctype.h>
  80. #include <flt_arith.h>
  81. int
  82. float_cst(str, sz, buf)
  83. char *str, *buf;
  84. int sz;
  85. {
  86. int overflow = 0;
  87. flt_arith e;
  88. if (sz!= 4 && sz!= 8) {
  89. return 1;
  90. }
  91. flt_str2flt(str, &e);
  92. #ifdef IEEEFLOAT
  93. if (sz == 4) {
  94. #endif
  95. #ifdef PDPFLOAT
  96. e.flt_exp += 129;
  97. #else
  98. e.flt_exp += 127;
  99. #endif
  100. if (e.flt_mantissa.flt_h_32 == 0) e.flt_exp = 0;
  101. #ifdef IEEEFLOAT
  102. if (e.flt_mantissa.flt_h_32 & 0x80) {
  103. /* rounding */
  104. if ((e.flt_mantissa.flt_h_32 & 0xffffff00) == 0xffffff00) {
  105. e.flt_exp++;
  106. e.flt_mantissa.flt_h_32 = 0x80000000;
  107. }
  108. else {
  109. e.flt_mantissa.flt_h_32 += 0x80;
  110. }
  111. }
  112. if (e.flt_exp >= 255) {
  113. overflow = 1;
  114. e.flt_exp = 255;
  115. e.flt_mantissa.flt_h_32 = e.flt_mantissa.flt_l_32 = 0;
  116. }
  117. if (e.flt_exp <= 0) {
  118. flt_b64_sft(&(e.flt_mantissa), 1);
  119. if (e.flt_exp < 0) {
  120. flt_b64_sft(&(e.flt_mantissa), -e.flt_exp);
  121. e.flt_exp = 0;
  122. }
  123. }
  124. #endif
  125. #ifndef IEEEFLOAT
  126. if (sz == 4 && (e.flt_mantissa.flt_h_32 & 0x80)) {
  127. /* rounding */
  128. if ((e.flt_mantissa.flt_h_32 & 0xffffff00) == 0xffffff00) {
  129. e.flt_exp++;
  130. e.flt_mantissa.flt_h_32 = 0x80000000;
  131. }
  132. else {
  133. e.flt_mantissa.flt_h_32 += 0x80;
  134. }
  135. }
  136. if (sz == 8 && (e.flt_mantissa.flt_l_32 & 0x80)) {
  137. /* rounding */
  138. if ((e.flt_mantissa.flt_l_32 & 0xffffff00) == 0xffffff00) {
  139. e.flt_mantissa.flt_l_32 = 0;
  140. if (e.flt_mantissa.flt_h_32 == 0xffffffff) {
  141. e.flt_exp++;
  142. e.flt_mantissa.flt_h_32 = 0x80000000;
  143. }
  144. else e.flt_mantissa.flt_h_32++;
  145. }
  146. else {
  147. e.flt_mantissa.flt_l_32 += 0x80;
  148. }
  149. }
  150. if (e.flt_exp > 255) {
  151. overflow = 1;
  152. e.flt_exp = 255;
  153. e.flt_mantissa.flt_h_32 = e.flt_mantissa.flt_l_32 = 0xffffffff;
  154. }
  155. #endif
  156. buf[I0] = (e.flt_sign << 7) | (e.flt_exp >> 1);
  157. buf[I1] = ((e.flt_exp&1) << 7) |
  158. ((e.flt_mantissa.flt_h_32 & 0x7fffffff) >> 24);
  159. buf[I2] = e.flt_mantissa.flt_h_32 >> 16;
  160. buf[I3] = e.flt_mantissa.flt_h_32 >> 8;
  161. #ifndef IEEEFLOAT
  162. if (sz == 8) {
  163. buf[I4] = e.flt_mantissa.flt_h_32;
  164. buf[I5] = e.flt_mantissa.flt_l_32 >> 24;
  165. buf[I6] = e.flt_mantissa.flt_l_32 >> 16;
  166. buf[I7] = e.flt_mantissa.flt_l_32 >> 8;
  167. flt_b64_sft(&(e.flt_mantissa), -56);
  168. }
  169. else
  170. #endif
  171. flt_b64_sft(&(e.flt_mantissa), -24);
  172. #ifdef IEEEFLOAT
  173. }
  174. else {
  175. e.flt_exp += 1023;
  176. if (e.flt_mantissa.flt_h_32 == 0) e.flt_exp = 0;
  177. if (e.flt_mantissa.flt_l_32 & 0x400) {
  178. /* rounding */
  179. if ((e.flt_mantissa.flt_l_32 & 0xfffff800) == 0xfffff800) {
  180. e.flt_mantissa.flt_l_32 = 0;
  181. if (e.flt_mantissa.flt_h_32 == 0xffffffff) {
  182. e.flt_exp++;
  183. e.flt_mantissa.flt_h_32 = 0x80000000;
  184. }
  185. else e.flt_mantissa.flt_h_32++;
  186. }
  187. else {
  188. e.flt_mantissa.flt_l_32 += 0x400;
  189. }
  190. }
  191. if (e.flt_exp >= 2047) {
  192. overflow = 1;
  193. e.flt_exp = 2047;
  194. e.flt_mantissa.flt_h_32 = e.flt_mantissa.flt_l_32 = 0;
  195. }
  196. if (e.flt_exp <= 0) {
  197. flt_b64_sft(&(e.flt_mantissa), 1);
  198. if (e.flt_exp < 0) {
  199. flt_b64_sft(&(e.flt_mantissa), -e.flt_exp);
  200. e.flt_exp = 0;
  201. }
  202. }
  203. buf[I0] = (e.flt_sign << 7) | (e.flt_exp >> 4);
  204. buf[I1] = ((e.flt_exp & 017)<< 4) | ((e.flt_mantissa.flt_h_32 >> 27) & 017);
  205. buf[I2] = e.flt_mantissa.flt_h_32 >> 19;
  206. buf[I3] = e.flt_mantissa.flt_h_32 >> 11;
  207. buf[I4] = e.flt_mantissa.flt_h_32 >> 3;
  208. buf[I5] = (e.flt_mantissa.flt_h_32 << 5) | ((e.flt_mantissa.flt_l_32 >> 27) & 037);
  209. buf[I6] = e.flt_mantissa.flt_l_32 >> 19;
  210. buf[I7] = e.flt_mantissa.flt_l_32 >> 11;
  211. flt_b64_sft(&(e.flt_mantissa), -53);
  212. }
  213. #endif
  214. #if ! FL_MSL_AT_LOW_ADDRESS
  215. if (sz == 4) {
  216. buf[I4] = buf[I0];
  217. buf[I5] = buf[I1];
  218. buf[I6] = buf[I2];
  219. buf[I7] = buf[I3];
  220. }
  221. #endif
  222. if (overflow) {
  223. return 2;
  224. }
  225. return 0;
  226. }
  227. #endif /* USE_FLT */
  228. #ifdef CODE_GENERATOR
  229. con_float()
  230. {
  231. char buf[8];
  232. int rval = float_cst(str, (int)argval, buf);
  233. int i;
  234. if (rval == 1) {
  235. fprintf(stderr,"float constant size = %d\n",(int)argval);
  236. fatal("bad fcon size");
  237. }
  238. fprintf(codefile,"!float %s sz %d\n", str, (int)argval);
  239. if (rval == 2) {
  240. fprintf(stderr, "Warning: overflow in floating point constant %s\n", str);
  241. }
  242. fprintf(codefile, ".data1 0%o", buf[0] & 0377);
  243. for (i = 1; i < (int)argval; i++) {
  244. fprintf(codefile, ",0%o", buf[i] & 0377);
  245. }
  246. putc('\n', codefile);
  247. }
  248. #endif /* CODE_GENERATOR */
  249. #ifdef CODE_EXPANDER
  250. con_float(str, argval)
  251. char *str;
  252. arith argval;
  253. {
  254. char buf[8];
  255. int rval = float_cst(str, (int)argval, buf);
  256. int i;
  257. if (rval == 1) {
  258. argval = 8;
  259. rval = float_cst(str, 8, buf);
  260. }
  261. for (i = 0; i < (int)argval; i++) {
  262. gen1(buf[i]);
  263. }
  264. }
  265. #endif /* CODE_EXPANDER */