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