dsputil_mmx_rnd_template.c 22.7 KB
Newer Older
1 2
/*
 * DSP utils mmx functions are compiled twice for rnd/no_rnd
3
 * Copyright (c) 2000, 2001 Fabrice Bellard
4
 * Copyright (c) 2003-2004 Michael Niedermayer <michaelni@gmx.at>
5
 *
6 7 8 9
 * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
 * mostly rewritten by Michael Niedermayer <michaelni@gmx.at>
 * and improved by Zdenek Kabelac <kabi@users.sf.net>
 *
10 11 12
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
13 14
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
15
 * version 2.1 of the License, or (at your option) any later version.
16
 *
17
 * FFmpeg is distributed in the hope that it will be useful,
18 19 20 21 22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
23
 * License along with FFmpeg; if not, write to the Free Software
24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 26 27
 */

// put_pixels
28
static void DEF(put, pixels8_x2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
29
{
30
    MOVQ_BFE(mm6);
31
    __asm__ volatile(
32
        "lea    (%3, %3), %%"REG_a"     \n\t"
33
        ASMALIGN(3)
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
        "1:                             \n\t"
        "movq   (%1), %%mm0             \n\t"
        "movq   1(%1), %%mm1            \n\t"
        "movq   (%1, %3), %%mm2         \n\t"
        "movq   1(%1, %3), %%mm3        \n\t"
        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
        "movq   %%mm4, (%2)             \n\t"
        "movq   %%mm5, (%2, %3)         \n\t"
        "add    %%"REG_a", %1           \n\t"
        "add    %%"REG_a", %2           \n\t"
        "movq   (%1), %%mm0             \n\t"
        "movq   1(%1), %%mm1            \n\t"
        "movq   (%1, %3), %%mm2         \n\t"
        "movq   1(%1, %3), %%mm3        \n\t"
        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
        "movq   %%mm4, (%2)             \n\t"
        "movq   %%mm5, (%2, %3)         \n\t"
        "add    %%"REG_a", %1           \n\t"
        "add    %%"REG_a", %2           \n\t"
        "subl   $4, %0                  \n\t"
        "jnz    1b                      \n\t"
        :"+g"(h), "+S"(pixels), "+D"(block)
56
        :"r"((x86_reg)line_size)
57
        :REG_a, "memory");
58 59
}

60
static void av_unused DEF(put, pixels8_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
Michael Niedermayer's avatar
Michael Niedermayer committed
61 62
{
    MOVQ_BFE(mm6);
63
    __asm__ volatile(
64 65 66 67 68 69 70 71 72 73
        "testl $1, %0                   \n\t"
        " jz 1f                         \n\t"
        "movq   (%1), %%mm0             \n\t"
        "movq   (%2), %%mm1             \n\t"
        "add    %4, %1                  \n\t"
        "add    $8, %2                  \n\t"
        PAVGB(%%mm0, %%mm1, %%mm4, %%mm6)
        "movq   %%mm4, (%3)             \n\t"
        "add    %5, %3                  \n\t"
        "decl   %0                      \n\t"
74
        ASMALIGN(3)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
        "1:                             \n\t"
        "movq   (%1), %%mm0             \n\t"
        "movq   (%2), %%mm1             \n\t"
        "add    %4, %1                  \n\t"
        "movq   (%1), %%mm2             \n\t"
        "movq   8(%2), %%mm3            \n\t"
        "add    %4, %1                  \n\t"
        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
        "movq   %%mm4, (%3)             \n\t"
        "add    %5, %3                  \n\t"
        "movq   %%mm5, (%3)             \n\t"
        "add    %5, %3                  \n\t"
        "movq   (%1), %%mm0             \n\t"
        "movq   16(%2), %%mm1           \n\t"
        "add    %4, %1                  \n\t"
        "movq   (%1), %%mm2             \n\t"
        "movq   24(%2), %%mm3           \n\t"
        "add    %4, %1                  \n\t"
        "add    $32, %2                 \n\t"
        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
        "movq   %%mm4, (%3)             \n\t"
        "add    %5, %3                  \n\t"
        "movq   %%mm5, (%3)             \n\t"
        "add    %5, %3                  \n\t"
        "subl   $4, %0                  \n\t"
        "jnz    1b                      \n\t"
101
#if !HAVE_EBX_AVAILABLE //Note "+bm" and "+mb" are buggy too (with gcc 3.2.2 at least) and cannot be used
Michael Niedermayer's avatar
Michael Niedermayer committed
102 103 104 105
        :"+m"(h), "+a"(src1), "+c"(src2), "+d"(dst)
#else
        :"+b"(h), "+a"(src1), "+c"(src2), "+d"(dst)
#endif
106
        :"S"((x86_reg)src1Stride), "D"((x86_reg)dstStride)
107
        :"memory");
Michael Niedermayer's avatar
Michael Niedermayer committed
108 109
}

110
static void DEF(put, pixels16_x2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
Michael Niedermayer's avatar
Michael Niedermayer committed
111 112
{
    MOVQ_BFE(mm6);
113
    __asm__ volatile(
114
        "lea        (%3, %3), %%"REG_a" \n\t"
115
        ASMALIGN(3)
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        "1:                             \n\t"
        "movq   (%1), %%mm0             \n\t"
        "movq   1(%1), %%mm1            \n\t"
        "movq   (%1, %3), %%mm2         \n\t"
        "movq   1(%1, %3), %%mm3        \n\t"
        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
        "movq   %%mm4, (%2)             \n\t"
        "movq   %%mm5, (%2, %3)         \n\t"
        "movq   8(%1), %%mm0            \n\t"
        "movq   9(%1), %%mm1            \n\t"
        "movq   8(%1, %3), %%mm2        \n\t"
        "movq   9(%1, %3), %%mm3        \n\t"
        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
        "movq   %%mm4, 8(%2)            \n\t"
        "movq   %%mm5, 8(%2, %3)        \n\t"
        "add    %%"REG_a", %1           \n\t"
        "add    %%"REG_a", %2           \n\t"
        "movq   (%1), %%mm0             \n\t"
        "movq   1(%1), %%mm1            \n\t"
        "movq   (%1, %3), %%mm2         \n\t"
        "movq   1(%1, %3), %%mm3        \n\t"
        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
        "movq   %%mm4, (%2)             \n\t"
        "movq   %%mm5, (%2, %3)         \n\t"
        "movq   8(%1), %%mm0            \n\t"
        "movq   9(%1), %%mm1            \n\t"
        "movq   8(%1, %3), %%mm2        \n\t"
        "movq   9(%1, %3), %%mm3        \n\t"
        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
        "movq   %%mm4, 8(%2)            \n\t"
        "movq   %%mm5, 8(%2, %3)        \n\t"
        "add    %%"REG_a", %1           \n\t"
        "add    %%"REG_a", %2           \n\t"
        "subl   $4, %0                  \n\t"
        "jnz    1b                      \n\t"
        :"+g"(h), "+S"(pixels), "+D"(block)
152
        :"r"((x86_reg)line_size)
153
        :REG_a, "memory");
Michael Niedermayer's avatar
Michael Niedermayer committed
154 155
}

156
static void av_unused DEF(put, pixels16_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
Michael Niedermayer's avatar
Michael Niedermayer committed
157 158
{
    MOVQ_BFE(mm6);
159
    __asm__ volatile(
160 161 162 163 164 165 166 167 168 169 170 171 172
        "testl $1, %0                   \n\t"
        " jz 1f                         \n\t"
        "movq   (%1), %%mm0             \n\t"
        "movq   (%2), %%mm1             \n\t"
        "movq   8(%1), %%mm2            \n\t"
        "movq   8(%2), %%mm3            \n\t"
        "add    %4, %1                  \n\t"
        "add    $16, %2                 \n\t"
        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
        "movq   %%mm4, (%3)             \n\t"
        "movq   %%mm5, 8(%3)            \n\t"
        "add    %5, %3                  \n\t"
        "decl   %0                      \n\t"
173
        ASMALIGN(3)
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
        "1:                             \n\t"
        "movq   (%1), %%mm0             \n\t"
        "movq   (%2), %%mm1             \n\t"
        "movq   8(%1), %%mm2            \n\t"
        "movq   8(%2), %%mm3            \n\t"
        "add    %4, %1                  \n\t"
        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
        "movq   %%mm4, (%3)             \n\t"
        "movq   %%mm5, 8(%3)            \n\t"
        "add    %5, %3                  \n\t"
        "movq   (%1), %%mm0             \n\t"
        "movq   16(%2), %%mm1           \n\t"
        "movq   8(%1), %%mm2            \n\t"
        "movq   24(%2), %%mm3           \n\t"
        "add    %4, %1                  \n\t"
        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
        "movq   %%mm4, (%3)             \n\t"
        "movq   %%mm5, 8(%3)            \n\t"
        "add    %5, %3                  \n\t"
        "add    $32, %2                 \n\t"
        "subl   $2, %0                  \n\t"
        "jnz    1b                      \n\t"
196
#if !HAVE_EBX_AVAILABLE  //Note "+bm" and "+mb" are buggy too (with gcc 3.2.2 at least) and cannot be used
197
        :"+m"(h), "+a"(src1), "+c"(src2), "+d"(dst)
Michael Niedermayer's avatar
Michael Niedermayer committed
198
#else
199
        :"+b"(h), "+a"(src1), "+c"(src2), "+d"(dst)
Michael Niedermayer's avatar
Michael Niedermayer committed
200
#endif
201
        :"S"((x86_reg)src1Stride), "D"((x86_reg)dstStride)
202
        :"memory");
Michael Niedermayer's avatar
Michael Niedermayer committed
203 204
}

205
static void DEF(put, pixels8_y2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
206
{
207
    MOVQ_BFE(mm6);
208
    __asm__ volatile(
209 210
        "lea (%3, %3), %%"REG_a"        \n\t"
        "movq (%1), %%mm0               \n\t"
211
        ASMALIGN(3)
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
        "1:                             \n\t"
        "movq   (%1, %3), %%mm1         \n\t"
        "movq   (%1, %%"REG_a"),%%mm2   \n\t"
        PAVGBP(%%mm1, %%mm0, %%mm4,   %%mm2, %%mm1, %%mm5)
        "movq   %%mm4, (%2)             \n\t"
        "movq   %%mm5, (%2, %3)         \n\t"
        "add    %%"REG_a", %1           \n\t"
        "add    %%"REG_a", %2           \n\t"
        "movq   (%1, %3), %%mm1         \n\t"
        "movq   (%1, %%"REG_a"),%%mm0   \n\t"
        PAVGBP(%%mm1, %%mm2, %%mm4,   %%mm0, %%mm1, %%mm5)
        "movq   %%mm4, (%2)             \n\t"
        "movq   %%mm5, (%2, %3)         \n\t"
        "add    %%"REG_a", %1           \n\t"
        "add    %%"REG_a", %2           \n\t"
        "subl   $4, %0                  \n\t"
        "jnz    1b                      \n\t"
        :"+g"(h), "+S"(pixels), "+D"(block)
230
        :"r"((x86_reg)line_size)
231
        :REG_a, "memory");
232 233
}

234
static void DEF(put, pixels8_xy2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
235
{
236 237
    MOVQ_ZERO(mm7);
    SET_RND(mm6); // =2 for rnd  and  =1 for no_rnd version
238
    __asm__ volatile(
239 240 241 242 243 244 245 246 247 248 249 250
        "movq   (%1), %%mm0             \n\t"
        "movq   1(%1), %%mm4            \n\t"
        "movq   %%mm0, %%mm1            \n\t"
        "movq   %%mm4, %%mm5            \n\t"
        "punpcklbw %%mm7, %%mm0         \n\t"
        "punpcklbw %%mm7, %%mm4         \n\t"
        "punpckhbw %%mm7, %%mm1         \n\t"
        "punpckhbw %%mm7, %%mm5         \n\t"
        "paddusw %%mm0, %%mm4           \n\t"
        "paddusw %%mm1, %%mm5           \n\t"
        "xor    %%"REG_a", %%"REG_a"    \n\t"
        "add    %3, %1                  \n\t"
251
        ASMALIGN(3)
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
        "1:                             \n\t"
        "movq   (%1, %%"REG_a"), %%mm0  \n\t"
        "movq   1(%1, %%"REG_a"), %%mm2 \n\t"
        "movq   %%mm0, %%mm1            \n\t"
        "movq   %%mm2, %%mm3            \n\t"
        "punpcklbw %%mm7, %%mm0         \n\t"
        "punpcklbw %%mm7, %%mm2         \n\t"
        "punpckhbw %%mm7, %%mm1         \n\t"
        "punpckhbw %%mm7, %%mm3         \n\t"
        "paddusw %%mm2, %%mm0           \n\t"
        "paddusw %%mm3, %%mm1           \n\t"
        "paddusw %%mm6, %%mm4           \n\t"
        "paddusw %%mm6, %%mm5           \n\t"
        "paddusw %%mm0, %%mm4           \n\t"
        "paddusw %%mm1, %%mm5           \n\t"
        "psrlw  $2, %%mm4               \n\t"
        "psrlw  $2, %%mm5               \n\t"
        "packuswb  %%mm5, %%mm4         \n\t"
        "movq   %%mm4, (%2, %%"REG_a")  \n\t"
        "add    %3, %%"REG_a"           \n\t"
272

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
        "movq   (%1, %%"REG_a"), %%mm2  \n\t" // 0 <-> 2   1 <-> 3
        "movq   1(%1, %%"REG_a"), %%mm4 \n\t"
        "movq   %%mm2, %%mm3            \n\t"
        "movq   %%mm4, %%mm5            \n\t"
        "punpcklbw %%mm7, %%mm2         \n\t"
        "punpcklbw %%mm7, %%mm4         \n\t"
        "punpckhbw %%mm7, %%mm3         \n\t"
        "punpckhbw %%mm7, %%mm5         \n\t"
        "paddusw %%mm2, %%mm4           \n\t"
        "paddusw %%mm3, %%mm5           \n\t"
        "paddusw %%mm6, %%mm0           \n\t"
        "paddusw %%mm6, %%mm1           \n\t"
        "paddusw %%mm4, %%mm0           \n\t"
        "paddusw %%mm5, %%mm1           \n\t"
        "psrlw  $2, %%mm0               \n\t"
        "psrlw  $2, %%mm1               \n\t"
        "packuswb  %%mm1, %%mm0         \n\t"
        "movq   %%mm0, (%2, %%"REG_a")  \n\t"
        "add    %3, %%"REG_a"           \n\t"
292

293 294 295
        "subl   $2, %0                  \n\t"
        "jnz    1b                      \n\t"
        :"+g"(h), "+S"(pixels)
296
        :"D"(block), "r"((x86_reg)line_size)
297
        :REG_a, "memory");
298 299 300
}

// avg_pixels
301
static void av_unused DEF(avg, pixels4)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
302 303 304 305
{
    MOVQ_BFE(mm6);
    JUMPALIGN();
    do {
306
        __asm__ volatile(
307 308
             "movd  %0, %%mm0           \n\t"
             "movd  %1, %%mm1           \n\t"
309
             OP_AVG(%%mm0, %%mm1, %%mm2, %%mm6)
310 311 312 313 314 315
             "movd  %%mm2, %0           \n\t"
             :"+m"(*block)
             :"m"(*pixels)
             :"memory");
        pixels += line_size;
        block += line_size;
316 317 318 319
    }
    while (--h);
}

320
// in case more speed is needed - unroling would certainly help
321
static void DEF(avg, pixels8)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
322 323 324 325
{
    MOVQ_BFE(mm6);
    JUMPALIGN();
    do {
326
        __asm__ volatile(
327 328
             "movq  %0, %%mm0           \n\t"
             "movq  %1, %%mm1           \n\t"
329
             OP_AVG(%%mm0, %%mm1, %%mm2, %%mm6)
330 331 332 333 334 335
             "movq  %%mm2, %0           \n\t"
             :"+m"(*block)
             :"m"(*pixels)
             :"memory");
        pixels += line_size;
        block += line_size;
336 337 338 339
    }
    while (--h);
}

340
static void DEF(avg, pixels16)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
Michael Niedermayer's avatar
Michael Niedermayer committed
341 342 343 344
{
    MOVQ_BFE(mm6);
    JUMPALIGN();
    do {
345
        __asm__ volatile(
346 347
             "movq  %0, %%mm0           \n\t"
             "movq  %1, %%mm1           \n\t"
348
             OP_AVG(%%mm0, %%mm1, %%mm2, %%mm6)
349 350 351
             "movq  %%mm2, %0           \n\t"
             "movq  8%0, %%mm0          \n\t"
             "movq  8%1, %%mm1          \n\t"
352
             OP_AVG(%%mm0, %%mm1, %%mm2, %%mm6)
353 354 355 356 357 358
             "movq  %%mm2, 8%0          \n\t"
             :"+m"(*block)
             :"m"(*pixels)
             :"memory");
        pixels += line_size;
        block += line_size;
Michael Niedermayer's avatar
Michael Niedermayer committed
359 360 361 362
    }
    while (--h);
}

363
static void DEF(avg, pixels8_x2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
364 365 366 367
{
    MOVQ_BFE(mm6);
    JUMPALIGN();
    do {
368
        __asm__ volatile(
369 370 371 372
            "movq  %1, %%mm0            \n\t"
            "movq  1%1, %%mm1           \n\t"
            "movq  %0, %%mm3            \n\t"
            PAVGB(%%mm0, %%mm1, %%mm2, %%mm6)
373
            OP_AVG(%%mm3, %%mm2, %%mm0, %%mm6)
374 375 376 377 378 379
            "movq  %%mm0, %0            \n\t"
            :"+m"(*block)
            :"m"(*pixels)
            :"memory");
        pixels += line_size;
        block += line_size;
380 381 382
    } while (--h);
}

383
static av_unused void DEF(avg, pixels8_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
Michael Niedermayer's avatar
Michael Niedermayer committed
384 385 386 387
{
    MOVQ_BFE(mm6);
    JUMPALIGN();
    do {
388
        __asm__ volatile(
389 390 391 392
            "movq  %1, %%mm0            \n\t"
            "movq  %2, %%mm1            \n\t"
            "movq  %0, %%mm3            \n\t"
            PAVGB(%%mm0, %%mm1, %%mm2, %%mm6)
393
            OP_AVG(%%mm3, %%mm2, %%mm0, %%mm6)
394 395 396 397 398
            "movq  %%mm0, %0            \n\t"
            :"+m"(*dst)
            :"m"(*src1), "m"(*src2)
            :"memory");
        dst += dstStride;
Michael Niedermayer's avatar
Michael Niedermayer committed
399 400 401 402 403
        src1 += src1Stride;
        src2 += 8;
    } while (--h);
}

404
static void DEF(avg, pixels16_x2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
Michael Niedermayer's avatar
Michael Niedermayer committed
405 406 407 408
{
    MOVQ_BFE(mm6);
    JUMPALIGN();
    do {
409
        __asm__ volatile(
410 411 412 413
            "movq  %1, %%mm0            \n\t"
            "movq  1%1, %%mm1           \n\t"
            "movq  %0, %%mm3            \n\t"
            PAVGB(%%mm0, %%mm1, %%mm2, %%mm6)
414
            OP_AVG(%%mm3, %%mm2, %%mm0, %%mm6)
415 416 417 418 419
            "movq  %%mm0, %0            \n\t"
            "movq  8%1, %%mm0           \n\t"
            "movq  9%1, %%mm1           \n\t"
            "movq  8%0, %%mm3           \n\t"
            PAVGB(%%mm0, %%mm1, %%mm2, %%mm6)
420
            OP_AVG(%%mm3, %%mm2, %%mm0, %%mm6)
421 422 423 424 425 426
            "movq  %%mm0, 8%0           \n\t"
            :"+m"(*block)
            :"m"(*pixels)
            :"memory");
        pixels += line_size;
        block += line_size;
Michael Niedermayer's avatar
Michael Niedermayer committed
427 428 429
    } while (--h);
}

430
static av_unused void DEF(avg, pixels16_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
Michael Niedermayer's avatar
Michael Niedermayer committed
431 432 433 434
{
    MOVQ_BFE(mm6);
    JUMPALIGN();
    do {
435
        __asm__ volatile(
436 437 438 439
            "movq  %1, %%mm0            \n\t"
            "movq  %2, %%mm1            \n\t"
            "movq  %0, %%mm3            \n\t"
            PAVGB(%%mm0, %%mm1, %%mm2, %%mm6)
440
            OP_AVG(%%mm3, %%mm2, %%mm0, %%mm6)
441 442 443 444 445
            "movq  %%mm0, %0            \n\t"
            "movq  8%1, %%mm0           \n\t"
            "movq  8%2, %%mm1           \n\t"
            "movq  8%0, %%mm3           \n\t"
            PAVGB(%%mm0, %%mm1, %%mm2, %%mm6)
446
            OP_AVG(%%mm3, %%mm2, %%mm0, %%mm6)
447 448 449 450 451
            "movq  %%mm0, 8%0           \n\t"
            :"+m"(*dst)
            :"m"(*src1), "m"(*src2)
            :"memory");
        dst += dstStride;
Michael Niedermayer's avatar
Michael Niedermayer committed
452 453 454 455 456
        src1 += src1Stride;
        src2 += 16;
    } while (--h);
}

457
static void DEF(avg, pixels8_y2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
458 459
{
    MOVQ_BFE(mm6);
460
    __asm__ volatile(
461 462
        "lea    (%3, %3), %%"REG_a"     \n\t"
        "movq   (%1), %%mm0             \n\t"
463
        ASMALIGN(3)
464 465 466 467 468
        "1:                             \n\t"
        "movq   (%1, %3), %%mm1         \n\t"
        "movq   (%1, %%"REG_a"), %%mm2  \n\t"
        PAVGBP(%%mm1, %%mm0, %%mm4,   %%mm2, %%mm1, %%mm5)
        "movq   (%2), %%mm3             \n\t"
469
        OP_AVG(%%mm3, %%mm4, %%mm0, %%mm6)
470
        "movq   (%2, %3), %%mm3         \n\t"
471
        OP_AVG(%%mm3, %%mm5, %%mm1, %%mm6)
472 473 474 475
        "movq   %%mm0, (%2)             \n\t"
        "movq   %%mm1, (%2, %3)         \n\t"
        "add    %%"REG_a", %1           \n\t"
        "add    %%"REG_a", %2           \n\t"
476

477 478 479 480
        "movq   (%1, %3), %%mm1         \n\t"
        "movq   (%1, %%"REG_a"), %%mm0  \n\t"
        PAVGBP(%%mm1, %%mm2, %%mm4,   %%mm0, %%mm1, %%mm5)
        "movq   (%2), %%mm3             \n\t"
481
        OP_AVG(%%mm3, %%mm4, %%mm2, %%mm6)
482
        "movq   (%2, %3), %%mm3         \n\t"
483
        OP_AVG(%%mm3, %%mm5, %%mm1, %%mm6)
484 485 486 487
        "movq   %%mm2, (%2)             \n\t"
        "movq   %%mm1, (%2, %3)         \n\t"
        "add    %%"REG_a", %1           \n\t"
        "add    %%"REG_a", %2           \n\t"
488

489 490 491
        "subl   $4, %0                  \n\t"
        "jnz    1b                      \n\t"
        :"+g"(h), "+S"(pixels), "+D"(block)
492
        :"r"((x86_reg)line_size)
493
        :REG_a, "memory");
494 495 496
}

// this routine is 'slightly' suboptimal but mostly unused
497
static void DEF(avg, pixels8_xy2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
498 499 500
{
    MOVQ_ZERO(mm7);
    SET_RND(mm6); // =2 for rnd  and  =1 for no_rnd version
501
    __asm__ volatile(
502 503 504 505 506 507 508 509 510 511 512 513
        "movq   (%1), %%mm0             \n\t"
        "movq   1(%1), %%mm4            \n\t"
        "movq   %%mm0, %%mm1            \n\t"
        "movq   %%mm4, %%mm5            \n\t"
        "punpcklbw %%mm7, %%mm0         \n\t"
        "punpcklbw %%mm7, %%mm4         \n\t"
        "punpckhbw %%mm7, %%mm1         \n\t"
        "punpckhbw %%mm7, %%mm5         \n\t"
        "paddusw %%mm0, %%mm4           \n\t"
        "paddusw %%mm1, %%mm5           \n\t"
        "xor    %%"REG_a", %%"REG_a"    \n\t"
        "add    %3, %1                  \n\t"
514
        ASMALIGN(3)
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
        "1:                             \n\t"
        "movq   (%1, %%"REG_a"), %%mm0  \n\t"
        "movq   1(%1, %%"REG_a"), %%mm2 \n\t"
        "movq   %%mm0, %%mm1            \n\t"
        "movq   %%mm2, %%mm3            \n\t"
        "punpcklbw %%mm7, %%mm0         \n\t"
        "punpcklbw %%mm7, %%mm2         \n\t"
        "punpckhbw %%mm7, %%mm1         \n\t"
        "punpckhbw %%mm7, %%mm3         \n\t"
        "paddusw %%mm2, %%mm0           \n\t"
        "paddusw %%mm3, %%mm1           \n\t"
        "paddusw %%mm6, %%mm4           \n\t"
        "paddusw %%mm6, %%mm5           \n\t"
        "paddusw %%mm0, %%mm4           \n\t"
        "paddusw %%mm1, %%mm5           \n\t"
        "psrlw  $2, %%mm4               \n\t"
        "psrlw  $2, %%mm5               \n\t"
                "movq   (%2, %%"REG_a"), %%mm3  \n\t"
        "packuswb  %%mm5, %%mm4         \n\t"
                "pcmpeqd %%mm2, %%mm2   \n\t"
                "paddb %%mm2, %%mm2     \n\t"
536
                OP_AVG(%%mm3, %%mm4, %%mm5, %%mm2)
537 538
                "movq   %%mm5, (%2, %%"REG_a")  \n\t"
        "add    %3, %%"REG_a"                \n\t"
539

540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
        "movq   (%1, %%"REG_a"), %%mm2  \n\t" // 0 <-> 2   1 <-> 3
        "movq   1(%1, %%"REG_a"), %%mm4 \n\t"
        "movq   %%mm2, %%mm3            \n\t"
        "movq   %%mm4, %%mm5            \n\t"
        "punpcklbw %%mm7, %%mm2         \n\t"
        "punpcklbw %%mm7, %%mm4         \n\t"
        "punpckhbw %%mm7, %%mm3         \n\t"
        "punpckhbw %%mm7, %%mm5         \n\t"
        "paddusw %%mm2, %%mm4           \n\t"
        "paddusw %%mm3, %%mm5           \n\t"
        "paddusw %%mm6, %%mm0           \n\t"
        "paddusw %%mm6, %%mm1           \n\t"
        "paddusw %%mm4, %%mm0           \n\t"
        "paddusw %%mm5, %%mm1           \n\t"
        "psrlw  $2, %%mm0               \n\t"
        "psrlw  $2, %%mm1               \n\t"
                "movq   (%2, %%"REG_a"), %%mm3  \n\t"
        "packuswb  %%mm1, %%mm0         \n\t"
                "pcmpeqd %%mm2, %%mm2   \n\t"
                "paddb %%mm2, %%mm2     \n\t"
560
                OP_AVG(%%mm3, %%mm0, %%mm1, %%mm2)
561 562
                "movq   %%mm1, (%2, %%"REG_a")  \n\t"
        "add    %3, %%"REG_a"           \n\t"
563

564 565 566
        "subl   $2, %0                  \n\t"
        "jnz    1b                      \n\t"
        :"+g"(h), "+S"(pixels)
567
        :"D"(block), "r"((x86_reg)line_size)
568
        :REG_a, "memory");
569
}
Michael Niedermayer's avatar
Michael Niedermayer committed
570 571

//FIXME optimize
572
static void DEF(put, pixels16_y2)(uint8_t *block, const uint8_t *pixels, int line_size, int h){
Michael Niedermayer's avatar
Michael Niedermayer committed
573 574 575 576
    DEF(put, pixels8_y2)(block  , pixels  , line_size, h);
    DEF(put, pixels8_y2)(block+8, pixels+8, line_size, h);
}

577
static void DEF(put, pixels16_xy2)(uint8_t *block, const uint8_t *pixels, int line_size, int h){
Michael Niedermayer's avatar
Michael Niedermayer committed
578 579 580 581
    DEF(put, pixels8_xy2)(block  , pixels  , line_size, h);
    DEF(put, pixels8_xy2)(block+8, pixels+8, line_size, h);
}

582
static void DEF(avg, pixels16_y2)(uint8_t *block, const uint8_t *pixels, int line_size, int h){
Michael Niedermayer's avatar
Michael Niedermayer committed
583 584 585 586
    DEF(avg, pixels8_y2)(block  , pixels  , line_size, h);
    DEF(avg, pixels8_y2)(block+8, pixels+8, line_size, h);
}

587
static void DEF(avg, pixels16_xy2)(uint8_t *block, const uint8_t *pixels, int line_size, int h){
Michael Niedermayer's avatar
Michael Niedermayer committed
588 589 590
    DEF(avg, pixels8_xy2)(block  , pixels  , line_size, h);
    DEF(avg, pixels8_xy2)(block+8, pixels+8, line_size, h);
}