~siretart/libav/trusty

« back to all changes in this revision

Viewing changes to libavcodec/x86/cabac.h

  • Committer: Reinhard Tartler
  • Date: 2013-10-23 03:04:17 UTC
  • mfrom: (1.3.36 sid)
  • Revision ID: siretart@tauware.de-20131023030417-1o6mpkl1l0raifjt
mergeĀ fromĀ debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
#include "libavcodec/cabac.h"
25
25
#include "libavutil/attributes.h"
26
 
#include "libavutil/x86_cpu.h"
 
26
#include "libavutil/x86/asm.h"
 
27
#include "libavutil/internal.h"
27
28
#include "config.h"
28
29
 
29
 
#if HAVE_FAST_CMOV
30
 
#define BRANCHLESS_GET_CABAC_UPDATE(ret, statep, low, lowword, range, tmp)\
 
30
#if HAVE_INLINE_ASM
 
31
 
 
32
#ifdef BROKEN_RELOCATIONS
 
33
#define TABLES_ARG , "r"(tables)
 
34
 
 
35
#if HAVE_FAST_CMOV
 
36
#define BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp) \
 
37
        "cmp    "low"       , "tmp"                        \n\t"\
 
38
        "cmova  %%ecx       , "range"                      \n\t"\
 
39
        "sbb    %%rcx       , %%rcx                        \n\t"\
 
40
        "and    %%ecx       , "tmp"                        \n\t"\
 
41
        "xor    %%rcx       , "retq"                       \n\t"\
 
42
        "sub    "tmp"       , "low"                        \n\t"
 
43
#else /* HAVE_FAST_CMOV */
 
44
#define BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp) \
 
45
/* P4 Prescott has crappy cmov,sbb,64bit shift so avoid them */ \
 
46
        "sub    "low"       , "tmp"                        \n\t"\
 
47
        "sar    $31         , "tmp"                        \n\t"\
 
48
        "sub    %%ecx       , "range"                      \n\t"\
 
49
        "and    "tmp"       , "range"                      \n\t"\
 
50
        "add    %%ecx       , "range"                      \n\t"\
 
51
        "shl    $17         , %%ecx                        \n\t"\
 
52
        "and    "tmp"       , %%ecx                        \n\t"\
 
53
        "sub    %%ecx       , "low"                        \n\t"\
 
54
        "xor    "tmp"       , "ret"                        \n\t"\
 
55
        "movslq "ret"       , "retq"                       \n\t"
 
56
#endif /* HAVE_FAST_CMOV */
 
57
 
 
58
#define BRANCHLESS_GET_CABAC(ret, retq, statep, low, lowword, range, rangeq, tmp, tmpbyte, byte, end, norm_off, lps_off, mlps_off, tables) \
 
59
        "movzbl "statep"    , "ret"                                     \n\t"\
 
60
        "mov    "range"     , "tmp"                                     \n\t"\
 
61
        "and    $0xC0       , "range"                                   \n\t"\
 
62
        "lea    ("ret", "range", 2), %%ecx                              \n\t"\
 
63
        "movzbl "lps_off"("tables", %%rcx), "range"                     \n\t"\
 
64
        "sub    "range"     , "tmp"                                     \n\t"\
 
65
        "mov    "tmp"       , %%ecx                                     \n\t"\
 
66
        "shl    $17         , "tmp"                                     \n\t"\
 
67
        BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp)              \
 
68
        "movzbl "norm_off"("tables", "rangeq"), %%ecx                   \n\t"\
 
69
        "shl    %%cl        , "range"                                   \n\t"\
 
70
        "movzbl "mlps_off"+128("tables", "retq"), "tmp"                 \n\t"\
 
71
        "shl    %%cl        , "low"                                     \n\t"\
 
72
        "mov    "tmpbyte"   , "statep"                                  \n\t"\
 
73
        "test   "lowword"   , "lowword"                                 \n\t"\
 
74
        "jnz    2f                                                      \n\t"\
 
