Script C++

Signaler
Messages postés
34
Date d'inscription
mercredi 3 juin 2009
Statut
Membre
Dernière intervention
20 juin 2011
-
Messages postés
3829
Date d'inscription
dimanche 12 décembre 2004
Statut
Modérateur
Dernière intervention
8 janvier 2021
-
Bonjour
si le vous plait est ce qu'il y a quelqu'un qui peut m'aider à comprendre ce code et surtout celui de la fonction MCheckCpu() et MCpuid()



#include "MUtils.h"

//~ static int WithMmxSseUse= 0;

using namespace std;

namespace local_vis_eric
{
void Memms() { // ==> should be done after MMX and before FPU calculations <==
#ifdef ARCH_X86
emms();
#endif
}


int MExists(const char* FileName) {
FILE* File;
if ((File=fopen(FileName,"r"))== NULL) return 0;
fclose(File);
return 1;
}
const char* MExistingFileName(const char* PrefixSuffix, int Number) {
static char FileName[1024];
if (PrefixSuffix== NULL || !strlen(PrefixSuffix))
return NULL;
int i, iMax= strlen(PrefixSuffix);
for (i=0; i< iMax; i++)
if (PrefixSuffix[i]=='*')
break;
if (i== iMax)
return NULL;
sprintf(FileName,"%s", PrefixSuffix);
sprintf(FileName+i,"%d%s", int(Number), PrefixSuffix+i+1);
if (MExists(FileName)) return FileName;
sprintf(FileName+i,"0%d%s", int(Number), PrefixSuffix+i+1);
if (MExists(FileName)) return FileName;
sprintf(FileName+i,"00%d%s", int(Number), PrefixSuffix+i+1);
if (MExists(FileName)) return FileName;
sprintf(FileName+i,"000%d%s", int(Number), PrefixSuffix+i+1);
if (MExists(FileName)) return FileName;
sprintf(FileName+i,"0000%d%s", int(Number), PrefixSuffix+i+1);
if (MExists(FileName)) return FileName;
return NULL;
}


#include <sys/time.h>

double MTime() {
//struct timeval tv;
//gettimeofday(&tv, (struct timezone *)NULL);
return 0.0;
}

double MTime2()
{
//struct tms buffer;
//(void) times(&buffer);
//double NbClkTck=sysconf(_SC_CLK_TCK);
return 0.0;
}





int MCpuid() { // This function is from the GNU mpeg2dec source.
// Copyright (C) 1999-2001 Aaron Holtzman
#ifdef ARCH_X86
uint32_t eax, ebx, ecx, edx;
int AMD;
uint32_t caps;

#define cpuid(op,eax,ebx,ecx,edx) \
asm ("cpuid" \
: "=a" (eax), \
"=b" (ebx), \
"=c" (ecx), \
"=d" (edx) \
: "a" (op) \
: "cc")

asm ("pushfl\n\t"
"popl %0\n\t"
"movl %0,%1\n\t"
"xorl $0x200000,%0\n\t"
"pushl %0\n\t"
"popfl\n\t"
"pushfl\n\t"
"popl %0"
: "=a" (eax),
"=b" (ebx)
:
: "cc");

if (eax == ebx) /* no cpuid */
return 0;
cpuid (0x0000int MCpuid() { // This function is from the GNU mpeg2dec source.
// Copyright (C) 1999-2001 Aaron Holtzman
#ifdef ARCH_X86
uint32_t eax, ebx, ecx, edx;
int AMD;
uint32_t caps;

#define cpuid(op,eax,ebx,ecx,edx) \
asm ("cpuid" \
: "=a" (eax), \
"=b" (ebx), \
"=c" (ecx), \
"=d" (edx) \
: "a" (op) \
: "cc")

asm ("pushfl\n\t"
"popl %0\n\t"
"movl %0,%1\n\t"
"xorl $0x200000,%0\n\t"
"pushl %0\n\t"
"popfl\n\t"
"pushfl\n\t"
"popl %0"
: "=a" (eax),
"=b" (ebx)
:
: "cc");

if (eax == ebx) /* no cpuid */
return 0;
cpuid (0x00000000, eax, ebx, ecx, edx);
if (!eax) /* vendor string only */
return 0;
AMD (ebx 0x68747541) && (ecx == 0x444d4163) && (edx == 0x69746e65);
cpuid (0x00000001, eax, ebx, ecx, edx);
if (! (edx & 0x00800000)) /* no MMX */
return 0;
caps = M_MM_ACCEL_X86_MMX;
if (edx & 0x02000000) /* SSE - identical to AMD MMX extensions */
caps = M_MM_ACCEL_X86_MMX | M_MM_ACCEL_X86_MMXEXT;
cpuid (0x80000000, eax, ebx, ecx, edx);
if (eax < 0x80000001) /* no extended capabilities */
return caps;
cpuid (0x80000001, eax, ebx, ecx, edx);
if (edx & 0x80000000)
caps |= M_MM_ACCEL_X86_3DNOW;
if (AMD && (edx & 0x00400000)) /* AMD MMX extensions */
caps |= M_MM_ACCEL_X86_MMXEXT;
return caps;
#endif
return 0;
};

void MCheckCpu() {
assert(sizeof(char)== 1);
assert(sizeof(short)== 2);
assert(sizeof(int)== 4);
assert(sizeof*/00000, eax, ebx, ecx, edx);
if (eax < 0x80000001) /* no extended capabilities */
return caps;
cpuid (0x80000001, eax, ebx, ecx, edx);
if (edx & 0x80000000)
caps |= M_MM_ACCEL_X86_3DNOW;
if (AMD && (edx & 0x00400000)) /* AMD MMX extensions */
caps |= M_MM_ACCEL_X86_MMXEXT;
return caps;
#endif
return 0;
};

void MCheckCpu() {
assert(sizeof(char)== 1);
assert(sizeof(short)== 2);
assert(sizeof(int)== 4);
assert(sizeof(float)== 4);
assert(sizeof(double)== 8);
assert(sizeof(long long)== 8);
assert((MCpuid()&(M_MM_ACCEL_X86_MMX|M_MM_ACCEL_X86_MMXEXT))==
(M_MM_ACCEL_X86_MMX|M_MM_ACCEL_X86_MMXEXT));
(float)== 4);
assert(sizeof(double)== 8);
assert(sizeof(long long)== 8);
assert((MCpuid()&(M_MM_ACCEL_X86_MMX|M_MM_ACCEL_X86_MMXEXT))==
(M_MM_ACCEL_X86_MMX|M_MM_ACCEL_X86_MMXEXT));
0000, eax, ebx, ecx, edx);
if (!eax) /* vendor string only */
return 0;
AMD (ebx 0x68747541) && (ecx == 0x444d4163) && (edx == 0x69746e65);
cpuid (0x00000001, eax, ebx, ecx, edx);
if (! (edx & 0x00800000)) /* no MMX */
return 0;
caps = M_MM_ACCEL_X86_MMX;
if (edx & 0x02000000) /* SSE - identical to AMD MMX extensions */
caps = M_MM_ACCEL_X86_MMX | M_MM_ACCEL_X86_MMXEXT;
cpuid (0x80000000, eax, ebx, ecx, edx);
if (eax < 0x80000001) /* no extended capabilities */
return caps;
cpuid (0x80000001, eax, ebx, ecx, edx);
if (edx & 0x80000000)
caps |= M_MM_ACCEL_X86_3DNOW;
if (AMD && (edx & 0x00400000)) /* AMD MMX extensions */
caps |= M_MM_ACCEL_X86_MMXEXT;
return caps;
#endif
return 0;
};

void MCheckCpu() {
assert(sizeof(char)== 1);
assert(sizeof(short)== 2);
assert(sizeof(int)== 4);
assert(sizeof(float)== 4);
assert(sizeof(double)== 8);
assert(sizeof(long long)== 8);
assert((MCpuid()&(M_MM_ACCEL_X86_MMX|M_MM_ACCEL_X86_MMXEXT))==
(M_MM_ACCEL_X86_MMX|M_MM_ACCEL_X86_MMXEXT));

return 0;
caps = M_MM_ACCEL_X86_MMX;
if (edx & 0x02000000) /* SSE - identical to AMD MMX extensions */
caps = M_MM_ACCEL_X86_MMX | M_MM_ACCEL_X86_MMXEXT;
cpuid (0x80000000, eax, ebx, ecx, edx);
if (eax < 0x80000001) /* no extended capabilities */
return caps;
cpuid (0x80000001, eax, ebx, ecx, edx);
if (edx & 0x80000000)
caps |= M_MM_ACCEL_X86_3DNOW;
if (AMD && (edx & 0x00400000)) /* AMD MMX extensions */
caps |= M_MM_ACCEL_X86_MMXEXT;
return caps;
#endif
return 0;
};

void MCheckCpu() {
assert(sizeof(char)== 1);
assert(sizeof(short)== 2);
assert(sizeof(int)== 4);
assert(sizeof(float)== 4);
assert(sizeof(double)== 8);
assert(sizeof(long long)== 8);
assert((MCpuid()&(M_MM_ACCEL_X86_MMX|M_MM_ACCEL_X86_MMXEXT))==
(M_MM_ACCEL_X86_MMX|M_MM_ACCEL_X86_MMXEXT));
0000, eax, ebx, ecx, edx);
if (!eax) /* vendor string only */
return 0;
AMD (ebx 0x68747541) && (ecx == 0x444d4163) && (edx == 0x69746e65);
cpuid (0x00000001, eax, ebx, ecx, edx);
if (! (edx & 0x00800000)) /* no MMX */
return 0;
caps = M_MM_ACCEL_X86_MMX;
if (edx & 0x02000000) /* SSE - identical to AMD MMX extensions */
caps = M_MM_ACCEL_X86_MMX | M_MM_ACCEL_X86_MMXEXT;
cpuid (0x80000000, eax, ebx, ecx, edx);
if (eax < 0x80000001) /* no extended capabilities */
return caps;
cpuid (0x80000001, eax, ebx, ecx, edx);
if (edx & 0x80000000)
caps |= M_MM_ACCEL_X86_3DNOW;
if (AMD && (edx & 0x00400000)) /* AMD MMX extensions */
caps |= M_MM_ACCEL_X86_MMXEXT;
return caps;
#endif
return 0;
};

