xref: /freebsd/contrib/gdtoa/arithchk.c (revision 0957b409)
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 
27 #include <stdio.h>
28 
29  static int dalign;
30  typedef struct
31 Akind {
32 	char *name;
33 	int   kind;
34 	} Akind;
35 
36  static Akind
37 IEEE_8087	= { "IEEE_8087", 1 },
38 IEEE_MC68k	= { "IEEE_MC68k", 2 },
39 IBM		= { "IBM", 3 },
40 VAX		= { "VAX", 4 },
41 CRAY		= { "CRAY", 5};
42 
43  static Akind *
44 Lcheck()
45 {
46 	union {
47 		double d;
48 		long L[2];
49 		} u;
50 	struct {
51 		double d;
52 		long L;
53 		} x[2];
54 
55 	if (sizeof(x) > 2*(sizeof(double) + sizeof(long)))
56 		dalign = 1;
57 	u.L[0] = u.L[1] = 0;
58 	u.d = 1e13;
59 	if (u.L[0] == 1117925532 && u.L[1] == -448790528)
60 		return &IEEE_MC68k;
61 	if (u.L[1] == 1117925532 && u.L[0] == -448790528)
62 		return &IEEE_8087;
63 	if (u.L[0] == -2065213935 && u.L[1] == 10752)
64 		return &VAX;
65 	if (u.L[0] == 1267827943 && u.L[1] == 704643072)
66 		return &IBM;
67 	return 0;
68 	}
69 
70  static Akind *
71 icheck()
72 {
73 	union {
74 		double d;
75 		int L[2];
76 		} u;
77 	struct {
78 		double d;
79 		int L;
80 		} x[2];
81 
82 	if (sizeof(x) > 2*(sizeof(double) + sizeof(int)))
83 		dalign = 1;
84 	u.L[0] = u.L[1] = 0;
85 	u.d = 1e13;
86 	if (u.L[0] == 1117925532 && u.L[1] == -448790528)
87 		return &IEEE_MC68k;
88 	if (u.L[1] == 1117925532 && u.L[0] == -448790528)
89 		return &IEEE_8087;
90 	if (u.L[0] == -2065213935 && u.L[1] == 10752)
91 		return &VAX;
92 	if (u.L[0] == 1267827943 && u.L[1] == 704643072)
93 		return &IBM;
94 	return 0;
95 	}
96 
97 char *emptyfmt = "";	/* avoid possible warning message with printf("") */
98 
99  static Akind *
100 ccheck()
101 {
102 	union {
103 		double d;
104 		long L;
105 		} u;
106 	long Cray1;
107 
108 	/* Cray1 = 4617762693716115456 -- without overflow on non-Crays */
109 	Cray1 = printf(emptyfmt) < 0 ? 0 : 4617762;
110 	if (printf(emptyfmt, Cray1) >= 0)
111 		Cray1 = 1000000*Cray1 + 693716;
112 	if (printf(emptyfmt, Cray1) >= 0)
113 		Cray1 = 1000000*Cray1 + 115456;
114 	u.d = 1e13;
115 	if (u.L == Cray1)
116 		return &CRAY;
117 	return 0;
118 	}
119 
120  static int
121 fzcheck()
122 {
123 	double a, b;
124 	int i;
125 
126 	a = 1.;
127 	b = .1;
128 	for(i = 155;; b *= b, i >>= 1) {
129 		if (i & 1) {
130 			a *= b;
131 			if (i == 1)
132 				break;
133 			}
134 		}
135 	b = a * a;
136 	return b == 0.;
137 	}
138 
139  int
140 main()
141 {
142 	Akind *a = 0;
143 	int Ldef = 0;
144 	FILE *f;
145 
146 #ifdef WRITE_ARITH_H	/* for Symantec's buggy "make" */
147 	f = fopen("arith.h", "w");
148 	if (!f) {
149 		printf("Cannot open arith.h\n");
150 		return 1;
151 		}
152 #else
153 	f = stdout;
154 #endif
155 
156 	if (sizeof(double) == 2*sizeof(long))
157 		a = Lcheck();
158 	else if (sizeof(double) == 2*sizeof(int)) {
159 		Ldef = 1;
160 		a = icheck();
161 		}
162 	else if (sizeof(double) == sizeof(long))
163 		a = ccheck();
164 	if (a) {
165 		fprintf(f, "#define %s\n#define Arith_Kind_ASL %d\n",
166 			a->name, a->kind);
167 		if (Ldef)
168 			fprintf(f, "#define Long int\n#define Intcast (int)(long)\n");
169 		if (dalign)
170 			fprintf(f, "#define Double_Align\n");
171 		if (sizeof(char*) == 8)
172 			fprintf(f, "#define X64_bit_pointers\n");
173 #ifndef NO_LONG_LONG
174 		if (sizeof(long long) < 8)
175 #endif
176 			fprintf(f, "#define NO_LONG_LONG\n");
177 		if (a->kind <= 2 && fzcheck())
178 			fprintf(f, "#define Sudden_Underflow\n");
179 		return 0;
180 		}
181 	fprintf(f, "/* Unknown arithmetic */\n");
182 	return 1;
183 	}
184