75
        "mov    "byte"      , %%"REG_c"                                 \n\t"\
 
76
        "cmp    "end"       , %%"REG_c"                                 \n\t"\
 
77
        "jge    1f                                                      \n\t"\
 
78
        "add"OPSIZE" $2     , "byte"                                    \n\t"\
 
79
        "1:                                                             \n\t"\
 
80
        "movzwl (%%"REG_c") , "tmp"                                     \n\t"\
 
81
        "lea    -1("low")   , %%ecx                                     \n\t"\
 
82
        "xor    "low"       , %%ecx                                     \n\t"\
 
83
        "shr    $15         , %%ecx                                     \n\t"\
 
84
        "bswap  "tmp"                                                   \n\t"\
 
85
        "shr    $15         , "tmp"                                     \n\t"\
 
86
        "movzbl "norm_off"("tables", %%rcx), %%ecx                      \n\t"\
 
87
        "sub    $0xFFFF     , "tmp"                                     \n\t"\
 
88
        "neg    %%ecx                                                   \n\t"\
 
89
        "add    $7          , %%ecx                                     \n\t"\
 
90
        "shl    %%cl        , "tmp"                                     \n\t"\
 
91
        "add    "tmp"       , "low"                                     \n\t"\
 
92
        "2:                                                             \n\t"
 
93
 
 
94
#else /* BROKEN_RELOCATIONS */
 
95
#define TABLES_ARG
 
96
 
 
97
#if HAVE_FAST_CMOV
 
98
#define BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)\
31
99
        "mov    "tmp"       , %%ecx     \n\t"\
32
100
        "shl    $17         , "tmp"     \n\t"\
33
101
        "cmp    "low"       , "tmp"     \n\t"\
37
105
        "xor    %%ecx       , "ret"     \n\t"\
38
106
        "sub    "tmp"       , "low"     \n\t"
39
107
#else /* HAVE_FAST_CMOV */
40
 
#define BRANCHLESS_GET_CABAC_UPDATE(ret, statep, low, lowword, range, tmp)\
 
108
#define BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)\
41
109
        "mov    "tmp"       , %%ecx     \n\t"\
42
110
        "shl    $17         , "tmp"     \n\t"\
43
111
        "sub    "low"       , "tmp"     \n\t"\
51
119
        "xor    "tmp"       , "ret"     \n\t"
52
120
#endif /* HAVE_FAST_CMOV */
53
121
 
54
 
#define BRANCHLESS_GET_CABAC(ret, statep, low, lowword, range, tmp, tmpbyte, byte) \
 
122
#define BRANCHLESS_GET_CABAC(ret, retq, statep, low, lowword, range, rangeq, tmp, tmpbyte, byte, end, norm_off, lps_off, mlps_off, tables) \
55
123
        "movzbl "statep"    , "ret"                                     \n\t"\
56
124
        "mov    "range"     , "tmp"                                     \n\t"\
57
125
        "and    $0xC0       , "range"                                   \n\t"\
58
 
        "movzbl "MANGLE(ff_h264_lps_range)"("ret", "range", 2), "range" \n\t"\
 
126
        "movzbl "MANGLE(ff_h264_cabac_tables)"+"lps_off"("ret", "range", 2), "range" \n\t"\
59
127
        "sub    "range"     , "tmp"                                     \n\t"\
60
 
        BRANCHLESS_GET_CABAC_UPDATE(ret, statep, low, lowword, range, tmp)   \
61
 
        "movzbl " MANGLE(ff_h264_norm_shift) "("range"), %%ecx          \n\t"\
 
128
        BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)                    \
 
129
        "movzbl "MANGLE(ff_h264_cabac_tables)"+"norm_off"("range"), %%ecx    \n\t"\
62
130
        "shl    %%cl        , "range"                                   \n\t"\
63
 
        "movzbl "MANGLE(ff_h264_mlps_state)"+128("ret"), "tmp"          \n\t"\
 
131
        "movzbl "MANGLE(ff_h264_cabac_tables)"+"mlps_off"+128("ret"), "tmp"  \n\t"\
