arithchk.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /****************************************************************
  2. Copyright (C) 1997, 1998, 2000 Lucent Technologies
  3. All Rights Reserved
  4. Permission to use, copy, modify, and distribute this software and
  5. its documentation for any purpose and without fee is hereby
  6. granted, provided that the above copyright notice appear in all
  7. copies and that both that the copyright notice and this
  8. permission notice and warranty disclaimer appear in supporting
  9. documentation, and that the name of Lucent or any of its entities
  10. not be used in advertising or publicity pertaining to
  11. distribution of the software without specific, written prior
  12. permission.
  13. LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  14. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  15. IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
  16. SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  17. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  18. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  19. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  20. THIS SOFTWARE.
  21. ****************************************************************/
  22. /* Try to deduce arith.h from arithmetic properties. */
  23. #include <stdio.h>
  24. #include <math.h>
  25. #include <errno.h>
  26. #ifdef NO_FPINIT
  27. #define fpinit_ASL()
  28. #else
  29. #ifndef KR_headers
  30. extern
  31. #ifdef __cplusplus
  32. "C"
  33. #endif
  34. void fpinit_ASL(void);
  35. #endif /*KR_headers*/
  36. #endif /*NO_FPINIT*/
  37. static int dalign;
  38. typedef struct
  39. Akind {
  40. char *name;
  41. int kind;
  42. } Akind;
  43. static Akind
  44. IEEE_8087 = { "IEEE_8087", 1 },
  45. IEEE_MC68k = { "IEEE_MC68k", 2 },
  46. IBM = { "IBM", 3 },
  47. VAX = { "VAX", 4 },
  48. CRAY = { "CRAY", 5};
  49. static double t_nan;
  50. static Akind *
  51. Lcheck(void)
  52. {
  53. union {
  54. double d;
  55. long L[2];
  56. } u;
  57. struct {
  58. double d;
  59. long L;
  60. } x[2];
  61. if (sizeof(x) > 2*(sizeof(double) + sizeof(long)))
  62. dalign = 1;
  63. u.L[0] = u.L[1] = 0;
  64. u.d = 1e13;
  65. if (u.L[0] == 1117925532 && u.L[1] == -448790528)
  66. return &IEEE_MC68k;
  67. if (u.L[1] == 1117925532 && u.L[0] == -448790528)
  68. return &IEEE_8087;
  69. if (u.L[0] == -2065213935 && u.L[1] == 10752)
  70. return &VAX;
  71. if (u.L[0] == 1267827943 && u.L[1] == 704643072)
  72. return &IBM;
  73. return 0;
  74. }
  75. static Akind *
  76. icheck(void)
  77. {
  78. union {
  79. double d;
  80. int L[2];
  81. } u;
  82. struct {
  83. double d;
  84. int L;
  85. } x[2];
  86. if (sizeof(x) > 2*(sizeof(double) + sizeof(int)))
  87. dalign = 1;
  88. u.L[0] = u.L[1] = 0;
  89. u.d = 1e13;
  90. if (u.L[0] == 1117925532 && u.L[1] == -448790528)
  91. return &IEEE_MC68k;
  92. if (u.L[1] == 1117925532 && u.L[0] == -448790528)
  93. return &IEEE_8087;
  94. if (u.L[0] == -2065213935 && u.L[1] == 10752)
  95. return &VAX;
  96. if (u.L[0] == 1267827943 && u.L[1] == 704643072)
  97. return &IBM;
  98. return 0;
  99. }
  100. char *emptyfmt = ""; /* avoid possible warning message with printf("") */
  101. static Akind *
  102. ccheck(void)
  103. {
  104. union {
  105. double d;
  106. long L;
  107. } u;
  108. long Cray1;
  109. /* Cray1 = 4617762693716115456 -- without overflow on non-Crays */
  110. Cray1 = printf(emptyfmt) < 0 ? 0 : 4617762;
  111. if (printf(emptyfmt, Cray1) >= 0)
  112. Cray1 = 1000000*Cray1 + 693716;
  113. if (printf(emptyfmt, Cray1) >= 0)
  114. Cray1 = 1000000*Cray1 + 115456;
  115. u.d = 1e13;
  116. if (u.L == Cray1)
  117. return &CRAY;
  118. return 0;
  119. }
  120. static int
  121. fzcheck(void)
  122. {
  123. double a, b;
  124. int i;
  125. a = 1.;
  126. b = .1;
  127. for(i = 155;; b *= b, i >>= 1) {
  128. if (i & 1) {
  129. a *= b;
  130. if (i == 1)
  131. break;
  132. }
  133. }
  134. b = a * a;
  135. return b == 0.;
  136. }
  137. static int
  138. need_nancheck(void)
  139. {
  140. double t;
  141. errno = 0;
  142. t = log(t_nan);
  143. if (errno == 0)
  144. return 1;
  145. errno = 0;
  146. t = sqrt(t_nan);
  147. return errno == 0;
  148. }
  149. void
  150. get_nanbits(unsigned int *b, int k)
  151. {
  152. union { double d; unsigned int z[2]; } u, u1, u2;
  153. k = 2 - k;
  154. u1.z[k] = u2.z[k] = 0x7ff00000;
  155. u1.z[1-k] = u2.z[1-k] = 0;
  156. u.d = u1.d - u2.d; /* Infinity - Infinity */
  157. b[0] = u.z[0];
  158. b[1] = u.z[1];
  159. }
  160. int
  161. main(void)
  162. {
  163. FILE *f;
  164. Akind *a = 0;
  165. int Ldef = 0;
  166. unsigned int nanbits[2];
  167. fpinit_ASL();
  168. #ifdef WRITE_ARITH_H /* for Symantec's buggy "make" */
  169. f = fopen("arith.h", "w");
  170. if (!f) {
  171. printf("Cannot open arith.h\n");
  172. return 1;
  173. }
  174. #else
  175. f = stdout;
  176. #endif
  177. if (sizeof(double) == 2*sizeof(long))
  178. a = Lcheck();
  179. else if (sizeof(double) == 2*sizeof(int)) {
  180. Ldef = 1;
  181. a = icheck();
  182. }
  183. else if (sizeof(double) == sizeof(long))
  184. a = ccheck();
  185. if (a) {
  186. fprintf(f, "#define %s\n#define Arith_Kind_ASL %d\n",
  187. a->name, a->kind);
  188. if (Ldef)
  189. fprintf(f, "#define Long int\n#define Intcast (int)(long)\n");
  190. if (dalign)
  191. fprintf(f, "#define Double_Align\n");
  192. if (sizeof(char*) == 8)
  193. fprintf(f, "#define X64_bit_pointers\n");
  194. #ifndef NO_LONG_LONG
  195. if (sizeof(long long) < 8)
  196. #endif
  197. fprintf(f, "#define NO_LONG_LONG\n");
  198. if (a->kind <= 2) {
  199. if (fzcheck())
  200. fprintf(f, "#define Sudden_Underflow\n");
  201. t_nan = -a->kind;
  202. if (need_nancheck())
  203. fprintf(f, "#define NANCHECK\n");
  204. if (sizeof(double) == 2*sizeof(unsigned int)) {
  205. get_nanbits(nanbits, a->kind);
  206. fprintf(f, "#define QNaN0 0x%x\n", nanbits[0]);
  207. fprintf(f, "#define QNaN1 0x%x\n", nanbits[1]);
  208. }
  209. }
  210. return 0;
  211. }
  212. fprintf(f, "/* Unknown arithmetic */\n");
  213. return 1;
  214. }
  215. #ifdef __sun
  216. #ifdef __i386
  217. /* kludge for Intel Solaris */
  218. void fpsetprec(int x) { }
  219. #endif
  220. #endif