void MCheckCpu() {
assert(sizeof(char)== 1);
assert(sizeof(short)== 2);
assert(sizeof(int)== 4);
assert(sizeof(float)== 4);
assert(sizeof(double)== 8);
assert(sizeof(long long)== 8);
assert((MCpuid()&(M_MM_ACCEL_X86_MMX|M_MM_ACCEL_X86_MMXEXT))==
(M_MM_ACCEL_X86_MMX|M_MM_ACCEL_X86_MMXEXT));
(float)== 4);
assert(sizeof(double)== 8);
assert(sizeof(long long)== 8);
assert((MCpuid()&(M_MM_ACCEL_X86_MMX|M_MM_ACCEL_X86_MMXEXT))==
(M_MM_ACCEL_X86_MMX|M_MM_ACCEL_X86_MMXEXT));
0000, eax, ebx, ecx, edx);
if (!eax) /* vendor string only */
return 0;
AMD (ebx 0x68747541) && (ecx == 0x444d4163) && (edx == 0x69746e65);
cpuid (0x00000001, eax, ebx, ecx, edx);
if (! (edx & 0x00800000)) /* no MMX */
return 0;
caps = M_MM_ACCEL_X86_MMX;
if (edx & 0x02000000) /* SSE - identical to AMD MMX extensions */
caps = M_MM_ACCEL_X86_MMX | M_MM_ACCEL_X86_MMXEXT;
cpuid (0x80000000, eax, ebx, ecx, edx);
if (eax < 0x80000001) /* no extended capabilities */
return caps;
cpuid (0x80000001, eax, ebx, ecx, edx);
if (edx & 0x80000000)
caps |= M_MM_ACCEL_X86_3DNOW;
if (AMD && (edx & 0x00400000)) /* AMD MMX extensions */
caps |= M_MM_ACCEL_X86_MMXEXT;
return caps;
#endif
return 0;
};

