2009-01-18 10:15:25 -05:00
|
|
|
/* x86 fat binary initializers.
|
|
|
|
|
|
|
|
THE FUNCTIONS AND VARIABLES IN THIS FILE ARE FOR INTERNAL USE ONLY.
|
|
|
|
THEY'RE ALMOST CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR
|
|
|
|
COMPLETELY IN FUTURE GNU MP RELEASES.
|
|
|
|
|
|
|
|
Copyright 2003, 2004 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
This file is part of the GNU MP Library.
|
|
|
|
|
|
|
|
The GNU MP Library is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
The GNU MP Library is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
|
|
|
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA. */
|
|
|
|
|
|
|
|
#include <stdio.h> /* for printf */
|
|
|
|
#include <stdlib.h> /* for getenv */
|
|
|
|
#include <string.h>
|
|
|
|
|
2009-02-12 05:24:24 -05:00
|
|
|
#include "mpir.h"
|
2009-01-18 10:15:25 -05:00
|
|
|
#include "gmp-impl.h"
|
|
|
|
|
|
|
|
/* Change this to "#define TRACE(x) x" for some traces. */
|
|
|
|
#define TRACE(x)
|
|
|
|
|
|
|
|
/* Change this to 1 to take the cpuid from GMP_CPU_TYPE env var. */
|
|
|
|
#define WANT_FAKE_CPUID 0
|
|
|
|
|
|
|
|
|
|
|
|
/* fat_entry.asm */
|
|
|
|
long __gmpn_cpuid __GMP_PROTO ((char dst[12], int id));
|
|
|
|
int __gmpn_cpuid_available __GMP_PROTO ((void));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if WANT_FAKE_CPUID
|
|
|
|
/* The "name"s in the table are values for the GMP_CPU_TYPE environment
|
|
|
|
variable. Anything can be used, but for now it's the canonical cpu types
|
|
|
|
as per config.guess/config.sub. */
|
|
|
|
|
|
|
|
#define __gmpn_cpuid fake_cpuid
|
|
|
|
#define __gmpn_cpuid_available fake_cpuid_available
|
|
|
|
|
|
|
|
#define MAKE_FMS(family, model) \
|
|
|
|
(((family) << 8) + ((model << 4)))
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
const char *name;
|
|
|
|
const char vendor[13];
|
|
|
|
unsigned fms;
|
|
|
|
} fake_cpuid_table[] = {
|
|
|
|
{ "i386", "" },
|
|
|
|
{ "i486", "GenuineIntel", MAKE_FMS (4, 0) },
|
|
|
|
{ "pentium", "GenuineIntel", MAKE_FMS (5, 0) },
|
|
|
|
{ "pentiummmx", "GenuineIntel", MAKE_FMS (5, 4) },
|
|
|
|
{ "pentiumpro", "GenuineIntel", MAKE_FMS (6, 0) },
|
|
|
|
{ "pentium2", "GenuineIntel", MAKE_FMS (6, 2) },
|
|
|
|
{ "pentium3", "GenuineIntel", MAKE_FMS (6, 7) },
|
|
|
|
{ "pentium4", "GenuineIntel", MAKE_FMS (7, 0) },
|
|
|
|
|
|
|
|
{ "k5", "AuthenticAMD", MAKE_FMS (5, 0) },
|
|
|
|
{ "k6", "AuthenticAMD", MAKE_FMS (5, 3) },
|
|
|
|
{ "k62", "AuthenticAMD", MAKE_FMS (5, 8) },
|
|
|
|
{ "k63", "AuthenticAMD", MAKE_FMS (5, 9) },
|
|
|
|
{ "athlon", "AuthenticAMD", MAKE_FMS (6, 0) },
|
|
|
|
{ "x86_64", "AuthenticAMD", MAKE_FMS (15, 0) },
|
|
|
|
|
|
|
|
{ "viac3", "CentaurHauls", MAKE_FMS (6, 0) },
|
|
|
|
{ "viac32", "CentaurHauls", MAKE_FMS (6, 9) },
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
fake_cpuid_lookup (void)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
s = getenv ("GMP_CPU_TYPE");
|
|
|
|
if (s == NULL)
|
|
|
|
{
|
|
|
|
printf ("Need GMP_CPU_TYPE environment variable for fake cpuid\n");
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < numberof (fake_cpuid_table); i++)
|
|
|
|
if (strcmp (s, fake_cpuid_table[i].name) == 0)
|
|
|
|
return i;
|
|
|
|
|
|
|
|
printf ("GMP_CPU_TYPE=%s unknown\n", s);
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
fake_cpuid_available (void)
|
|
|
|
{
|
|
|
|
return fake_cpuid_table[fake_cpuid_lookup()].vendor[0] != '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
static long
|
|
|
|
fake_cpuid (char dst[12], int id)
|
|
|
|
{
|
|
|
|
int i = fake_cpuid_lookup();
|
|
|
|
|
|
|
|
switch (id) {
|
|
|
|
case 0:
|
|
|
|
memcpy (dst, fake_cpuid_table[i].vendor, 12);
|
|
|
|
return 0;
|
|
|
|
case 1:
|
|
|
|
return fake_cpuid_table[i].fms;
|
|
|
|
default:
|
|
|
|
printf ("fake_cpuid(): oops, unknown id %d\n", id);
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
typedef DECL_preinv_divrem_1 ((*preinv_divrem_1_t));
|
|
|
|
typedef DECL_preinv_mod_1 ((*preinv_mod_1_t));
|
|
|
|
|
|
|
|
struct cpuvec_t __gmpn_cpuvec = {
|
|
|
|
__MPN(add_n_init),
|
|
|
|
__MPN(addmul_1_init),
|
|
|
|
__MPN(copyd_init),
|
|
|
|
__MPN(copyi_init),
|
|
|
|
__MPN(divexact_1_init),
|
|
|
|
__MPN(divexact_by3c_init),
|
2010-08-13 09:53:34 -04:00
|
|
|
__MPN(divexact_byfobm1_init),
|
2009-01-18 10:15:25 -05:00
|
|
|
__MPN(divrem_1_init),
|
2010-03-18 11:27:42 -04:00
|
|
|
__MPN(divrem_2_init),
|
2009-04-13 17:42:55 -04:00
|
|
|
__MPN(divrem_euclidean_qr_1_init),
|
2009-05-19 05:30:34 -04:00
|
|
|
__MPN(divrem_euclidean_qr_2_init),
|
2009-01-18 10:15:25 -05:00
|
|
|
__MPN(gcd_1_init),
|
|
|
|
__MPN(lshift_init),
|
|
|
|
__MPN(mod_1_init),
|
|
|
|
__MPN(mod_34lsub1_init),
|
|
|
|
__MPN(modexact_1c_odd_init),
|
|
|
|
__MPN(mul_1_init),
|
|
|
|
__MPN(mul_basecase_init),
|
|
|
|
__MPN(preinv_divrem_1_init),
|
|
|
|
__MPN(preinv_mod_1_init),
|
2010-12-15 21:32:35 -05:00
|
|
|
__MPN(redc_1_init),
|
2009-01-18 10:15:25 -05:00
|
|
|
__MPN(rshift_init),
|
|
|
|
__MPN(sqr_basecase_init),
|
|
|
|
__MPN(sub_n_init),
|
|
|
|
__MPN(submul_1_init),
|
2009-02-27 12:59:28 -05:00
|
|
|
__MPN(sumdiff_n_init),
|
2009-01-18 10:15:25 -05:00
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* The following setups start with generic x86, then overwrite with
|
|
|
|
specifics for a chip, and higher versions of that chip.
|
|
|
|
|
|
|
|
The arrangement of the setups here will normally be the same as the $path
|
|
|
|
selections in configure.in for the respective chips.
|
|
|
|
|
|
|
|
This code is reentrant and thread safe. We always calculate the same
|
|
|
|
decided_cpuvec, so if two copies of the code are running it doesn't
|
|
|
|
matter which completes first, both write the same to __gmpn_cpuvec.
|
|
|
|
|
|
|
|
We need to go via decided_cpuvec because if one thread has completed
|
|
|
|
__gmpn_cpuvec then it may be making use of the threshold values in that
|
|
|
|
vector. If another thread is still running __gmpn_cpuvec_init then we
|
|
|
|
don't want it to write different values to those fields since some of the
|
|
|
|
asm routines only operate correctly up to their own defined threshold,
|
|
|
|
not an arbitrary value. */
|
|
|
|
|
|
|
|
void
|
|
|
|
__gmpn_cpuvec_init (void)
|
|
|
|
{
|
|
|
|
struct cpuvec_t decided_cpuvec;
|
|
|
|
|
|
|
|
TRACE (printf ("__gmpn_cpuvec_init:\n"));
|
|
|
|
|
|
|
|
memset (&decided_cpuvec, '\0', sizeof (decided_cpuvec));
|
|
|
|
|
2009-01-18 18:21:54 -05:00
|
|
|
CPUVEC_SETUP_x86_64;
|
2009-01-18 10:15:25 -05:00
|
|
|
CPUVEC_SETUP_fat;
|
|
|
|
|
|
|
|
if (! __gmpn_cpuid_available ())
|
|
|
|
{
|
|
|
|
TRACE (printf (" 80386, or early 80486 without cpuid\n"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
2009-03-19 15:52:22 -04:00
|
|
|
#define CONFIG_GUESS 0
|
|
|
|
#define CONFIG_GUESS_32BIT 0
|
|
|
|
#define CONFIG_GUESS_64BIT 0
|
|
|
|
#define FAT32 0
|
|
|
|
#define FAT64 1
|
|
|
|
#define INFAT 1
|
|
|
|
|
|
|
|
#define CPUSETUP_core2 CPUVEC_SETUP_core2
|
|
|
|
#define CPUSETUP_penryn CPUVEC_SETUP_core2;CPUVEC_SETUP_core2_penryn
|
|
|
|
#define CPUSETUP_nehalem CPUVEC_SETUP_nehalem
|
2010-06-26 05:58:50 -04:00
|
|
|
#define CPUSETUP_westmere CPUVEC_SETUP_nehalem;CPUVEC_SETUP_nehalem_westmere
|
2010-12-02 18:05:35 -05:00
|
|
|
#define CPUSETUP_sandybridge CPUVEC_SETUP_nehalem;CPUVEC_SETUP_nehalem_westmere
|
2009-03-19 15:52:22 -04:00
|
|
|
#define CPUSETUP_atom CPUVEC_SETUP_atom
|
2009-09-27 19:12:12 -04:00
|
|
|
#define CPUSETUP_nano CPUVEC_SETUP_nano
|
2009-03-19 15:52:22 -04:00
|
|
|
#define CPUSETUP_netburst CPUVEC_SETUP_netburst
|
|
|
|
#define CPUSETUP_netburstlahf CPUVEC_SETUP_netburst;CPUVEC_SETUP_netburst_netburstlahf
|
2010-07-22 07:29:38 -04:00
|
|
|
#define CPUSETUP_k8 CPUVEC_SETUP_k8;CPUVEC_SETUP_k8_k8only
|
2009-03-19 15:52:22 -04:00
|
|
|
#define CPUSETUP_k10 CPUVEC_SETUP_k8;CPUVEC_SETUP_k8_k10
|
2009-09-07 22:05:31 -04:00
|
|
|
#define CPUSETUP_k102 CPUVEC_SETUP_k8;CPUVEC_SETUP_k8_k10;CPUVEC_SETUP_k8_k10_k102
|
2011-03-04 03:38:40 -05:00
|
|
|
#define CPUSETUP_bobcat CPUVEC_SETUP_bobcat
|
2009-03-19 15:52:22 -04:00
|
|
|
|
|
|
|
#include "cpuid.c"
|
|
|
|
|
|
|
|
}
|
2009-01-18 10:15:25 -05:00
|
|
|
/* There's no x86 generic mpn_preinv_divrem_1 or mpn_preinv_mod_1.
|
|
|
|
Instead default to the plain versions from whichever CPU we detected.
|
|
|
|
The function arguments are compatible, no need for any glue code. */
|
|
|
|
if (decided_cpuvec.preinv_divrem_1 == NULL)
|
|
|
|
decided_cpuvec.preinv_divrem_1 =(preinv_divrem_1_t)decided_cpuvec.divrem_1;
|
|
|
|
if (decided_cpuvec.preinv_mod_1 == NULL)
|
|
|
|
decided_cpuvec.preinv_mod_1 =(preinv_mod_1_t) decided_cpuvec.mod_1;
|
|
|
|
|
|
|
|
ASSERT_CPUVEC (decided_cpuvec);
|
|
|
|
CPUVEC_INSTALL (decided_cpuvec);
|
|
|
|
|
|
|
|
/* Set this once the threshold fields are ready.
|
|
|
|
Use volatile to prevent it getting moved. */
|
|
|
|
((volatile struct cpuvec_t *) &__gmpn_cpuvec)->initialized = 1;
|
|
|
|
}
|