📄 mmx.h
字号:
/* mmx.h MultiMedia eXtensions GCC interface library for IA32. To use this library, simply include this header file and compile with GCC. You MUST have inlining enabled in order for mmx_ok() to work; this can be done by simply using -O on the GCC command line. Compiling with -DMMX_TRACE will cause detailed trace output to be sent to stderr for each mmx operation. This adds lots of code, and obviously slows execution to a crawl, but can be very useful for debugging. THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY PARTICULAR PURPOSE. 1997-98 by H. Dietz and R. Fisher History: 97-98* R.Fisher Early versions 980501 R.Fisher Original Release 980611* H.Dietz Rewrite, correctly implementing inlines, and R.Fisher including direct register accesses. 980616 R.Fisher Release of 980611 as 980616. 980714 R.Fisher Minor corrections to Makefile, etc. 980715 R.Fisher mmx_ok() now prevents optimizer from using clobbered values. mmx_ok() now checks if cpuid instruction is available before trying to use it. 980726* R.Fisher mm_support() searches for AMD 3DNow, Cyrix Extended MMX, and standard MMX. It returns a value which is positive if any of these are supported, and can be masked with constants to see which. mmx_ok() is now a call to this 980726* R.Fisher Added i2r support for shift functions 980919 R.Fisher Fixed AMD extended feature recognition bug. 980921 R.Fisher Added definition/check for _MMX_H. Added "float s[2]" to mmx_t for use with 3DNow and EMMX. So same mmx_t can be used. 981013 R.Fisher Fixed cpuid function 1 bug (looked at wrong reg) Fixed psllq_i2r error in mmxtest.c * Unreleased (internal or interim) versions Notes: It appears that the latest gas has the pand problem fixed, therefore I'll undefine BROKEN_PAND by default. String compares may be quicker than the multiple test/jumps in vendor test sequence in mmx_ok(), but I'm not concerned with that right now. Acknowledgments: Jussi Laako for pointing out the errors ultimately found to be connected to the failure to notify the optimizer of clobbered values. Roger Hardiman for reminding us that CPUID isn't everywhere, and that someone may actually try to use this on a machine without CPUID. Also for suggesting code for checking this. Robert Dale for pointing out the AMD recognition bug. Jimmy Mayfield and Carl Witty for pointing out the Intel recognition bug. Carl Witty for pointing out the psllq_i2r test bug.*/#ifndef _MMX_H#define _MMX_H/* Warning: at this writing, the version of GAS packaged with most Linux distributions does not handle the parallel AND operation mnemonic correctly. If the symbol BROKEN_PAND is defined, a slower alternative coding will be used. If execution of mmxtest results in an illegal instruction fault, define this symbol.*/#undef BROKEN_PAND/* The type of an value that fits in an MMX register (note that long long constant values MUST be suffixed by LL and unsigned long long values by ULL, lest they be truncated by the compiler)*/typedef union { long long q; /* Quadword (64-bit) value */ unsigned long long uq; /* Unsigned Quadword */ int d[2]; /* 2 Doubleword (32-bit) values */ unsigned int ud[2]; /* 2 Unsigned Doubleword */ short w[4]; /* 4 Word (16-bit) values */ unsigned short uw[4]; /* 4 Unsigned Word */ char b[8]; /* 8 Byte (8-bit) values */ unsigned char ub[8]; /* 8 Unsigned Byte */ float s[2]; /* Single-precision (32-bit) value */} mmx_t;/* Function to test if multimedia instructions are supported...*/inline extern intmm_support(void){ /* Returns 1 if MMX instructions are supported, 3 if Cyrix MMX and Extended MMX instructions are supported 5 if AMD MMX and 3DNow! instructions are supported 0 if hardware does not support any of these */ register int rval = 0; __asm__ __volatile__ ( /* See if CPUID instruction is supported ... */ /* ... Get copies of EFLAGS into eax and ecx */ "pushf\n\t" "popl %%eax\n\t" "movl %%eax, %%ecx\n\t" /* ... Toggle the ID bit in one copy and store */ /* to the EFLAGS reg */ "xorl $0x200000, %%eax\n\t" "push %%eax\n\t" "popf\n\t" /* ... Get the (hopefully modified) EFLAGS */ "pushf\n\t" "popl %%eax\n\t" /* ... Compare and test result */ "xorl %%eax, %%ecx\n\t" "testl $0x200000, %%ecx\n\t" "jz NotSupported1\n\t" /* Nothing supported */ /* Get standard CPUID information, and go to a specific vendor section */ "movl $0, %%eax\n\t" "cpuid\n\t" /* Check for Intel */ "cmpl $0x756e6547, %%ebx\n\t" "jne TryAMD\n\t" "cmpl $0x49656e69, %%edx\n\t" "jne TryAMD\n\t" "cmpl $0x6c65746e, %%ecx\n" "jne TryAMD\n\t" "jmp Intel\n\t" /* Check for AMD */ "\nTryAMD:\n\t" "cmpl $0x68747541, %%ebx\n\t" "jne TryCyrix\n\t" "cmpl $0x69746e65, %%edx\n\t" "jne TryCyrix\n\t" "cmpl $0x444d4163, %%ecx\n" "jne TryCyrix\n\t" "jmp AMD\n\t" /* Check for Cyrix */ "\nTryCyrix:\n\t" "cmpl $0x69727943, %%ebx\n\t" "jne NotSupported2\n\t" "cmpl $0x736e4978, %%edx\n\t" "jne NotSupported3\n\t" "cmpl $0x64616574, %%ecx\n\t" "jne NotSupported4\n\t" /* Drop through to Cyrix... */ /* Cyrix Section */ /* See if extended CPUID is supported */ "movl $0x80000000, %%eax\n\t" "cpuid\n\t" "cmpl $0x80000000, %%eax\n\t" "jl MMXtest\n\t" /* Try standard CPUID instead */ /* Extended CPUID supported, so get extended features */ "movl $0x80000001, %%eax\n\t" "cpuid\n\t" "testl $0x00800000, %%eax\n\t" /* Test for MMX */ "jz NotSupported5\n\t" /* MMX not supported */ "testl $0x01000000, %%eax\n\t" /* Test for Ext'd MMX */ "jnz EMMXSupported\n\t" "movl $1, %0:\n\n\t" /* MMX Supported */ "jmp Return\n\n" "EMMXSupported:\n\t" "movl $3, %0:\n\n\t" /* EMMX and MMX Supported */ "jmp Return\n\t" /* AMD Section */ "AMD:\n\t" /* See if extended CPUID is supported */ "movl $0x80000000, %%eax\n\t" "cpuid\n\t" "cmpl $0x80000000, %%eax\n\t" "jl MMXtest\n\t" /* Try standard CPUID instead */ /* Extended CPUID supported, so get extended features */ "movl $0x80000001, %%eax\n\t" "cpuid\n\t" "testl $0x00800000, %%edx\n\t" /* Test for MMX */ "jz NotSupported6\n\t" /* MMX not supported */ "testl $0x80000000, %%edx\n\t" /* Test for 3DNow! */ "jnz ThreeDNowSupported\n\t" "movl $1, %0:\n\n\t" /* MMX Supported */ "jmp Return\n\n" "ThreeDNowSupported:\n\t" "movl $5, %0:\n\n\t" /* 3DNow! and MMX Supported */ "jmp Return\n\t" /* Intel Section */ "Intel:\n\t" /* Check for MMX */ "MMXtest:\n\t" "movl $1, %%eax\n\t" "cpuid\n\t" "testl $0x00800000, %%edx\n\t" /* Test for MMX */ "jz NotSupported7\n\t" /* MMX Not supported */ "movl $1, %0:\n\n\t" /* MMX Supported */ "jmp Return\n\t" /* Nothing supported */ "\nNotSupported1:\n\t" "#movl $101, %0:\n\n\t" "\nNotSupported2:\n\t" "#movl $102, %0:\n\n\t" "\nNotSupported3:\n\t" "#movl $103, %0:\n\n\t" "\nNotSupported4:\n\t" "#movl $104, %0:\n\n\t" "\nNotSupported5:\n\t" "#movl $105, %0:\n\n\t" "\nNotSupported6:\n\t" "#movl $106, %0:\n\n\t" "\nNotSupported7:\n\t" "#movl $107, %0:\n\n\t" "movl $0, %0:\n\n\t" "Return:\n\t" : "=a" (rval) : /* no input */ : "eax", "ebx", "ecx", "edx" ); /* Return */ return(rval);}/* Function to test if mmx instructions are supported...*/inline extern intmmx_ok(void){ /* Returns 1 if MMX instructions are supported, 0 otherwise */ return ( mm_support() & 0x1 );}/* Helper functions for the instruction macros that follow... (note that memory-to-register, m2r, instructions are nearly as efficient as register-to-register, r2r, instructions; however, memory-to-memory instructions are really simulated as a convenience, and are only 1/3 as efficient)*/#ifdef MMX_TRACE/* Include the stuff for printing a trace to stderr...*/#include <stdio.h>#define mmx_i2r(op, imm, reg) \ { \ mmx_t mmx_trace; \ mmx_trace = (imm); \ fprintf(stderr, #op "_i2r(" #imm "=0x%016llx, ", mmx_trace.q); \ __asm__ __volatile__ ("movq %%" #reg ", %0" \ : "=X" (mmx_trace) \ : /* nothing */ ); \ fprintf(stderr, #reg "=0x%016llx) => ", mmx_trace.q); \ __asm__ __volatile__ (#op " %0, %%" #reg \ : /* nothing */ \ : "X" (imm)); \ __asm__ __volatile__ ("movq %%" #reg ", %0" \ : "=X" (mmx_trace) \ : /* nothing */ ); \ fprintf(stderr, #reg "=0x%016llx\n", mmx_trace.q); \ }#define mmx_m2r(op, mem, reg) \ { \ mmx_t mmx_trace; \ mmx_trace = (mem); \ fprintf(stderr, #op "_m2r(" #mem "=0x%016llx, ", mmx_trace.q); \ __asm__ __volatile__ ("movq %%" #reg ", %0" \ : "=X" (mmx_trace) \ : /* nothing */ ); \ fprintf(stderr, #reg "=0x%016llx) => ", mmx_trace.q); \ __asm__ __volatile__ (#op " %0, %%" #reg \ : /* nothing */ \ : "X" (mem)); \ __asm__ __volatile__ ("movq %%" #reg ", %0" \ : "=X" (mmx_trace) \ : /* nothing */ ); \ fprintf(stderr, #reg "=0x%016llx\n", mmx_trace.q); \ }#define mmx_r2m(op, reg, mem) \ { \ mmx_t mmx_trace; \ __asm__ __volatile__ ("movq %%" #reg ", %0" \ : "=X" (mmx_trace) \ : /* nothing */ ); \ fprintf(stderr, #op "_r2m(" #reg "=0x%016llx, ", mmx_trace.q); \ mmx_trace = (mem); \ fprintf(stderr, #mem "=0x%016llx) => ", mmx_trace.q); \ __asm__ __volatile__ (#op " %%" #reg ", %0" \ : "=X" (mem) \ : /* nothing */ ); \ mmx_trace = (mem); \ fprintf(stderr, #mem "=0x%016llx\n", mmx_trace.q); \ }#define mmx_r2r(op, regs, regd) \ { \ mmx_t mmx_trace; \ __asm__ __volatile__ ("movq %%" #regs ", %0" \ : "=X" (mmx_trace) \ : /* nothing */ ); \ fprintf(stderr, #op "_r2r(" #regs "=0x%016llx, ", mmx_trace.q); \ __asm__ __volatile__ ("movq %%" #regd ", %0" \ : "=X" (mmx_trace) \ : /* nothing */ ); \ fprintf(stderr, #regd "=0x%016llx) => ", mmx_trace.q); \ __asm__ __volatile__ (#op " %" #regs ", %" #regd); \ __asm__ __volatile__ ("movq %%" #regd ", %0" \ : "=X" (mmx_trace) \ : /* nothing */ ); \ fprintf(stderr, #regd "=0x%016llx\n", mmx_trace.q); \ }#define mmx_m2m(op, mems, memd) \ { \ mmx_t mmx_trace; \ mmx_trace = (mems); \ fprintf(stderr, #op "_m2m(" #mems "=0x%016llx, ", mmx_trace.q); \ mmx_trace = (memd); \ fprintf(stderr, #memd "=0x%016llx) => ", mmx_trace.q); \ __asm__ __volatile__ ("movq %0, %%mm0\n\t" \ #op " %1, %%mm0\n\t" \ "movq %%mm0, %0" \ : "=X" (memd) \ : "X" (mems)); \ mmx_trace = (memd); \ fprintf(stderr, #memd "=0x%016llx\n", mmx_trace.q); \ }#else/* These macros are a lot simpler without the tracing...
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -