]> git.sesse.net Git - x264/blob - common/x86/util.h
Various optimizations and cosmetics
[x264] / common / x86 / util.h
1 /*****************************************************************************
2  * mc.h: h264 encoder library
3  *****************************************************************************
4  * Copyright (C) 2008 x264 Project
5  *
6  * Authors: Fiona Glaser <fiona@x264.com>
7  *          Loren Merritt <lorenm@u.washington.edu>
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  02111, USA.
22  *****************************************************************************/
23
24 #ifndef X264_X86_UTIL_H
25 #define X264_X86_UTIL_H
26
27 #ifdef __GNUC__
28 #define x264_median_mv x264_median_mv_mmxext
29 static inline void x264_median_mv_mmxext( int16_t *dst, int16_t *a, int16_t *b, int16_t *c )
30 {
31     asm(
32         "movd   %1,    %%mm0 \n"
33         "movd   %2,    %%mm1 \n"
34         "movq   %%mm0, %%mm3 \n"
35         "movd   %3,    %%mm2 \n"
36         "pmaxsw %%mm1, %%mm0 \n"
37         "pminsw %%mm3, %%mm1 \n"
38         "pminsw %%mm2, %%mm0 \n"
39         "pmaxsw %%mm1, %%mm0 \n"
40         "movd   %%mm0, %0    \n"
41         :"=m"(*(uint32_t*)dst)
42         :"m"(*(uint32_t*)a), "m"(*(uint32_t*)b), "m"(*(uint32_t*)c)
43     );
44 }
45 #define x264_predictor_difference x264_predictor_difference_mmxext
46 static inline int x264_predictor_difference_mmxext( int16_t (*mvc)[2], intptr_t i_mvc )
47 {
48     int sum = 0;
49     uint16_t output[4];
50     asm(
51         "pxor    %%mm4, %%mm4 \n"
52         "test    $1, %1       \n"
53         "jnz 3f               \n"
54         "movd    -8(%2,%1,4), %%mm0 \n"
55         "movd    -4(%2,%1,4), %%mm3 \n"
56         "psubw   %%mm3, %%mm0 \n"
57         "jmp 2f               \n"
58         "3:                   \n"
59         "sub     $1,    %1    \n"
60         "1:                   \n"
61         "movq    -8(%2,%1,4), %%mm0 \n"
62         "psubw   -4(%2,%1,4), %%mm0 \n"
63         "2:                   \n"
64         "sub     $2,    %1    \n"
65         "pxor    %%mm2, %%mm2 \n"
66         "psubw   %%mm0, %%mm2 \n"
67         "pmaxsw  %%mm2, %%mm0 \n"
68         "paddusw %%mm0, %%mm4 \n"
69         "jg 1b                \n"
70         "movq    %%mm4, %0    \n"
71         :"=m"(output), "+r"(i_mvc)
72         :"r"(mvc)
73     );
74     sum += output[0] + output[1] + output[2] + output[3];
75     return sum;
76 }
77 #define array_non_zero_count array_non_zero_count_mmx
78 static inline int array_non_zero_count_mmx( int16_t *v )
79 {
80     int count;
81     asm(
82         "pxor     %%mm7,  %%mm7 \n"
83         "movq     (%1),   %%mm0 \n"
84         "movq     8(%1),  %%mm1 \n"
85         "packsswb 16(%1), %%mm0 \n"
86         "packsswb 24(%1), %%mm1 \n"
87         "pcmpeqb  %%mm7,  %%mm0 \n"
88         "pcmpeqb  %%mm7,  %%mm1 \n"
89         "paddb    %%mm0,  %%mm1 \n"
90         "psadbw   %%mm7,  %%mm1 \n"
91         "movd     %%mm1,  %0    \n"
92         :"=r"(count)
93         :"r"(v)
94     );
95     return (count+0x10)&0xff;
96 }
97 #undef array_non_zero_int
98 #define array_non_zero_int array_non_zero_int_mmx
99 static ALWAYS_INLINE int array_non_zero_int_mmx( void *v, int i_count )
100 {
101     if(i_count == 128)
102     {
103         int nonzero;
104         asm(
105             "movq     (%1),    %%mm0 \n"
106             "por      8(%1),   %%mm0 \n"
107             "por      16(%1),  %%mm0 \n"
108             "por      24(%1),  %%mm0 \n"
109             "por      32(%1),  %%mm0 \n"
110             "por      40(%1),  %%mm0 \n"
111             "por      48(%1),  %%mm0 \n"
112             "por      56(%1),  %%mm0 \n"
113             "por      64(%1),  %%mm0 \n"
114             "por      72(%1),  %%mm0 \n"
115             "por      80(%1),  %%mm0 \n"
116             "por      88(%1),  %%mm0 \n"
117             "por      96(%1),  %%mm0 \n"
118             "por      104(%1), %%mm0 \n"
119             "por      112(%1), %%mm0 \n"
120             "por      120(%1), %%mm0 \n"
121             "packsswb %%mm0,   %%mm0 \n"
122             "movd     %%mm0,   %0    \n"
123             :"=r"(nonzero)
124             :"r"(v)
125         );
126         return !!nonzero;
127     }
128     else return array_non_zero_int_c( v, i_count );
129 }
130 #endif
131
132 #endif