* ./src/libvlc.c: p_vlc->pf_memset is now usable (it's always the libc
[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.1 2002/08/04 17:23:43 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     set_capability( "memcpy", 100 );
54     set_callbacks( Activate, NULL );
55     add_shortcut( "altivec" );
56 vlc_module_end();
57
58 #else
59 typedef unsigned long size_t;
60 #endif /* __BUILD_ALTIVEC_ASM__ */
61
62 #if defined(CAN_COMPILE_C_ALTIVEC) || defined( __BUILD_ALTIVEC_ASM__ )
63
64 #define vector_s16_t vector signed short
65 #define vector_u16_t vector unsigned short
66 #define vector_s8_t vector signed char
67 #define vector_u8_t vector unsigned char
68 #define vector_s32_t vector signed int
69 #define vector_u32_t vector unsigned int
70 #define MMREG_SIZE 16
71
72 #define SMALL_MEMCPY(to, from, len)                                         \
73 {                                                                           \
74     unsigned char * end = to + len;                                         \
75     while( to < end )                                                       \
76     {                                                                       \
77         *to++ = *from++;                                                    \
78     }                                                                       \
79 }
80
81 static void * fast_memcpy( void * _to, const void * _from, size_t len )
82 {
83     void * retval = _to;
84     unsigned char * to = (unsigned char *)_to;
85     unsigned char * from = (unsigned char *)_from;
86
87     if( len > 16 )
88     {
89         /* Align destination to MMREG_SIZE -boundary */
90         register unsigned long int delta;
91
92         delta = ((unsigned long)to)&(MMREG_SIZE-1);
93         if( delta )
94         {
95             delta = MMREG_SIZE - delta;
96             len -= delta;
97             SMALL_MEMCPY(to, from, delta);
98         }
99
100         if( len & ~(MMREG_SIZE-1) )
101         {
102             vector_u8_t perm, ref0, ref1, tmp;
103
104             perm = vec_lvsl( 0, from );
105             ref0 = vec_ld( 0, from );
106             ref1 = vec_ld( 15, from );
107             from += 16;
108             len -= 16;
109             tmp = vec_perm( ref0, ref1, perm );
110             while( len & ~(MMREG_SIZE-1) )
111             {
112                 ref0 = vec_ld( 0, from );
113                 ref1 = vec_ld( 15, from );
114                 from += 16;
115                 len -= 16;
116                 vec_st( tmp, 0, to );
117                 tmp = vec_perm( ref0, ref1, perm );
118                 to += 16;
119             }
120             vec_st( tmp, 0, to );
121             to += 16;
122         }
123     }
124
125     if( len )
126     {
127         SMALL_MEMCPY( to, from, len );
128     }
129
130     return retval;
131 }
132
133 #endif
134
135 #if !defined(CAN_COMPILE_C_ALTIVEC) && !defined(__BUILD_ALTIVEC_ASM__)
136
137 /*
138  * The asm code is generated with:
139  *
140  * gcc-2.95 -fvec -D__BUILD_ALTIVEC_ASM__ -O9 -fomit-frame-pointer -mregnames -S *      memcpyaltivec.c
141  *
142  * sed 's/.L/._L/g' memcpyaltivec.s |
143  * awk '{args=""; len=split ($2, arg, ",");
144  *      for (i=1; i<=len; i++) { a=arg[i]; if (i<len) a=a",";
145  *                               args = args sprintf ("%-6s", a) }
146  *      printf ("\t\"\t%-16s%-24s\\n\"\n", $1, args) }' |
147  * unexpand -a
148  */
149
150 static void * fast_memcpy( void * _to, const void * _from, size_t len )
151 {
152     asm ("                                              \n"                     
153         "       cmplwi          %cr0, %r5,  16          \n"
154         "       mr              %r9,  %r3               \n"
155         "       bc              4,    1,    ._L3        \n"
156         "       andi.           %r0,  %r3,  15          \n"
157         "       bc              12,   2,    ._L4        \n"
158         "       subfic          %r0,  %r0,  16          \n"
159         "       add             %r11, %r3,  %r0         \n"
160         "       cmplw           %cr0, %r3,  %r11        \n"
161         "       subf            %r5,  %r0,  %r5         \n"
162         "       bc              4,    0,    ._L4        \n"
163         "       ._L7:                                   \n"
164         "       lbz             %r0,  0(%r4)            \n"
165         "       stb             %r0,  0(%r9)            \n"
166         "       addi            %r9,  %r9,  1           \n"
167         "       cmplw           %cr0, %r9,  %r11        \n"
168         "       addi            %r4,  %r4,  1           \n"
169         "       bc              12,   0,    ._L7        \n"
170         "       ._L4:                                   \n"
171         "       rlwinm.         %r0,  %r5,  0,    0,    27    \n"
172         "       bc              12,   2,    ._L3        \n"
173         "       addi            %r5,  %r5,  -16         \n"
174         "       li              %r11, 15                \n"
175         "       lvsl            %v12, 0,    %r4         \n"
176         "       lvx             %v1,  0,    %r4         \n"
177         "       lvx             %v0,  %r11, %r4         \n"
178         "       rlwinm.         %r0,  %r5,  0,    0,    27    \n"
179         "       vperm           %v13, %v1,  %v0,  %v12  \n"
180         "       addi            %r4,  %r4,  16          \n"
181         "       bc              12,   2,    ._L11       \n"
182         "       ._L12:                                  \n"
183         "       addi            %r5,  %r5,  -16         \n"
184         "       li              %r11, 15                \n"
185         "       lvx             %v1,  0,    %r4         \n"
186         "       lvx             %v0,  %r11, %r4         \n"
187         "       rlwinm.         %r0,  %r5,  0,    0,    27    \n"
188         "       stvx            %v13, 0,    %r9         \n"
189         "       vperm           %v13, %v1,  %v0,  %v12  \n"
190         "       addi            %r4,  %r4,  16          \n"
191         "       addi            %r9,  %r9,  16          \n"
192         "       bc              4,    2,    ._L12       \n"
193         "       ._L11:                                  \n"
194         "       stvx            %v13, 0,    %r9         \n"
195         "       addi            %r9,  %r9,  16          \n"
196         "       ._L3:                                   \n"
197         "       cmpwi           %cr0, %r5,  0           \n"
198         "       bclr            12,   2                 \n"
199         "       add             %r5,  %r9,  %r5         \n"
200         "       cmplw           %cr0, %r9,  %r5         \n"
201         "       bclr            4,    0                 \n"
202         "       ._L17:                                  \n"
203         "       lbz             %r0,  0(%r4)            \n"
204         "       stb             %r0,  0(%r9)            \n"
205         "       addi            %r9,  %r9,  1           \n"
206         "       cmplw           %cr0, %r9,  %r5         \n"
207         "       addi            %r4,  %r4,  1           \n"
208         "       bc              12,   0,    ._L17       \n"
209         );
210 }
211
212 #endif