64
132
        "shl    %%cl        , "low"                                     \n\t"\
65
133
        "mov    "tmpbyte"   , "statep"                                  \n\t"\
66
134
        "test   "lowword"   , "lowword"                                 \n\t"\
67
 
        " jnz   1f                                                      \n\t"\
 
135
        " jnz   2f                                                      \n\t"\
68
136
        "mov    "byte"      , %%"REG_c"                                 \n\t"\
 
137
        "cmp    "end"       , %%"REG_c"                                 \n\t"\
 
138
        "jge    1f                                                      \n\t"\
69
139
        "add"OPSIZE" $2     , "byte"                                    \n\t"\
 
140
        "1:                                                             \n\t"\
70
141
        "movzwl (%%"REG_c")     , "tmp"                                 \n\t"\
71
142
        "lea    -1("low")   , %%ecx                                     \n\t"\
72
143
        "xor    "low"       , %%ecx                                     \n\t"\
73
144
        "shr    $15         , %%ecx                                     \n\t"\
74
145
        "bswap  "tmp"                                                   \n\t"\
75
146
        "shr    $15         , "tmp"                                     \n\t"\
76
 
        "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx            \n\t"\
 
147
        "movzbl "MANGLE(ff_h264_cabac_tables)"+"norm_off"(%%ecx), %%ecx \n\t"\
77
148
        "sub    $0xFFFF     , "tmp"                                     \n\t"\
78
149
        "neg    %%ecx                                                   \n\t"\
79
150
        "add    $7          , %%ecx                                     \n\t"\
80
151
        "shl    %%cl        , "tmp"                                     \n\t"\
81
152
        "add    "tmp"       , "low"                                     \n\t"\
82
 
        "1:                                                             \n\t"
83
 
 
84
 
#if HAVE_7REGS && !defined(BROKEN_RELOCATIONS)
 
153
        "2:                                                             \n\t"
 
154
 
 
155
#endif /* BROKEN_RELOCATIONS */
 
156
 
 
157
 
 
158
#if HAVE_7REGS
85
159
#define get_cabac_inline get_cabac_inline_x86
86
160
static av_always_inline int get_cabac_inline_x86(CABACContext *c,
87
161
                                                 uint8_t *const state)
