sad_inline.h revision f5af6314db25ff3bef9bd2eeba201bc6cc60805d
129a84457aed4c45bc900998b5e11c03023264208James Dong/* ------------------------------------------------------------------
229a84457aed4c45bc900998b5e11c03023264208James Dong * Copyright (C) 1998-2009 PacketVideo
329a84457aed4c45bc900998b5e11c03023264208James Dong *
429a84457aed4c45bc900998b5e11c03023264208James Dong * Licensed under the Apache License, Version 2.0 (the "License");
529a84457aed4c45bc900998b5e11c03023264208James Dong * you may not use this file except in compliance with the License.
629a84457aed4c45bc900998b5e11c03023264208James Dong * You may obtain a copy of the License at
729a84457aed4c45bc900998b5e11c03023264208James Dong *
829a84457aed4c45bc900998b5e11c03023264208James Dong *      http://www.apache.org/licenses/LICENSE-2.0
929a84457aed4c45bc900998b5e11c03023264208James Dong *
1029a84457aed4c45bc900998b5e11c03023264208James Dong * Unless required by applicable law or agreed to in writing, software
1129a84457aed4c45bc900998b5e11c03023264208James Dong * distributed under the License is distributed on an "AS IS" BASIS,
1229a84457aed4c45bc900998b5e11c03023264208James Dong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
1329a84457aed4c45bc900998b5e11c03023264208James Dong * express or implied.
1429a84457aed4c45bc900998b5e11c03023264208James Dong * See the License for the specific language governing permissions
1529a84457aed4c45bc900998b5e11c03023264208James Dong * and limitations under the License.
1629a84457aed4c45bc900998b5e11c03023264208James Dong * -------------------------------------------------------------------
1729a84457aed4c45bc900998b5e11c03023264208James Dong */
1829a84457aed4c45bc900998b5e11c03023264208James Dong#ifndef _SAD_INLINE_H_
1929a84457aed4c45bc900998b5e11c03023264208James Dong#define _SAD_INLINE_H_
2029a84457aed4c45bc900998b5e11c03023264208James Dong
2129a84457aed4c45bc900998b5e11c03023264208James Dong#ifdef __cplusplus
2229a84457aed4c45bc900998b5e11c03023264208James Dongextern "C"
2329a84457aed4c45bc900998b5e11c03023264208James Dong{
2429a84457aed4c45bc900998b5e11c03023264208James Dong#endif
2529a84457aed4c45bc900998b5e11c03023264208James Dong
26f5af6314db25ff3bef9bd2eeba201bc6cc60805dMartin Storsjo/* Intentionally not using the gcc asm version, since it (if fixed so
27f5af6314db25ff3bef9bd2eeba201bc6cc60805dMartin Storsjo * as to not crash - the current register constraints are faulty) is
28f5af6314db25ff3bef9bd2eeba201bc6cc60805dMartin Storsjo * slightly slower than the plain C version on modern GCC versions. */
29f5af6314db25ff3bef9bd2eeba201bc6cc60805dMartin Storsjo#if !defined(__CC_ARM) /* Generic C version */
3029a84457aed4c45bc900998b5e11c03023264208James Dong
3129a84457aed4c45bc900998b5e11c03023264208James Dong    __inline int32 SUB_SAD(int32 sad, int32 tmp, int32 tmp2)
3229a84457aed4c45bc900998b5e11c03023264208James Dong    {
3329a84457aed4c45bc900998b5e11c03023264208James Dong        tmp = tmp - tmp2;
3429a84457aed4c45bc900998b5e11c03023264208James Dong        if (tmp > 0) sad += tmp;
3529a84457aed4c45bc900998b5e11c03023264208James Dong        else sad -= tmp;
3629a84457aed4c45bc900998b5e11c03023264208James Dong
3729a84457aed4c45bc900998b5e11c03023264208James Dong        return sad;
3829a84457aed4c45bc900998b5e11c03023264208James Dong    }
3929a84457aed4c45bc900998b5e11c03023264208James Dong
4029a84457aed4c45bc900998b5e11c03023264208James Dong    __inline int32 sad_4pixel(int32 src1, int32 src2, int32 mask)
4129a84457aed4c45bc900998b5e11c03023264208James Dong    {
4229a84457aed4c45bc900998b5e11c03023264208James Dong        int32 x7;
4329a84457aed4c45bc900998b5e11c03023264208James Dong
4429a84457aed4c45bc900998b5e11c03023264208James Dong        x7 = src2 ^ src1;       /* check odd/even combination */
4529a84457aed4c45bc900998b5e11c03023264208James Dong        if ((uint32)src2 >= (uint32)src1)
4629a84457aed4c45bc900998b5e11c03023264208James Dong        {
4729a84457aed4c45bc900998b5e11c03023264208James Dong            src1 = src2 - src1;     /* subs */
4829a84457aed4c45bc900998b5e11c03023264208James Dong        }
4929a84457aed4c45bc900998b5e11c03023264208James Dong        else
5029a84457aed4c45bc900998b5e11c03023264208James Dong        {
5129a84457aed4c45bc900998b5e11c03023264208James Dong            src1 = src1 - src2;
5229a84457aed4c45bc900998b5e11c03023264208James Dong        }
5329a84457aed4c45bc900998b5e11c03023264208James Dong        x7 = x7 ^ src1;     /* only odd bytes need to add carry */
5429a84457aed4c45bc900998b5e11c03023264208James Dong        x7 = mask & ((uint32)x7 >> 1);
5529a84457aed4c45bc900998b5e11c03023264208James Dong        x7 = (x7 << 8) - x7;
5629a84457aed4c45bc900998b5e11c03023264208James Dong        src1 = src1 + (x7 >> 7); /* add 0xFF to the negative byte, add back carry */
5729a84457aed4c45bc900998b5e11c03023264208James Dong        src1 = src1 ^(x7 >> 7);   /* take absolute value of negative byte */
5829a84457aed4c45bc900998b5e11c03023264208James Dong
5929a84457aed4c45bc900998b5e11c03023264208James Dong        return src1;
6029a84457aed4c45bc900998b5e11c03023264208James Dong    }
6129a84457aed4c45bc900998b5e11c03023264208James Dong
6229a84457aed4c45bc900998b5e11c03023264208James Dong#define NUMBER 3
6329a84457aed4c45bc900998b5e11c03023264208James Dong#define SHIFT 24
6429a84457aed4c45bc900998b5e11c03023264208James Dong
6529a84457aed4c45bc900998b5e11c03023264208James Dong#include "sad_mb_offset.h"
6629a84457aed4c45bc900998b5e11c03023264208James Dong
6729a84457aed4c45bc900998b5e11c03023264208James Dong#undef NUMBER
6829a84457aed4c45bc900998b5e11c03023264208James Dong#define NUMBER 2
6929a84457aed4c45bc900998b5e11c03023264208James Dong#undef SHIFT
7029a84457aed4c45bc900998b5e11c03023264208James Dong#define SHIFT 16
7129a84457aed4c45bc900998b5e11c03023264208James Dong#include "sad_mb_offset.h"
7229a84457aed4c45bc900998b5e11c03023264208James Dong
7329a84457aed4c45bc900998b5e11c03023264208James Dong#undef NUMBER
7429a84457aed4c45bc900998b5e11c03023264208James Dong#define NUMBER 1
7529a84457aed4c45bc900998b5e11c03023264208James Dong#undef SHIFT
7629a84457aed4c45bc900998b5e11c03023264208James Dong#define SHIFT 8
7729a84457aed4c45bc900998b5e11c03023264208James Dong#include "sad_mb_offset.h"
7829a84457aed4c45bc900998b5e11c03023264208James Dong
7929a84457aed4c45bc900998b5e11c03023264208James Dong
8029a84457aed4c45bc900998b5e11c03023264208James Dong    __inline int32 simd_sad_mb(uint8 *ref, uint8 *blk, int dmin, int lx)
8129a84457aed4c45bc900998b5e11c03023264208James Dong    {
8229a84457aed4c45bc900998b5e11c03023264208James Dong        int32 x4, x5, x6, x8, x9, x10, x11, x12, x14;
8329a84457aed4c45bc900998b5e11c03023264208James Dong
8429a84457aed4c45bc900998b5e11c03023264208James Dong        x9 = 0x80808080; /* const. */
8529a84457aed4c45bc900998b5e11c03023264208James Dong
864b43b41eaf8c4c80f66185e13620cf94b8b2ef5bMartin Storsjo        x8 = (intptr_t)ref & 0x3;
8729a84457aed4c45bc900998b5e11c03023264208James Dong        if (x8 == 3)
8829a84457aed4c45bc900998b5e11c03023264208James Dong            goto SadMBOffset3;
8929a84457aed4c45bc900998b5e11c03023264208James Dong        if (x8 == 2)
9029a84457aed4c45bc900998b5e11c03023264208James Dong            goto SadMBOffset2;
9129a84457aed4c45bc900998b5e11c03023264208James Dong        if (x8 == 1)
9229a84457aed4c45bc900998b5e11c03023264208James Dong            goto SadMBOffset1;
9329a84457aed4c45bc900998b5e11c03023264208James Dong
9429a84457aed4c45bc900998b5e11c03023264208James Dong//  x5 = (x4<<8)-x4; /* x5 = x4*255; */
9529a84457aed4c45bc900998b5e11c03023264208James Dong        x4 = x5 = 0;
9629a84457aed4c45bc900998b5e11c03023264208James Dong
9729a84457aed4c45bc900998b5e11c03023264208James Dong        x6 = 0xFFFF00FF;
9829a84457aed4c45bc900998b5e11c03023264208James Dong
9929a84457aed4c45bc900998b5e11c03023264208James Dong        ref -= lx;
10029a84457aed4c45bc900998b5e11c03023264208James Dong        blk -= 16;
10129a84457aed4c45bc900998b5e11c03023264208James Dong
10229a84457aed4c45bc900998b5e11c03023264208James Dong        x8 = 16;
10329a84457aed4c45bc900998b5e11c03023264208James Dong
10429a84457aed4c45bc900998b5e11c03023264208James DongLOOP_SAD0:
10529a84457aed4c45bc900998b5e11c03023264208James Dong        /****** process 8 pixels ******/
10629a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = *((uint32*)(ref += lx));
10729a84457aed4c45bc900998b5e11c03023264208James Dong        x11 = *((uint32*)(ref + 4));
10829a84457aed4c45bc900998b5e11c03023264208James Dong        x12 = *((uint32*)(blk += 16));
10929a84457aed4c45bc900998b5e11c03023264208James Dong        x14 = *((uint32*)(blk + 4));
11029a84457aed4c45bc900998b5e11c03023264208James Dong
11129a84457aed4c45bc900998b5e11c03023264208James Dong        /* process x11 & x14 */
11229a84457aed4c45bc900998b5e11c03023264208James Dong        x11 = sad_4pixel(x11, x14, x9);
11329a84457aed4c45bc900998b5e11c03023264208James Dong
11429a84457aed4c45bc900998b5e11c03023264208James Dong        /* process x12 & x10 */
11529a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = sad_4pixel(x10, x12, x9);
11629a84457aed4c45bc900998b5e11c03023264208James Dong
11729a84457aed4c45bc900998b5e11c03023264208James Dong        x5 = x5 + x10; /* accumulate low bytes */
11829a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = x10 & (x6 << 8); /* x10 & 0xFF00FF00 */
11929a84457aed4c45bc900998b5e11c03023264208James Dong        x4 = x4 + ((uint32)x10 >> 8);  /* accumulate high bytes */
12029a84457aed4c45bc900998b5e11c03023264208James Dong        x5 = x5 + x11;  /* accumulate low bytes */
12129a84457aed4c45bc900998b5e11c03023264208James Dong        x11 = x11 & (x6 << 8); /* x11 & 0xFF00FF00 */
12229a84457aed4c45bc900998b5e11c03023264208James Dong        x4 = x4 + ((uint32)x11 >> 8);  /* accumulate high bytes */
12329a84457aed4c45bc900998b5e11c03023264208James Dong
12429a84457aed4c45bc900998b5e11c03023264208James Dong        /****** process 8 pixels ******/
12529a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = *((uint32*)(ref + 8));
12629a84457aed4c45bc900998b5e11c03023264208James Dong        x11 = *((uint32*)(ref + 12));
12729a84457aed4c45bc900998b5e11c03023264208James Dong        x12 = *((uint32*)(blk + 8));
12829a84457aed4c45bc900998b5e11c03023264208James Dong        x14 = *((uint32*)(blk + 12));
12929a84457aed4c45bc900998b5e11c03023264208James Dong
13029a84457aed4c45bc900998b5e11c03023264208James Dong        /* process x11 & x14 */
13129a84457aed4c45bc900998b5e11c03023264208James Dong        x11 = sad_4pixel(x11, x14, x9);
13229a84457aed4c45bc900998b5e11c03023264208James Dong
13329a84457aed4c45bc900998b5e11c03023264208James Dong        /* process x12 & x10 */
13429a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = sad_4pixel(x10, x12, x9);
13529a84457aed4c45bc900998b5e11c03023264208James Dong
13629a84457aed4c45bc900998b5e11c03023264208James Dong        x5 = x5 + x10;  /* accumulate low bytes */
13729a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = x10 & (x6 << 8); /* x10 & 0xFF00FF00 */
13829a84457aed4c45bc900998b5e11c03023264208James Dong        x4 = x4 + ((uint32)x10 >> 8); /* accumulate high bytes */
13929a84457aed4c45bc900998b5e11c03023264208James Dong        x5 = x5 + x11;  /* accumulate low bytes */
14029a84457aed4c45bc900998b5e11c03023264208James Dong        x11 = x11 & (x6 << 8); /* x11 & 0xFF00FF00 */
14129a84457aed4c45bc900998b5e11c03023264208James Dong        x4 = x4 + ((uint32)x11 >> 8);  /* accumulate high bytes */
14229a84457aed4c45bc900998b5e11c03023264208James Dong
14329a84457aed4c45bc900998b5e11c03023264208James Dong        /****************/
14429a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = x5 - (x4 << 8); /* extract low bytes */
14529a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = x10 + x4;     /* add with high bytes */
14629a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = x10 + (x10 << 16); /* add with lower half word */
14729a84457aed4c45bc900998b5e11c03023264208James Dong
14829a84457aed4c45bc900998b5e11c03023264208James Dong        if ((int)((uint32)x10 >> 16) <= dmin) /* compare with dmin */
14929a84457aed4c45bc900998b5e11c03023264208James Dong        {
15029a84457aed4c45bc900998b5e11c03023264208James Dong            if (--x8)
15129a84457aed4c45bc900998b5e11c03023264208James Dong            {
15229a84457aed4c45bc900998b5e11c03023264208James Dong                goto LOOP_SAD0;
15329a84457aed4c45bc900998b5e11c03023264208James Dong            }
15429a84457aed4c45bc900998b5e11c03023264208James Dong
15529a84457aed4c45bc900998b5e11c03023264208James Dong        }
15629a84457aed4c45bc900998b5e11c03023264208James Dong
15729a84457aed4c45bc900998b5e11c03023264208James Dong        return ((uint32)x10 >> 16);
15829a84457aed4c45bc900998b5e11c03023264208James Dong
15929a84457aed4c45bc900998b5e11c03023264208James DongSadMBOffset3:
16029a84457aed4c45bc900998b5e11c03023264208James Dong
16129a84457aed4c45bc900998b5e11c03023264208James Dong        return sad_mb_offset3(ref, blk, lx, dmin);
16229a84457aed4c45bc900998b5e11c03023264208James Dong
16329a84457aed4c45bc900998b5e11c03023264208James DongSadMBOffset2:
16429a84457aed4c45bc900998b5e11c03023264208James Dong
16529a84457aed4c45bc900998b5e11c03023264208James Dong        return sad_mb_offset2(ref, blk, lx, dmin);
16629a84457aed4c45bc900998b5e11c03023264208James Dong
16729a84457aed4c45bc900998b5e11c03023264208James DongSadMBOffset1:
16829a84457aed4c45bc900998b5e11c03023264208James Dong
16929a84457aed4c45bc900998b5e11c03023264208James Dong        return sad_mb_offset1(ref, blk, lx, dmin);
17029a84457aed4c45bc900998b5e11c03023264208James Dong
17129a84457aed4c45bc900998b5e11c03023264208James Dong    }
17229a84457aed4c45bc900998b5e11c03023264208James Dong
17329a84457aed4c45bc900998b5e11c03023264208James Dong#elif defined(__CC_ARM)  /* only work with arm v5 */
17429a84457aed4c45bc900998b5e11c03023264208James Dong
17529a84457aed4c45bc900998b5e11c03023264208James Dong    __inline int32 SUB_SAD(int32 sad, int32 tmp, int32 tmp2)
17629a84457aed4c45bc900998b5e11c03023264208James Dong    {
17729a84457aed4c45bc900998b5e11c03023264208James Dong        __asm
17829a84457aed4c45bc900998b5e11c03023264208James Dong        {
17929a84457aed4c45bc900998b5e11c03023264208James Dong            rsbs    tmp, tmp, tmp2 ;
18029a84457aed4c45bc900998b5e11c03023264208James Dong            rsbmi   tmp, tmp, #0 ;
18129a84457aed4c45bc900998b5e11c03023264208James Dong            add     sad, sad, tmp ;
18229a84457aed4c45bc900998b5e11c03023264208James Dong        }
18329a84457aed4c45bc900998b5e11c03023264208James Dong
18429a84457aed4c45bc900998b5e11c03023264208James Dong        return sad;
18529a84457aed4c45bc900998b5e11c03023264208James Dong    }
18629a84457aed4c45bc900998b5e11c03023264208James Dong
18729a84457aed4c45bc900998b5e11c03023264208James Dong    __inline int32 sad_4pixel(int32 src1, int32 src2, int32 mask)
18829a84457aed4c45bc900998b5e11c03023264208James Dong    {
18929a84457aed4c45bc900998b5e11c03023264208James Dong        int32 x7;
19029a84457aed4c45bc900998b5e11c03023264208James Dong
19129a84457aed4c45bc900998b5e11c03023264208James Dong        __asm
19229a84457aed4c45bc900998b5e11c03023264208James Dong        {
19329a84457aed4c45bc900998b5e11c03023264208James Dong            EOR     x7, src2, src1;     /* check odd/even combination */
19429a84457aed4c45bc900998b5e11c03023264208James Dong            SUBS    src1, src2, src1;
19529a84457aed4c45bc900998b5e11c03023264208James Dong            EOR     x7, x7, src1;
19629a84457aed4c45bc900998b5e11c03023264208James Dong            AND     x7, mask, x7, lsr #1;
19729a84457aed4c45bc900998b5e11c03023264208James Dong            ORRCC   x7, x7, #0x80000000;
19829a84457aed4c45bc900998b5e11c03023264208James Dong            RSB     x7, x7, x7, lsl #8;
19929a84457aed4c45bc900998b5e11c03023264208James Dong            ADD     src1, src1, x7, asr #7;   /* add 0xFF to the negative byte, add back carry */
20029a84457aed4c45bc900998b5e11c03023264208James Dong            EOR     src1, src1, x7, asr #7;   /* take absolute value of negative byte */
20129a84457aed4c45bc900998b5e11c03023264208James Dong        }
20229a84457aed4c45bc900998b5e11c03023264208James Dong
20329a84457aed4c45bc900998b5e11c03023264208James Dong        return src1;
20429a84457aed4c45bc900998b5e11c03023264208James Dong    }
20529a84457aed4c45bc900998b5e11c03023264208James Dong
20629a84457aed4c45bc900998b5e11c03023264208James Dong    __inline int32 sad_4pixelN(int32 src1, int32 src2, int32 mask)
20729a84457aed4c45bc900998b5e11c03023264208James Dong    {
20829a84457aed4c45bc900998b5e11c03023264208James Dong        int32 x7;
20929a84457aed4c45bc900998b5e11c03023264208James Dong
21029a84457aed4c45bc900998b5e11c03023264208James Dong        __asm
21129a84457aed4c45bc900998b5e11c03023264208James Dong        {
21229a84457aed4c45bc900998b5e11c03023264208James Dong            EOR      x7, src2, src1;        /* check odd/even combination */
21329a84457aed4c45bc900998b5e11c03023264208James Dong            ADDS     src1, src2, src1;
21429a84457aed4c45bc900998b5e11c03023264208James Dong            EOR      x7, x7, src1;      /* only odd bytes need to add carry */
21529a84457aed4c45bc900998b5e11c03023264208James Dong            ANDS     x7, mask, x7, rrx;
21629a84457aed4c45bc900998b5e11c03023264208James Dong            RSB      x7, x7, x7, lsl #8;
21729a84457aed4c45bc900998b5e11c03023264208James Dong            SUB      src1, src1, x7, asr #7;  /* add 0xFF to the negative byte, add back carry */
21829a84457aed4c45bc900998b5e11c03023264208James Dong            EOR      src1, src1, x7, asr #7; /* take absolute value of negative byte */
21929a84457aed4c45bc900998b5e11c03023264208James Dong        }
22029a84457aed4c45bc900998b5e11c03023264208James Dong
22129a84457aed4c45bc900998b5e11c03023264208James Dong        return src1;
22229a84457aed4c45bc900998b5e11c03023264208James Dong    }
22329a84457aed4c45bc900998b5e11c03023264208James Dong
22429a84457aed4c45bc900998b5e11c03023264208James Dong#define sum_accumulate  __asm{      SBC      x5, x5, x10;  /* accumulate low bytes */ \
22529a84457aed4c45bc900998b5e11c03023264208James Dong        BIC      x10, x6, x10;   /* x10 & 0xFF00FF00 */ \
22629a84457aed4c45bc900998b5e11c03023264208James Dong        ADD      x4, x4, x10,lsr #8;   /* accumulate high bytes */ \
22729a84457aed4c45bc900998b5e11c03023264208James Dong        SBC      x5, x5, x11;    /* accumulate low bytes */ \
22829a84457aed4c45bc900998b5e11c03023264208James Dong        BIC      x11, x6, x11;   /* x11 & 0xFF00FF00 */ \
22929a84457aed4c45bc900998b5e11c03023264208James Dong        ADD      x4, x4, x11,lsr #8; } /* accumulate high bytes */
23029a84457aed4c45bc900998b5e11c03023264208James Dong
23129a84457aed4c45bc900998b5e11c03023264208James Dong
23229a84457aed4c45bc900998b5e11c03023264208James Dong#define NUMBER 3
23329a84457aed4c45bc900998b5e11c03023264208James Dong#define SHIFT 24
23429a84457aed4c45bc900998b5e11c03023264208James Dong#define INC_X8 0x08000001
23529a84457aed4c45bc900998b5e11c03023264208James Dong
23629a84457aed4c45bc900998b5e11c03023264208James Dong#include "sad_mb_offset.h"
23729a84457aed4c45bc900998b5e11c03023264208James Dong
23829a84457aed4c45bc900998b5e11c03023264208James Dong#undef NUMBER
23929a84457aed4c45bc900998b5e11c03023264208James Dong#define NUMBER 2
24029a84457aed4c45bc900998b5e11c03023264208James Dong#undef SHIFT
24129a84457aed4c45bc900998b5e11c03023264208James Dong#define SHIFT 16
24229a84457aed4c45bc900998b5e11c03023264208James Dong#undef INC_X8
24329a84457aed4c45bc900998b5e11c03023264208James Dong#define INC_X8 0x10000001
24429a84457aed4c45bc900998b5e11c03023264208James Dong#include "sad_mb_offset.h"
24529a84457aed4c45bc900998b5e11c03023264208James Dong
24629a84457aed4c45bc900998b5e11c03023264208James Dong#undef NUMBER
24729a84457aed4c45bc900998b5e11c03023264208James Dong#define NUMBER 1
24829a84457aed4c45bc900998b5e11c03023264208James Dong#undef SHIFT
24929a84457aed4c45bc900998b5e11c03023264208James Dong#define SHIFT 8
25029a84457aed4c45bc900998b5e11c03023264208James Dong#undef INC_X8
25129a84457aed4c45bc900998b5e11c03023264208James Dong#define INC_X8 0x08000001
25229a84457aed4c45bc900998b5e11c03023264208James Dong#include "sad_mb_offset.h"
25329a84457aed4c45bc900998b5e11c03023264208James Dong
25429a84457aed4c45bc900998b5e11c03023264208James Dong
25529a84457aed4c45bc900998b5e11c03023264208James Dong    __inline int32 simd_sad_mb(uint8 *ref, uint8 *blk, int dmin, int lx)
25629a84457aed4c45bc900998b5e11c03023264208James Dong    {
25729a84457aed4c45bc900998b5e11c03023264208James Dong        int32 x4, x5, x6, x8, x9, x10, x11, x12, x14;
25829a84457aed4c45bc900998b5e11c03023264208James Dong
25929a84457aed4c45bc900998b5e11c03023264208James Dong        x9 = 0x80808080; /* const. */
26029a84457aed4c45bc900998b5e11c03023264208James Dong        x4 = x5 = 0;
26129a84457aed4c45bc900998b5e11c03023264208James Dong
26229a84457aed4c45bc900998b5e11c03023264208James Dong        __asm
26329a84457aed4c45bc900998b5e11c03023264208James Dong        {
26429a84457aed4c45bc900998b5e11c03023264208James Dong            MOVS    x8, ref, lsl #31 ;
26529a84457aed4c45bc900998b5e11c03023264208James Dong            BHI     SadMBOffset3;
26629a84457aed4c45bc900998b5e11c03023264208James Dong            BCS     SadMBOffset2;
26729a84457aed4c45bc900998b5e11c03023264208James Dong            BMI     SadMBOffset1;
26829a84457aed4c45bc900998b5e11c03023264208James Dong
26929a84457aed4c45bc900998b5e11c03023264208James Dong            MVN     x6, #0xFF00;
27029a84457aed4c45bc900998b5e11c03023264208James Dong        }
27129a84457aed4c45bc900998b5e11c03023264208James DongLOOP_SAD0:
27229a84457aed4c45bc900998b5e11c03023264208James Dong        /****** process 8 pixels ******/
27329a84457aed4c45bc900998b5e11c03023264208James Dong        x11 = *((int32*)(ref + 12));
27429a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = *((int32*)(ref + 8));
27529a84457aed4c45bc900998b5e11c03023264208James Dong        x14 = *((int32*)(blk + 12));
27629a84457aed4c45bc900998b5e11c03023264208James Dong        x12 = *((int32*)(blk + 8));
27729a84457aed4c45bc900998b5e11c03023264208James Dong
27829a84457aed4c45bc900998b5e11c03023264208James Dong        /* process x11 & x14 */
27929a84457aed4c45bc900998b5e11c03023264208James Dong        x11 = sad_4pixel(x11, x14, x9);
28029a84457aed4c45bc900998b5e11c03023264208James Dong
28129a84457aed4c45bc900998b5e11c03023264208James Dong        /* process x12 & x10 */
28229a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = sad_4pixel(x10, x12, x9);
28329a84457aed4c45bc900998b5e11c03023264208James Dong
28429a84457aed4c45bc900998b5e11c03023264208James Dong        x5 = x5 + x10;  /* accumulate low bytes */
28529a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = x10 & (x6 << 8); /* x10 & 0xFF00FF00 */
28629a84457aed4c45bc900998b5e11c03023264208James Dong        x4 = x4 + ((uint32)x10 >> 8); /* accumulate high bytes */
28729a84457aed4c45bc900998b5e11c03023264208James Dong        x5 = x5 + x11;  /* accumulate low bytes */
28829a84457aed4c45bc900998b5e11c03023264208James Dong        x11 = x11 & (x6 << 8); /* x11 & 0xFF00FF00 */
28929a84457aed4c45bc900998b5e11c03023264208James Dong        x4 = x4 + ((uint32)x11 >> 8);  /* accumulate high bytes */
29029a84457aed4c45bc900998b5e11c03023264208James Dong
29129a84457aed4c45bc900998b5e11c03023264208James Dong        __asm
29229a84457aed4c45bc900998b5e11c03023264208James Dong        {
29329a84457aed4c45bc900998b5e11c03023264208James Dong            /****** process 8 pixels ******/
29429a84457aed4c45bc900998b5e11c03023264208James Dong            LDR     x11, [ref, #4];
29529a84457aed4c45bc900998b5e11c03023264208James Dong            LDR     x10, [ref], lx ;
29629a84457aed4c45bc900998b5e11c03023264208James Dong            LDR     x14, [blk, #4];
29729a84457aed4c45bc900998b5e11c03023264208James Dong            LDR     x12, [blk], #16 ;
29829a84457aed4c45bc900998b5e11c03023264208James Dong        }
29929a84457aed4c45bc900998b5e11c03023264208James Dong
30029a84457aed4c45bc900998b5e11c03023264208James Dong        /* process x11 & x14 */
30129a84457aed4c45bc900998b5e11c03023264208James Dong        x11 = sad_4pixel(x11, x14, x9);
30229a84457aed4c45bc900998b5e11c03023264208James Dong
30329a84457aed4c45bc900998b5e11c03023264208James Dong        /* process x12 & x10 */
30429a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = sad_4pixel(x10, x12, x9);
30529a84457aed4c45bc900998b5e11c03023264208James Dong
30629a84457aed4c45bc900998b5e11c03023264208James Dong        x5 = x5 + x10;  /* accumulate low bytes */
30729a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = x10 & (x6 << 8); /* x10 & 0xFF00FF00 */
30829a84457aed4c45bc900998b5e11c03023264208James Dong        x4 = x4 + ((uint32)x10 >> 8); /* accumulate high bytes */
30929a84457aed4c45bc900998b5e11c03023264208James Dong        x5 = x5 + x11;  /* accumulate low bytes */
31029a84457aed4c45bc900998b5e11c03023264208James Dong        x11 = x11 & (x6 << 8); /* x11 & 0xFF00FF00 */
31129a84457aed4c45bc900998b5e11c03023264208James Dong        x4 = x4 + ((uint32)x11 >> 8);  /* accumulate high bytes */
31229a84457aed4c45bc900998b5e11c03023264208James Dong
31329a84457aed4c45bc900998b5e11c03023264208James Dong        /****************/
31429a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = x5 - (x4 << 8); /* extract low bytes */
31529a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = x10 + x4;     /* add with high bytes */
31629a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = x10 + (x10 << 16); /* add with lower half word */
31729a84457aed4c45bc900998b5e11c03023264208James Dong
31829a84457aed4c45bc900998b5e11c03023264208James Dong        __asm
31929a84457aed4c45bc900998b5e11c03023264208James Dong        {
32029a84457aed4c45bc900998b5e11c03023264208James Dong            /****************/
32129a84457aed4c45bc900998b5e11c03023264208James Dong            RSBS    x11, dmin, x10, lsr #16;
32229a84457aed4c45bc900998b5e11c03023264208James Dong            ADDLSS  x8, x8, #0x10000001;
32329a84457aed4c45bc900998b5e11c03023264208James Dong            BLS     LOOP_SAD0;
32429a84457aed4c45bc900998b5e11c03023264208James Dong        }
32529a84457aed4c45bc900998b5e11c03023264208James Dong
32629a84457aed4c45bc900998b5e11c03023264208James Dong        return ((uint32)x10 >> 16);
32729a84457aed4c45bc900998b5e11c03023264208James Dong
32829a84457aed4c45bc900998b5e11c03023264208James DongSadMBOffset3:
32929a84457aed4c45bc900998b5e11c03023264208James Dong
33029a84457aed4c45bc900998b5e11c03023264208James Dong        return sad_mb_offset3(ref, blk, lx, dmin, x8);
33129a84457aed4c45bc900998b5e11c03023264208James Dong
33229a84457aed4c45bc900998b5e11c03023264208James DongSadMBOffset2:
33329a84457aed4c45bc900998b5e11c03023264208James Dong
33429a84457aed4c45bc900998b5e11c03023264208James Dong        return sad_mb_offset2(ref, blk, lx, dmin, x8);
33529a84457aed4c45bc900998b5e11c03023264208James Dong
33629a84457aed4c45bc900998b5e11c03023264208James DongSadMBOffset1:
33729a84457aed4c45bc900998b5e11c03023264208James Dong
33829a84457aed4c45bc900998b5e11c03023264208James Dong        return sad_mb_offset1(ref, blk, lx, dmin, x8);
33929a84457aed4c45bc900998b5e11c03023264208James Dong    }
34029a84457aed4c45bc900998b5e11c03023264208James Dong
34129a84457aed4c45bc900998b5e11c03023264208James Dong
34229a84457aed4c45bc900998b5e11c03023264208James Dong#elif defined(__GNUC__) && defined(__arm__) /* ARM GNU COMPILER  */
34329a84457aed4c45bc900998b5e11c03023264208James Dong
34429a84457aed4c45bc900998b5e11c03023264208James Dong    __inline int32 SUB_SAD(int32 sad, int32 tmp, int32 tmp2)
34529a84457aed4c45bc900998b5e11c03023264208James Dong    {
34629a84457aed4c45bc900998b5e11c03023264208James Dong__asm__ volatile("rsbs	%1, %1, %2\n\trsbmi %1, %1, #0\n\tadd	%0, %0, %1": "=r"(sad): "r"(tmp), "r"(tmp2));
34729a84457aed4c45bc900998b5e11c03023264208James Dong        return sad;
34829a84457aed4c45bc900998b5e11c03023264208James Dong    }
34929a84457aed4c45bc900998b5e11c03023264208James Dong
35029a84457aed4c45bc900998b5e11c03023264208James Dong    __inline int32 sad_4pixel(int32 src1, int32 src2, int32 mask)
35129a84457aed4c45bc900998b5e11c03023264208James Dong    {
35229a84457aed4c45bc900998b5e11c03023264208James Dong        int32 x7;
35329a84457aed4c45bc900998b5e11c03023264208James Dong
35429a84457aed4c45bc900998b5e11c03023264208James Dong__asm__ volatile("EOR	%1, %2, %0\n\tSUBS  %0, %2, %0\n\tEOR	%1, %1, %0\n\tAND  %1, %3, %1, lsr #1\n\tORRCC	%1, %1, #0x80000000\n\tRSB  %1, %1, %1, lsl #8\n\tADD  %0, %0, %1, asr #7\n\tEOR  %0, %0, %1, asr #7": "=r"(src1), "=&r"(x7): "r"(src2), "r"(mask));
35529a84457aed4c45bc900998b5e11c03023264208James Dong
35629a84457aed4c45bc900998b5e11c03023264208James Dong        return src1;
35729a84457aed4c45bc900998b5e11c03023264208James Dong    }
35829a84457aed4c45bc900998b5e11c03023264208James Dong
35929a84457aed4c45bc900998b5e11c03023264208James Dong    __inline int32 sad_4pixelN(int32 src1, int32 src2, int32 mask)
36029a84457aed4c45bc900998b5e11c03023264208James Dong    {
36129a84457aed4c45bc900998b5e11c03023264208James Dong        int32 x7;
36229a84457aed4c45bc900998b5e11c03023264208James Dong
36329a84457aed4c45bc900998b5e11c03023264208James Dong__asm__ volatile("EOR	%1, %2, %0\n\tADDS  %0, %2, %0\n\tEOR  %1, %1, %0\n\tANDS  %1, %3, %1, rrx\n\tRSB  %1, %1, %1, lsl #8\n\tSUB	%0, %0, %1, asr #7\n\tEOR   %0, %0, %1, asr #7": "=r"(src1), "=&r"(x7): "r"(src2), "r"(mask));
36429a84457aed4c45bc900998b5e11c03023264208James Dong
36529a84457aed4c45bc900998b5e11c03023264208James Dong        return src1;
36629a84457aed4c45bc900998b5e11c03023264208James Dong    }
36729a84457aed4c45bc900998b5e11c03023264208James Dong
36829a84457aed4c45bc900998b5e11c03023264208James Dong#define sum_accumulate  __asm__ volatile("SBC  %0, %0, %1\n\tBIC   %1, %4, %1\n\tADD   %2, %2, %1, lsr #8\n\tSBC   %0, %0, %3\n\tBIC   %3, %4, %3\n\tADD   %2, %2, %3, lsr #8": "=&r" (x5), "=&r" (x10), "=&r" (x4), "=&r" (x11): "r" (x6));
36929a84457aed4c45bc900998b5e11c03023264208James Dong
37029a84457aed4c45bc900998b5e11c03023264208James Dong#define NUMBER 3
37129a84457aed4c45bc900998b5e11c03023264208James Dong#define SHIFT 24
37229a84457aed4c45bc900998b5e11c03023264208James Dong#define INC_X8 0x08000001
37329a84457aed4c45bc900998b5e11c03023264208James Dong
37429a84457aed4c45bc900998b5e11c03023264208James Dong#include "sad_mb_offset.h"
37529a84457aed4c45bc900998b5e11c03023264208James Dong
37629a84457aed4c45bc900998b5e11c03023264208James Dong#undef NUMBER
37729a84457aed4c45bc900998b5e11c03023264208James Dong#define NUMBER 2
37829a84457aed4c45bc900998b5e11c03023264208James Dong#undef SHIFT
37929a84457aed4c45bc900998b5e11c03023264208James Dong#define SHIFT 16
38029a84457aed4c45bc900998b5e11c03023264208James Dong#undef INC_X8
38129a84457aed4c45bc900998b5e11c03023264208James Dong#define INC_X8 0x10000001
38229a84457aed4c45bc900998b5e11c03023264208James Dong#include "sad_mb_offset.h"
38329a84457aed4c45bc900998b5e11c03023264208James Dong
38429a84457aed4c45bc900998b5e11c03023264208James Dong#undef NUMBER
38529a84457aed4c45bc900998b5e11c03023264208James Dong#define NUMBER 1
38629a84457aed4c45bc900998b5e11c03023264208James Dong#undef SHIFT
38729a84457aed4c45bc900998b5e11c03023264208James Dong#define SHIFT 8
38829a84457aed4c45bc900998b5e11c03023264208James Dong#undef INC_X8
38929a84457aed4c45bc900998b5e11c03023264208James Dong#define INC_X8 0x08000001
39029a84457aed4c45bc900998b5e11c03023264208James Dong#include "sad_mb_offset.h"
39129a84457aed4c45bc900998b5e11c03023264208James Dong
39229a84457aed4c45bc900998b5e11c03023264208James Dong
39329a84457aed4c45bc900998b5e11c03023264208James Dong    __inline int32 simd_sad_mb(uint8 *ref, uint8 *blk, int dmin, int lx)
39429a84457aed4c45bc900998b5e11c03023264208James Dong    {
39529a84457aed4c45bc900998b5e11c03023264208James Dong        int32 x4, x5, x6, x8, x9, x10, x11, x12, x14;
39629a84457aed4c45bc900998b5e11c03023264208James Dong
39729a84457aed4c45bc900998b5e11c03023264208James Dong        x9 = 0x80808080; /* const. */
39829a84457aed4c45bc900998b5e11c03023264208James Dong        x4 = x5 = 0;
39929a84457aed4c45bc900998b5e11c03023264208James Dong
40029a84457aed4c45bc900998b5e11c03023264208James Dong        x8 = (uint32)ref & 0x3;
40129a84457aed4c45bc900998b5e11c03023264208James Dong        if (x8 == 3)
40229a84457aed4c45bc900998b5e11c03023264208James Dong            goto SadMBOffset3;
40329a84457aed4c45bc900998b5e11c03023264208James Dong        if (x8 == 2)
40429a84457aed4c45bc900998b5e11c03023264208James Dong            goto SadMBOffset2;
40529a84457aed4c45bc900998b5e11c03023264208James Dong        if (x8 == 1)
40629a84457aed4c45bc900998b5e11c03023264208James Dong            goto SadMBOffset1;
40729a84457aed4c45bc900998b5e11c03023264208James Dong
40829a84457aed4c45bc900998b5e11c03023264208James Dong        x8 = 16;
40929a84457aed4c45bc900998b5e11c03023264208James Dong///
41029a84457aed4c45bc900998b5e11c03023264208James Dong__asm__ volatile("MVN	%0, #0xFF00": "=r"(x6));
41129a84457aed4c45bc900998b5e11c03023264208James Dong
41229a84457aed4c45bc900998b5e11c03023264208James DongLOOP_SAD0:
41329a84457aed4c45bc900998b5e11c03023264208James Dong        /****** process 8 pixels ******/
41429a84457aed4c45bc900998b5e11c03023264208James Dong        x11 = *((int32*)(ref + 12));
41529a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = *((int32*)(ref + 8));
41629a84457aed4c45bc900998b5e11c03023264208James Dong        x14 = *((int32*)(blk + 12));
41729a84457aed4c45bc900998b5e11c03023264208James Dong        x12 = *((int32*)(blk + 8));
41829a84457aed4c45bc900998b5e11c03023264208James Dong
41929a84457aed4c45bc900998b5e11c03023264208James Dong        /* process x11 & x14 */
42029a84457aed4c45bc900998b5e11c03023264208James Dong        x11 = sad_4pixel(x11, x14, x9);
42129a84457aed4c45bc900998b5e11c03023264208James Dong
42229a84457aed4c45bc900998b5e11c03023264208James Dong        /* process x12 & x10 */
42329a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = sad_4pixel(x10, x12, x9);
42429a84457aed4c45bc900998b5e11c03023264208James Dong
42529a84457aed4c45bc900998b5e11c03023264208James Dong        x5 = x5 + x10;  /* accumulate low bytes */
42629a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = x10 & (x6 << 8); /* x10 & 0xFF00FF00 */
42729a84457aed4c45bc900998b5e11c03023264208James Dong        x4 = x4 + ((uint32)x10 >> 8); /* accumulate high bytes */
42829a84457aed4c45bc900998b5e11c03023264208James Dong        x5 = x5 + x11;  /* accumulate low bytes */
42929a84457aed4c45bc900998b5e11c03023264208James Dong        x11 = x11 & (x6 << 8); /* x11 & 0xFF00FF00 */
43029a84457aed4c45bc900998b5e11c03023264208James Dong        x4 = x4 + ((uint32)x11 >> 8);  /* accumulate high bytes */
43129a84457aed4c45bc900998b5e11c03023264208James Dong
43229a84457aed4c45bc900998b5e11c03023264208James Dong        /****** process 8 pixels ******/
43329a84457aed4c45bc900998b5e11c03023264208James Dong        x11 = *((int32*)(ref + 4));
43429a84457aed4c45bc900998b5e11c03023264208James Dong__asm__ volatile("LDR	%0, [%1], %2": "=&r"(x10), "=r"(ref): "r"(lx));
43529a84457aed4c45bc900998b5e11c03023264208James Dong        //x10 = *((int32*)ref); ref+=lx;
43629a84457aed4c45bc900998b5e11c03023264208James Dong        x14 = *((int32*)(blk + 4));
43729a84457aed4c45bc900998b5e11c03023264208James Dong__asm__ volatile("LDR	%0, [%1], #16": "=&r"(x12), "=r"(blk));
43829a84457aed4c45bc900998b5e11c03023264208James Dong
43929a84457aed4c45bc900998b5e11c03023264208James Dong        /* process x11 & x14 */
44029a84457aed4c45bc900998b5e11c03023264208James Dong        x11 = sad_4pixel(x11, x14, x9);
44129a84457aed4c45bc900998b5e11c03023264208James Dong
44229a84457aed4c45bc900998b5e11c03023264208James Dong        /* process x12 & x10 */
44329a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = sad_4pixel(x10, x12, x9);
44429a84457aed4c45bc900998b5e11c03023264208James Dong
44529a84457aed4c45bc900998b5e11c03023264208James Dong        x5 = x5 + x10;  /* accumulate low bytes */
44629a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = x10 & (x6 << 8); /* x10 & 0xFF00FF00 */
44729a84457aed4c45bc900998b5e11c03023264208James Dong        x4 = x4 + ((uint32)x10 >> 8); /* accumulate high bytes */
44829a84457aed4c45bc900998b5e11c03023264208James Dong        x5 = x5 + x11;  /* accumulate low bytes */
44929a84457aed4c45bc900998b5e11c03023264208James Dong        x11 = x11 & (x6 << 8); /* x11 & 0xFF00FF00 */
45029a84457aed4c45bc900998b5e11c03023264208James Dong        x4 = x4 + ((uint32)x11 >> 8);  /* accumulate high bytes */
45129a84457aed4c45bc900998b5e11c03023264208James Dong
45229a84457aed4c45bc900998b5e11c03023264208James Dong        /****************/
45329a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = x5 - (x4 << 8); /* extract low bytes */
45429a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = x10 + x4;     /* add with high bytes */
45529a84457aed4c45bc900998b5e11c03023264208James Dong        x10 = x10 + (x10 << 16); /* add with lower half word */
45629a84457aed4c45bc900998b5e11c03023264208James Dong
45729a84457aed4c45bc900998b5e11c03023264208James Dong        /****************/
45829a84457aed4c45bc900998b5e11c03023264208James Dong
45929a84457aed4c45bc900998b5e11c03023264208James Dong        if (((uint32)x10 >> 16) <= dmin) /* compare with dmin */
46029a84457aed4c45bc900998b5e11c03023264208James Dong        {
46129a84457aed4c45bc900998b5e11c03023264208James Dong            if (--x8)
46229a84457aed4c45bc900998b5e11c03023264208James Dong            {
46329a84457aed4c45bc900998b5e11c03023264208James Dong                goto LOOP_SAD0;
46429a84457aed4c45bc900998b5e11c03023264208James Dong            }
46529a84457aed4c45bc900998b5e11c03023264208James Dong
46629a84457aed4c45bc900998b5e11c03023264208James Dong        }
46729a84457aed4c45bc900998b5e11c03023264208James Dong
46829a84457aed4c45bc900998b5e11c03023264208James Dong        return ((uint32)x10 >> 16);
46929a84457aed4c45bc900998b5e11c03023264208James Dong
47029a84457aed4c45bc900998b5e11c03023264208James DongSadMBOffset3:
47129a84457aed4c45bc900998b5e11c03023264208James Dong
47229a84457aed4c45bc900998b5e11c03023264208James Dong        return sad_mb_offset3(ref, blk, lx, dmin);
47329a84457aed4c45bc900998b5e11c03023264208James Dong
47429a84457aed4c45bc900998b5e11c03023264208James DongSadMBOffset2:
47529a84457aed4c45bc900998b5e11c03023264208James Dong
47629a84457aed4c45bc900998b5e11c03023264208James Dong        return sad_mb_offset2(ref, blk, lx, dmin);
47729a84457aed4c45bc900998b5e11c03023264208James Dong
47829a84457aed4c45bc900998b5e11c03023264208James DongSadMBOffset1:
47929a84457aed4c45bc900998b5e11c03023264208James Dong
48029a84457aed4c45bc900998b5e11c03023264208James Dong        return sad_mb_offset1(ref, blk, lx, dmin);
48129a84457aed4c45bc900998b5e11c03023264208James Dong    }
48229a84457aed4c45bc900998b5e11c03023264208James Dong
48329a84457aed4c45bc900998b5e11c03023264208James Dong
48429a84457aed4c45bc900998b5e11c03023264208James Dong#endif
48529a84457aed4c45bc900998b5e11c03023264208James Dong
48629a84457aed4c45bc900998b5e11c03023264208James Dong#ifdef __cplusplus
48729a84457aed4c45bc900998b5e11c03023264208James Dong}
48829a84457aed4c45bc900998b5e11c03023264208James Dong#endif
48929a84457aed4c45bc900998b5e11c03023264208James Dong
49029a84457aed4c45bc900998b5e11c03023264208James Dong#endif // _SAD_INLINE_H_
49129a84457aed4c45bc900998b5e11c03023264208James Dong
492