summaryrefslogblamecommitdiffstats
path: root/target/arm/iwmmxt_helper.c
blob: 610b1b2103eb8d0b85d488c952dce434b42510fb (plain) (tree)
1
2
3
4
5
6
7
8
9
10









                                                                
                                                                     






                                                                    
                                                                               

   
                       

                
                              



                                                                   



                                                                      
                                  



                          
                                 







                                                     
                             




                                                  











































































































                                                                            
                                                                         
                                                                    
                                                                          










                                                                         
                                                                 
                                                                 
                                                                    
                                                                 








                                                                         
                                                                 
                                                                 
                                                                    
                                                                 





                                                                         
                                                                 
                                                                 
                                                                     
                                                                 








                                                                         
                                                                 
                                                                 
                                                                     
                                                                 





                                                                         
                                                                 
                                                                 
                                                                     
                                                                 


                                                                 
                                                                 
                                                                 
                                                                     
                                                                 








                                                                         
                                                                 
                                                                 
                                                                     
                                                                 





                                                                         
                                                                 
                                                                 
                                                                     
                                                                 


                                                                 




                                                                 
                                                                 
                                                                    
                                                                 










                                                                         
                                                                 
                                                                 
                                                                    
                                                                 





                                                                 
                                                                 
                                                                 
                                                                    
                                                                 




                                                                 
































                                                                             
                                                                              






















                                                                            
                                                                            














                                                                         





















































































                                                                            
                                                                      










                                                            
                                                                      







                                                 
                                                                      





                                                     
                                                                      










                                                            
                                                                      







                                                 
                                                                      





                                                     
                                                                      










                                                                
                                                                      







                                                        
                                                                      





                                                     
                                                                      














                                                                     
                                                                      









                                                 
                                                                      
 
                    



                                                     
                                                                       











                                                     
                                                                        












                                                                 
                                                                        








                                                                   
                                                                        







                                                    
                                                                        












                                                                 
                                                                        








                                                                   
                                                                        

























                                                                     
/*
 * iwMMXt micro operations for XScale.
 *
 * Copyright (c) 2007 OpenedHand, Ltd.
 * Written by Andrzej Zaborowski <andrew@openedhand.com>
 * Copyright (c) 2008 CodeSourcery
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * 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
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include "qemu/osdep.h"

#include "cpu.h"
#include "exec/helper-proto.h"

/* iwMMXt macros extracted from GNU gdb.  */

/* Set the SIMD wCASF flags for 8, 16, 32 or 64-bit operations.  */
#define SIMD8_SET(v, n, b)      ((v != 0) << ((((b) + 1) * 4) + (n)))
#define SIMD16_SET(v, n, h)     ((v != 0) << ((((h) + 1) * 8) + (n)))
#define SIMD32_SET(v, n, w)     ((v != 0) << ((((w) + 1) * 16) + (n)))
#define SIMD64_SET(v, n)        ((v != 0) << (32 + (n)))
/* Flags to pass as "n" above.  */
#define SIMD_NBIT       -1
#define SIMD_ZBIT       -2
#define SIMD_CBIT       -3
#define SIMD_VBIT       -4
/* Various status bit macros.  */
#define NBIT8(x)        ((x) & 0x80)
#define NBIT16(x)       ((x) & 0x8000)
#define NBIT32(x)       ((x) & 0x80000000)
#define NBIT64(x)       ((x) & 0x8000000000000000ULL)
#define ZBIT8(x)        (((x) & 0xff) == 0)
#define ZBIT16(x)       (((x) & 0xffff) == 0)
#define ZBIT32(x)       (((x) & 0xffffffff) == 0)
#define ZBIT64(x)       (x == 0)
/* Sign extension macros.  */
#define EXTEND8H(a)     ((uint16_t) (int8_t) (a))
#define EXTEND8(a)      ((uint32_t) (int8_t) (a))
#define EXTEND16(a)     ((uint32_t) (int16_t) (a))
#define EXTEND16S(a)    ((int32_t) (int16_t) (a))
#define EXTEND32(a)     ((uint64_t) (int32_t) (a))

uint64_t HELPER(iwmmxt_maddsq)(uint64_t a, uint64_t b)
{
    a = ((
            EXTEND16S((a >> 0) & 0xffff) * EXTEND16S((b >> 0) & 0xffff) +
            EXTEND16S((a >> 16) & 0xffff) * EXTEND16S((b >> 16) & 0xffff)
        ) & 0xffffffff) | ((uint64_t) (
            EXTEND16S((a >> 32) & 0xffff) * EXTEND16S((b >> 32) & 0xffff) +
            EXTEND16S((a >> 48) & 0xffff) * EXTEND16S((b >> 48) & 0xffff)
        ) << 32);
    return a;
}

