]> git.sesse.net Git - x264/blob - common/x86/util.h
Slightly faster predictor_difference_mmxext
[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"(*(x264_union32_t*)dst)
42         :"m"(M32( a )), "m"(M32( b )), "m"(M32( 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;
49     static const uint64_t pw_1 = 0x0001000100010001ULL;
50
51     asm(
52         "pxor    %%mm4, %%mm4 \n"
53         "test    $1, %1       \n"
54         "jnz 3f               \n"
55         "movd    -8(%2,%1,4), %%mm0 \n"
56         "movd    -4(%2,%1,4), %%mm3 \n"
57         "psubw   %%mm3, %%mm0 \n"
58         "jmp 2f               \n"
59         "3:                   \n"
60         "dec     %1           \n"
61         "1:                   \n"
62         "movq    -8(%2,%1,4), %%mm0 \n"
63         "psubw   -4(%2,%1,4), %%mm0 \n"
64         "2:                   \n"
65         "sub     $2,    %1    \n"
66         "pxor    %%mm2, %%mm2 \n"
67         "psubw   %%mm0, %%mm2 \n"
68         "pmaxsw  %%mm2, %%mm0 \n"
69         "paddusw %%mm0, %%mm4 \n"
70         "jg 1b                \n"
71         "pmaddwd %4, %%mm4    \n"
72         "pshufw $14, %%mm4, %%mm0 \n"
73         "paddd   %%mm0, %%mm4 \n"
74         "movd    %%mm4, %0    \n"
75         :"=r"(sum), "+r"(i_mvc)
76         :"r"(mvc), "m"(M64( mvc )), "m"(pw_1)
77     );
78     return sum;
79 }
80 #define x264_cabac_amvd_sum x264_cabac_amvd_sum_mmxext
81 static ALWAYS_INLINE uint32_t x264_cabac_amvd_sum_mmxext(int16_t *mvdleft, int16_t *mvdtop)
82 {
83     static const uint64_t pw_2    = 0x0002000200020002ULL;
84     static const uint64_t pw_28   = 0x001C001C001C001CULL;
85     static const uint64_t pw_2184 = 0x0888088808880888ULL;
86     /* MIN(((x+28)*2184)>>16,2) = (x>2) + (x>32) */
87     /* 2184 = fix16(1/30) */
88     uint32_t amvd;
89     asm(
90         "movd      %1, %%mm0 \n"
91         "movd      %2, %%mm1 \n"
92         "pxor   %%mm2, %%mm2 \n"
93         "pxor   %%mm3, %%mm3 \n"
94         "psubw  %%mm0, %%mm2 \n"
95         "psubw  %%mm1, %%mm3 \n"
96         "pmaxsw %%mm2, %%mm0 \n"
97         "pmaxsw %%mm3, %%mm1 \n"
98         "paddw     %3, %%mm0 \n"
99         "paddw  %%mm1, %%mm0 \n"
100         "pmulhuw   %4, %%mm0 \n"
101         "pminsw    %5, %%mm0 \n"
102         "movd   %%mm0, %0    \n"
103         :"=r"(amvd)
104         :"m"(M32( mvdleft )),"m"(M32( mvdtop )),
105          "m"(pw_28),"m"(pw_2184),"m"(pw_2)
106     );
107     return amvd;
108 }
109 #endif
110
111 #endif