Add a growl (http://growl.info/) notification plugin (most of code from msn.c)
[vlc.git] / modules / misc / memcpy / memcpyaltivec.c
1 /*****************************************************************************
2  * memcpyaltivec.c : AltiVec memcpy module
3  *****************************************************************************
4  * Copyright (C) 2001 the VideoLAN team
5  * $Id$
6  *
7  * Author: 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 #ifdef HAVE_ALTIVEC_H
35 #   include <altivec.h>
36 #endif
37
38 /*****************************************************************************
39  * Local prototypes.
40  *****************************************************************************/
41 static void * fast_memcpy ( void * to, const void * from, size_t len );
42
43 /*****************************************************************************
44  * Module initializer.
45  *****************************************************************************/
46 static int Activate ( vlc_object_t *p_this )
47 {
48     p_this->p_vlc->pf_memcpy = fast_memcpy;
49     return VLC_SUCCESS;
50 }
51
52 /*****************************************************************************
53  * Module descriptor.
54  *****************************************************************************/
55 vlc_module_begin();
56     set_description( _("AltiVec memcpy") );
57     set_category( CAT_ADVANCED );
58     set_subcategory( SUBCAT_ADVANCED_MISC );
59     add_requirement( ALTIVEC );
60     set_capability( "memcpy", 100 );
61     set_callbacks( Activate, NULL );
62     add_shortcut( "altivec" );
63 vlc_module_end();
64
65 #else
66 typedef unsigned long size_t;
67 #endif /* __BUILD_ALTIVEC_ASM__ */
68
69 #if defined(CAN_COMPILE_C_ALTIVEC) || defined( __BUILD_ALTIVEC_ASM__ )
70
71 #define vector_s16_t vector signed short
72 #define vector_u16_t vector unsigned short
73 #define vector_s8_t vector signed char
74 #define vector_u8_t vector unsigned char
75 #define vector_s32_t vector signed int
76 #define vector_u32_t vector unsigned int
77 #define MMREG_SIZE 16
78
79 #define SMALL_MEMCPY(to, from, len)                                         \
80 {                                                                           \
81     unsigned char * end = to + len;                                         \
82     while( to < end )                                                       \
83     {                                                                       \
84         *to++ = *from++;                                                    \
85     }                                                                       \
86 }
87
88 static void * fast_memcpy( void * _to, const void * _from, size_t len )
89 {
90     void * retval = _to;
91     unsigned char * to = (unsigned char *)_to;
92     unsigned char * from = (unsigned char *)_from;
93
94     if( len > 16 )
95     {
96         /* Align destination to MMREG_SIZE -boundary */
97         register unsigned long int delta;
98
99         delta = ((unsigned long)to)&(MMREG_SIZE-1);
100         if( delta )
101         {
102             delta = MMREG_SIZE - delta;
103             len -= delta;
104             SMALL_MEMCPY(to, from, delta);
105         }
106
107         if( len & ~(MMREG_SIZE-1) )
108         {
109             vector_u8_t perm, ref0, ref1, tmp;
110
111             perm = vec_lvsl( 0, from );
112             ref0 = vec_ld( 0, from );
113             ref1 = vec_ld( 15, from );
114             from += 16;
115             len -= 16;
116             tmp = vec_perm( ref0, ref1, perm );
117             while( len & ~(MMREG_SIZE-1) )
118             {
119                 ref0 = vec_ld( 0, from );
120                 ref1 = vec_ld( 15, from );
121                 from += 16;
122                 len -= 16;
123                 vec_st( tmp, 0, to );
124                 tmp = vec_perm( ref0, ref1, perm );
125                 to += 16;
126             }
127             vec_st( tmp, 0, to );
128             to += 16;
129         }
130     }
131
132     if( len )
133     {
134         SMALL_MEMCPY( to, from, len );
135     }
136
137     return retval;
138 }
139
140 #endif
141
142 #if !defined(CAN_COMPILE_C_ALTIVEC) && !defined(__BUILD_ALTIVEC_ASM__)
143
144 /*
145  * The asm code is generated with:
146  *
147  * gcc-2.95 -fvec -D__BUILD_ALTIVEC_ASM__ -O9 -fomit-frame-pointer -mregnames -S *      memcpyaltivec.c
148  *
149  * sed 's/.L/._L/g' memcpyaltivec.s |
150  * awk '{args=""; len=split ($2, arg, ",");
151  *      for (i=1; i<=len; i++) { a=arg[i]; if (i<len) a=a",";
152  *                               args = args sprintf ("%-6s", a) }
153  *      printf ("\t\"\t%-16s%-24s\\n\"\n", $1, args) }' |
154  * unexpand -a
155  */
156
157 static void * fast_memcpy( void * _to, const void * _from, size_t len )
158 {
159     asm ("                                              \n"
160         "       cmplwi          %cr0, %r5,  16          \n"
161         "       mr              %r9,  %r3               \n"
162         "       bc              4,    1,    ._L3        \n"
163         "       andi.           %r0,  %r3,  15          \n"
164         "       bc              12,   2,    ._L4        \n"
165         "       subfic          %r0,  %r0,  16          \n"
166         "       add             %r11, %r3,  %r0         \n"
167         "       cmplw           %cr0, %r3,  %r11        \n"
168         "       subf            %r5,  %r0,  %r5         \n"
169         "       bc              4,    0,    ._L4        \n"
170         "       ._L7:                                   \n"
171         "       lbz             %r0,  0(%r4)            \n"
172         "       stb             %r0,  0(%r9)            \n"
173         "       addi            %r9,  %r9,  1           \n"
174         "       cmplw           %cr0, %r9,  %r11        \n"
175         "       addi            %r4,  %r4,  1           \n"
176         "       bc              12,   0,    ._L7        \n"
177         "       ._L4:                                   \n"
178         "       rlwinm.         %r0,  %r5,  0,    0,    27    \n"
179         "       bc              12,   2,    ._L3        \n"
180         "       addi            %r5,  %r5,  -16         \n"
181         "       li              %r11, 15                \n"
182         "       lvsl            %v12, 0,    %r4         \n"
183         "       lvx             %v1,  0,    %r4         \n"
184         "       lvx             %v0,  %r11, %r4         \n"
185         "       rlwinm.         %r0,  %r5,  0,    0,    27    \n"
186         "       vperm           %v13, %v1,  %v0,  %v12  \n"
187         "       addi            %r4,  %r4,  16          \n"
188         "       bc              12,   2,    ._L11       \n"
189         "       ._L12:                                  \n"
190         "       addi            %r5,  %r5,  -16         \n"
191         "       li              %r11, 15                \n"
192         "       lvx             %v1,  0,    %r4         \n"
193         "       lvx             %v0,  %r11, %r4         \n"
194         "       rlwinm.         %r0,  %r5,  0,    0,    27    \n"
195         "       stvx            %v13, 0,    %r9         \n"
196         "       vperm           %v13, %v1,  %v0,  %v12  \n"
197         "       addi            %r4,  %r4,  16          \n"
198         "       addi            %r9,  %r9,  16          \n"
199         "       bc              4,    2,    ._L12       \n"
200         "       ._L11:                                  \n"
201         "       stvx            %v13, 0,    %r9         \n"
202         "       addi            %r9,  %r9,  16          \n"
203         "       ._L3:                                   \n"
204         "       cmpwi           %cr0, %r5,  0           \n"
205         "       bclr            12,   2                 \n"
206         "       add             %r5,  %r9,  %r5         \n"
207         "       cmplw           %cr0, %r9,  %r5         \n"
208         "       bclr            4,    0                 \n"
209         "       ._L17:                                  \n"
210         "       lbz             %r0,  0(%r4)            \n"
211         "       stb             %r0,  0(%r9)            \n"
212         "       addi            %r9,  %r9,  1           \n"
213         "       cmplw           %cr0, %r9,  %r5         \n"
214         "       addi            %r4,  %r4,  1           \n"
215         "       bc              12,   0,    ._L17       \n"
216         );
217 }
218
219 #endif