uint64_t HELPER(iwmmxt_madduq)(uint64_t a, uint64_t b)
{
    a = ((
            ((a >> 0) & 0xffff) * ((b >> 0) & 0xffff) +
            ((a >> 16) & 0xffff) * ((b >> 16) & 0xffff)
        ) & 0xffffffff) | ((
            ((a >> 32) & 0xffff) * ((b >> 32) & 0xffff) +
            ((a >> 48) & 0xffff) * ((b >> 48) & 0xffff)
        ) << 32);
    return a;
}

uint64_t HELPER(iwmmxt_sadb)(uint64_t a, uint64_t b)
{
#define abs(x) (((x) >= 0) ? x : -x)
#define SADB(SHR) abs((int) ((a >> SHR) & 0xff) - (int) ((b >> SHR) & 0xff))
    return
        SADB(0) + SADB(8) + SADB(16) + SADB(24) +
        SADB(32) + SADB(40) + SADB(48) + SADB(56);
#undef SADB
}

uint64_t HELPER(iwmmxt_sadw)(uint64_t a, uint64_t b)
{
#define SADW(SHR) \
    abs((int) ((a >> SHR) & 0xffff) - (int) ((b >> SHR) & 0xffff))
    return SADW(0) + SADW(16) + SADW(32) + SADW(48);
#undef SADW
}

uint64_t HELPER(iwmmxt_mulslw)(uint64_t a, uint64_t b)
{
#define MULS(SHR) ((uint64_t) ((( \
        EXTEND16S((a >> SHR) & 0xffff) * EXTEND16S((b >> SHR) & 0xffff) \
    ) >> 0) & 0xffff) << SHR)
    return MULS(0) | MULS(16) | MULS(32) | MULS(48);
#undef MULS
}

uint64_t HELPER(iwmmxt_mulshw)(uint64_t a, uint64_t b)
{
#define MULS(SHR) ((uint64_t) ((( \
        EXTEND16S((a >> SHR) & 0xffff) * EXTEND16S((b >> SHR) & 0xffff) \
    ) >> 16) & 0xffff) << SHR)
    return MULS(0) | MULS(16) | MULS(32) | MULS(48);
#undef MULS
}

uint64_t HELPER(iwmmxt_mululw)(uint64_t a, uint64_t b)
{
#define MULU(SHR) ((uint64_t) ((( \
        ((a >> SHR) & 0xffff) * ((b >> SHR) & 0xffff) \
    ) >> 0) & 0xffff) << SHR)
    return MULU(0) | MULU(16) | MULU(32) | MULU(48);
#undef MULU
}

uint64_t HELPER(iwmmxt_muluhw)(uint64_t a, uint64_t b)
{
#define MULU(SHR) ((uint64_t) ((( \
        ((a >> SHR) & 0xffff) * ((b >> SHR) & 0xffff) \
    ) >> 16) & 0xffff) << SHR)
    return MULU(0) | MULU(16) | MULU(32) | MULU(48);
#undef MULU
}

uint64_t HELPER(iwmmxt_macsw)(uint64_t a, uint64_t b)
{
#define MACS(SHR) ( \
        EXTEND16((a >> SHR) & 0xffff) * EXTEND16S((b >> SHR) & 0xffff))
    return (int64_t) (MACS(0) + MACS(16) + MACS(32) + MACS(48));
#undef MACS
}

uint64_t HELPER(iwmmxt_macuw)(uint64_t a, uint64_t b)
{
#define MACU(SHR) ( \
        (uint32_t) ((a >> SHR) & 0xffff) * \
        (uint32_t) ((b >> SHR) & 0xffff))
    return MACU(0) + MACU(16) + MACU(32) + MACU(48);
#undef MACU
}

#define NZBIT8(x, i) \
    SIMD8_SET(NBIT8((x) & 0xff), SIMD_NBIT, i) | \
    SIMD8_SET(ZBIT8((x) & 0xff), SIMD_ZBIT, i)
#define NZBIT16(x, i) \
    SIMD16_SET(NBIT16((x) & 0xffff), SIMD_NBIT, i) | \
    SIMD16_SET(ZBIT16((x) & 0xffff), SIMD_ZBIT, i)
#define NZBIT32(x, i) \
    SIMD32_SET(NBIT32((x) & 0xffffffff), SIMD_NBIT, i) | \
    SIMD32_SET(ZBIT32((x) & 0xffffffff), SIMD_ZBIT, i)
