]> git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/ArithChk/arithchk.c
BaseTools/BinToPcd: Fix Python 2.7.x compatibility issue
[mirror_edk2.git] / AppPkg / Applications / ArithChk / arithchk.c
1 /** @file
2 Program to generate an arith.h for use with the gdtoa binary to decimal and decimal to binary
3 conversion library.
4
5 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 Copyright (C) 1997, 1998 Lucent Technologies
15 All Rights Reserved
16
17 Permission to use, copy, modify, and distribute this software and
18 its documentation for any purpose and without fee is hereby
19 granted, provided that the above copyright notice appear in all
20 copies and that both that the copyright notice and this
21 permission notice and warranty disclaimer appear in supporting
22 documentation, and that the name of Lucent or any of its entities
23 not be used in advertising or publicity pertaining to
24 distribution of the software without specific, written prior
25 permission.
26
27 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
28 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
29 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
30 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
31 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
32 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
33 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
34 THIS SOFTWARE.
35
36 NetBSD: arithchk.c,v 1.2 2006/01/25 15:27:42 kleink Exp
37 ****************************************************************/
38 #include <sys/EfiCdefs.h>
39
40 /* Try to deduce arith.h from arithmetic properties. */
41
42 #include <stdio.h>
43
44 static int dalign;
45
46 typedef struct Akind {
47 char *name;
48 int kind;
49 } Akind;
50
51 static Akind IEEE_LITTLE_ENDIAN = { "IEEE_LITTLE_ENDIAN", 1 };
52 static Akind IEEE_BIG_ENDIAN = { "IEEE_BIG_ENDIAN", 2 };
53 static Akind IBM = { "IBM", 3 };
54 static Akind VAX = { "VAX", 4 };
55 static Akind CRAY = { "CRAY", 5};
56
57 static Akind *
58 Lcheck()
59 {
60 union {
61 double d;
62 long L[2];
63 } u;
64 struct {
65 double d;
66 long L;
67 } x[2];
68
69 if (sizeof(x) > 2*(sizeof(double) + sizeof(long)))
70 dalign = 1;
71 u.L[0] = u.L[1] = 0;
72 u.d = 1e13;
73 if (u.L[0] == 1117925532 && u.L[1] == -448790528)
74 return &IEEE_BIG_ENDIAN;
75 if (u.L[1] == 1117925532 && u.L[0] == -448790528)
76 return &IEEE_LITTLE_ENDIAN;
77 if (u.L[0] == -2065213935 && u.L[1] == 10752)
78 return &VAX;
79 if (u.L[0] == 1267827943 && u.L[1] == 704643072)
80 return &IBM;
81 return 0;
82 }
83
84 static Akind *
85 icheck()
86 {
87 union {
88 double d;
89 int L[2];
90 } u;
91 struct {
92 double d;
93 int L;
94 } x[2];
95
96 if (sizeof(x) > 2*(sizeof(double) + sizeof(int)))
97 dalign = 1;
98 u.L[0] = u.L[1] = 0;
99 u.d = 1e13;
100 if (u.L[0] == 1117925532 && u.L[1] == -448790528)
101 return &IEEE_BIG_ENDIAN;
102 if (u.L[1] == 1117925532 && u.L[0] == -448790528)
103 return &IEEE_LITTLE_ENDIAN;
104 if (u.L[0] == -2065213935 && u.L[1] == 10752)
105 return &VAX;
106 if (u.L[0] == 1267827943 && u.L[1] == 704643072)
107 return &IBM;
108 return 0;
109 }
110
111 char *emptyfmt = ""; /* avoid possible warning message with printf("") */
112
113 static Akind *
114 ccheck()
115 {
116 union {
117 double d;
118 long L;
119 } u;
120 long Cray1;
121
122 /* Cray1 = 4617762693716115456 -- without overflow on non-Crays */
123 Cray1 = printf(emptyfmt) < 0 ? 0 : 4617762;
124 if (printf(emptyfmt, Cray1) >= 0)
125 Cray1 = 1000000*Cray1 + 693716;
126 if (printf(emptyfmt, Cray1) >= 0)
127 Cray1 = 1000000*Cray1 + 115456;
128 u.d = 1e13;
129 if (u.L == Cray1)
130 return &CRAY;
131 return 0;
132 }
133
134 static int
135 fzcheck()
136 {
137 double a, b;
138 int i;
139
140 a = 1.;
141 b = .1;
142 for(i = 155;; b *= b, i >>= 1) {
143 if (i & 1) {
144 a *= b;
145 if (i == 1)
146 break;
147 }
148 }
149 b = a * a;
150 return b == 0.;
151 }
152
153 int
154 main()
155 {
156 Akind *a = 0;
157 int Ldef = 0;
158 FILE *f;
159
160 #ifdef WRITE_ARITH_H /* for Symantec's buggy "make" */
161 f = fopen("arith.h", "w");
162 if (!f) {
163 printf("Cannot open arith.h\n");
164 return 1;
165 }
166 #else
167 f = stdout;
168 #endif
169
170 if (sizeof(double) == 2*sizeof(long))
171 a = Lcheck();
172 else if (sizeof(double) == 2*sizeof(int)) {
173 Ldef = 1;
174 a = icheck();
175 }
176 else if (sizeof(double) == sizeof(long))
177 a = ccheck();
178 if (a) {
179 fprintf(f, "#define %s\n#define Arith_Kind_ASL %d\n",
180 a->name, a->kind);
181 if (Ldef)
182 fprintf(f, "#define Long int\n#define Intcast (int)(long)\n");
183 if (dalign)
184 fprintf(f, "#define Double_Align\n");
185 if (sizeof(char*) == 8)
186 fprintf(f, "#define X64_bit_pointers\n");
187 #ifndef NO_LONG_LONG
188 if (sizeof(long long) < 8)
189 #endif
190 fprintf(f, "#define NO_LONG_LONG\n");
191 if (a->kind <= 2 && fzcheck())
192 fprintf(f, "#define Sudden_Underflow\n");
193 return 0;
194 }
195 fprintf(f, "/* Unknown arithmetic */\n");
196 return 1;
197 }