void MCheckCpu() {
assert(sizeof(char)== 1);
assert(sizeof(short)== 2);
assert(sizeof(int)== 4);
assert(sizeof(float)== 4);
assert(sizeof(double)== 8);
assert(sizeof(long long)== 8);
assert((MCpuid()&(M_MM_ACCEL_X86_MMX|M_MM_ACCEL_X86_MMXEXT))==
(M_MM_ACCEL_X86_MMX|M_MM_ACCEL_X86_MMXEXT));
//assert(sseUse==2);
//~ WithMmxSseUse= 1;
}
//~ int MWithMmxSseUse() {return WithMmxSseUse; };
int MWithMmxSseUse() {return sseUse; };
//~ void MSetWithMmxSseUse(int w) { std::cout << " Settings SSE " << w << std::endl;WithMmxSseUse=w;}

}

3 réponses

Messages postés
3829
Date d'inscription
dimanche 12 décembre 2004
Statut
Modérateur
Dernière intervention
8 janvier 2021
114
Évite les "using namespace", voir: http://0217021.free.fr/portfolio/axel.berardino/articles/bon-usage-using-namespace

A noter pour ceux qui prennent le thread en route, qu'il y a un rapport avec la question posée ici:
http://www.cppfrance.com/forum/sujet-PORTAGE-PROJET-CPLUSPLUS-LINUX-VERS-WINDOWS_1512482.aspx#9

J'ai l'impression que ce code check le type de cpu que tu as. Sûrement pour vérifier qu'il a le droit d'utiliser certaines instructions qui ne sont pas présentes (ou différentes) sur certains processeurs.
Tu devrais poster cette question sur le forum asm, ils répondront sans doute mieux que moi.

________________________________________________________________________
Historique de mes créations, et quelques articles:
[ http://0217021.free.fr/portfolio http://0217021.free.fr/portfolio]
Merci d'utiliser Réponse acceptée si un post répond à votre question
Messages postés
34
Date d'inscription
mercredi 3 juin 2009
Statut
Membre
Dernière intervention
20 juin 2011

ok merci , mais comment je peus transmettre cette discutions sur ce salon de asm ?
Messages postés
3829
Date d'inscription
dimanche 12 décembre 2004
Statut
Modérateur
Dernière intervention
8 janvier 2021
114
Poste une nouvelle question sur le forum asm.

________________________________________________________________________
Historique de mes créations, et quelques articles:
[ http://0217021.free.fr/portfolio http://0217021.free.fr/portfolio]
Merci d'utiliser Réponse acceptée si un post répond à votre question