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