#define NZBIT64(x) \
    SIMD64_SET(NBIT64(x), SIMD_NBIT) | \
    SIMD64_SET(ZBIT64(x), SIMD_ZBIT)
#define IWMMXT_OP_UNPACK(S, SH0, SH1, SH2, SH3)                         \
uint64_t HELPER(glue(iwmmxt_unpack, glue(S, b)))(CPUARMState *env, \
                                                 uint64_t a, uint64_t b) \
{                                                               \
    a =                                                                 \
        (((a >> SH0) & 0xff) << 0) | (((b >> SH0) & 0xff) << 8) |       \
        (((a >> SH1) & 0xff) << 16) | (((b >> SH1) & 0xff) << 24) |     \
        (((a >> SH2) & 0xff) << 32) | (((b >> SH2) & 0xff) << 40) |     \
        (((a >> SH3) & 0xff) << 48) | (((b >> SH3) & 0xff) << 56);      \
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =                       \
        NZBIT8(a >> 0, 0) | NZBIT8(a >> 8, 1) |                         \
        NZBIT8(a >> 16, 2) | NZBIT8(a >> 24, 3) |               \
        NZBIT8(a >> 32, 4) | NZBIT8(a >> 40, 5) |               \
        NZBIT8(a >> 48, 6) | NZBIT8(a >> 56, 7);                \
    return a;                                                   \
}                                                               \
uint64_t HELPER(glue(iwmmxt_unpack, glue(S, w)))(CPUARMState *env, \
                                        uint64_t a, uint64_t b) \
{                                                               \
    a =                                                                 \
        (((a >> SH0) & 0xffff) << 0) |                          \
        (((b >> SH0) & 0xffff) << 16) |                                 \
        (((a >> SH2) & 0xffff) << 32) |                                 \
        (((b >> SH2) & 0xffff) << 48);                          \
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =                       \
        NZBIT8(a >> 0, 0) | NZBIT8(a >> 16, 1) |                \
        NZBIT8(a >> 32, 2) | NZBIT8(a >> 48, 3);                \
    return a;                                                   \
}                                                               \
uint64_t HELPER(glue(iwmmxt_unpack, glue(S, l)))(CPUARMState *env, \
                                        uint64_t a, uint64_t b) \
{                                                               \
    a =                                                                 \
        (((a >> SH0) & 0xffffffff) << 0) |                      \
        (((b >> SH0) & 0xffffffff) << 32);                      \
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =                       \
        NZBIT32(a >> 0, 0) | NZBIT32(a >> 32, 1);               \
    return a;                                                   \
}                                                               \
uint64_t HELPER(glue(iwmmxt_unpack, glue(S, ub)))(CPUARMState *env, \
                                                  uint64_t x)   \
{                                                               \
    x =                                                                 \
        (((x >> SH0) & 0xff) << 0) |                            \
        (((x >> SH1) & 0xff) << 16) |                           \
        (((x >> SH2) & 0xff) << 32) |                           \
        (((x >> SH3) & 0xff) << 48);                            \
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =                       \
        NZBIT16(x >> 0, 0) | NZBIT16(x >> 16, 1) |              \
        NZBIT16(x >> 32, 2) | NZBIT16(x >> 48, 3);              \
    return x;                                                   \
}                                                               \
uint64_t HELPER(glue(iwmmxt_unpack, glue(S, uw)))(CPUARMState *env, \
                                                  uint64_t x)   \
{                                                               \
    x =                                                                 \
        (((x >> SH0) & 0xffff) << 0) |                          \
        (((x >> SH2) & 0xffff) << 32);                          \
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =                       \
        NZBIT32(x >> 0, 0) | NZBIT32(x >> 32, 1);               \
    return x;                                                   \
}                                                               \
uint64_t HELPER(glue(iwmmxt_unpack, glue(S, ul)))(CPUARMState *env, \
                                                  uint64_t x)   \
{                                                               \
    x = (((x >> SH0) & 0xffffffff) << 0);                       \
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] = NZBIT64(x >> 0);      \
    return x;                                                   \
}                                                               \
uint64_t HELPER(glue(iwmmxt_unpack, glue(S, sb)))(CPUARMState *env, \
                                                  uint64_t x)   \
{                                                               \
    x =                                                                 \
        ((uint64_t) EXTEND8H((x >> SH0) & 0xff) << 0) |                 \
        ((uint64_t) EXTEND8H((x >> SH1) & 0xff) << 16) |        \
        ((uint64_t) EXTEND8H((x >> SH2) & 0xff) << 32) |        \
        ((uint64_t) EXTEND8H((x >> SH3) & 0xff) << 48);                 \
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =                       \
        NZBIT16(x >> 0, 0) | NZBIT16(x >> 16, 1) |              \
        NZBIT16(x >> 32, 2) | NZBIT16(x >> 48, 3);              \
    return x;                                                   \
}                                                               \
uint64_t HELPER(glue(iwmmxt_unpack, glue(S, sw)))(CPUARMState *env, \
                                                  uint64_t x)   \
{                                                               \
    x =                                                                 \
        ((uint64_t) EXTEND16((x >> SH0) & 0xffff) << 0) |       \
        ((uint64_t) EXTEND16((x >> SH2) & 0xffff) << 32);       \
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =                       \
        NZBIT32(x >> 0, 0) | NZBIT32(x >> 32, 1);               \
    return x;                                                   \
}                                                               \
uint64_t HELPER(glue(iwmmxt_unpack, glue(S, sl)))(CPUARMState *env, \
                                                  uint64_t x)   \
{                                                               \
    x = EXTEND32((x >> SH0) & 0xffffffff);                      \
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] = NZBIT64(x >> 0);      \
    return x;                                                   \
}
IWMMXT_OP_UNPACK(l, 0, 8, 16, 24)
IWMMXT_OP_UNPACK(h, 32, 40, 48, 56)

