Debian Codegraph @

gmp4/mpn/x86_64/fat/fat.c

raw
1 /* x86_64 fat binary initializers.
2
3 Contributed to the GNU project by Kevin Ryde (original x86_32 code) and
4 Torbjorn Granlund (port to x86_64)
5
6 THE FUNCTIONS AND VARIABLES IN THIS FILE ARE FOR INTERNAL USE ONLY.
7 THEY'RE ALMOST CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR
8 COMPLETELY IN FUTURE GNU MP RELEASES.
9
10 Copyright 2003, 2004, 2009, 2011-2014 Free Software Foundation, Inc.
11
12 This file is part of the GNU MP Library.
13
14 The GNU MP Library is free software; you can redistribute it and/or modify
15 it under the terms of either:
16
17 * the GNU Lesser General Public License as published by the Free
18 Software Foundation; either version 3 of the License, or (at your
19 option) any later version.
20
21 or
22
23 * the GNU General Public License as published by the Free Software
24 Foundation; either version 2 of the License, or (at your option) any
25 later version.
26
27 or both in parallel, as here.
28
29 The GNU MP Library is distributed in the hope that it will be useful, but
30 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
31 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
32 for more details.
33
34 You should have received copies of the GNU General Public License and the
35 GNU Lesser General Public License along with the GNU MP Library. If not,
36 see https://www.gnu.org/licenses/. */
37
38 #include <stdio.h> /* for printf */
39 #include <stdlib.h> /* for getenv */
40 #include <string.h>
41
42 #include "gmp.h"
43 #include "gmp-impl.h"
44
45 /* Change this to "#define TRACE(x) x" for some traces. */
46 #define TRACE(x)
47
48
49 /* fat_entry.asm */
50 long __gmpn_cpuid (char [12], int);
51
52
53 #if WANT_FAKE_CPUID
54 /* The "name"s in the table are values for the GMP_CPU_TYPE environment
55 variable. Anything can be used, but for now it's the canonical cpu types
56 as per config.guess/config.sub. */
57
58 #define __gmpn_cpuid fake_cpuid
59
60 #define MAKE_FMS(family, model) \
61 ((((family) & 0xf) << 8) + (((family) & 0xff0) << 20) \
62 + (((model) & 0xf) << 4) + (((model) & 0xf0) << 12))
63
64 static struct {
65 const char *name;
66 const char vendor[13];
67 unsigned fms;
68 } fake_cpuid_table[] = {
69 { "core2", "GenuineIntel", MAKE_FMS (6, 0xf) },
70 { "coreinhm", "GenuineIntel", MAKE_FMS (6, 0x1a) },
71 { "coreiwsm", "GenuineIntel", MAKE_FMS (6, 0x25) },
72 { "coreisbr", "GenuineIntel", MAKE_FMS (6, 0x2a) },
73 { "coreihwl", "GenuineIntel", MAKE_FMS (6, 0x3c) },
74 { "atom", "GenuineIntel", MAKE_FMS (6, 0x1c) },
75 { "pentium4", "GenuineIntel", MAKE_FMS (15, 3) },
76
77 { "k8", "AuthenticAMD", MAKE_FMS (15, 0) },
78 { "k10", "AuthenticAMD", MAKE_FMS (16, 0) },
79 { "bobcat", "AuthenticAMD", MAKE_FMS (20, 1) },
80 { "bulldozer", "AuthenticAMD", MAKE_FMS (21, 1) },
81 { "piledriver", "AuthenticAMD", MAKE_FMS (21, 2) },
82 { "steamroller","AuthenticAMD", MAKE_FMS (21, 0x30) },
83 { "excavator", "AuthenticAMD", MAKE_FMS (21, 0x60) },
84 { "jaguar", "AuthenticAMD", MAKE_FMS (22, 1) },
85
86 { "nano", "CentaurHauls", MAKE_FMS (6, 15) },
87 };
88
89 static int
90 fake_cpuid_lookup (void)
91 {
92 char *s;
93 int i;
94
95 s = getenv ("GMP_CPU_TYPE");
96 if (s == NULL)
97 {
98 printf ("Need GMP_CPU_TYPE environment variable for fake cpuid\n");
99 abort ();
100 }
101
102 for (i = 0; i < numberof (fake_cpuid_table); i++)
103 if (strcmp (s, fake_cpuid_table[i].name) == 0)
104 return i;
105
106 printf ("GMP_CPU_TYPE=%s unknown\n", s);
107 abort ();
108 }
109
110 static long
111 fake_cpuid (char dst[12], unsigned int id)
112 {
113 int i = fake_cpuid_lookup();
114
115 switch (id) {
116 case 0:
117 memcpy (dst, fake_cpuid_table[i].vendor, 12);
118 return 0;
119 case 1:
120 return fake_cpuid_table[i].fms;
121 case 7:
122 dst[0] = 0xff; /* BMI1, AVX2, etc */
123 dst[1] = 0xff; /* BMI2, etc */
124 return 0;
125 case 0x80000001:
126 dst[4 + 29 / 8] = (1 << (29 % 8)); /* "long" mode */
127 return 0;
128 default:
129 printf ("fake_cpuid(): oops, unknown id %d\n", id);
130 abort ();
131 }
132 }
133 #endif
134
135
136 typedef DECL_preinv_divrem_1 ((*preinv_divrem_1_t));
137 typedef DECL_preinv_mod_1 ((*preinv_mod_1_t));
138
139 struct cpuvec_t __gmpn_cpuvec = {
140 __MPN(add_n_init),
141 __MPN(addlsh1_n_init),
142 __MPN(addlsh2_n_init),
143 __MPN(addmul_1_init),
144 __MPN(addmul_2_init),
145 __MPN(bdiv_dbm1c_init),
146 __MPN(cnd_add_n_init),
147 __MPN(cnd_sub_n_init),
148 __MPN(com_init),
149 __MPN(copyd_init),
150 __MPN(copyi_init),
151 __MPN(divexact_1_init),
152 __MPN(divrem_1_init),
153 __MPN(gcd_1_init),
154 __MPN(lshift_init),
155 __MPN(lshiftc_init),
156 __MPN(mod_1_init),
157 __MPN(mod_1_1p_init),
158 __MPN(mod_1_1p_cps_init),
159 __MPN(mod_1s_2p_init),
160 __MPN(mod_1s_2p_cps_init),
161 __MPN(mod_1s_4p_init),
162 __MPN(mod_1s_4p_cps_init),
163 __MPN(mod_34lsub1_init),
164 __MPN(modexact_1c_odd_init),
165 __MPN(mul_1_init),
166 __MPN(mul_basecase_init),
167 __MPN(mullo_basecase_init),
168 __MPN(preinv_divrem_1_init),
169 __MPN(preinv_mod_1_init),
170 __MPN(redc_1_init),
171 __MPN(redc_2_init),
172 __MPN(rshift_init),
173 __MPN(sqr_basecase_init),
174 __MPN(sub_n_init),
175 __MPN(sublsh1_n_init),
176 __MPN(submul_1_init),
177 0
178 };
179
180 int __gmpn_cpuvec_initialized = 0;
181
182 /* The following setups start with generic x86, then overwrite with
183 specifics for a chip, and higher versions of that chip.
184
185 The arrangement of the setups here will normally be the same as the $path
186 selections in configure.in for the respective chips.
187
188 This code is reentrant and thread safe. We always calculate the same
189 decided_cpuvec, so if two copies of the code are running it doesn't
190 matter which completes first, both write the same to __gmpn_cpuvec.
191
192 We need to go via decided_cpuvec because if one thread has completed
193 __gmpn_cpuvec then it may be making use of the threshold values in that
194 vector. If another thread is still running __gmpn_cpuvec_init then we
195 don't want it to write different values to those fields since some of the
196 asm routines only operate correctly up to their own defined threshold,
197 not an arbitrary value. */
198
199 void
200 __gmpn_cpuvec_init (void)
201 {
202 struct cpuvec_t decided_cpuvec;
203 char vendor_string[13];
204 char dummy_string[12];
205 long fms;
206 int family, model;
207
208 TRACE (printf ("__gmpn_cpuvec_init:\n"));
209
210 memset (&decided_cpuvec, '\0', sizeof (decided_cpuvec));
211
212 CPUVEC_SETUP_x86_64;
213 CPUVEC_SETUP_fat;
214
215 __gmpn_cpuid (vendor_string, 0);
216 vendor_string[12] = 0;
217
218 fms = __gmpn_cpuid (dummy_string, 1);
219 family = ((fms >> 8) & 0xf) + ((fms >> 20) & 0xff);
220 model = ((fms >> 4) & 0xf) + ((fms >> 12) & 0xf0);
221
222 /* Check extended feature flags */
223 __gmpn_cpuid (dummy_string, 0x80000001);
224 if ((dummy_string[4 + 29 / 8] & (1 << (29 % 8))) == 0)
225 abort (); /* longmode-capable-bit turned off! */
226
227 /*********************************************************/
228 /*** WARNING: keep this list in sync with config.guess ***/
229 /*********************************************************/
230 if (strcmp (vendor_string, "GenuineIntel") == 0)
231 {
232 switch (family)
233 {
234 case 6:
235 switch (model)
236 {
237 case 0x0f: /* Conroe Merom Kentsfield Allendale */
238 case 0x10:
239 case 0x11:
240 case 0x12:
241 case 0x13:
242 case 0x14:
243 case 0x15:
244 case 0x16:
245 case 0x17: /* PNR Wolfdale Yorkfield */
246 case 0x18:
247 case 0x19:
248 case 0x1d: /* PNR Dunnington */
249 CPUVEC_SETUP_core2;
250 break;
251
252 case 0x1c: /* Atom Silverthorne */
253 case 0x26: /* Atom Lincroft */
254 case 0x27: /* Atom Saltwell? */
255 case 0x36: /* Atom Cedarview/Saltwell */
256 CPUVEC_SETUP_atom;
257 break;
258
259 case 0x1a: /* NHM Gainestown */
260 case 0x1b:
261 case 0x1e: /* NHM Lynnfield/Jasper */
262 case 0x1f:
263 case 0x20:
264 case 0x21:
265 case 0x22:
266 case 0x23:
267 case 0x24:
268 case 0x25: /* WSM Clarkdale/Arrandale */
269 case 0x28:
270 case 0x29:
271 case 0x2b:
272 case 0x2c: /* WSM Gulftown */
273 case 0x2e: /* NHM Beckton */
274 case 0x2f: /* WSM Eagleton */
275 case 0x37: /* Atom Silvermont */
276 case 0x4d: /* Atom Silvermont/Avoton */
277 CPUVEC_SETUP_core2;
278 CPUVEC_SETUP_coreinhm;
279 break;
280
281 case 0x2a: /* SB */
282 case 0x2d: /* SBC-EP */
283 case 0x3a: /* IBR */
284 case 0x3e: /* IBR Ivytown */
285 CPUVEC_SETUP_core2;
286 CPUVEC_SETUP_coreinhm;
287 CPUVEC_SETUP_coreisbr;
288 break;
289 case 0x3c: /* Haswell client */
290 case 0x3d: /* Broadwell */
291 case 0x3f: /* Haswell server */
292 case 0x45: /* Haswell ULT */
293 case 0x46: /* Crystal Well */
294 case 0x4f: /* Broadwell server */
295 case 0x56: /* Broadwell microserver */
296 CPUVEC_SETUP_core2;
297 CPUVEC_SETUP_coreinhm;
298 CPUVEC_SETUP_coreisbr;
299 /* Some Haswells lack BMI2. Let them appear as Sandybridges for
300 now. */
301 __gmpn_cpuid (dummy_string, 7);
302 if ((dummy_string[0 + 8 / 8] & (1 << (8 % 8))) != 0)
303 CPUVEC_SETUP_coreihwl;
304 break;
305 }
306 break;
307
308 case 15:
309 CPUVEC_SETUP_pentium4;
310 break;
311 }
312 }
313 else if (strcmp (vendor_string, "AuthenticAMD") == 0)
314 {
315 switch (family)
316 {
317 case 0x0f: /* k8 */
318 case 0x11: /* "fam 11h", mix of k8 and k10 */
319 case 0x13:
320 case 0x17:
321 CPUVEC_SETUP_k8;
322 break;
323
324 case 0x10: /* k10 */
325 case 0x12: /* k10 (llano) */
326 CPUVEC_SETUP_k8;
327 CPUVEC_SETUP_k10;
328 break;
329
330 case 0x14: /* bobcat */
331 case 0x16: /* jaguar */
332 CPUVEC_SETUP_k8;
333 CPUVEC_SETUP_k10;
334 CPUVEC_SETUP_bobcat;
335 break;
336
337 case 0x15: /* bulldozer, piledriver, steamroller, excavator */
338 CPUVEC_SETUP_k8;
339 CPUVEC_SETUP_k10;
340 CPUVEC_SETUP_bd1;
341 }
342 }
343 else if (strcmp (vendor_string, "CentaurHauls") == 0)
344 {
345 switch (family)
346 {
347 case 6:
348 if (model >= 15)
349 CPUVEC_SETUP_nano;
350 break;
351 }
352 }
353
354 /* There's no x86 generic mpn_preinv_divrem_1 or mpn_preinv_mod_1.
355 Instead default to the plain versions from whichever CPU we detected.
356 The function arguments are compatible, no need for any glue code. */
357 if (decided_cpuvec.preinv_divrem_1 == NULL)
358 decided_cpuvec.preinv_divrem_1 =(preinv_divrem_1_t)decided_cpuvec.divrem_1;
359 if (decided_cpuvec.preinv_mod_1 == NULL)
360 decided_cpuvec.preinv_mod_1 =(preinv_mod_1_t) decided_cpuvec.mod_1;
361
362 ASSERT_CPUVEC (decided_cpuvec);
363 CPUVEC_INSTALL (decided_cpuvec);
364
365 /* Set this once the threshold fields are ready.
366 Use volatile to prevent it getting moved. */
367 *((volatile int *) &__gmpn_cpuvec_initialized) = 1;
368 }