Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

fastmemcpy.h

00001 /*****************************************************************************
00002  * fastmemcpy.h : fast memcpy routines
00003  *****************************************************************************
00004  * $Id: fastmemcpy.h 11709 2005-07-11 16:20:33Z massiot $
00005  *
00006  * Authors: various Linux kernel hackers
00007  *          various MPlayer hackers
00008  *          Nick Kurshev <[email protected]>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  * 
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00023  *****************************************************************************/
00024 
00025 /*
00026   aclib - advanced C library ;)
00027   This file contains functions which improve and expand standard C-library
00028 */
00029 
00030 #define BLOCK_SIZE 4096
00031 #define CONFUSION_FACTOR 0
00032 /*Feel free to fine-tune the above 2, it might be possible to get some speedup with them :)*/
00033 
00034 /*#define STATISTICS*/
00035 
00036 #ifndef HAVE_SSE2
00037 /*
00038    P3 processor has only one SSE decoder so can execute only 1 sse insn per
00039    cpu clock, but it has 3 mmx decoders (include load/store unit)
00040    and executes 3 mmx insns per cpu clock.
00041    P4 processor has some chances, but after reading:
00042    http://www.emulators.com/pentium4.htm
00043    I have doubts. Anyway SSE2 version of this code can be written better.
00044 */
00045 #undef HAVE_SSE
00046 #endif
00047 
00048 
00049 /*
00050  This part of code was taken by me from Linux-2.4.3 and slightly modified
00051 for MMX, MMX2, SSE instruction set. I have done it since linux uses page aligned
00052 blocks but mplayer uses weakly ordered data and original sources can not
00053 speedup them. Only using PREFETCHNTA and MOVNTQ together have effect!
00054 
00055 >From IA-32 Intel Architecture Software Developer's Manual Volume 1,
00056 
00057 Order Number 245470:
00058 "10.4.6. Cacheability Control, Prefetch, and Memory Ordering Instructions"
00059 
00060 Data referenced by a program can be temporal (data will be used again) or
00061 non-temporal (data will be referenced once and not reused in the immediate
00062 future). To make efficient use of the processor's caches, it is generally
00063 desirable to cache temporal data and not cache non-temporal data. Overloading
00064 the processor's caches with non-temporal data is sometimes referred to as
00065 "polluting the caches".
00066 The non-temporal data is written to memory with Write-Combining semantics.
00067 
00068 The PREFETCHh instructions permits a program to load data into the processor
00069 at a suggested cache level, so that it is closer to the processors load and
00070 store unit when it is needed. If the data is already present in a level of
00071 the cache hierarchy that is closer to the processor, the PREFETCHh instruction
00072 will not result in any data movement.
00073 But we should you PREFETCHNTA: Non-temporal data fetch data into location
00074 close to the processor, minimizing cache pollution.
00075 
00076 The MOVNTQ (store quadword using non-temporal hint) instruction stores
00077 packed integer data from an MMX register to memory, using a non-temporal hint.
00078 The MOVNTPS (store packed single-precision floating-point values using
00079 non-temporal hint) instruction stores packed floating-point data from an
00080 XMM register to memory, using a non-temporal hint.
00081 
00082 The SFENCE (Store Fence) instruction controls write ordering by creating a
00083 fence for memory store operations. This instruction guarantees that the results
00084 of every store instruction that precedes the store fence in program order is
00085 globally visible before any store instruction that follows the fence. The
00086 SFENCE instruction provides an efficient way of ensuring ordering between
00087 procedures that produce weakly-ordered data and procedures that consume that
00088 data.
00089 
00090 If you have questions please contact with me: Nick Kurshev: [email protected].
00091 */
00092 
00093 /* 3dnow memcpy support from kernel 2.4.2 */
00094 /*  by Pontscho/fresh!mindworkz           */
00095 
00096 #if defined( HAVE_MMX2 ) || defined( HAVE_3DNOW ) || defined( HAVE_MMX )
00097 
00098 #undef HAVE_MMX1
00099 #if defined(HAVE_MMX) && !defined(HAVE_MMX2) && !defined(HAVE_3DNOW) && !defined(HAVE_SSE)
00100 /*  means: mmx v.1. Note: Since we added alignment of destinition it speedups
00101     of memory copying on PentMMX, Celeron-1 and P2 upto 12% versus
00102     standard (non MMX-optimized) version.
00103     Note: on K6-2+ it speedups memory copying upto 25% and
00104           on K7 and P3 about 500% (5 times). */
00105 #define HAVE_MMX1
00106 #endif
00107 
00108 
00109 #undef HAVE_K6_2PLUS
00110 #if !defined( HAVE_MMX2) && defined( HAVE_3DNOW)
00111 #define HAVE_K6_2PLUS
00112 #endif
00113 
00114 /* for small memory blocks (<256 bytes) this version is faster */
00115 #define small_memcpy(to,from,n)\
00116 {\
00117 register unsigned long int dummy;\
00118 __asm__ __volatile__(\
00119         "rep; movsb"\
00120         :"=&D"(to), "=&S"(from), "=&c"(dummy)\
00121 /* It's most portable way to notify compiler */\
00122 /* that edi, esi and ecx are clobbered in asm block. */\
00123 /* Thanks to A'rpi for hint!!! */\
00124         :"0" (to), "1" (from),"2" (n)\
00125         : "memory");\
00126 }
00127 
00128 #ifdef HAVE_SSE
00129 #define MMREG_SIZE 16
00130 #else
00131 #define MMREG_SIZE 64 /*8*/
00132 #endif
00133 
00134 /* Small defines (for readability only) ;) */
00135 #ifdef HAVE_K6_2PLUS
00136 #define PREFETCH "prefetch"
00137 /* On K6 femms is faster of emms. On K7 femms is directly mapped on emms. */
00138 #define EMMS     "femms"
00139 #else
00140 #define PREFETCH "prefetchnta"
00141 #define EMMS     "emms"
00142 #endif
00143 
00144 #ifdef HAVE_MMX2
00145 #define MOVNTQ "movntq"
00146 #else
00147 #define MOVNTQ "movq"
00148 #endif
00149 
00150 #ifdef HAVE_MMX1
00151 #define MIN_LEN 0x800  /* 2K blocks */
00152 #else
00153 #define MIN_LEN 0x40  /* 64-byte blocks */
00154 #endif
00155 
00156 void * fast_memcpy(void * to, const void * from, size_t len)
00157 {
00158         void *retval;
00159         size_t i;
00160         retval = to;
00161 #ifdef STATISTICS
00162         {
00163                 static int freq[33];
00164                 static int t=0;
00165                 int i;
00166                 for(i=0; len>(1<<i); i++);
00167                 freq[i]++;
00168                 t++;
00169                 if(1024*1024*1024 % t == 0)
00170                         for(i=0; i<32; i++)
00171                                 printf("freq < %8d %4d\n", 1<<i, freq[i]);
00172         }
00173 #endif
00174 #ifndef HAVE_MMX1
00175         /* PREFETCH has effect even for MOVSB instruction ;) */
00176         __asm__ __volatile__ (
00177                 PREFETCH" (%0)\n"
00178                 PREFETCH" 64(%0)\n"
00179                 PREFETCH" 128(%0)\n"
00180                 PREFETCH" 192(%0)\n"
00181                 PREFETCH" 256(%0)\n"
00182                 : : "r" (from) );
00183 #endif
00184         if(len >= MIN_LEN)
00185         {
00186           register unsigned long int delta;
00187           /* Align destinition to MMREG_SIZE -boundary */
00188           delta = ((unsigned long int)to)&(MMREG_SIZE-1);
00189           if(delta)
00190           {
00191             delta=MMREG_SIZE-delta;
00192             len -= delta;
00193             small_memcpy(to, from, delta);
00194           }
00195           i = len >> 6; /* len/64 */
00196           len&=63;
00197         /*
00198            This algorithm is top effective when the code consequently
00199            reads and writes blocks which have size of cache line.
00200            Size of cache line is processor-dependent.
00201            It will, however, be a minimum of 32 bytes on any processors.
00202            It would be better to have a number of instructions which
00203            perform reading and writing to be multiple to a number of
00204            processor's decoders, but it's not always possible.
00205         */
00206 #ifdef HAVE_SSE /* Only P3 (may be Cyrix3) */
00207         if(((unsigned long)from) & 15)
00208         /* if SRC is misaligned */
00209         for(; i>0; i--)
00210         {
00211                 __asm__ __volatile__ (
00212                 PREFETCH" 320(%0)\n"
00213                 "movups (%0), %%xmm0\n"
00214                 "movups 16(%0), %%xmm1\n"
00215                 "movups 32(%0), %%xmm2\n"
00216                 "movups 48(%0), %%xmm3\n"
00217                 "movntps %%xmm0, (%1)\n"
00218                 "movntps %%xmm1, 16(%1)\n"
00219                 "movntps %%xmm2, 32(%1)\n"
00220                 "movntps %%xmm3, 48(%1)\n"
00221                 :: "r" (from), "r" (to) : "memory");
00222                 ((const unsigned char *)from)+=64;
00223                 ((unsigned char *)to)+=64;
00224         }
00225         else
00226         /*
00227            Only if SRC is aligned on 16-byte boundary.
00228            It allows to use movaps instead of movups, which required data
00229            to be aligned or a general-protection exception (#GP) is generated.
00230         */
00231         for(; i>0; i--)
00232         {
00233                 __asm__ __volatile__ (
00234                 PREFETCH" 320(%0)\n"
00235                 "movaps (%0), %%xmm0\n"
00236                 "movaps 16(%0), %%xmm1\n"
00237                 "movaps 32(%0), %%xmm2\n"
00238                 "movaps 48(%0), %%xmm3\n"
00239                 "movntps %%xmm0, (%1)\n"
00240                 "movntps %%xmm1, 16(%1)\n"
00241                 "movntps %%xmm2, 32(%1)\n"
00242                 "movntps %%xmm3, 48(%1)\n"
00243                 :: "r" (from), "r" (to) : "memory");
00244                 ((const unsigned char *)from)+=64;
00245                 ((unsigned char *)to)+=64;
00246         }
00247 #else
00248         /* Align destination at BLOCK_SIZE boundary */
00249         for(; ((ptrdiff_t)to & (BLOCK_SIZE-1)) && i>0; i--)
00250         {
00251                 __asm__ __volatile__ (
00252 #ifndef HAVE_MMX1
00253                 PREFETCH" 320(%0)\n"
00254 #endif
00255                 "movq (%0), %%mm0\n"
00256                 "movq 8(%0), %%mm1\n"
00257                 "movq 16(%0), %%mm2\n"
00258                 "movq 24(%0), %%mm3\n"
00259                 "movq 32(%0), %%mm4\n"
00260                 "movq 40(%0), %%mm5\n"
00261                 "movq 48(%0), %%mm6\n"
00262                 "movq 56(%0), %%mm7\n"
00263                 MOVNTQ" %%mm0, (%1)\n"
00264                 MOVNTQ" %%mm1, 8(%1)\n"
00265                 MOVNTQ" %%mm2, 16(%1)\n"
00266                 MOVNTQ" %%mm3, 24(%1)\n"
00267                 MOVNTQ" %%mm4, 32(%1)\n"
00268                 MOVNTQ" %%mm5, 40(%1)\n"
00269                 MOVNTQ" %%mm6, 48(%1)\n"
00270                 MOVNTQ" %%mm7, 56(%1)\n"
00271                 :: "r" (from), "r" (to) : "memory");
00272                 from = (const void *) (((const unsigned char *)from)+64);
00273                 to = (void *) (((unsigned char *)to)+64);
00274         }
00275 
00276 /*      printf(" %p %p\n", (ptrdiff_t)from&1023, (ptrdiff_t)to&1023); */
00277         /* Pure Assembly cuz gcc is a bit unpredictable ;) */
00278 # if 0
00279         if(i>=BLOCK_SIZE/64)
00280                 asm volatile(
00281                         "xorl %%eax, %%eax      \n\t"
00282                         ".balign 16             \n\t"
00283                         "1:                     \n\t"
00284                                 "movl (%0, %%eax), %%ebx        \n\t"
00285                                 "movl 32(%0, %%eax), %%ebx      \n\t"
00286                                 "movl 64(%0, %%eax), %%ebx      \n\t"
00287                                 "movl 96(%0, %%eax), %%ebx      \n\t"
00288                                 "addl $128, %%eax               \n\t"
00289                                 "cmpl %3, %%eax                 \n\t"
00290                                 " jb 1b                         \n\t"
00291 
00292                         "xorl %%eax, %%eax      \n\t"
00293 
00294                                 ".balign 16             \n\t"
00295                                 "2:                     \n\t"
00296                                 "movq (%0, %%eax), %%mm0\n"
00297                                 "movq 8(%0, %%eax), %%mm1\n"
00298                                 "movq 16(%0, %%eax), %%mm2\n"
00299                                 "movq 24(%0, %%eax), %%mm3\n"
00300                                 "movq 32(%0, %%eax), %%mm4\n"
00301                                 "movq 40(%0, %%eax), %%mm5\n"
00302                                 "movq 48(%0, %%eax), %%mm6\n"
00303                                 "movq 56(%0, %%eax), %%mm7\n"
00304                                 MOVNTQ" %%mm0, (%1, %%eax)\n"
00305                                 MOVNTQ" %%mm1, 8(%1, %%eax)\n"
00306                                 MOVNTQ" %%mm2, 16(%1, %%eax)\n"
00307                                 MOVNTQ" %%mm3, 24(%1, %%eax)\n"
00308                                 MOVNTQ" %%mm4, 32(%1, %%eax)\n"
00309                                 MOVNTQ" %%mm5, 40(%1, %%eax)\n"
00310                                 MOVNTQ" %%mm6, 48(%1, %%eax)\n"
00311                                 MOVNTQ" %%mm7, 56(%1, %%eax)\n"
00312                                 "addl $64, %%eax                \n\t"
00313                                 "cmpl %3, %%eax         \n\t"
00314                                 "jb 2b                          \n\t"
00315 
00316 #if CONFUSION_FACTOR > 0
00317         /* a few percent speedup on out of order executing CPUs */
00318                         "movl %5, %%eax         \n\t"
00319                                 "2:                     \n\t"
00320                                 "movl (%0), %%ebx       \n\t"
00321                                 "movl (%0), %%ebx       \n\t"
00322                                 "movl (%0), %%ebx       \n\t"
00323                                 "movl (%0), %%ebx       \n\t"
00324                                 "decl %%eax             \n\t"
00325                                 " jnz 2b                \n\t"
00326 #endif
00327 
00328                         "xorl %%eax, %%eax      \n\t"
00329                         "addl %3, %0            \n\t"
00330                         "addl %3, %1            \n\t"
00331                         "subl %4, %2            \n\t"
00332                         "cmpl %4, %2            \n\t"
00333                         " jae 1b                \n\t"
00334                                 : "+r" (from), "+r" (to), "+r" (i)
00335                                 : "r" (BLOCK_SIZE), "i" (BLOCK_SIZE/64), "i" (CONFUSION_FACTOR)
00336                                 : "%eax", "%ebx"
00337                 );
00338 #endif
00339 
00340         for(; i>0; i--)
00341         {
00342                 __asm__ __volatile__ (
00343 #ifndef HAVE_MMX1
00344                 PREFETCH" 320(%0)\n"
00345 #endif
00346                 "movq (%0), %%mm0\n"
00347                 "movq 8(%0), %%mm1\n"
00348                 "movq 16(%0), %%mm2\n"
00349                 "movq 24(%0), %%mm3\n"
00350                 "movq 32(%0), %%mm4\n"
00351                 "movq 40(%0), %%mm5\n"
00352                 "movq 48(%0), %%mm6\n"
00353                 "movq 56(%0), %%mm7\n"
00354                 MOVNTQ" %%mm0, (%1)\n"
00355                 MOVNTQ" %%mm1, 8(%1)\n"
00356                 MOVNTQ" %%mm2, 16(%1)\n"
00357                 MOVNTQ" %%mm3, 24(%1)\n"
00358                 MOVNTQ" %%mm4, 32(%1)\n"
00359                 MOVNTQ" %%mm5, 40(%1)\n"
00360                 MOVNTQ" %%mm6, 48(%1)\n"
00361                 MOVNTQ" %%mm7, 56(%1)\n"
00362                 :: "r" (from), "r" (to) : "memory");
00363                 from = (const void *) (((const unsigned char *)from)+64);
00364                 to = (void *) (((unsigned char *)to)+64);
00365         }
00366 
00367 #endif /* Have SSE */
00368 #ifdef HAVE_MMX2
00369                 /* since movntq is weakly-ordered, a "sfence"
00370                  * is needed to become ordered again. */
00371                 __asm__ __volatile__ ("sfence":::"memory");
00372 #endif
00373 #ifndef HAVE_SSE
00374                 /* enables to use FPU */
00375                 __asm__ __volatile__ (EMMS:::"memory");
00376 #endif
00377         }
00378         /*
00379          *      Now do the tail of the block
00380          */
00381         if(len) small_memcpy(to, from, len);
00382         return retval;
00383 }
00384 
00385 
00386 #endif /* #if defined( HAVE_MMX2 ) || defined( HAVE_3DNOW ) || defined( HAVE_MMX ) */

Generated on Tue Dec 20 10:14:48 2005 for vlc-0.8.4a by  doxygen 1.4.2