#define IWMMXT_OP_CMP(SUFF, Tb, Tw, Tl, O)                      \
uint64_t HELPER(glue(iwmmxt_, glue(SUFF, b)))(CPUARMState *env,    \
                                        uint64_t a, uint64_t b) \
{                                                               \
    a =                                                                 \
        CMP(0, Tb, O, 0xff) | CMP(8, Tb, O, 0xff) |             \
        CMP(16, Tb, O, 0xff) | CMP(24, Tb, O, 0xff) |           \
        CMP(32, Tb, O, 0xff) | CMP(40, Tb, O, 0xff) |           \
        CMP(48, Tb, O, 0xff) | CMP(56, Tb, O, 0xff);            \
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =                       \
        NZBIT8(a >> 0, 0) | NZBIT8(a >> 8, 1) |                         \
        NZBIT8(a >> 16, 2) | NZBIT8(a >> 24, 3) |               \
        NZBIT8(a >> 32, 4) | NZBIT8(a >> 40, 5) |               \
        NZBIT8(a >> 48, 6) | NZBIT8(a >> 56, 7);                \
    return a;                                                   \
}                                                               \
uint64_t HELPER(glue(iwmmxt_, glue(SUFF, w)))(CPUARMState *env,    \
                                        uint64_t a, uint64_t b) \
{                                                               \
    a = CMP(0, Tw, O, 0xffff) | CMP(16, Tw, O, 0xffff) |        \
        CMP(32, Tw, O, 0xffff) | CMP(48, Tw, O, 0xffff);        \
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =                       \
        NZBIT16(a >> 0, 0) | NZBIT16(a >> 16, 1) |              \
        NZBIT16(a >> 32, 2) | NZBIT16(a >> 48, 3);              \
    return a;                                                   \
}                                                               \
uint64_t HELPER(glue(iwmmxt_, glue(SUFF, l)))(CPUARMState *env,    \
                                        uint64_t a, uint64_t b) \
{                                                               \
    a = CMP(0, Tl, O, 0xffffffff) |                             \
        CMP(32, Tl, O, 0xffffffff);                             \
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =                       \
        NZBIT32(a >> 0, 0) | NZBIT32(a >> 32, 1);               \
    return a;                                                   \
}
#define CMP(SHR, TYPE, OPER, MASK) ((((TYPE) ((a >> SHR) & MASK) OPER \
            (TYPE) ((b >> SHR) & MASK)) ? (uint64_t) MASK : 0) << SHR)
IWMMXT_OP_CMP(cmpeq, uint8_t, uint16_t, uint32_t, ==)
IWMMXT_OP_CMP(cmpgts, int8_t, int16_t, int32_t, >)
IWMMXT_OP_CMP(cmpgtu, uint8_t, uint16_t, uint32_t, >)
#undef CMP
#define CMP(SHR, TYPE, OPER, MASK) ((((TYPE) ((a >> SHR) & MASK) OPER \
            (TYPE) ((b >> SHR) & MASK)) ? a : b) & ((uint64_t) MASK << SHR))
