[Zlib-devel] [PATCH 02/13] For x86, add CPUID check.

Mike Frysinger vapier at gentoo.org
Fri Nov 29 09:00:42 EST 2013


On Friday 29 November 2013 05:02:35 Jim Kukunas wrote:
> On Thu, Nov 28, 2013 at 01:51:43AM -0500, Mike Frysinger wrote:
> > > +int x86_cpu_has_sse2;
> > > +int x86_cpu_has_sse42;
> > > +int x86_cpu_has_pclmulqdq;
> > > +
> > > +void x86_check_features(void)
> > > +{
> > > +    unsigned eax, ebx, ecx, edx;
> > > +
> > > +    eax = 1;
> > > +    __asm__ __volatile__ (
> > > +        "cpuid\n\t"
> > > +    : "+a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx)
> > > +    );
> > 
> > pretty sure this won't compile under PIC.  it'll complain about ebx being
> > 
> > clobbered.  this one will always work:
> > 	__asm__ __volatile__ (
> > 	
> > 		"xchg %%ebx, %%esi;"
> > 		"cpuid;"
> > 		"xchg %%esi, %%ebx;"
> > 		
> > 		: "=S" (ebx), "=a" (eax), "=c" (ecx), "=d" (edx)
> > 		: "a" (1));
> 
> Do you actually see a compiler error here?
> 
> In the extended assembler syntax, ebx is marked as being clobbered,
> so the compiler should automatically handle the save and restore. I know
> GCC does.

GCC has long barfed when you declared ebx as clobbered.

here's your code:
$ cat test.c
int x86_cpu_has_sse2;
int x86_cpu_has_sse42;
int x86_cpu_has_pclmulqdq;

void x86_check_features(void)
{
    unsigned eax, ebx, ecx, edx;

    eax = 1;
    __asm__ __volatile__ (
        "cpuid\n\t"
    : "+a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx)
    );

    x86_cpu_has_sse2 = edx & 0x4000000;
    x86_cpu_has_sse42= ecx & 0x100000;
    x86_cpu_has_pclmulqdq = ecx & 0x2;
}

and here's it failing to build:
$ gcc -c -m32 test.c -fPIC
test.c: In function ‘x86_check_features’:
test.c:10:5: error: inconsistent operand constraints in an ‘asm’
$ gcc -c -m32 test.c -fPIE
test.c: In function ‘x86_check_features’:
test.c:10:5: error: inconsistent operand constraints in an ‘asm’
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://madler.net/pipermail/zlib-devel_madler.net/attachments/20131129/7bcbcebd/attachment-0001.sig>


More information about the Zlib-devel mailing list