88
162
{
89
163
    int bit, tmp;
90
 
 
91
 
    __asm__ volatile(
92
 
        BRANCHLESS_GET_CABAC("%0", "(%5)", "%1", "%w1", "%2",
93
 
                             "%3", "%b3", "%4")
94
 
        :"=&r"(bit), "+&r"(c->low), "+&r"(c->range), "=&q"(tmp),
95
 
         "+m"(c->bytestream)
96
 
        :"r"(state)
 
164
#ifdef BROKEN_RELOCATIONS
 
165
    void *tables;
 
166
 
 
167
    __asm__ volatile(
 
168
        "lea    "MANGLE(ff_h264_cabac_tables)", %0      \n\t"
 
169
        : "=&r"(tables)
 
170
    );
 
171
#endif
 
172
 
 
173
    __asm__ volatile(
 
174
        BRANCHLESS_GET_CABAC("%0", "%q0", "(%4)", "%1", "%w1",
 
175
                             "%2", "%q2", "%3", "%b3",
 
176
                             "%c6(%5)", "%c7(%5)",
 
177
                             AV_STRINGIFY(H264_NORM_SHIFT_OFFSET),
 
178
                             AV_STRINGIFY(H264_LPS_RANGE_OFFSET),
 
179
                             AV_STRINGIFY(H264_MLPS_STATE_OFFSET),
 
180
                             "%8")
 
181
        : "=&r"(bit), "+&r"(c->low), "+&r"(c->range), "=&q"(tmp)
 
182
        : "r"(state), "r"(c),
 
183
          "i"(offsetof(CABACContext, bytestream)),
 
184
          "i"(offsetof(CABACContext, bytestream_end))
 
185
          TABLES_ARG
97
186
        : "%"REG_c, "memory"
98
187
    );
99
188
    return bit & 1;
100
189
}
101
 
#endif /* HAVE_7REGS && !defined(BROKEN_RELOCATIONS) */
 
190
#endif /* HAVE_7REGS */
102
191
 
103
192
#define get_cabac_bypass_sign get_cabac_bypass_sign_x86
104
193
static av_always_inline int get_cabac_bypass_sign_x86(CABACContext *c, int val)
105
194
{
106
195
    x86_reg tmp;
107
196
    __asm__ volatile(
108
 
        "movl %4, %k1                           \n\t"
109
 
        "movl %2, %%eax                         \n\t"
110
 
        "shl $17, %k1                           \n\t"
111
 
        "add %%eax, %%eax                       \n\t"
112
 
        "sub %k1, %%eax                         \n\t"
113
 
        "cltd                                   \n\t"
114
 
        "and %%edx, %k1                         \n\t"
115
 
        "add %k1, %%eax                         \n\t"
116
 
        "xor %%edx, %%ecx                       \n\t"
117
 
        "sub %%edx, %%ecx                       \n\t"
118
 
        "test %%ax, %%ax                        \n\t"
119
 
        " jnz 1f                                \n\t"
120
 
        "mov  %3, %1                            \n\t"
121
 
        "subl $0xFFFF, %%eax                    \n\t"
122
 
        "movzwl (%1), %%edx                     \n\t"
123
 
        "bswap %%edx                            \n\t"
124
 
        "shrl $15, %%edx                        \n\t"
125
 
        "add  $2, %1                            \n\t"
126
 
        "addl %%edx, %%eax                      \n\t"
127
 
        "mov  %1, %3                            \n\t"
128
 
        "1:                                     \n\t"
129
 
        "movl %%eax, %2                         \n\t"
 
197
        "movl        %c6(%2), %k1       \n\t"
 
198
        "movl        %c3(%2), %%eax     \n\t"
 
199
        "shl             $17, %k1       \n\t"
 
200
        "add           %%eax, %%eax     \n\t"
 
201
        "sub             %k1, %%eax     \n\t"
 
202
        "cltd                           \n\t"
 
203
        "and           %%edx, %k1       \n\t"
 
204
        "add             %k1, %%eax     \n\t"
 
205
        "xor           %%edx, %%ecx     \n\t"
 
206
        "sub           %%edx, %%ecx     \n\t"
 
207
        "test           %%ax, %%ax      \n\t"
 
208
        "jnz              1f            \n\t"
 
209
        "mov         %c4(%2), %1        \n\t"
 
210
        "subl        $0xFFFF, %%eax     \n\t"
 
211
        "movzwl         (%1), %%edx     \n\t"
 
212
        "bswap         %%edx            \n\t"
 
213
        "shrl            $15, %%edx     \n\t"
 
214
        "addl          %%edx, %%eax     \n\t"
 
215
        "cmp         %c5(%2), %1        \n\t"
 
216
        "jge              1f            \n\t"
 
217
        "add"OPSIZE"      $2, %c4(%2)   \n\t"
 
218
        "1:                             \n\t"
 
219
        "movl          %%eax, %c3(%2)   \n\t"
130
220
 
131
 
        :"+c"(val), "=&r"(tmp), "+m"(c->low), "+m"(c->bytestream)
132
 
        :"m"(c->range)
133
 
        : "%eax", "%edx"
 
221
        : "+c"(val), "=&r"(tmp)
 
222
        : "r"(c),
 
223
          "i"(offsetof(CABACContext, low)),
 
224
          "i"(offsetof(CABACContext, bytestream)),
 
225
          "i"(offsetof(CABACContext, bytestream_end)),
 
226
          "i"(offsetof(CABACContext, range))
 
227
        : "%eax", "%edx", "memory"
134
228
    );
135
229
    return val;
136
230
}
137
231
 
 
232
#endif /* HAVE_INLINE_ASM */
138
233
#endif /* AVCODEC_X86_CABAC_H */