IWMMXT_OP_CMP(mins, int8_t, int16_t, int32_t, <)
IWMMXT_OP_CMP(minu, uint8_t, uint16_t, uint32_t, <)
IWMMXT_OP_CMP(maxs, int8_t, int16_t, int32_t, >)
IWMMXT_OP_CMP(maxu, uint8_t, uint16_t, uint32_t, >)
#undef CMP
#define CMP(SHR, TYPE, OPER, MASK) ((uint64_t) (((TYPE) ((a >> SHR) & MASK) \
            OPER (TYPE) ((b >> SHR) & MASK)) & MASK) << SHR)
IWMMXT_OP_CMP(subn, uint8_t, uint16_t, uint32_t, -)
IWMMXT_OP_CMP(addn, uint8_t, uint16_t, uint32_t, +)
#undef CMP
/* TODO Signed- and Unsigned-Saturation */
#define CMP(SHR, TYPE, OPER, MASK) ((uint64_t) (((TYPE) ((a >> SHR) & MASK) \
            OPER (TYPE) ((b >> SHR) & MASK)) & MASK) << SHR)
IWMMXT_OP_CMP(subu, uint8_t, uint16_t, uint32_t, -)
IWMMXT_OP_CMP(addu, uint8_t, uint16_t, uint32_t, +)
IWMMXT_OP_CMP(subs, int8_t, int16_t, int32_t, -)
IWMMXT_OP_CMP(adds, int8_t, int16_t, int32_t, +)
#undef CMP
#undef IWMMXT_OP_CMP

#define AVGB(SHR) ((( \
        ((a >> SHR) & 0xff) + ((b >> SHR) & 0xff) + round) >> 1) << SHR)
#define IWMMXT_OP_AVGB(r)                                                 \
uint64_t HELPER(iwmmxt_avgb##r)(CPUARMState *env, uint64_t a, uint64_t b)    \
{                                                                         \
    const int round = r;                                                  \
    a = AVGB(0) | AVGB(8) | AVGB(16) | AVGB(24) |                         \
        AVGB(32) | AVGB(40) | AVGB(48) | AVGB(56);                        \
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =                                 \
        SIMD8_SET(ZBIT8((a >> 0) & 0xff), SIMD_ZBIT, 0) |                 \
        SIMD8_SET(ZBIT8((a >> 8) & 0xff), SIMD_ZBIT, 1) |                 \
        SIMD8_SET(ZBIT8((a >> 16) & 0xff), SIMD_ZBIT, 2) |                \
        SIMD8_SET(ZBIT8((a >> 24) & 0xff), SIMD_ZBIT, 3) |                \
        SIMD8_SET(ZBIT8((a >> 32) & 0xff), SIMD_ZBIT, 4) |                \
        SIMD8_SET(ZBIT8((a >> 40) & 0xff), SIMD_ZBIT, 5) |                \
        SIMD8_SET(ZBIT8((a >> 48) & 0xff), SIMD_ZBIT, 6) |                \
        SIMD8_SET(ZBIT8((a >> 56) & 0xff), SIMD_ZBIT, 7);                 \
    return a;                                                             \
}
IWMMXT_OP_AVGB(0)
IWMMXT_OP_AVGB(1)
#undef IWMMXT_OP_AVGB
#undef AVGB

#define AVGW(SHR) ((( \
        ((a >> SHR) & 0xffff) + ((b >> SHR) & 0xffff) + round) >> 1) << SHR)
