* modules/codec/ffmpeg/*: modified the ffmpeg video codec to use direct
[vlc.git] / modules / misc / memcpy / memcpyaltivec.c
1 /*****************************************************************************
2  * memcpyaltivec.c : AltiVec memcpy module
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: memcpyaltivec.c,v 1.2 2002/08/11 12:27:11 sam Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #ifndef __BUILD_ALTIVEC_ASM__
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <vlc/vlc.h>
33
34 /*****************************************************************************
35  * Local prototypes.
36  *****************************************************************************/
37 static void * fast_memcpy ( void * to, const void * from, size_t len );
38
39 /*****************************************************************************
40  * Module initializer.
41  *****************************************************************************/
42 static int Activate ( vlc_object_t *p_this )
43 {
44     p_this->p_vlc->pf_memcpy = fast_memcpy;
45     return VLC_SUCCESS;
46 }
47
48 /*****************************************************************************
49  * Module descriptor.
50  *****************************************************************************/
51 vlc_module_begin();
52     set_description( _("AltiVec memcpy module") );
53     add_requirement( ALTIVEC );
54     set_capability( "memcpy", 100 );
55     set_callbacks( Activate, NULL );
56     add_shortcut( "altivec" );
57 vlc_module_end();
58
59 #else
60 typedef unsigned long size_t;
61 #endif /* __BUILD_ALTIVEC_ASM__ */
62
63 #if defined(CAN_COMPILE_C_ALTIVEC) || defined( __BUILD_ALTIVEC_ASM__ )
64
65 #define vector_s16_t vector signed short
66 #define vector_u16_t vector unsigned short
67 #define vector_s8_t vector signed char
68 #define vector_u8_t vector unsigned char
69 #define vector_s32_t vector signed int
70 #define vector_u32_t vector unsigned int
71 #define MMREG_SIZE 16
72
73 #define SMALL_MEMCPY(to, from, len)                                         \
74 {                                                                           \
75     unsigned char * end = to + len;                                         \
76     while( to < end )                                                       \
77     {                                                                       \
78         *to++ = *from++;                                                    \
79     }                                                                       \
80 }
81
82 static void * fast_memcpy( void * _to, const void * _from, size_t len )
83 {
84     void * retval = _to;
85     unsigned char * to = (unsigned char *)_to;
86     unsigned char * from = (unsigned char *)_from;
87
88     if( len > 16 )
89     {
90         /* Align destination to MMREG_SIZE -boundary */
91         register unsigned long int delta;
92
93         delta = ((unsigned long)to)&(MMREG_SIZE-1);
94         if( delta )
95         {
96             delta = MMREG_SIZE - delta;
97             len -= delta;
98             SMALL_MEMCPY(to, from, delta);
99         }
100
101         if( len & ~(MMREG_SIZE-1) )
102         {
103             vector_u8_t perm, ref0, ref1, tmp;
104
105             perm = vec_lvsl( 0, from );
106             ref0 = vec_ld( 0, from );
107             ref1 = vec_ld( 15, from );
108             from += 16;
109             len -= 16;
110             tmp = vec_perm( ref0, ref1, perm );
111             while( len & ~(MMREG_SIZE-1) )
112             {
113                 ref0 = vec_ld( 0, from );
114                 ref1 = vec_ld( 15, from );
115                 from += 16;
116                 len -= 16;
117                 vec_st( tmp, 0, to );
118                 tmp = vec_perm( ref0, ref1, perm );
119                 to += 16;
120             }
121             vec_st( tmp, 0, to );
122             to += 16;
123         }
124     }
125
126     if( len )
127     {
128         SMALL_MEMCPY( to, from, len );
129     }
130
131     return retval;
132 }
133
134 #endif
135
136 #if !defined(CAN_COMPILE_C_ALTIVEC) && !defined(__BUILD_ALTIVEC_ASM__)
137
138 /*
139  * The asm code is generated with:
140  *
141  * gcc-2.95 -fvec -D__BUILD_ALTIVEC_ASM__ -O9 -fomit-frame-pointer -mregnames -S *      memcpyaltivec.c
142  *
143  * sed 's/.L/._L/g' memcpyaltivec.s |
144  * awk '{args=""; len=split ($2, arg, ",");
145  *      for (i=1; i<=len; i++) { a=arg[i]; if (i<len) a=a",";
146  *                               args = args sprintf ("%-6s", a) }
147  *      printf ("\t\"\t%-16s%-24s\\n\"\n", $1, args) }' |
148  * unexpand -a
149  */
150
151 static void * fast_memcpy( void * _to, const void * _from, size_t len )
152 {
153     asm ("                                              \n"                     
154         "       cmplwi          %cr0, %r5,  16          \n"
155         "       mr              %r9,  %r3               \n"
156         "       bc              4,    1,    ._L3        \n"
157         "       andi.           %r0,  %r3,  15          \n"
158         "       bc              12,   2,    ._L4        \n"
159         "       subfic          %r0,  %r0,  16          \n"
160         "       add             %r11, %r3,  %r0         \n"
161         "       cmplw           %cr0, %r3,  %r11        \n"
162         "       subf            %r5,  %r0,  %r5         \n"
163         "       bc              4,    0,    ._L4        \n"
164         "       ._L7:                                   \n"
165         "       lbz             %r0,  0(%r4)            \n"
166         "       stb             %r0,  0(%r9)            \n"
167         "       addi            %r9,  %r9,  1           \n"
168         "       cmplw           %cr0, %r9,  %r11        \n"
169         "       addi            %r4,  %r4,  1           \n"
170         "       bc              12,   0,    ._L7        \n"
171         "       ._L4:                                   \n"
172         "       rlwinm.         %r0,  %r5,  0,    0,    27    \n"
173         "       bc              12,   2,    ._L3        \n"
174         "       addi            %r5,  %r5,  -16         \n"
175         "       li              %r11, 15                \n"
176         "       lvsl            %v12, 0,    %r4         \n"
177         "       lvx             %v1,  0,    %r4         \n"
178         "       lvx             %v0,  %r11, %r4         \n"
179         "       rlwinm.         %r0,  %r5,  0,    0,    27    \n"
180         "       vperm           %v13, %v1,  %v0,  %v12  \n"
181         "       addi            %r4,  %r4,  16          \n"
182         "       bc              12,   2,    ._L11       \n"
183         "       ._L12:                                  \n"
184         "       addi            %r5,  %r5,  -16         \n"
185         "       li              %r11, 15                \n"
186         "       lvx             %v1,  0,    %r4         \n"
187         "       lvx             %v0,  %r11, %r4         \n"
188         "       rlwinm.         %r0,  %r5,  0,    0,    27    \n"
189         "       stvx            %v13, 0,    %r9         \n"
190         "       vperm           %v13, %v1,  %v0,  %v12  \n"
191         "       addi            %r4,  %r4,  16          \n"
192         "       addi            %r9,  %r9,  16          \n"
193         "       bc              4,    2,    ._L12       \n"
194         "       ._L11:                                  \n"
195         "       stvx            %v13, 0,    %r9         \n"
196         "       addi            %r9,  %r9,  16          \n"
197         "       ._L3:                                   \n"
198         "       cmpwi           %cr0, %r5,  0           \n"
199         "       bclr            12,   2                 \n"
200         "       add             %r5,  %r9,  %r5         \n"
201         "       cmplw           %cr0, %r9,  %r5         \n"
202         "       bclr            4,    0                 \n"
203         "       ._L17:                                  \n"
204         "       lbz             %r0,  0(%r4)            \n"
205         "       stb             %r0,  0(%r9)            \n"
206         "       addi            %r9,  %r9,  1           \n"
207         "       cmplw           %cr0, %r9,  %r5         \n"
208         "       addi            %r4,  %r4,  1           \n"
209         "       bc              12,   0,    ._L17       \n"
210         );
211 }
212
213 #endif