xref: /freebsd/contrib/gdtoa/arithchk.c (revision 2546665afcaf0d53dc2c7058fee96354b3680f5a)
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 main()
140 {
141 	Akind *a = 0;
142 	int Ldef = 0;
143 	FILE *f;
144 
145 #ifdef WRITE_ARITH_H	/* for Symantec's buggy "make" */
146 	f = fopen("arith.h", "w");
147 	if (!f) {
148 		printf("Cannot open arith.h\n");
149 		return 1;
150 		}
151 #else
152 	f = stdout;
153 #endif
154 
155 	if (sizeof(double) == 2*sizeof(long))
156 		a = Lcheck();
157 	else if (sizeof(double) == 2*sizeof(int)) {
158 		Ldef = 1;
159 		a = icheck();
160 		}
161 	else if (sizeof(double) == sizeof(long))
162 		a = ccheck();
163 	if (a) {
164 		fprintf(f, "#define %s\n#define Arith_Kind_ASL %d\n",
165 			a->name, a->kind);
166 		if (Ldef)
167 			fprintf(f, "#define Long int\n#define Intcast (int)(long)\n");
168 		if (dalign)
169 			fprintf(f, "#define Double_Align\n");
170 		if (sizeof(char*) == 8)
171 			fprintf(f, "#define X64_bit_pointers\n");
172 #ifndef NO_LONG_LONG
173 		if (sizeof(long long) < 8)
174 #endif
175 			fprintf(f, "#define NO_LONG_LONG\n");
176 		if (a->kind <= 2 && fzcheck())
177 			fprintf(f, "#define Sudden_Underflow\n");
178 		return 0;
179 		}
180 	fprintf(f, "/* Unknown arithmetic */\n");
181 	return 1;
182 	}
183