#define IWMMXT_OP_AVGW(r)                                               \
uint64_t HELPER(iwmmxt_avgw##r)(CPUARMState *env, uint64_t a, uint64_t b)  \
{                                                                       \
    const int round = r;                                                \
    a = AVGW(0) | AVGW(16) | AVGW(32) | AVGW(48);                       \
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =                               \
        SIMD16_SET(ZBIT16((a >> 0) & 0xffff), SIMD_ZBIT, 0) |           \
        SIMD16_SET(ZBIT16((a >> 16) & 0xffff), SIMD_ZBIT, 1) |          \
        SIMD16_SET(ZBIT16((a >> 32) & 0xffff), SIMD_ZBIT, 2) |          \
        SIMD16_SET(ZBIT16((a >> 48) & 0xffff), SIMD_ZBIT, 3);           \
    return a;                                                           \
}
IWMMXT_OP_AVGW(0)
IWMMXT_OP_AVGW(1)
#undef IWMMXT_OP_AVGW
#undef AVGW

uint64_t HELPER(iwmmxt_align)(uint64_t a, uint64_t b, uint32_t n)
{
    a >>= n << 3;
    a |= b << (64 - (n << 3));
    return a;
}

uint64_t HELPER(iwmmxt_insr)(uint64_t x, uint32_t a, uint32_t b, uint32_t n)
{
    x &= ~((uint64_t) b << n);
    x |= (uint64_t) (a & b) << n;
    return x;
}

uint32_t HELPER(iwmmxt_setpsr_nz)(uint64_t x)
{
    return SIMD64_SET((x == 0), SIMD_ZBIT) |
           SIMD64_SET((x & (1ULL << 63)), SIMD_NBIT);
}

uint64_t HELPER(iwmmxt_bcstb)(uint32_t arg)
{
    arg &= 0xff;
    return
        ((uint64_t) arg << 0 ) | ((uint64_t) arg << 8 ) |
        ((uint64_t) arg << 16) | ((uint64_t) arg << 24) |
        ((uint64_t) arg << 32) | ((uint64_t) arg << 40) |
        ((uint64_t) arg << 48) | ((uint64_t) arg << 56);
}

uint64_t HELPER(iwmmxt_bcstw)(uint32_t arg)
{
    arg &= 0xffff;
    return
        ((uint64_t) arg << 0 ) | ((uint64_t) arg << 16) |
        ((uint64_t) arg << 32) | ((uint64_t) arg << 48);
}

uint64_t HELPER(iwmmxt_bcstl)(uint32_t arg)
{
    return arg | ((uint64_t) arg << 32);
}

uint64_t HELPER(iwmmxt_addcb)(uint64_t x)
{
    return
        ((x >> 0) & 0xff) + ((x >> 8) & 0xff) +
        ((x >> 16) & 0xff) + ((x >> 24) & 0xff) +
        ((x >> 32) & 0xff) + ((x >> 40) & 0xff) +
        ((x >> 48) & 0xff) + ((x >> 56) & 0xff);
}

uint64_t HELPER(iwmmxt_addcw)(uint64_t x)
{
    return
        ((x >> 0) & 0xffff) + ((x >> 16) & 0xffff) +
        ((x >> 32) & 0xffff) + ((x >> 48) & 0xffff);
}

uint64_t HELPER(iwmmxt_addcl)(uint64_t x)
{
    return (x & 0xffffffff) + (x >> 32);
}

uint32_t HELPER(iwmmxt_msbb)(uint64_t x)
{
    return
        ((x >> 7) & 0x01) | ((x >> 14) & 0x02) |
        ((x >> 21) & 0x04) | ((x >> 28) & 0x08) |
        ((x >> 35) & 0x10) | ((x >> 42) & 0x20) |
        ((x >> 49) & 0x40) | ((x >> 56) & 0x80);
}

uint32_t HELPER(iwmmxt_msbw)(uint64_t x)
{
    return
        ((x >> 15) & 0x01) | ((x >> 30) & 0x02) |
        ((x >> 45) & 0x04) | ((x >> 52) & 0x08);
}

uint32_t HELPER(iwmmxt_msbl)(uint64_t x)
{
    return ((x >> 31) & 0x01) | ((x >> 62) & 0x02);
}

/* FIXME: Split wCASF setting into a separate op to avoid env use.  */
uint64_t HELPER(iwmmxt_srlw)(CPUARMState *env, uint64_t x, uint32_t n)
{
    x = (((x & (0xffffll << 0)) >> n) & (0xffffll << 0)) |
        (((x & (0xffffll << 16)) >> n) & (0xffffll << 16)) |
        (((x & (0xffffll << 32)) >> n) & (0xffffll << 32)) |
        (((x & (0xffffll << 48)) >> n) & (0xffffll << 48));
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =
        NZBIT16(x >> 0, 0) | NZBIT16(x >> 16, 1) |
        NZBIT16(x >> 32, 2) | NZBIT16(x >> 48, 3);
    return x;
}

uint64_t HELPER(iwmmxt_srll)(CPUARMState *env, uint64_t x, uint32_t n)
{
    x = ((x & (0xffffffffll << 0)) >> n) |
        ((x >> n) & (0xffffffffll << 32));
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =
        NZBIT32(x >> 0, 0) | NZBIT32(x >> 32, 1);
    return x;
}

uint64_t HELPER(iwmmxt_srlq)(CPUARMState *env, uint64_t x, uint32_t n)
{
    x >>= n;
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] = NZBIT64(x);
    return x;
}

