src/video/SDL_blit_A.c
changeset 5389 24903690f48a
parent 5262 b530ef003506
child 5407 40c9d744e595
     1.1 --- a/src/video/SDL_blit_A.c	Mon Feb 21 23:45:48 2011 -0800
     1.2 +++ b/src/video/SDL_blit_A.c	Tue Feb 22 21:44:36 2011 -0800
     1.3 @@ -419,6 +419,806 @@
     1.4  
     1.5  #endif /* __MMX__ */
     1.6  
     1.7 +#if SDL_ALTIVEC_BLITTERS
     1.8 +#if __MWERKS__
     1.9 +#pragma altivec_model on
    1.10 +#endif
    1.11 +#if HAVE_ALTIVEC_H
    1.12 +#include <altivec.h>
    1.13 +#endif
    1.14 +#include <assert.h>
    1.15 +
    1.16 +#if (defined(__MACOSX__) && (__GNUC__ < 4))
    1.17 +#define VECUINT8_LITERAL(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) \
    1.18 +        (vector unsigned char) ( a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p )
    1.19 +#define VECUINT16_LITERAL(a,b,c,d,e,f,g,h) \
    1.20 +        (vector unsigned short) ( a,b,c,d,e,f,g,h )
    1.21 +#else
    1.22 +#define VECUINT8_LITERAL(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) \
    1.23 +        (vector unsigned char) { a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p }
    1.24 +#define VECUINT16_LITERAL(a,b,c,d,e,f,g,h) \
    1.25 +        (vector unsigned short) { a,b,c,d,e,f,g,h }
    1.26 +#endif
    1.27 +
    1.28 +#define UNALIGNED_PTR(x) (((size_t) x) & 0x0000000F)
    1.29 +#define VECPRINT(msg, v) do { \
    1.30 +    vector unsigned int tmpvec = (vector unsigned int)(v); \
    1.31 +    unsigned int *vp = (unsigned int *)&tmpvec; \
    1.32 +    printf("%s = %08X %08X %08X %08X\n", msg, vp[0], vp[1], vp[2], vp[3]); \
    1.33 +} while (0)
    1.34 +
    1.35 +/* the permuation vector that takes the high bytes out of all the appropriate shorts 
    1.36 +    (vector unsigned char)(
    1.37 +        0x00, 0x10, 0x02, 0x12,
    1.38 +        0x04, 0x14, 0x06, 0x16,
    1.39 +        0x08, 0x18, 0x0A, 0x1A,
    1.40 +        0x0C, 0x1C, 0x0E, 0x1E );
    1.41 +*/
    1.42 +#define VEC_MERGE_PERMUTE() (vec_add(vec_lvsl(0, (int*)NULL), (vector unsigned char)vec_splat_u16(0x0F)))
    1.43 +#define VEC_U32_24() (vec_add(vec_splat_u32(12), vec_splat_u32(12)))
    1.44 +#define VEC_ALPHA_MASK() ((vector unsigned char)vec_sl((vector unsigned int)vec_splat_s8(-1), VEC_U32_24()))
    1.45 +#define VEC_ALIGNER(src) ((UNALIGNED_PTR(src)) \
    1.46 +    ? vec_lvsl(0, src) \
    1.47 +    : vec_add(vec_lvsl(8, src), vec_splat_u8(8)))
    1.48 +
    1.49 +
    1.50 +#define VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1_16, v8_16) do { \
    1.51 +    /* vtemp1 contains source AAGGAAGGAAGGAAGG */ \
    1.52 +    vector unsigned short vtemp1 = vec_mule(vs, valpha); \
    1.53 +    /* vtemp2 contains source RRBBRRBBRRBBRRBB */ \
    1.54 +    vector unsigned short vtemp2 = vec_mulo(vs, valpha); \
    1.55 +    /* valpha2 is 255-alpha */ \
    1.56 +    vector unsigned char valpha2 = vec_nor(valpha, valpha); \
    1.57 +    /* vtemp3 contains dest AAGGAAGGAAGGAAGG */ \
    1.58 +    vector unsigned short vtemp3 = vec_mule(vd, valpha2); \
    1.59 +    /* vtemp4 contains dest RRBBRRBBRRBBRRBB */ \
    1.60 +    vector unsigned short vtemp4 = vec_mulo(vd, valpha2); \
    1.61 +    /* add source and dest */ \
    1.62 +    vtemp1 = vec_add(vtemp1, vtemp3); \
    1.63 +    vtemp2 = vec_add(vtemp2, vtemp4); \
    1.64 +    /* vtemp1 = (vtemp1 + 1) + ((vtemp1 + 1) >> 8) */ \
    1.65 +    vtemp1 = vec_add(vtemp1, v1_16); \
    1.66 +    vtemp3 = vec_sr(vtemp1, v8_16); \
    1.67 +    vtemp1 = vec_add(vtemp1, vtemp3); \
    1.68 +    /* vtemp2 = (vtemp2 + 1) + ((vtemp2 + 1) >> 8) */ \
    1.69 +    vtemp2 = vec_add(vtemp2, v1_16); \
    1.70 +    vtemp4 = vec_sr(vtemp2, v8_16); \
    1.71 +    vtemp2 = vec_add(vtemp2, vtemp4); \
    1.72 +    /* (>>8) and get ARGBARGBARGBARGB */ \
    1.73 +    vd = (vector unsigned char)vec_perm(vtemp1, vtemp2, mergePermute); \
    1.74 +} while (0)
    1.75 +
    1.76 +/* Calculate the permute vector used for 32->32 swizzling */
    1.77 +static vector unsigned char
    1.78 +calc_swizzle32(const SDL_PixelFormat * srcfmt, const SDL_PixelFormat * dstfmt)
    1.79 +{
    1.80 +    /*
    1.81 +     * We have to assume that the bits that aren't used by other
    1.82 +     *  colors is alpha, and it's one complete byte, since some formats
    1.83 +     *  leave alpha with a zero mask, but we should still swizzle the bits.
    1.84 +     */
    1.85 +    /* ARGB */
    1.86 +    const static struct SDL_PixelFormat default_pixel_format = {
    1.87 +        NULL, 0, 0,
    1.88 +        0, 0, 0, 0,
    1.89 +        16, 8, 0, 24,
    1.90 +        0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000
    1.91 +    };
    1.92 +    if (!srcfmt) {
    1.93 +        srcfmt = &default_pixel_format;
    1.94 +    }
    1.95 +    if (!dstfmt) {
    1.96 +        dstfmt = &default_pixel_format;
    1.97 +    }
    1.98 +    const vector unsigned char plus = VECUINT8_LITERAL(0x00, 0x00, 0x00, 0x00,
    1.99 +                                                       0x04, 0x04, 0x04, 0x04,
   1.100 +                                                       0x08, 0x08, 0x08, 0x08,
   1.101 +                                                       0x0C, 0x0C, 0x0C,
   1.102 +                                                       0x0C);
   1.103 +    vector unsigned char vswiz;
   1.104 +    vector unsigned int srcvec;
   1.105 +#define RESHIFT(X) (3 - ((X) >> 3))
   1.106 +    Uint32 rmask = RESHIFT(srcfmt->Rshift) << (dstfmt->Rshift);
   1.107 +    Uint32 gmask = RESHIFT(srcfmt->Gshift) << (dstfmt->Gshift);
   1.108 +    Uint32 bmask = RESHIFT(srcfmt->Bshift) << (dstfmt->Bshift);
   1.109 +    Uint32 amask;
   1.110 +    /* Use zero for alpha if either surface doesn't have alpha */
   1.111 +    if (dstfmt->Amask) {
   1.112 +        amask =
   1.113 +            ((srcfmt->Amask) ? RESHIFT(srcfmt->
   1.114 +                                       Ashift) : 0x10) << (dstfmt->Ashift);
   1.115 +    } else {
   1.116 +        amask =
   1.117 +            0x10101010 & ((dstfmt->Rmask | dstfmt->Gmask | dstfmt->Bmask) ^
   1.118 +                          0xFFFFFFFF);
   1.119 +    }
   1.120 +#undef RESHIFT
   1.121 +    ((unsigned int *) (char *) &srcvec)[0] = (rmask | gmask | bmask | amask);
   1.122 +    vswiz = vec_add(plus, (vector unsigned char) vec_splat(srcvec, 0));
   1.123 +    return (vswiz);
   1.124 +}
   1.125 +
   1.126 +static void
   1.127 +Blit32to565PixelAlphaAltivec(SDL_BlitInfo * info)
   1.128 +{
   1.129 +    int height = info->dst_h;
   1.130 +    Uint8 *src = (Uint8 *) info->src;
   1.131 +    int srcskip = info->src_skip;
   1.132 +    Uint8 *dst = (Uint8 *) info->dst;
   1.133 +    int dstskip = info->dst_skip;
   1.134 +    SDL_PixelFormat *srcfmt = info->src_fmt;
   1.135 +
   1.136 +    vector unsigned char v0 = vec_splat_u8(0);
   1.137 +    vector unsigned short v8_16 = vec_splat_u16(8);
   1.138 +    vector unsigned short v1_16 = vec_splat_u16(1);
   1.139 +    vector unsigned short v2_16 = vec_splat_u16(2);
   1.140 +    vector unsigned short v3_16 = vec_splat_u16(3);
   1.141 +    vector unsigned int v8_32 = vec_splat_u32(8);
   1.142 +    vector unsigned int v16_32 = vec_add(v8_32, v8_32);
   1.143 +    vector unsigned short v3f =
   1.144 +        VECUINT16_LITERAL(0x003f, 0x003f, 0x003f, 0x003f,
   1.145 +                          0x003f, 0x003f, 0x003f, 0x003f);
   1.146 +    vector unsigned short vfc =
   1.147 +        VECUINT16_LITERAL(0x00fc, 0x00fc, 0x00fc, 0x00fc,
   1.148 +                          0x00fc, 0x00fc, 0x00fc, 0x00fc);
   1.149 +
   1.150 +    /* 
   1.151 +       0x10 - 0x1f is the alpha
   1.152 +       0x00 - 0x0e evens are the red
   1.153 +       0x01 - 0x0f odds are zero
   1.154 +     */
   1.155 +    vector unsigned char vredalpha1 = VECUINT8_LITERAL(0x10, 0x00, 0x01, 0x01,
   1.156 +                                                       0x10, 0x02, 0x01, 0x01,
   1.157 +                                                       0x10, 0x04, 0x01, 0x01,
   1.158 +                                                       0x10, 0x06, 0x01,
   1.159 +                                                       0x01);
   1.160 +    vector unsigned char vredalpha2 =
   1.161 +        (vector unsigned char) (vec_add((vector unsigned int) vredalpha1,
   1.162 +                                        vec_sl(v8_32, v16_32))
   1.163 +        );
   1.164 +    /*
   1.165 +       0x00 - 0x0f is ARxx ARxx ARxx ARxx
   1.166 +       0x11 - 0x0f odds are blue
   1.167 +     */
   1.168 +    vector unsigned char vblue1 = VECUINT8_LITERAL(0x00, 0x01, 0x02, 0x11,
   1.169 +                                                   0x04, 0x05, 0x06, 0x13,
   1.170 +                                                   0x08, 0x09, 0x0a, 0x15,
   1.171 +                                                   0x0c, 0x0d, 0x0e, 0x17);
   1.172 +    vector unsigned char vblue2 =
   1.173 +        (vector unsigned char) (vec_add((vector unsigned int) vblue1, v8_32)
   1.174 +        );
   1.175 +    /*
   1.176 +       0x00 - 0x0f is ARxB ARxB ARxB ARxB
   1.177 +       0x10 - 0x0e evens are green
   1.178 +     */
   1.179 +    vector unsigned char vgreen1 = VECUINT8_LITERAL(0x00, 0x01, 0x10, 0x03,
   1.180 +                                                    0x04, 0x05, 0x12, 0x07,
   1.181 +                                                    0x08, 0x09, 0x14, 0x0b,
   1.182 +                                                    0x0c, 0x0d, 0x16, 0x0f);
   1.183 +    vector unsigned char vgreen2 =
   1.184 +        (vector unsigned
   1.185 +         char) (vec_add((vector unsigned int) vgreen1, vec_sl(v8_32, v8_32))
   1.186 +        );
   1.187 +    vector unsigned char vgmerge = VECUINT8_LITERAL(0x00, 0x02, 0x00, 0x06,
   1.188 +                                                    0x00, 0x0a, 0x00, 0x0e,
   1.189 +                                                    0x00, 0x12, 0x00, 0x16,
   1.190 +                                                    0x00, 0x1a, 0x00, 0x1e);
   1.191 +    vector unsigned char mergePermute = VEC_MERGE_PERMUTE();
   1.192 +    vector unsigned char vpermute = calc_swizzle32(srcfmt, NULL);
   1.193 +    vector unsigned char valphaPermute =
   1.194 +        vec_and(vec_lvsl(0, (int *) NULL), vec_splat_u8(0xC));
   1.195 +
   1.196 +    vector unsigned short vf800 = (vector unsigned short) vec_splat_u8(-7);
   1.197 +    vf800 = vec_sl(vf800, vec_splat_u16(8));
   1.198 +
   1.199 +    while (height--) {
   1.200 +        int extrawidth;
   1.201 +        vector unsigned char valigner;
   1.202 +        vector unsigned char vsrc;
   1.203 +        vector unsigned char voverflow;
   1.204 +        int width = info->dst_w;
   1.205 +
   1.206 +#define ONE_PIXEL_BLEND(condition, widthvar) \
   1.207 +        while (condition) { \
   1.208 +            Uint32 Pixel; \
   1.209 +            unsigned sR, sG, sB, dR, dG, dB, sA; \
   1.210 +            DISEMBLE_RGBA(src, 4, srcfmt, Pixel, sR, sG, sB, sA); \
   1.211 +            if(sA) { \
   1.212 +                unsigned short dstpixel = *((unsigned short *)dst); \
   1.213 +                dR = (dstpixel >> 8) & 0xf8; \
   1.214 +                dG = (dstpixel >> 3) & 0xfc; \
   1.215 +                dB = (dstpixel << 3) & 0xf8; \
   1.216 +                ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
   1.217 +                *((unsigned short *)dst) = ( \
   1.218 +                    ((dR & 0xf8) << 8) | ((dG & 0xfc) << 3) | (dB >> 3) \
   1.219 +                ); \
   1.220 +            } \
   1.221 +            src += 4; \
   1.222 +            dst += 2; \
   1.223 +            widthvar--; \
   1.224 +        }
   1.225 +        ONE_PIXEL_BLEND((UNALIGNED_PTR(dst)) && (width), width);
   1.226 +        extrawidth = (width % 8);
   1.227 +        valigner = VEC_ALIGNER(src);
   1.228 +        vsrc = (vector unsigned char) vec_ld(0, src);
   1.229 +        width -= extrawidth;
   1.230 +        while (width) {
   1.231 +            vector unsigned char valpha;
   1.232 +            vector unsigned char vsrc1, vsrc2;
   1.233 +            vector unsigned char vdst1, vdst2;
   1.234 +            vector unsigned short vR, vG, vB;
   1.235 +            vector unsigned short vpixel, vrpixel, vgpixel, vbpixel;
   1.236 +
   1.237 +            /* Load 8 pixels from src as ARGB */
   1.238 +            voverflow = (vector unsigned char) vec_ld(15, src);
   1.239 +            vsrc = vec_perm(vsrc, voverflow, valigner);
   1.240 +            vsrc1 = vec_perm(vsrc, vsrc, vpermute);
   1.241 +            src += 16;
   1.242 +            vsrc = (vector unsigned char) vec_ld(15, src);
   1.243 +            voverflow = vec_perm(voverflow, vsrc, valigner);
   1.244 +            vsrc2 = vec_perm(voverflow, voverflow, vpermute);
   1.245 +            src += 16;
   1.246 +
   1.247 +            /* Load 8 pixels from dst as XRGB */
   1.248 +            voverflow = vec_ld(0, dst);
   1.249 +            vR = vec_and((vector unsigned short) voverflow, vf800);
   1.250 +            vB = vec_sl((vector unsigned short) voverflow, v3_16);
   1.251 +            vG = vec_sl(vB, v2_16);
   1.252 +            vdst1 =
   1.253 +                (vector unsigned char) vec_perm((vector unsigned char) vR,
   1.254 +                                                (vector unsigned char) vR,
   1.255 +                                                vredalpha1);
   1.256 +            vdst1 = vec_perm(vdst1, (vector unsigned char) vB, vblue1);
   1.257 +            vdst1 = vec_perm(vdst1, (vector unsigned char) vG, vgreen1);
   1.258 +            vdst2 =
   1.259 +                (vector unsigned char) vec_perm((vector unsigned char) vR,
   1.260 +                                                (vector unsigned char) vR,
   1.261 +                                                vredalpha2);
   1.262 +            vdst2 = vec_perm(vdst2, (vector unsigned char) vB, vblue2);
   1.263 +            vdst2 = vec_perm(vdst2, (vector unsigned char) vG, vgreen2);
   1.264 +
   1.265 +            /* Alpha blend 8 pixels as ARGB */
   1.266 +            valpha = vec_perm(vsrc1, v0, valphaPermute);
   1.267 +            VEC_MULTIPLY_ALPHA(vsrc1, vdst1, valpha, mergePermute, v1_16,
   1.268 +                               v8_16);
   1.269 +            valpha = vec_perm(vsrc2, v0, valphaPermute);
   1.270 +            VEC_MULTIPLY_ALPHA(vsrc2, vdst2, valpha, mergePermute, v1_16,
   1.271 +                               v8_16);
   1.272 +
   1.273 +            /* Convert 8 pixels to 565 */
   1.274 +            vpixel = (vector unsigned short) vec_packpx((vector unsigned int)
   1.275 +                                                        vdst1,
   1.276 +                                                        (vector unsigned int)
   1.277 +                                                        vdst2);
   1.278 +            vgpixel = (vector unsigned short) vec_perm(vdst1, vdst2, vgmerge);
   1.279 +            vgpixel = vec_and(vgpixel, vfc);
   1.280 +            vgpixel = vec_sl(vgpixel, v3_16);
   1.281 +            vrpixel = vec_sl(vpixel, v1_16);
   1.282 +            vrpixel = vec_and(vrpixel, vf800);
   1.283 +            vbpixel = vec_and(vpixel, v3f);
   1.284 +            vdst1 =
   1.285 +                vec_or((vector unsigned char) vrpixel,
   1.286 +                       (vector unsigned char) vgpixel);
   1.287 +            vdst1 = vec_or(vdst1, (vector unsigned char) vbpixel);
   1.288 +
   1.289 +            /* Store 8 pixels */
   1.290 +            vec_st(vdst1, 0, dst);
   1.291 +
   1.292 +            width -= 8;
   1.293 +            dst += 16;
   1.294 +        }
   1.295 +        ONE_PIXEL_BLEND((extrawidth), extrawidth);
   1.296 +#undef ONE_PIXEL_BLEND
   1.297 +        src += srcskip;
   1.298 +        dst += dstskip;
   1.299 +    }
   1.300 +}
   1.301 +
   1.302 +static void
   1.303 +Blit32to32SurfaceAlphaKeyAltivec(SDL_BlitInfo * info)
   1.304 +{
   1.305 +    int height = info->dst_h;
   1.306 +    Uint32 *srcp = (Uint32 *) info->src;
   1.307 +    int srcskip = info->src_skip >> 2;
   1.308 +    Uint32 *dstp = (Uint32 *) info->dst;
   1.309 +    int dstskip = info->dst_skip >> 2;
   1.310 +    SDL_PixelFormat *srcfmt = info->src_fmt;
   1.311 +    SDL_PixelFormat *dstfmt = info->dst_fmt;
   1.312 +    unsigned sA = info->a;
   1.313 +    unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
   1.314 +    Uint32 rgbmask = srcfmt->Rmask | srcfmt->Gmask | srcfmt->Bmask;
   1.315 +    Uint32 ckey = info->colorkey;
   1.316 +    vector unsigned char mergePermute;
   1.317 +    vector unsigned char vsrcPermute;
   1.318 +    vector unsigned char vdstPermute;
   1.319 +    vector unsigned char vsdstPermute;
   1.320 +    vector unsigned char valpha;
   1.321 +    vector unsigned char valphamask;
   1.322 +    vector unsigned char vbits;
   1.323 +    vector unsigned char v0;
   1.324 +    vector unsigned short v1;
   1.325 +    vector unsigned short v8;
   1.326 +    vector unsigned int vckey;
   1.327 +    vector unsigned int vrgbmask;
   1.328 +
   1.329 +    mergePermute = VEC_MERGE_PERMUTE();
   1.330 +    v0 = vec_splat_u8(0);
   1.331 +    v1 = vec_splat_u16(1);
   1.332 +    v8 = vec_splat_u16(8);
   1.333 +
   1.334 +    /* set the alpha to 255 on the destination surf */
   1.335 +    valphamask = VEC_ALPHA_MASK();
   1.336 +
   1.337 +    vsrcPermute = calc_swizzle32(srcfmt, NULL);
   1.338 +    vdstPermute = calc_swizzle32(NULL, dstfmt);
   1.339 +    vsdstPermute = calc_swizzle32(dstfmt, NULL);
   1.340 +
   1.341 +    /* set a vector full of alpha and 255-alpha */
   1.342 +    ((unsigned char *) &valpha)[0] = sA;
   1.343 +    valpha = vec_splat(valpha, 0);
   1.344 +    vbits = (vector unsigned char) vec_splat_s8(-1);
   1.345 +
   1.346 +    ckey &= rgbmask;
   1.347 +    ((unsigned int *) (char *) &vckey)[0] = ckey;
   1.348 +    vckey = vec_splat(vckey, 0);
   1.349 +    ((unsigned int *) (char *) &vrgbmask)[0] = rgbmask;
   1.350 +    vrgbmask = vec_splat(vrgbmask, 0);
   1.351 +
   1.352 +    while (height--) {
   1.353 +        int width = info->dst_w;
   1.354 +#define ONE_PIXEL_BLEND(condition, widthvar) \
   1.355 +        while (condition) { \
   1.356 +            Uint32 Pixel; \
   1.357 +            unsigned sR, sG, sB, dR, dG, dB; \
   1.358 +            RETRIEVE_RGB_PIXEL(((Uint8 *)srcp), 4, Pixel); \
   1.359 +            if(sA && Pixel != ckey) { \
   1.360 +                RGB_FROM_PIXEL(Pixel, srcfmt, sR, sG, sB); \
   1.361 +                DISEMBLE_RGB(((Uint8 *)dstp), 4, dstfmt, Pixel, dR, dG, dB); \
   1.362 +                ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
   1.363 +                ASSEMBLE_RGBA(((Uint8 *)dstp), 4, dstfmt, dR, dG, dB, dA); \
   1.364 +            } \
   1.365 +            dstp++; \
   1.366 +            srcp++; \
   1.367 +            widthvar--; \
   1.368 +        }
   1.369 +        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
   1.370 +        if (width > 0) {
   1.371 +            int extrawidth = (width % 4);
   1.372 +            vector unsigned char valigner = VEC_ALIGNER(srcp);
   1.373 +            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
   1.374 +            width -= extrawidth;
   1.375 +            while (width) {
   1.376 +                vector unsigned char vsel;
   1.377 +                vector unsigned char voverflow;
   1.378 +                vector unsigned char vd;
   1.379 +                vector unsigned char vd_orig;
   1.380 +
   1.381 +                /* s = *srcp */
   1.382 +                voverflow = (vector unsigned char) vec_ld(15, srcp);
   1.383 +                vs = vec_perm(vs, voverflow, valigner);
   1.384 +
   1.385 +                /* vsel is set for items that match the key */
   1.386 +                vsel =
   1.387 +                    (vector unsigned char) vec_and((vector unsigned int) vs,
   1.388 +                                                   vrgbmask);
   1.389 +                vsel = (vector unsigned char) vec_cmpeq((vector unsigned int)
   1.390 +                                                        vsel, vckey);
   1.391 +
   1.392 +                /* permute to source format */
   1.393 +                vs = vec_perm(vs, valpha, vsrcPermute);
   1.394 +
   1.395 +                /* d = *dstp */
   1.396 +                vd = (vector unsigned char) vec_ld(0, dstp);
   1.397 +                vd_orig = vd = vec_perm(vd, v0, vsdstPermute);
   1.398 +
   1.399 +                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
   1.400 +
   1.401 +                /* set the alpha channel to full on */
   1.402 +                vd = vec_or(vd, valphamask);
   1.403 +
   1.404 +                /* mask out color key */
   1.405 +                vd = vec_sel(vd, vd_orig, vsel);
   1.406 +
   1.407 +                /* permute to dest format */
   1.408 +                vd = vec_perm(vd, vbits, vdstPermute);
   1.409 +
   1.410 +                /* *dstp = res */
   1.411 +                vec_st((vector unsigned int) vd, 0, dstp);
   1.412 +
   1.413 +                srcp += 4;
   1.414 +                dstp += 4;
   1.415 +                width -= 4;
   1.416 +                vs = voverflow;
   1.417 +            }
   1.418 +            ONE_PIXEL_BLEND((extrawidth), extrawidth);
   1.419 +        }
   1.420 +#undef ONE_PIXEL_BLEND
   1.421 +
   1.422 +        srcp += srcskip;
   1.423 +        dstp += dstskip;
   1.424 +    }
   1.425 +}
   1.426 +
   1.427 +
   1.428 +static void
   1.429 +Blit32to32PixelAlphaAltivec(SDL_BlitInfo * info)
   1.430 +{
   1.431 +    int width = info->dst_w;
   1.432 +    int height = info->dst_h;
   1.433 +    Uint32 *srcp = (Uint32 *) info->src;
   1.434 +    int srcskip = info->src_skip >> 2;
   1.435 +    Uint32 *dstp = (Uint32 *) info->dst;
   1.436 +    int dstskip = info->dst_skip >> 2;
   1.437 +    SDL_PixelFormat *srcfmt = info->src_fmt;
   1.438 +    SDL_PixelFormat *dstfmt = info->dst_fmt;
   1.439 +    vector unsigned char mergePermute;
   1.440 +    vector unsigned char valphaPermute;
   1.441 +    vector unsigned char vsrcPermute;
   1.442 +    vector unsigned char vdstPermute;
   1.443 +    vector unsigned char vsdstPermute;
   1.444 +    vector unsigned char valphamask;
   1.445 +    vector unsigned char vpixelmask;
   1.446 +    vector unsigned char v0;
   1.447 +    vector unsigned short v1;
   1.448 +    vector unsigned short v8;
   1.449 +
   1.450 +    v0 = vec_splat_u8(0);
   1.451 +    v1 = vec_splat_u16(1);
   1.452 +    v8 = vec_splat_u16(8);
   1.453 +    mergePermute = VEC_MERGE_PERMUTE();
   1.454 +    valphamask = VEC_ALPHA_MASK();
   1.455 +    valphaPermute = vec_and(vec_lvsl(0, (int *) NULL), vec_splat_u8(0xC));
   1.456 +    vpixelmask = vec_nor(valphamask, v0);
   1.457 +    vsrcPermute = calc_swizzle32(srcfmt, NULL);
   1.458 +    vdstPermute = calc_swizzle32(NULL, dstfmt);
   1.459 +    vsdstPermute = calc_swizzle32(dstfmt, NULL);
   1.460 +
   1.461 +    while (height--) {
   1.462 +        width = info->dst_w;
   1.463 +#define ONE_PIXEL_BLEND(condition, widthvar) while ((condition)) { \
   1.464 +            Uint32 Pixel; \
   1.465 +            unsigned sR, sG, sB, dR, dG, dB, sA, dA; \
   1.466 +            DISEMBLE_RGBA((Uint8 *)srcp, 4, srcfmt, Pixel, sR, sG, sB, sA); \
   1.467 +            if(sA) { \
   1.468 +              DISEMBLE_RGBA((Uint8 *)dstp, 4, dstfmt, Pixel, dR, dG, dB, dA); \
   1.469 +              ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
   1.470 +              ASSEMBLE_RGBA((Uint8 *)dstp, 4, dstfmt, dR, dG, dB, dA); \
   1.471 +            } \
   1.472 +            ++srcp; \
   1.473 +            ++dstp; \
   1.474 +            widthvar--; \
   1.475 +        }
   1.476 +        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
   1.477 +        if (width > 0) {
   1.478 +            /* vsrcPermute */
   1.479 +            /* vdstPermute */
   1.480 +            int extrawidth = (width % 4);
   1.481 +            vector unsigned char valigner = VEC_ALIGNER(srcp);
   1.482 +            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
   1.483 +            width -= extrawidth;
   1.484 +            while (width) {
   1.485 +                vector unsigned char voverflow;
   1.486 +                vector unsigned char vd;
   1.487 +                vector unsigned char valpha;
   1.488 +                vector unsigned char vdstalpha;
   1.489 +                /* s = *srcp */
   1.490 +                voverflow = (vector unsigned char) vec_ld(15, srcp);
   1.491 +                vs = vec_perm(vs, voverflow, valigner);
   1.492 +                vs = vec_perm(vs, v0, vsrcPermute);
   1.493 +
   1.494 +                valpha = vec_perm(vs, v0, valphaPermute);
   1.495 +
   1.496 +                /* d = *dstp */
   1.497 +                vd = (vector unsigned char) vec_ld(0, dstp);
   1.498 +                vd = vec_perm(vd, v0, vsdstPermute);
   1.499 +                vdstalpha = vec_and(vd, valphamask);
   1.500 +
   1.501 +                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
   1.502 +
   1.503 +                /* set the alpha to the dest alpha */
   1.504 +                vd = vec_and(vd, vpixelmask);
   1.505 +                vd = vec_or(vd, vdstalpha);
   1.506 +                vd = vec_perm(vd, v0, vdstPermute);
   1.507 +
   1.508 +                /* *dstp = res */
   1.509 +                vec_st((vector unsigned int) vd, 0, dstp);
   1.510 +
   1.511 +                srcp += 4;
   1.512 +                dstp += 4;
   1.513 +                width -= 4;
   1.514 +                vs = voverflow;
   1.515 +
   1.516 +            }
   1.517 +            ONE_PIXEL_BLEND((extrawidth), extrawidth);
   1.518 +        }
   1.519 +        srcp += srcskip;
   1.520 +        dstp += dstskip;
   1.521 +#undef ONE_PIXEL_BLEND
   1.522 +    }
   1.523 +}
   1.524 +
   1.525 +/* fast ARGB888->(A)RGB888 blending with pixel alpha */
   1.526 +static void
   1.527 +BlitRGBtoRGBPixelAlphaAltivec(SDL_BlitInfo * info)
   1.528 +{
   1.529 +    int width = info->dst_w;
   1.530 +    int height = info->dst_h;
   1.531 +    Uint32 *srcp = (Uint32 *) info->src;
   1.532 +    int srcskip = info->src_skip >> 2;
   1.533 +    Uint32 *dstp = (Uint32 *) info->dst;
   1.534 +    int dstskip = info->dst_skip >> 2;
   1.535 +    vector unsigned char mergePermute;
   1.536 +    vector unsigned char valphaPermute;
   1.537 +    vector unsigned char valphamask;
   1.538 +    vector unsigned char vpixelmask;
   1.539 +    vector unsigned char v0;
   1.540 +    vector unsigned short v1;
   1.541 +    vector unsigned short v8;
   1.542 +    v0 = vec_splat_u8(0);
   1.543 +    v1 = vec_splat_u16(1);
   1.544 +    v8 = vec_splat_u16(8);
   1.545 +    mergePermute = VEC_MERGE_PERMUTE();
   1.546 +    valphamask = VEC_ALPHA_MASK();
   1.547 +    valphaPermute = vec_and(vec_lvsl(0, (int *) NULL), vec_splat_u8(0xC));
   1.548 +
   1.549 +
   1.550 +    vpixelmask = vec_nor(valphamask, v0);
   1.551 +    while (height--) {
   1.552 +        width = info->dst_w;
   1.553 +#define ONE_PIXEL_BLEND(condition, widthvar) \
   1.554 +        while ((condition)) { \
   1.555 +            Uint32 dalpha; \
   1.556 +            Uint32 d; \
   1.557 +            Uint32 s1; \
   1.558 +            Uint32 d1; \
   1.559 +            Uint32 s = *srcp; \
   1.560 +            Uint32 alpha = s >> 24; \
   1.561 +            if(alpha) { \
   1.562 +              if(alpha == SDL_ALPHA_OPAQUE) { \
   1.563 +                *dstp = (s & 0x00ffffff) | (*dstp & 0xff000000); \
   1.564 +              } else { \
   1.565 +                d = *dstp; \
   1.566 +                dalpha = d & 0xff000000; \
   1.567 +                s1 = s & 0xff00ff; \
   1.568 +                d1 = d & 0xff00ff; \
   1.569 +                d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff; \
   1.570 +                s &= 0xff00; \
   1.571 +                d &= 0xff00; \
   1.572 +                d = (d + ((s - d) * alpha >> 8)) & 0xff00; \
   1.573 +                *dstp = d1 | d | dalpha; \
   1.574 +              } \
   1.575 +            } \
   1.576 +            ++srcp; \
   1.577 +            ++dstp; \
   1.578 +            widthvar--; \
   1.579 +	    }
   1.580 +        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
   1.581 +        if (width > 0) {
   1.582 +            int extrawidth = (width % 4);
   1.583 +            vector unsigned char valigner = VEC_ALIGNER(srcp);
   1.584 +            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
   1.585 +            width -= extrawidth;
   1.586 +            while (width) {
   1.587 +                vector unsigned char voverflow;
   1.588 +                vector unsigned char vd;
   1.589 +                vector unsigned char valpha;
   1.590 +                vector unsigned char vdstalpha;
   1.591 +                /* s = *srcp */
   1.592 +                voverflow = (vector unsigned char) vec_ld(15, srcp);
   1.593 +                vs = vec_perm(vs, voverflow, valigner);
   1.594 +
   1.595 +                valpha = vec_perm(vs, v0, valphaPermute);
   1.596 +
   1.597 +                /* d = *dstp */
   1.598 +                vd = (vector unsigned char) vec_ld(0, dstp);
   1.599 +                vdstalpha = vec_and(vd, valphamask);
   1.600 +
   1.601 +                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
   1.602 +
   1.603 +                /* set the alpha to the dest alpha */
   1.604 +                vd = vec_and(vd, vpixelmask);
   1.605 +                vd = vec_or(vd, vdstalpha);
   1.606 +
   1.607 +                /* *dstp = res */
   1.608 +                vec_st((vector unsigned int) vd, 0, dstp);
   1.609 +
   1.610 +                srcp += 4;
   1.611 +                dstp += 4;
   1.612 +                width -= 4;
   1.613 +                vs = voverflow;
   1.614 +            }
   1.615 +            ONE_PIXEL_BLEND((extrawidth), extrawidth);
   1.616 +        }
   1.617 +        srcp += srcskip;
   1.618 +        dstp += dstskip;
   1.619 +    }
   1.620 +#undef ONE_PIXEL_BLEND
   1.621 +}
   1.622 +
   1.623 +static void
   1.624 +Blit32to32SurfaceAlphaAltivec(SDL_BlitInfo * info)
   1.625 +{
   1.626 +    /* XXX : 6 */
   1.627 +    int height = info->dst_h;
   1.628 +    Uint32 *srcp = (Uint32 *) info->src;
   1.629 +    int srcskip = info->src_skip >> 2;
   1.630 +    Uint32 *dstp = (Uint32 *) info->dst;
   1.631 +    int dstskip = info->dst_skip >> 2;
   1.632 +    SDL_PixelFormat *srcfmt = info->src_fmt;
   1.633 +    SDL_PixelFormat *dstfmt = info->dst_fmt;
   1.634 +    unsigned sA = info->a;
   1.635 +    unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
   1.636 +    vector unsigned char mergePermute;
   1.637 +    vector unsigned char vsrcPermute;
   1.638 +    vector unsigned char vdstPermute;
   1.639 +    vector unsigned char vsdstPermute;
   1.640 +    vector unsigned char valpha;
   1.641 +    vector unsigned char valphamask;
   1.642 +    vector unsigned char vbits;
   1.643 +    vector unsigned short v1;
   1.644 +    vector unsigned short v8;
   1.645 +
   1.646 +    mergePermute = VEC_MERGE_PERMUTE();
   1.647 +    v1 = vec_splat_u16(1);
   1.648 +    v8 = vec_splat_u16(8);
   1.649 +
   1.650 +    /* set the alpha to 255 on the destination surf */
   1.651 +    valphamask = VEC_ALPHA_MASK();
   1.652 +
   1.653 +    vsrcPermute = calc_swizzle32(srcfmt, NULL);
   1.654 +    vdstPermute = calc_swizzle32(NULL, dstfmt);
   1.655 +    vsdstPermute = calc_swizzle32(dstfmt, NULL);
   1.656 +
   1.657 +    /* set a vector full of alpha and 255-alpha */
   1.658 +    ((unsigned char *) &valpha)[0] = sA;
   1.659 +    valpha = vec_splat(valpha, 0);
   1.660 +    vbits = (vector unsigned char) vec_splat_s8(-1);
   1.661 +
   1.662 +    while (height--) {
   1.663 +        int width = info->dst_w;
   1.664 +#define ONE_PIXEL_BLEND(condition, widthvar) while ((condition)) { \
   1.665 +            Uint32 Pixel; \
   1.666 +            unsigned sR, sG, sB, dR, dG, dB; \
   1.667 +            DISEMBLE_RGB(((Uint8 *)srcp), 4, srcfmt, Pixel, sR, sG, sB); \
   1.668 +            DISEMBLE_RGB(((Uint8 *)dstp), 4, dstfmt, Pixel, dR, dG, dB); \
   1.669 +            ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
   1.670 +            ASSEMBLE_RGBA(((Uint8 *)dstp), 4, dstfmt, dR, dG, dB, dA); \
   1.671 +            ++srcp; \
   1.672 +            ++dstp; \
   1.673 +            widthvar--; \
   1.674 +        }
   1.675 +        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
   1.676 +        if (width > 0) {
   1.677 +            int extrawidth = (width % 4);
   1.678 +            vector unsigned char valigner = VEC_ALIGNER(srcp);
   1.679 +            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
   1.680 +            width -= extrawidth;
   1.681 +            while (width) {
   1.682 +                vector unsigned char voverflow;
   1.683 +                vector unsigned char vd;
   1.684 +
   1.685 +                /* s = *srcp */
   1.686 +                voverflow = (vector unsigned char) vec_ld(15, srcp);
   1.687 +                vs = vec_perm(vs, voverflow, valigner);
   1.688 +                vs = vec_perm(vs, valpha, vsrcPermute);
   1.689 +
   1.690 +                /* d = *dstp */
   1.691 +                vd = (vector unsigned char) vec_ld(0, dstp);
   1.692 +                vd = vec_perm(vd, vd, vsdstPermute);
   1.693 +
   1.694 +                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
   1.695 +
   1.696 +                /* set the alpha channel to full on */
   1.697 +                vd = vec_or(vd, valphamask);
   1.698 +                vd = vec_perm(vd, vbits, vdstPermute);
   1.699 +
   1.700 +                /* *dstp = res */
   1.701 +                vec_st((vector unsigned int) vd, 0, dstp);
   1.702 +
   1.703 +                srcp += 4;
   1.704 +                dstp += 4;
   1.705 +                width -= 4;
   1.706 +                vs = voverflow;
   1.707 +            }
   1.708 +            ONE_PIXEL_BLEND((extrawidth), extrawidth);
   1.709 +        }
   1.710 +#undef ONE_PIXEL_BLEND
   1.711 +
   1.712 +        srcp += srcskip;
   1.713 +        dstp += dstskip;
   1.714 +    }
   1.715 +
   1.716 +}
   1.717 +
   1.718 +
   1.719 +/* fast RGB888->(A)RGB888 blending */
   1.720 +static void
   1.721 +BlitRGBtoRGBSurfaceAlphaAltivec(SDL_BlitInfo * info)
   1.722 +{
   1.723 +    unsigned alpha = info->a;
   1.724 +    int height = info->dst_h;
   1.725 +    Uint32 *srcp = (Uint32 *) info->src;
   1.726 +    int srcskip = info->src_skip >> 2;
   1.727 +    Uint32 *dstp = (Uint32 *) info->dst;
   1.728 +    int dstskip = info->dst_skip >> 2;
   1.729 +    vector unsigned char mergePermute;
   1.730 +    vector unsigned char valpha;
   1.731 +    vector unsigned char valphamask;
   1.732 +    vector unsigned short v1;
   1.733 +    vector unsigned short v8;
   1.734 +
   1.735 +    mergePermute = VEC_MERGE_PERMUTE();
   1.736 +    v1 = vec_splat_u16(1);
   1.737 +    v8 = vec_splat_u16(8);
   1.738 +
   1.739 +    /* set the alpha to 255 on the destination surf */
   1.740 +    valphamask = VEC_ALPHA_MASK();
   1.741 +
   1.742 +    /* set a vector full of alpha and 255-alpha */
   1.743 +    ((unsigned char *) &valpha)[0] = alpha;
   1.744 +    valpha = vec_splat(valpha, 0);
   1.745 +
   1.746 +    while (height--) {
   1.747 +        int width = info->dst_w;
   1.748 +#define ONE_PIXEL_BLEND(condition, widthvar) while ((condition)) { \
   1.749 +            Uint32 s = *srcp; \
   1.750 +            Uint32 d = *dstp; \
   1.751 +            Uint32 s1 = s & 0xff00ff; \
   1.752 +            Uint32 d1 = d & 0xff00ff; \
   1.753 +            d1 = (d1 + ((s1 - d1) * alpha >> 8)) \
   1.754 +                 & 0xff00ff; \
   1.755 +            s &= 0xff00; \
   1.756 +            d &= 0xff00; \
   1.757 +            d = (d + ((s - d) * alpha >> 8)) & 0xff00; \
   1.758 +            *dstp = d1 | d | 0xff000000; \
   1.759 +            ++srcp; \
   1.760 +            ++dstp; \
   1.761 +            widthvar--; \
   1.762 +        }
   1.763 +        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
   1.764 +        if (width > 0) {
   1.765 +            int extrawidth = (width % 4);
   1.766 +            vector unsigned char valigner = VEC_ALIGNER(srcp);
   1.767 +            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
   1.768 +            width -= extrawidth;
   1.769 +            while (width) {
   1.770 +                vector unsigned char voverflow;
   1.771 +                vector unsigned char vd;
   1.772 +
   1.773 +                /* s = *srcp */
   1.774 +                voverflow = (vector unsigned char) vec_ld(15, srcp);
   1.775 +                vs = vec_perm(vs, voverflow, valigner);
   1.776 +
   1.777 +                /* d = *dstp */
   1.778 +                vd = (vector unsigned char) vec_ld(0, dstp);
   1.779 +
   1.780 +                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
   1.781 +
   1.782 +                /* set the alpha channel to full on */
   1.783 +                vd = vec_or(vd, valphamask);
   1.784 +
   1.785 +                /* *dstp = res */
   1.786 +                vec_st((vector unsigned int) vd, 0, dstp);
   1.787 +
   1.788 +                srcp += 4;
   1.789 +                dstp += 4;
   1.790 +                width -= 4;
   1.791 +                vs = voverflow;
   1.792 +            }
   1.793 +            ONE_PIXEL_BLEND((extrawidth), extrawidth);
   1.794 +        }
   1.795 +#undef ONE_PIXEL_BLEND
   1.796 +
   1.797 +        srcp += srcskip;
   1.798 +        dstp += dstskip;
   1.799 +    }
   1.800 +}
   1.801 +
   1.802 +#if __MWERKS__
   1.803 +#pragma altivec_model off
   1.804 +#endif
   1.805 +#endif /* SDL_ALTIVEC_BLITTERS */
   1.806 +
   1.807  /* fast RGB888->(A)RGB888 blending with surface alpha=128 special case */
   1.808  static void
   1.809  BlitRGBtoRGBSurfaceAlpha128(SDL_BlitInfo * info)
   1.810 @@ -538,6 +1338,79 @@
   1.811      }
   1.812  }
   1.813  
   1.814 +#ifdef __3dNOW__
   1.815 +/* fast (as in MMX with prefetch) ARGB888->(A)RGB888 blending with pixel alpha */
   1.816 +static void
   1.817 +BlitRGBtoRGBPixelAlphaMMX3DNOW(SDL_BlitInfo * info)
   1.818 +{
   1.819 +    int width = info->dst_w;
   1.820 +    int height = info->dst_h;
   1.821 +    Uint32 *srcp = (Uint32 *) info->src;
   1.822 +    int srcskip = info->src_skip >> 2;
   1.823 +    Uint32 *dstp = (Uint32 *) info->dst;
   1.824 +    int dstskip = info->dst_skip >> 2;
   1.825 +    SDL_PixelFormat *sf = info->src_fmt;
   1.826 +    Uint32 chanmask = sf->Rmask | sf->Gmask | sf->Bmask;
   1.827 +    Uint32 amask = sf->Amask;
   1.828 +    Uint32 ashift = sf->Ashift;
   1.829 +    Uint64 multmask;
   1.830 +
   1.831 +    __m64 src1, dst1, mm_alpha, mm_zero, dmask;
   1.832 +
   1.833 +    mm_zero = _mm_setzero_si64();       /* 0 -> mm_zero */
   1.834 +    multmask = 0xFFFF;
   1.835 +    multmask <<= (ashift * 2);
   1.836 +    multmask = ~multmask;
   1.837 +    dmask = *(__m64 *) & multmask;      /* dst alpha mask -> dmask */
   1.838 +
   1.839 +    while (height--) {
   1.840 +	    /* *INDENT-OFF* */
   1.841 +	    DUFFS_LOOP4({
   1.842 +		Uint32 alpha;
   1.843 +
   1.844 +		_m_prefetch(srcp + 16);
   1.845 +		_m_prefetch(dstp + 16);
   1.846 +
   1.847 +		alpha = *srcp & amask;
   1.848 +		if (alpha == 0) {
   1.849 +			/* do nothing */
   1.850 +		} else if (alpha == amask) {
   1.851 +			/* copy RGB, keep dst alpha */
   1.852 +			*dstp = (*srcp & chanmask) | (*dstp & ~chanmask);
   1.853 +		} else {
   1.854 +			src1 = _mm_cvtsi32_si64(*srcp); /* src(ARGB) -> src1 (0000ARGB)*/
   1.855 +			src1 = _mm_unpacklo_pi8(src1, mm_zero); /* 0A0R0G0B -> src1 */
   1.856 +
   1.857 +			dst1 = _mm_cvtsi32_si64(*dstp); /* dst(ARGB) -> dst1 (0000ARGB)*/
   1.858 +			dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* 0A0R0G0B -> dst1 */
   1.859 +
   1.860 +			mm_alpha = _mm_cvtsi32_si64(alpha); /* alpha -> mm_alpha (0000000A) */
   1.861 +			mm_alpha = _mm_srli_si64(mm_alpha, ashift); /* mm_alpha >> ashift -> mm_alpha(0000000A) */
   1.862 +			mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha); /* 00000A0A -> mm_alpha */
   1.863 +			mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha); /* 0A0A0A0A -> mm_alpha */
   1.864 +			mm_alpha = _mm_and_si64(mm_alpha, dmask); /* 000A0A0A -> mm_alpha, preserve dst alpha on add */
   1.865 +
   1.866 +			/* blend */		    
   1.867 +			src1 = _mm_sub_pi16(src1, dst1);/* src - dst -> src1 */
   1.868 +			src1 = _mm_mullo_pi16(src1, mm_alpha); /* (src - dst) * alpha -> src1 */
   1.869 +			src1 = _mm_srli_pi16(src1, 8); /* src1 >> 8 -> src1(000R0G0B) */
   1.870 +			dst1 = _mm_add_pi8(src1, dst1); /* src1 + dst1(dst) -> dst1(0A0R0G0B) */
   1.871 +			dst1 = _mm_packs_pu16(dst1, mm_zero);  /* 0000ARGB -> dst1 */
   1.872 +			
   1.873 +			*dstp = _mm_cvtsi64_si32(dst1); /* dst1 -> pixel */
   1.874 +		}
   1.875 +		++srcp;
   1.876 +		++dstp;
   1.877 +	    }, width);
   1.878 +	    /* *INDENT-ON* */
   1.879 +        srcp += srcskip;
   1.880 +        dstp += dstskip;
   1.881 +    }
   1.882 +    _mm_empty();
   1.883 +}
   1.884 +
   1.885 +#endif /* __MMX__ */
   1.886 +
   1.887  /* 16bpp special case for per-surface alpha=50%: blend 2 pixels in parallel */
   1.888  
   1.889  /* blend a single 16 bit pixel at 50% */
   1.890 @@ -1257,10 +2130,17 @@
   1.891              return BlitNto1PixelAlpha;
   1.892  
   1.893          case 2:
   1.894 -            if (sf->BytesPerPixel == 4 && sf->Amask == 0xff000000
   1.895 -                && sf->Gmask == 0xff00
   1.896 -                && ((sf->Rmask == 0xff && df->Rmask == 0x1f)
   1.897 -                    || (sf->Bmask == 0xff && df->Bmask == 0x1f))) {
   1.898 +#if SDL_ALTIVEC_BLITTERS
   1.899 +            if (sf->BytesPerPixel == 4
   1.900 +                && df->Gmask == 0x7e0 && df->Bmask == 0x1f
   1.901 +                && SDL_HasAltiVec())
   1.902 +                return Blit32to565PixelAlphaAltivec;
   1.903 +            else
   1.904 +#endif
   1.905 +                if (sf->BytesPerPixel == 4 && sf->Amask == 0xff000000
   1.906 +                    && sf->Gmask == 0xff00
   1.907 +                    && ((sf->Rmask == 0xff && df->Rmask == 0x1f)
   1.908 +                        || (sf->Bmask == 0xff && df->Bmask == 0x1f))) {
   1.909                  if (df->Gmask == 0x7e0)
   1.910                      return BlitARGBto565PixelAlpha;
   1.911                  else if (df->Gmask == 0x3e0)
   1.912 @@ -1272,20 +2152,35 @@
   1.913              if (sf->Rmask == df->Rmask
   1.914                  && sf->Gmask == df->Gmask
   1.915                  && sf->Bmask == df->Bmask && sf->BytesPerPixel == 4) {
   1.916 -#if defined(__MMX__)
   1.917 +#if defined(__MMX__) || defined(__3dNOW__)
   1.918                  if (sf->Rshift % 8 == 0
   1.919                      && sf->Gshift % 8 == 0
   1.920                      && sf->Bshift % 8 == 0
   1.921                      && sf->Ashift % 8 == 0 && sf->Aloss == 0) {
   1.922 +#ifdef __3dNOW__
   1.923 +                    if (SDL_Has3DNow())
   1.924 +                        return BlitRGBtoRGBPixelAlphaMMX3DNOW;
   1.925 +#endif
   1.926 +#ifdef __MMX__
   1.927                      if (SDL_HasMMX())
   1.928                          return BlitRGBtoRGBPixelAlphaMMX;
   1.929 +#endif
   1.930                  }
   1.931 -#endif /* __MMX__ */
   1.932 +#endif /* __MMX__ || __3dNOW__ */
   1.933                  if (sf->Amask == 0xff000000) {
   1.934 +#if SDL_ALTIVEC_BLITTERS
   1.935 +                    if (SDL_HasAltiVec())
   1.936 +                        return BlitRGBtoRGBPixelAlphaAltivec;
   1.937 +#endif
   1.938                      return BlitRGBtoRGBPixelAlpha;
   1.939                  }
   1.940              }
   1.941 -            return BlitNtoNPixelAlpha;
   1.942 +#if SDL_ALTIVEC_BLITTERS
   1.943 +            if (sf->Amask && sf->BytesPerPixel == 4 && SDL_HasAltiVec())
   1.944 +                return Blit32to32PixelAlphaAltivec;
   1.945 +            else
   1.946 +#endif
   1.947 +                return BlitNtoNPixelAlpha;
   1.948  
   1.949          case 3:
   1.950          default:
   1.951 @@ -1331,10 +2226,19 @@
   1.952                          return BlitRGBtoRGBSurfaceAlphaMMX;
   1.953  #endif
   1.954                      if ((sf->Rmask | sf->Gmask | sf->Bmask) == 0xffffff) {
   1.955 +#if SDL_ALTIVEC_BLITTERS
   1.956 +                        if (SDL_HasAltiVec())
   1.957 +                            return BlitRGBtoRGBSurfaceAlphaAltivec;
   1.958 +#endif
   1.959                          return BlitRGBtoRGBSurfaceAlpha;
   1.960                      }
   1.961                  }
   1.962 -                return BlitNtoNSurfaceAlpha;
   1.963 +#if SDL_ALTIVEC_BLITTERS
   1.964 +                if ((sf->BytesPerPixel == 4) && SDL_HasAltiVec())
   1.965 +                    return Blit32to32SurfaceAlphaAltivec;
   1.966 +                else
   1.967 +#endif
   1.968 +                    return BlitNtoNSurfaceAlpha;
   1.969  
   1.970              case 3:
   1.971              default:
   1.972 @@ -1348,6 +2252,12 @@
   1.973              if (df->BytesPerPixel == 1)
   1.974                  return BlitNto1SurfaceAlphaKey;
   1.975              else
   1.976 +#if SDL_ALTIVEC_BLITTERS
   1.977 +            if (sf->BytesPerPixel == 4 && df->BytesPerPixel == 4 &&
   1.978 +                    SDL_HasAltiVec())
   1.979 +                return Blit32to32SurfaceAlphaKeyAltivec;
   1.980 +            else
   1.981 +#endif
   1.982                  return BlitNtoNSurfaceAlphaKey;
   1.983          }
   1.984          break;