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