uint64_t HELPER(iwmmxt_sllw)(CPUARMState *env, uint64_t x, uint32_t n)
{
    x = (((x & (0xffffll << 0)) << n) & (0xffffll << 0)) |
        (((x & (0xffffll << 16)) << n) & (0xffffll << 16)) |
        (((x & (0xffffll << 32)) << n) & (0xffffll << 32)) |
        (((x & (0xffffll << 48)) << n) & (0xffffll << 48));
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =
        NZBIT16(x >> 0, 0) | NZBIT16(x >> 16, 1) |
        NZBIT16(x >> 32, 2) | NZBIT16(x >> 48, 3);
    return x;
}

uint64_t HELPER(iwmmxt_slll)(CPUARMState *env, uint64_t x, uint32_t n)
{
    x = ((x << n) & (0xffffffffll << 0)) |
        ((x & (0xffffffffll << 32)) << n);
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =
        NZBIT32(x >> 0, 0) | NZBIT32(x >> 32, 1);
    return x;
}

uint64_t HELPER(iwmmxt_sllq)(CPUARMState *env, uint64_t x, uint32_t n)
{
    x <<= n;
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] = NZBIT64(x);
    return x;
}

uint64_t HELPER(iwmmxt_sraw)(CPUARMState *env, uint64_t x, uint32_t n)
{
    x = ((uint64_t) ((EXTEND16(x >> 0) >> n) & 0xffff) << 0) |
        ((uint64_t) ((EXTEND16(x >> 16) >> n) & 0xffff) << 16) |
        ((uint64_t) ((EXTEND16(x >> 32) >> n) & 0xffff) << 32) |
        ((uint64_t) ((EXTEND16(x >> 48) >> n) & 0xffff) << 48);
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =
        NZBIT16(x >> 0, 0) | NZBIT16(x >> 16, 1) |
        NZBIT16(x >> 32, 2) | NZBIT16(x >> 48, 3);
    return x;
}

uint64_t HELPER(iwmmxt_sral)(CPUARMState *env, uint64_t x, uint32_t n)
{
    x = (((EXTEND32(x >> 0) >> n) & 0xffffffff) << 0) |
        (((EXTEND32(x >> 32) >> n) & 0xffffffff) << 32);
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =
        NZBIT32(x >> 0, 0) | NZBIT32(x >> 32, 1);
    return x;
}

uint64_t HELPER(iwmmxt_sraq)(CPUARMState *env, uint64_t x, uint32_t n)
{
    x = (int64_t) x >> n;
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] = NZBIT64(x);
    return x;
}

uint64_t HELPER(iwmmxt_rorw)(CPUARMState *env, uint64_t x, uint32_t n)
{
    x = ((((x & (0xffffll << 0)) >> n) |
          ((x & (0xffffll << 0)) << (16 - n))) & (0xffffll << 0)) |
        ((((x & (0xffffll << 16)) >> n) |
          ((x & (0xffffll << 16)) << (16 - n))) & (0xffffll << 16)) |
        ((((x & (0xffffll << 32)) >> n) |
          ((x & (0xffffll << 32)) << (16 - n))) & (0xffffll << 32)) |
        ((((x & (0xffffll << 48)) >> n) |
          ((x & (0xffffll << 48)) << (16 - n))) & (0xffffll << 48));
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =
        NZBIT16(x >> 0, 0) | NZBIT16(x >> 16, 1) |
        NZBIT16(x >> 32, 2) | NZBIT16(x >> 48, 3);
    return x;
}

uint64_t HELPER(iwmmxt_rorl)(CPUARMState *env, uint64_t x, uint32_t n)
{
    x = ((x & (0xffffffffll << 0)) >> n) |
        ((x >> n) & (0xffffffffll << 32)) |
        ((x << (32 - n)) & (0xffffffffll << 0)) |
        ((x & (0xffffffffll << 32)) << (32 - n));
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =
        NZBIT32(x >> 0, 0) | NZBIT32(x >> 32, 1);
    return x;
}

uint64_t HELPER(iwmmxt_rorq)(CPUARMState *env, uint64_t x, uint32_t n)
{
    x = ror64(x, n);
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] = NZBIT64(x);
    return x;
}

uint64_t HELPER(iwmmxt_shufh)(CPUARMState *env, uint64_t x, uint32_t n)
{
    x = (((x >> ((n << 4) & 0x30)) & 0xffff) << 0) |
        (((x >> ((n << 2) & 0x30)) & 0xffff) << 16) |
        (((x >> ((n << 0) & 0x30)) & 0xffff) << 32) |
        (((x >> ((n >> 2) & 0x30)) & 0xffff) << 48);
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =
        NZBIT16(x >> 0, 0) | NZBIT16(x >> 16, 1) |
        NZBIT16(x >> 32, 2) | NZBIT16(x >> 48, 3);
    return x;
}

