]> git.sesse.net Git - vlc/blob - modules/mmx/fastmemcpy.h
MMX memcpy: set clobber list
[vlc] / modules / mmx / fastmemcpy.h
1 /*****************************************************************************
2  * fastmemcpy.h : fast memcpy routines
3  *****************************************************************************
4  * $Id$
5  *
6  * Authors: various Linux kernel hackers
7  *          various MPlayer hackers
8  *          Nick Kurshev <nickols_k@mail.ru>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*
26   aclib - advanced C library ;)
27   This file contains functions which improve and expand standard C-library
28 */
29
30 #define BLOCK_SIZE 4096
31 #define CONFUSION_FACTOR 0
32 /*Feel free to fine-tune the above 2, it might be possible to get some speedup with them :)*/
33
34 /*#define STATISTICS*/
35
36 #ifndef HAVE_SSE2
37 /*
38    P3 processor has only one SSE decoder so can execute only 1 sse insn per
39    cpu clock, but it has 3 mmx decoders (include load/store unit)
40    and executes 3 mmx insns per cpu clock.
41    P4 processor has some chances, but after reading:
42    http://www.emulators.com/pentium4.htm
43    I have doubts. Anyway SSE2 version of this code can be written better.
44 */
45 #undef HAVE_SSE
46 #endif
47
48
49 /*
50  This part of code was taken by me from Linux-2.4.3 and slightly modified
51 for MMX, MMX2, SSE instruction set. I have done it since linux uses page aligned
52 blocks but mplayer uses weakly ordered data and original sources can not
53 speedup them. Only using PREFETCHNTA and MOVNTQ together have effect!
54
55 >From IA-32 Intel Architecture Software Developer's Manual Volume 1,
56
57 Order Number 245470:
58 "10.4.6. Cacheability Control, Prefetch, and Memory Ordering Instructions"
59
60 Data referenced by a program can be temporal (data will be used again) or
61 non-temporal (data will be referenced once and not reused in the immediate
62 future). To make efficient use of the processor's caches, it is generally
63 desirable to cache temporal data and not cache non-temporal data. Overloading
64 the processor's caches with non-temporal data is sometimes referred to as
65 "polluting the caches".
66 The non-temporal data is written to memory with Write-Combining semantics.
67
68 The PREFETCHh instructions permits a program to load data into the processor
69 at a suggested cache level, so that it is closer to the processors load and
70 store unit when it is needed. If the data is already present in a level of
71 the cache hierarchy that is closer to the processor, the PREFETCHh instruction
72 will not result in any data movement.
73 But we should you PREFETCHNTA: Non-temporal data fetch data into location
74 close to the processor, minimizing cache pollution.
75
76 The MOVNTQ (store quadword using non-temporal hint) instruction stores
77 packed integer data from an MMX register to memory, using a non-temporal hint.
78 The MOVNTPS (store packed single-precision floating-point values using
79 non-temporal hint) instruction stores packed floating-point data from an
80 XMM register to memory, using a non-temporal hint.
81
82 The SFENCE (Store Fence) instruction controls write ordering by creating a
83 fence for memory store operations. This instruction guarantees that the results
84 of every store instruction that precedes the store fence in program order is
85 globally visible before any store instruction that follows the fence. The
86 SFENCE instruction provides an efficient way of ensuring ordering between
87 procedures that produce weakly-ordered data and procedures that consume that
88 data.
89
90 If you have questions please contact with me: Nick Kurshev: nickols_k@mail.ru.
91 */
92
93 /* 3dnow memcpy support from kernel 2.4.2 */
94 /*  by Pontscho/fresh!mindworkz           */
95
96 #if defined( HAVE_MMX2 ) || defined( HAVE_3DNOW ) || defined( HAVE_MMX )
97
98 #undef HAVE_MMX1
99 #if defined(HAVE_MMX) && !defined(HAVE_MMX2) && !defined(HAVE_3DNOW) && !defined(HAVE_SSE)
100 /*  means: mmx v.1. Note: Since we added alignment of destinition it speedups
101     of memory copying on PentMMX, Celeron-1 and P2 upto 12% versus
102     standard (non MMX-optimized) version.
103     Note: on K6-2+ it speedups memory copying upto 25% and
104           on K7 and P3 about 500% (5 times). */
105 #define HAVE_MMX1
106 #endif
107
108
109 #undef HAVE_K6_2PLUS
110 #if !defined( HAVE_MMX2) && defined( HAVE_3DNOW)
111 #define HAVE_K6_2PLUS
112 #endif
113
114 /* for small memory blocks (<256 bytes) this version is faster */
115 #define small_memcpy(to,from,n)\
116 {\
117 register unsigned long int dummy;\
118 __asm__ __volatile__(\
119     "rep; movsb"\
120     :"=&D"(to), "=&S"(from), "=&c"(dummy)\
121 /* It's most portable way to notify compiler */\
122 /* that edi, esi and ecx are clobbered in asm block. */\
123 /* Thanks to A'rpi for hint!!! */\
124         :"0" (to), "1" (from),"2" (n)\
125     : "memory");\
126 }
127
128 #ifdef HAVE_SSE
129 #define MMREG_SIZE 16
130 #else
131 #define MMREG_SIZE 64 /*8*/
132 #endif
133
134 /* Small defines (for readability only) ;) */
135 #ifdef HAVE_K6_2PLUS
136 #define PREFETCH "prefetch"
137 /* On K6 femms is faster of emms. On K7 femms is directly mapped on emms. */
138 #define EMMS     "femms"
139 #else
140 #define PREFETCH "prefetchnta"
141 #define EMMS     "emms"
142 #endif
143
144 #ifdef HAVE_MMX2
145 #define MOVNTQ "movntq"
146 #else
147 #define MOVNTQ "movq"
148 #endif
149
150 #ifdef HAVE_MMX1
151 #define MIN_LEN 0x800  /* 2K blocks */
152 #else
153 #define MIN_LEN 0x40  /* 64-byte blocks */
154 #endif
155
156 #ifdef HAVE_SSE
157 VLC_SSE
158 #else
159 VLC_MMX
160 #endif
161 static void * fast_memcpy(void * to, const void * from, size_t len)
162 {
163     void *retval;
164     size_t i;
165     retval = to;
166 #ifdef STATISTICS
167     {
168         static int freq[33];
169         static int t=0;
170         int i;
171         for(i=0; len>(1<<i); i++);
172         freq[i]++;
173         t++;
174         if(1024*1024*1024 % t == 0)
175             for(i=0; i<32; i++)
176                 printf("freq < %8d %4d\n", 1<<i, freq[i]);
177     }
178 #endif
179 #ifndef HAVE_MMX1
180         /* PREFETCH has effect even for MOVSB instruction ;) */
181     __asm__ __volatile__ (
182             PREFETCH" (%0)\n"
183             PREFETCH" 64(%0)\n"
184             PREFETCH" 128(%0)\n"
185             PREFETCH" 192(%0)\n"
186             PREFETCH" 256(%0)\n"
187         : : "r" (from) );
188 #endif
189         if(len >= MIN_LEN)
190     {
191       register unsigned long int delta;
192           /* Align destinition to MMREG_SIZE -boundary */
193           delta = ((unsigned long int)to)&(MMREG_SIZE-1);
194           if(delta)
195       {
196         delta=MMREG_SIZE-delta;
197         len -= delta;
198         small_memcpy(to, from, delta);
199       }
200       i = len >> 6; /* len/64 */
201       len&=63;
202         /*
203            This algorithm is top effective when the code consequently
204            reads and writes blocks which have size of cache line.
205            Size of cache line is processor-dependent.
206            It will, however, be a minimum of 32 bytes on any processors.
207            It would be better to have a number of instructions which
208            perform reading and writing to be multiple to a number of
209            processor's decoders, but it's not always possible.
210         */
211 #ifdef HAVE_SSE /* Only P3 (may be Cyrix3) */
212     if(((unsigned long)from) & 15)
213     /* if SRC is misaligned */
214     for(; i>0; i--)
215     {
216         __asm__ __volatile__ (
217         PREFETCH" 320(%0)\n"
218         "movups (%0), %%xmm0\n"
219         "movups 16(%0), %%xmm1\n"
220         "movups 32(%0), %%xmm2\n"
221         "movups 48(%0), %%xmm3\n"
222         "movntps %%xmm0, (%1)\n"
223         "movntps %%xmm1, 16(%1)\n"
224         "movntps %%xmm2, 32(%1)\n"
225         "movntps %%xmm3, 48(%1)\n"
226         :: "r" (from), "r" (to) : "memory", "xmm0", "xmm1", "xmm2", "xmm3");
227         ((const unsigned char *)from)+=64;
228         ((unsigned char *)to)+=64;
229     }
230     else
231     /*
232        Only if SRC is aligned on 16-byte boundary.
233        It allows using movaps instead of movups, which required data
234        to be aligned or a general-protection exception (#GP) is generated.
235     */
236     for(; i>0; i--)
237     {
238         __asm__ __volatile__ (
239         PREFETCH" 320(%0)\n"
240         "movaps (%0), %%xmm0\n"
241         "movaps 16(%0), %%xmm1\n"
242         "movaps 32(%0), %%xmm2\n"
243         "movaps 48(%0), %%xmm3\n"
244         "movntps %%xmm0, (%1)\n"
245         "movntps %%xmm1, 16(%1)\n"
246         "movntps %%xmm2, 32(%1)\n"
247         "movntps %%xmm3, 48(%1)\n"
248         :: "r" (from), "r" (to) : "memory", "xmm0", "xmm1", "xmm2", "xmm3");
249         ((const unsigned char *)from)+=64;
250         ((unsigned char *)to)+=64;
251     }
252 #else
253     /* Align destination at BLOCK_SIZE boundary */
254     for(; ((uintptr_t)to & (BLOCK_SIZE-1)) && i>0; i--)
255     {
256         __asm__ __volatile__ (
257 #ifndef HAVE_MMX1
258             PREFETCH" 320(%0)\n"
259 #endif
260         "movq (%0), %%mm0\n"
261         "movq 8(%0), %%mm1\n"
262         "movq 16(%0), %%mm2\n"
263         "movq 24(%0), %%mm3\n"
264         "movq 32(%0), %%mm4\n"
265         "movq 40(%0), %%mm5\n"
266         "movq 48(%0), %%mm6\n"
267         "movq 56(%0), %%mm7\n"
268         MOVNTQ" %%mm0, (%1)\n"
269         MOVNTQ" %%mm1, 8(%1)\n"
270         MOVNTQ" %%mm2, 16(%1)\n"
271         MOVNTQ" %%mm3, 24(%1)\n"
272         MOVNTQ" %%mm4, 32(%1)\n"
273         MOVNTQ" %%mm5, 40(%1)\n"
274         MOVNTQ" %%mm6, 48(%1)\n"
275         MOVNTQ" %%mm7, 56(%1)\n"
276         :: "r" (from), "r" (to) : "memory", "mm0", "mm1", "mm2", "mm3",
277                                             "mm4", "mm5", "mm6", "mm7");
278                 from = (const void *) (((const unsigned char *)from)+64);
279         to = (void *) (((unsigned char *)to)+64);
280     }
281
282 /*    printf(" %p %p\n", (uintptr_t)from&1023, (uintptr_t)to&1023); */
283     /* Pure Assembly cuz gcc is a bit unpredictable ;) */
284 # if 0
285     if(i>=BLOCK_SIZE/64)
286         asm volatile(
287             "xorl %%eax, %%eax    \n\t"
288             ".balign 16        \n\t"
289             "1:            \n\t"
290                 "movl (%0, %%eax), %%ebx     \n\t"
291                 "movl 32(%0, %%eax), %%ebx     \n\t"
292                 "movl 64(%0, %%eax), %%ebx     \n\t"
293                 "movl 96(%0, %%eax), %%ebx     \n\t"
294                 "addl $128, %%eax        \n\t"
295                 "cmpl %3, %%eax            \n\t"
296                 " jb 1b                \n\t"
297
298             "xorl %%eax, %%eax    \n\t"
299
300                 ".balign 16        \n\t"
301                 "2:            \n\t"
302                 "movq (%0, %%eax), %%mm0\n"
303                 "movq 8(%0, %%eax), %%mm1\n"
304                 "movq 16(%0, %%eax), %%mm2\n"
305                 "movq 24(%0, %%eax), %%mm3\n"
306                 "movq 32(%0, %%eax), %%mm4\n"
307                 "movq 40(%0, %%eax), %%mm5\n"
308                 "movq 48(%0, %%eax), %%mm6\n"
309                 "movq 56(%0, %%eax), %%mm7\n"
310                 MOVNTQ" %%mm0, (%1, %%eax)\n"
311                 MOVNTQ" %%mm1, 8(%1, %%eax)\n"
312                 MOVNTQ" %%mm2, 16(%1, %%eax)\n"
313                 MOVNTQ" %%mm3, 24(%1, %%eax)\n"
314                 MOVNTQ" %%mm4, 32(%1, %%eax)\n"
315                 MOVNTQ" %%mm5, 40(%1, %%eax)\n"
316                 MOVNTQ" %%mm6, 48(%1, %%eax)\n"
317                 MOVNTQ" %%mm7, 56(%1, %%eax)\n"
318                 "addl $64, %%eax        \n\t"
319                 "cmpl %3, %%eax        \n\t"
320                 "jb 2b                \n\t"
321
322 #if CONFUSION_FACTOR > 0
323     /* a few percent speedup on out of order executing CPUs */
324             "movl %5, %%eax        \n\t"
325                 "2:            \n\t"
326                 "movl (%0), %%ebx    \n\t"
327                 "movl (%0), %%ebx    \n\t"
328                 "movl (%0), %%ebx    \n\t"
329                 "movl (%0), %%ebx    \n\t"
330                 "decl %%eax        \n\t"
331                 " jnz 2b        \n\t"
332 #endif
333
334             "xorl %%eax, %%eax    \n\t"
335             "addl %3, %0        \n\t"
336             "addl %3, %1        \n\t"
337             "subl %4, %2        \n\t"
338             "cmpl %4, %2        \n\t"
339             " jae 1b        \n\t"
340                 : "+r" (from), "+r" (to), "+r" (i)
341                 : "r" (BLOCK_SIZE), "i" (BLOCK_SIZE/64), "i" (CONFUSION_FACTOR)
342                 : "%eax", "%ebx", "mm0", "mm1", "mm2", "mm3",
343                                   "mm4", "mm5", "mm6", "mm7"
344         );
345 #endif
346
347     for(; i>0; i--)
348     {
349         __asm__ __volatile__ (
350 #ifndef HAVE_MMX1
351             PREFETCH" 320(%0)\n"
352 #endif
353         "movq (%0), %%mm0\n"
354         "movq 8(%0), %%mm1\n"
355         "movq 16(%0), %%mm2\n"
356         "movq 24(%0), %%mm3\n"
357         "movq 32(%0), %%mm4\n"
358         "movq 40(%0), %%mm5\n"
359         "movq 48(%0), %%mm6\n"
360         "movq 56(%0), %%mm7\n"
361         MOVNTQ" %%mm0, (%1)\n"
362         MOVNTQ" %%mm1, 8(%1)\n"
363         MOVNTQ" %%mm2, 16(%1)\n"
364         MOVNTQ" %%mm3, 24(%1)\n"
365         MOVNTQ" %%mm4, 32(%1)\n"
366         MOVNTQ" %%mm5, 40(%1)\n"
367         MOVNTQ" %%mm6, 48(%1)\n"
368         MOVNTQ" %%mm7, 56(%1)\n"
369         :: "r" (from), "r" (to) : "memory", "mm0", "mm1", "mm2", "mm3",
370                                             "mm4", "mm5", "mm6", "mm7");
371         from = (const void *) (((const unsigned char *)from)+64);
372         to = (void *) (((unsigned char *)to)+64);
373     }
374
375 #endif /* Have SSE */
376 #ifdef HAVE_MMX2
377                 /* since movntq is weakly-ordered, a "sfence"
378          * is needed to become ordered again. */
379         __asm__ __volatile__ ("sfence":::"memory");
380 #endif
381 #ifndef HAVE_SSE
382         /* enables to use FPU */
383         __asm__ __volatile__ (EMMS:::"memory");
384 #endif
385     }
386     /*
387      *    Now do the tail of the block
388      */
389     if(len) small_memcpy(to, from, len);
390     return retval;
391 }
392
393
394 #endif /* #if defined( HAVE_MMX2 ) || defined( HAVE_3DNOW ) || defined( HAVE_MMX ) */