2 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef AVCODEC_X86_CABAC_H
22 #define AVCODEC_X86_CABAC_H
24 #include "libavcodec/cabac.h"
25 #include "libavutil/attributes.h"
26 #include "libavutil/x86_cpu.h"
27 #include "libavutil/internal.h"
31 #define BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)\
32 "mov "tmp" , %%ecx \n\t"\
33 "shl $17 , "tmp" \n\t"\
34 "cmp "low" , "tmp" \n\t"\
35 "cmova %%ecx , "range" \n\t"\
36 "sbb %%ecx , %%ecx \n\t"\
37 "and %%ecx , "tmp" \n\t"\
38 "xor %%ecx , "ret" \n\t"\
39 "sub "tmp" , "low" \n\t"
40 #else /* HAVE_FAST_CMOV */
41 #define BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)\
42 "mov "tmp" , %%ecx \n\t"\
43 "shl $17 , "tmp" \n\t"\
44 "sub "low" , "tmp" \n\t"\
45 "sar $31 , "tmp" \n\t" /*lps_mask*/\
46 "sub %%ecx , "range" \n\t" /*RangeLPS - range*/\
47 "and "tmp" , "range" \n\t" /*(RangeLPS - range)&lps_mask*/\
48 "add %%ecx , "range" \n\t" /*new range*/\
49 "shl $17 , %%ecx \n\t"\
50 "and "tmp" , %%ecx \n\t"\
51 "sub %%ecx , "low" \n\t"\
52 "xor "tmp" , "ret" \n\t"
53 #endif /* HAVE_FAST_CMOV */
55 #define BRANCHLESS_GET_CABAC(ret, statep, low, lowword, range, tmp, tmpbyte, byte, end, norm_off, lps_off, mlps_off) \
56 "movzbl "statep" , "ret" \n\t"\
57 "mov "range" , "tmp" \n\t"\
58 "and $0xC0 , "range" \n\t"\
59 "movzbl "MANGLE(ff_h264_cabac_tables)"+"lps_off"("ret", "range", 2), "range" \n\t"\
60 "sub "range" , "tmp" \n\t"\
61 BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp) \
62 "movzbl "MANGLE(ff_h264_cabac_tables)"+"norm_off"("range"), %%ecx \n\t"\
63 "shl %%cl , "range" \n\t"\
64 "movzbl "MANGLE(ff_h264_cabac_tables)"+"mlps_off"+128("ret"), "tmp" \n\t"\
65 "shl %%cl , "low" \n\t"\
66 "mov "tmpbyte" , "statep" \n\t"\
67 "test "lowword" , "lowword" \n\t"\
69 "mov "byte" , %%"REG_c" \n\t"\
70 "cmp "end" , %%"REG_c" \n\t"\
72 "add"OPSIZE" $2 , "byte" \n\t"\
74 "movzwl (%%"REG_c") , "tmp" \n\t"\
75 "lea -1("low") , %%ecx \n\t"\
76 "xor "low" , %%ecx \n\t"\
77 "shr $15 , %%ecx \n\t"\
79 "shr $15 , "tmp" \n\t"\
80 "movzbl "MANGLE(ff_h264_cabac_tables)"+"norm_off"(%%ecx), %%ecx \n\t"\
81 "sub $0xFFFF , "tmp" \n\t"\
83 "add $7 , %%ecx \n\t"\
84 "shl %%cl , "tmp" \n\t"\
85 "add "tmp" , "low" \n\t"\
88 #if HAVE_7REGS && !defined(BROKEN_RELOCATIONS)
89 #define get_cabac_inline get_cabac_inline_x86
90 static av_always_inline int get_cabac_inline_x86(CABACContext *c,
96 BRANCHLESS_GET_CABAC("%0", "(%4)", "%1", "%w1",
98 "%a6(%5)", "%a7(%5)", "%a8", "%a9", "%a10")
99 : "=&r"(bit), "+&r"(c->low), "+&r"(c->range), "=&q"(tmp)
100 : "r"(state), "r"(c),
101 "i"(offsetof(CABACContext, bytestream)),
102 "i"(offsetof(CABACContext, bytestream_end)),
103 "i"(H264_NORM_SHIFT_OFFSET),
104 "i"(H264_LPS_RANGE_OFFSET),
105 "i"(H264_MLPS_STATE_OFFSET)
110 #endif /* HAVE_7REGS && !defined(BROKEN_RELOCATIONS) */
112 #define get_cabac_bypass_sign get_cabac_bypass_sign_x86
113 static av_always_inline int get_cabac_bypass_sign_x86(CABACContext *c, int val)
117 "movl %a6(%2), %k1 \n\t"
118 "movl %a3(%2), %%eax \n\t"
120 "add %%eax, %%eax \n\t"
121 "sub %k1, %%eax \n\t"
123 "and %%edx, %k1 \n\t"
124 "add %k1, %%eax \n\t"
125 "xor %%edx, %%ecx \n\t"
126 "sub %%edx, %%ecx \n\t"
127 "test %%ax, %%ax \n\t"
129 "mov %a4(%2), %1 \n\t"
130 "subl $0xFFFF, %%eax \n\t"
131 "movzwl (%1), %%edx \n\t"
133 "shrl $15, %%edx \n\t"
134 "addl %%edx, %%eax \n\t"
135 "cmp %a5(%2), %1 \n\t"
137 "add"OPSIZE" $2, %a4(%2) \n\t"
139 "movl %%eax, %a3(%2) \n\t"
141 : "+c"(val), "=&r"(tmp)
143 "i"(offsetof(CABACContext, low)),
144 "i"(offsetof(CABACContext, bytestream)),
145 "i"(offsetof(CABACContext, bytestream_end)),
146 "i"(offsetof(CABACContext, range))
147 : "%eax", "%edx", "memory"
152 #endif /* AVCODEC_X86_CABAC_H */