]> git.sesse.net Git - x264/blob - common/x86/util.h
Add more inline asm and a runtime check for MMXEXT support
[x264] / common / x86 / util.h
1 /*****************************************************************************
2  * mc.h: h264 encoder library
3  *****************************************************************************
4  * Copyright (C) 2008 Loren Merritt
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
19  *****************************************************************************/
20
21 #ifndef X264_X86_UTIL_H
22 #define X264_X86_UTIL_H
23
24 #ifdef __GNUC__
25 #define x264_median_mv x264_median_mv_mmxext
26 static inline void x264_median_mv_mmxext( int16_t *dst, int16_t *a, int16_t *b, int16_t *c )
27 {
28     asm(
29         "movd   %1,    %%mm0 \n"
30         "movd   %2,    %%mm1 \n"
31         "movq   %%mm0, %%mm3 \n"
32         "movd   %3,    %%mm2 \n"
33         "pmaxsw %%mm1, %%mm0 \n"
34         "pminsw %%mm3, %%mm1 \n"
35         "pminsw %%mm2, %%mm0 \n"
36         "pmaxsw %%mm1, %%mm0 \n"
37         "movd   %%mm0, %0    \n"
38         :"=m"(*(uint32_t*)dst)
39         :"m"(*(uint32_t*)a), "m"(*(uint32_t*)b), "m"(*(uint32_t*)c)
40     );
41 }
42 #define x264_predictor_difference x264_predictor_difference_mmxext
43 static inline int x264_predictor_difference_mmxext( int16_t (*mvc)[2], intptr_t i_mvc )
44 {
45     int sum = 0;
46     uint16_t output[4];
47     asm(
48         "pxor    %%mm4, %%mm4 \n"
49         "test    $1, %1       \n"
50         "jnz 3f               \n"
51         "movd    -8(%2,%1,4), %%mm0 \n"
52         "movd    -4(%2,%1,4), %%mm3 \n"
53         "psubw   %%mm3, %%mm0 \n"
54         "jmp 2f               \n"
55         "3:                   \n"
56         "sub     $1,    %1    \n"
57         "1:                   \n"
58         "movq    -8(%2,%1,4), %%mm0 \n"
59         "psubw   -4(%2,%1,4), %%mm0 \n"
60         "2:                   \n"
61         "sub     $2,    %1    \n"
62         "pxor    %%mm2, %%mm2 \n"
63         "psubw   %%mm0, %%mm2 \n"
64         "pmaxsw  %%mm2, %%mm0 \n"
65         "paddusw %%mm0, %%mm4 \n"
66         "jg 1b                \n"
67         "movq    %%mm4, %0    \n"
68         :"=m"(output), "+r"(i_mvc)
69         :"r"(mvc)
70     );
71     sum += output[0] + output[1] + output[2] + output[3];
72     return sum;
73 }
74 #define array_non_zero_count array_non_zero_count_mmx
75 static inline int array_non_zero_count_mmx( int16_t *v )
76 {
77     static const uint64_t pw_2 = 0x0202020202020202ULL;
78     int count;
79     asm(
80         "pxor     %%mm7,  %%mm7 \n"
81         "movq     (%1),   %%mm0 \n"
82         "movq     16(%1), %%mm1 \n"
83         "packsswb 8(%1),  %%mm0 \n"
84         "packsswb 24(%1), %%mm1 \n"
85         "pcmpeqb  %%mm7,  %%mm0 \n"
86         "pcmpeqb  %%mm7,  %%mm1 \n"
87         "paddb    %%mm0,  %%mm1 \n"
88         "paddb    %2,     %%mm1 \n"
89         "psadbw   %%mm7,  %%mm1 \n"
90         "movd     %%mm1,  %0    \n"
91         :"=r"(count)
92         :"r"(v), "m"(pw_2)
93     );
94     return count;
95 }
96 #undef array_non_zero_int
97 #define array_non_zero_int array_non_zero_int_mmx
98 static ALWAYS_INLINE int array_non_zero_int_mmx( void *v, int i_count )
99 {
100     if(i_count == 128)
101     {
102         int nonzero;
103         asm(
104             "movq     (%1),    %%mm0 \n"
105             "por      8(%1),   %%mm0 \n"
106             "por      16(%1),  %%mm0 \n"
107             "por      24(%1),  %%mm0 \n"
108             "por      32(%1),  %%mm0 \n"
109             "por      40(%1),  %%mm0 \n"
110             "por      48(%1),  %%mm0 \n"
111             "por      56(%1),  %%mm0 \n"
112             "por      64(%1),  %%mm0 \n"
113             "por      72(%1),  %%mm0 \n"
114             "por      80(%1),  %%mm0 \n"
115             "por      88(%1),  %%mm0 \n"
116             "por      96(%1),  %%mm0 \n"
117             "por      104(%1), %%mm0 \n"
118             "por      112(%1), %%mm0 \n"
119             "por      120(%1), %%mm0 \n"
120             "packsswb %%mm0,   %%mm0 \n"
121             "movd     %%mm0,   %0    \n"
122             :"=r"(nonzero)
123             :"r"(v)
124         );
125         return !!nonzero;
126     }
127     else return array_non_zero_int_c( v, i_count );
128 }
129 #endif
130
131 #endif