/* TODO: Unsigned-Saturation */
uint64_t HELPER(iwmmxt_packuw)(CPUARMState *env, uint64_t a, uint64_t b)
{
    a = (((a >> 0) & 0xff) << 0) | (((a >> 16) & 0xff) << 8) |
        (((a >> 32) & 0xff) << 16) | (((a >> 48) & 0xff) << 24) |
        (((b >> 0) & 0xff) << 32) | (((b >> 16) & 0xff) << 40) |
        (((b >> 32) & 0xff) << 48) | (((b >> 48) & 0xff) << 56);
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =
        NZBIT8(a >> 0, 0) | NZBIT8(a >> 8, 1) |
        NZBIT8(a >> 16, 2) | NZBIT8(a >> 24, 3) |
        NZBIT8(a >> 32, 4) | NZBIT8(a >> 40, 5) |
        NZBIT8(a >> 48, 6) | NZBIT8(a >> 56, 7);
    return a;
}

uint64_t HELPER(iwmmxt_packul)(CPUARMState *env, uint64_t a, uint64_t b)
{
    a = (((a >> 0) & 0xffff) << 0) | (((a >> 32) & 0xffff) << 16) |
        (((b >> 0) & 0xffff) << 32) | (((b >> 32) & 0xffff) << 48);
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =
        NZBIT16(a >> 0, 0) | NZBIT16(a >> 16, 1) |
        NZBIT16(a >> 32, 2) | NZBIT16(a >> 48, 3);
    return a;
}

uint64_t HELPER(iwmmxt_packuq)(CPUARMState *env, uint64_t a, uint64_t b)
{
    a = (a & 0xffffffff) | ((b & 0xffffffff) << 32);
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =
        NZBIT32(a >> 0, 0) | NZBIT32(a >> 32, 1);
    return a;
}

/* TODO: Signed-Saturation */
uint64_t HELPER(iwmmxt_packsw)(CPUARMState *env, uint64_t a, uint64_t b)
{
    a = (((a >> 0) & 0xff) << 0) | (((a >> 16) & 0xff) << 8) |
        (((a >> 32) & 0xff) << 16) | (((a >> 48) & 0xff) << 24) |
        (((b >> 0) & 0xff) << 32) | (((b >> 16) & 0xff) << 40) |
        (((b >> 32) & 0xff) << 48) | (((b >> 48) & 0xff) << 56);
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =
        NZBIT8(a >> 0, 0) | NZBIT8(a >> 8, 1) |
        NZBIT8(a >> 16, 2) | NZBIT8(a >> 24, 3) |
        NZBIT8(a >> 32, 4) | NZBIT8(a >> 40, 5) |
        NZBIT8(a >> 48, 6) | NZBIT8(a >> 56, 7);
    return a;
}

uint64_t HELPER(iwmmxt_packsl)(CPUARMState *env, uint64_t a, uint64_t b)
{
    a = (((a >> 0) & 0xffff) << 0) | (((a >> 32) & 0xffff) << 16) |
        (((b >> 0) & 0xffff) << 32) | (((b >> 32) & 0xffff) << 48);
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =
        NZBIT16(a >> 0, 0) | NZBIT16(a >> 16, 1) |
        NZBIT16(a >> 32, 2) | NZBIT16(a >> 48, 3);
    return a;
}

uint64_t HELPER(iwmmxt_packsq)(CPUARMState *env, uint64_t a, uint64_t b)
{
    a = (a & 0xffffffff) | ((b & 0xffffffff) << 32);
    env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =
        NZBIT32(a >> 0, 0) | NZBIT32(a >> 32, 1);
    return a;
}

uint64_t HELPER(iwmmxt_muladdsl)(uint64_t c, uint32_t a, uint32_t b)
{
    return c + ((int32_t) EXTEND32(a) * (int32_t) EXTEND32(b));
}

uint64_t HELPER(iwmmxt_muladdsw)(uint64_t c, uint32_t a, uint32_t b)
{
    c += EXTEND32(EXTEND16S((a >> 0) & 0xffff) *
                  EXTEND16S((b >> 0) & 0xffff));
    c += EXTEND32(EXTEND16S((a >> 16) & 0xffff) *
                  EXTEND16S((b >> 16) & 0xffff));
    return c;
}

uint64_t HELPER(iwmmxt_muladdswl)(uint64_t c, uint32_t a, uint32_t b)
{
    return c + (EXTEND32(EXTEND16S(a & 0xffff) *
                         EXTEND16S(b & 0xffff)));
}