gimp-composite-sse.c 69.8 KB
Newer Older
1 2 3
/* -*- mode: c tab-width: 2; c-basic-indent: 2; indent-tabs-mode: nil -*-
 *
 * The GIMP -- an image manipulation program
4 5
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
6
 *
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/* Much of the content of this file are derivative works of David
 * Monniaux which are Copyright (C) 1999, 2001 David Monniaux
 * Tip-o-the-hat to David for pioneering this effort.
 *
 * All of these functions use the mmx and sse registers and expect
 * them to remain intact across multiple asm() constructs.  This may
 * not work in the future, if the compiler allocates mmx/sse registers
 * for it's own use. XXX
 */

#include "config.h"

#include <stdio.h>

#include <glib-object.h>

#include "base/base-types.h"
40
#include "base/cpu-accel.h"
41 42 43

#include "gimp-composite.h"
#include "gimp-composite-sse.h"
44
#include "gimp-composite-x86.h"
45

46 47 48 49
#if defined(USE_SSE)
#if defined(ARCH_X86)
#if __GNUC__ >= 3

50 51 52 53 54 55
#define pminub(src,dst,tmp)  "pminub " "%%" #src ", %%" #dst
#define pmaxub(src,dst,tmp)  "pmaxub " "%%" #src ", %%" #dst

/*
 * Double-word divide.  Adjusted for subsequent unsigned packing
 * (high-order bit of each word is cleared)
56
 * Clobbers eax, ecx edx
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
 */
#define pdivwX(dividend,divisor,quotient) "movd %%" #dividend ",%%eax; " \
                                          "movd %%" #divisor  ",%%ecx; " \
                                          "xorl %%edx,%%edx; "           \
                                          "divw %%cx; "                  \
                                          "roll $16, %%eax; "            \
                                          "roll $16, %%ecx; "            \
                                          "xorl %%edx,%%edx; "           \
                                          "divw %%cx; "                  \
                                          "btr $15, %%eax; "             \
                                          "roll $16, %%eax; "            \
                                          "btr $15, %%eax; "             \
                                          "movd %%eax,%%" #quotient ";"

/*
 * Quadword divide.  No adjustment for subsequent unsigned packing
 * (high-order bit of each word is left alone)
 */
#define pdivwqX(dividend,divisor,quotient) "movd   %%" #dividend ",%%eax; " \
                                          "movd   %%" #divisor  ",%%ecx; " \
                                          "xorl   %%edx,%%edx; "           \
                                          "divw   %%cx; "                  \
                                          "roll   $16, %%eax; "            \
                                          "roll   $16, %%ecx; "            \
                                          "xorl   %%edx,%%edx; "           \
                                          "divw   %%cx; "                  \
                                          "roll   $16, %%eax; "            \
                                          "movd   %%eax,%%" #quotient "; " \
                                          "psrlq $32,%%" #dividend ";"     \
                                          "psrlq $32,%%" #divisor ";"      \
                                          "movd   %%" #dividend ",%%eax; " \
                                          "movd   %%" #divisor  ",%%ecx; " \
                                          "xorl   %%edx,%%edx; "           \
                                          "divw   %%cx; "                  \
                                          "roll   $16, %%eax; "            \
                                          "roll   $16, %%ecx; "            \
                                          "xorl   %%edx,%%edx; "           \
                                          "divw   %%cx; "                  \
                                          "roll   $16, %%eax; "            \
                                          "movd   %%eax,%%" #divisor ";"   \
                                          "psllq  $32,%%" #divisor ";"     \
                                          "por    %%" #divisor ",%%" #quotient ";"
99

100 101 102 103 104 105 106 107 108 109 110 111 112
/*
 * Quadword divide.  Adjusted for subsequent unsigned packing
 * (high-order bit of each word is cleared)
 */
#define pdivwuqX(dividend,divisor,quotient) \
                                          pdivwX(dividend,divisor,quotient) \
                                            "psrlq  $32,%%" #dividend ";"   \
                                            "psrlq  $32,%%" #divisor ";"    \
                                          pdivwX(dividend,divisor,quotient) \
                                          "movd   %%eax,%%" #divisor ";"    \
                                            "psllq  $32,%%" #divisor ";"    \
                                            "por    %%" #divisor ",%%" #quotient ";"

113
/* equivalent to the INT_MULT() macro in gimp-composite-generic.c */
114 115 116
/*
 * opr2 = INT_MULT(opr1, opr2, t)
 *
117
 * Operates across quad-words using x86 word (16bit) value.
118 119
 * Result is left in opr2
 *
120 121 122
 * opr1 = opr1 * opr2 + w128
 * opr2 = opr1
 * opr2 = ((opr2 >> 8) + opr1) >> 8
123 124 125 126 127 128 129 130
 */
#define pmulwX(opr1,opr2,w128) \
                  "\tpmullw    %%"#opr2", %%"#opr1"; " \
                  "\tpaddw     %%"#w128", %%"#opr1"; " \
                  "\tmovq      %%"#opr1", %%"#opr2"; " \
                  "\tpsrlw     $8,        %%"#opr2"; " \
                  "\tpaddw     %%"#opr1", %%"#opr2"; " \
                  "\tpsrlw     $8,        %%"#opr2"\n"
131

132 133 134 135 136 137 138 139 140
/* a = INT_MULT(a,b) */
#define mmx_int_mult(a,b,w128) \
                  "\tpmullw    %%"#b",    %%"#a"; " \
                  "\tpaddw     %%"#w128", %%"#a"; " \
                  "\tmovq      %%"#a",    %%"#b"; " \
                  "\tpsrlw     $8,        %%"#b"; " \
                  "\tpaddw     %%"#a",    %%"#b"; " \
                  "\tpsrlw     $8,        %%"#b"\n"

141 142 143 144 145 146 147 148
static const guint32 rgba8_alpha_mask_64[2] = { 0xFF000000, 0xFF000000 };
static const guint32 rgba8_b1_64[2] =         { 0x01010101, 0x01010101 };
static const guint32 rgba8_b255_64[2] =       { 0xFFFFFFFF, 0xFFFFFFFF };
static const guint32 rgba8_w1_64[2] =         { 0x00010001, 0x00010001 };
static const guint32 rgba8_w2_64[2] =         { 0x00020002, 0x00020002 };
static const guint32 rgba8_w128_64[2] =       { 0x00800080, 0x00800080 };
static const guint32 rgba8_w256_64[2] =       { 0x01000100, 0x01000100 };
static const guint32 rgba8_w255_64[2] =       { 0X00FF00FF, 0X00FF00FF };
149

150 151 152 153
static const guint32 va8_alpha_mask[2] =   { 0xFF00FF00, 0xFF00FF00 };
static const guint32 va8_b255[2] =         { 0xFFFFFFFF, 0xFFFFFFFF };
static const guint32 va8_w1[2] =           { 0x00010001, 0x00010001 };
static const guint32 va8_w255[2] =         { 0x00FF00FF, 0x00FF00FF };
154

155 156 157 158
/*
 *
 */
void
159
gimp_composite_addition_rgba8_rgba8_rgba8_sse (GimpCompositeContext *_op)
160 161 162
{
  GimpCompositeContext op = *_op;

163
  asm volatile ("movq    %0,%%mm0"
164
                : /* empty */
165
                : "m" (*rgba8_alpha_mask_64)
166
                : "%mm0");
167

168
  for (; op.n_pixels >= 2; op.n_pixels -= 2)
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    {
      asm ("  movq       %1, %%mm2\n"
           "\tmovq       %2, %%mm3\n"
           "\tmovq    %%mm2, %%mm4\n"
           "\tpaddusb %%mm3, %%mm4\n"
           "\tmovq    %%mm0, %%mm1\n"
           "\tpandn   %%mm4, %%mm1\n"
           "\tpminub  %%mm3, %%mm2\n"
           "\tpand    %%mm0, %%mm2\n"
           "\tpor     %%mm2, %%mm1\n"
           "\tmovq    %%mm1, %0\n"
           : "=m" (*op.D)
           : "m" (*op.A), "m" (*op.B)
           : "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7");
      op.A += 8;
      op.B += 8;
      op.D += 8;
    }
187

188
  if (op.n_pixels)
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    {
      asm volatile ("  movd       %1, %%mm2\n"
                    "\tmovd       %2, %%mm3\n"
                    "\tmovq    %%mm2, %%mm4\n"
                    "\tpaddusb %%mm3, %%mm4\n"
                    "\tmovq    %%mm0, %%mm1\n"
                    "\tpandn   %%mm4, %%mm1\n"
                    "\tpminub  %%mm3, %%mm2\n"
                    "\tpand    %%mm0, %%mm2\n"
                    "\tpor     %%mm2, %%mm1\n"
                    "\tmovd    %%mm1, %0\n"
                    : "=m" (*op.D)
                    : "m" (*op.A), "m" (*op.B)
                    : "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7");
    }
204

205 206 207
  asm("emms");
}

208

209 210
void
gimp_composite_burn_rgba8_rgba8_rgba8_sse (GimpCompositeContext *_op)
211 212
{
  GimpCompositeContext op = *_op;
213

214
  for (; op.n_pixels >= 2; op.n_pixels -= 2)
215 216 217
    {
      asm ("  movq         %1,%%mm0\n"
           "\tmovq         %2,%%mm1\n"
218

219 220 221 222
           "\tmovq         %3,%%mm2\n"
           "\tpsubb     %%mm0,%%mm2\n" /* mm2 = 255 - A */
           "\tpxor      %%mm4,%%mm4\n"
           "\tpunpcklbw %%mm2,%%mm4\n" /* mm4 = (255- A) * 256  */
223

224 225 226 227 228
           "\tmovq      %%mm1,%%mm3\n"
           "\tpxor      %%mm5,%%mm5\n"
           "\tpunpcklbw %%mm5,%%mm3\n"
           "\tmovq         %4,%%mm5\n"
           "\tpaddusw   %%mm3,%%mm5\n" /* mm5 = B + 1 */
229

230
           "\t" pdivwqX(mm4,mm5,mm7) "\n"
231

232 233 234 235
           "\tmovq         %3,%%mm2\n"
           "\tpsubb     %%mm0,%%mm2\n" /* mm2 = 255 - A */
           "\tpxor      %%mm4,%%mm4\n"
           "\tpunpckhbw %%mm2,%%mm4\n" /* mm4 = (255- A) * 256  */
236

237 238 239 240 241 242
           "\tmovq      %%mm1,%%mm3\n"
           "\tpxor      %%mm5,%%mm5\n"
           "\tpunpckhbw %%mm5,%%mm3\n"
           "\tmovq         %4,%%mm5\n"
           "\tpaddusw   %%mm3,%%mm5\n" /* mm5 = B + 1 */
           "\t" pdivwqX(mm4,mm5,mm6) "\n"
243

244 245 246 247
           "\tmovq         %5,%%mm4\n"
           "\tmovq      %%mm4,%%mm5\n"
           "\tpsubusw   %%mm6,%%mm4\n"
           "\tpsubusw   %%mm7,%%mm5\n"
248

249
           "\tpackuswb  %%mm4,%%mm5\n"
250

251
           "\tpminub    %%mm0,%%mm1\n" /* mm1 = min(mm0,mm1) clobber mm3 */
252

253 254
           "\tmovq         %6,%%mm7\n"
           "\tpand      %%mm7,%%mm1\n" /* mm1 = mm7 & alpha_mask */
255

256 257
           "\tpandn     %%mm5,%%mm7\n" /* mm7 = ~mm7 & mm5 */
           "\tpor       %%mm1,%%mm7\n" /* mm7 = mm7 | mm1 */
258

259 260 261 262 263 264 265 266
           "\tmovq      %%mm7,%0\n"
           : "=m" (*op.D)
           : "m" (*op.A), "m" (*op.B), "m" (*rgba8_b255_64), "m" (*rgba8_w1_64), "m" (*rgba8_w255_64), "m" (*rgba8_alpha_mask_64)
           : "0", "1", "2", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7");
      op.A += 8;
      op.B += 8;
      op.D += 8;
    }
267

268
  if (op.n_pixels)
269 270 271
    {
      asm volatile ("  movd         %1,%%mm0\n"
                    "\tmovd         %2,%%mm1\n"
272

273 274 275 276
                    "\tmovq         %3,%%mm2\n"
                    "\tpsubb     %%mm0,%%mm2\n" /* mm2 = 255 - A */
                    "\tpxor      %%mm4,%%mm4\n"
                    "\tpunpcklbw %%mm2,%%mm4\n" /* mm4 = (255- A) * 256  */
277

278 279 280 281 282
                    "\tmovq      %%mm1,%%mm3\n"
                    "\tpxor      %%mm5,%%mm5\n"
                    "\tpunpcklbw %%mm5,%%mm3\n"
                    "\tmovq         %4,%%mm5\n"
                    "\tpaddusw   %%mm3,%%mm5\n" /* mm5 = B + 1 */
283

284
                    "\t" pdivwqX(mm4,mm5,mm7) "\n"
285

286 287 288 289
                    "\tmovq         %3,%%mm2\n"
                    "\tpsubb     %%mm0,%%mm2\n" /* mm2 = 255 - A */
                    "\tpxor      %%mm4,%%mm4\n"
                    "\tpunpckhbw %%mm2,%%mm4\n" /* mm4 = (255- A) * 256  */
290

291 292 293 294 295 296
                    "\tmovq      %%mm1,%%mm3\n"
                    "\tpxor      %%mm5,%%mm5\n"
                    "\tpunpckhbw %%mm5,%%mm3\n"
                    "\tmovq         %4,%%mm5\n"
                    "\tpaddusw   %%mm3,%%mm5\n" /* mm5 = B + 1 */
                    "\t" pdivwqX(mm4,mm5,mm6) "\n"
297

298 299 300 301
                    "\tmovq         %5,%%mm4\n"
                    "\tmovq      %%mm4,%%mm5\n"
                    "\tpsubusw   %%mm6,%%mm4\n"
                    "\tpsubusw   %%mm7,%%mm5\n"
302

303
                    "\tpackuswb  %%mm4,%%mm5\n"
304

305
                    "\tpminub    %%mm0,%%mm1\n" /* mm1 = min(mm0,mm1) clobber mm3 */
306

307 308
                    "\tmovq         %6,%%mm7\n"
                    "\tpand      %%mm7,%%mm1\n" /* mm1 = mm7 & alpha_mask */
309

310 311
                    "\tpandn     %%mm5,%%mm7\n" /* mm7 = ~mm7 & mm5 */
                    "\tpor       %%mm1,%%mm7\n" /* mm7 = mm7 | mm1 */
312

313 314 315 316 317
                    "\tmovd      %%mm7,%0\n"
                    : "=m" (*op.D)
                    : "m" (*op.A), "m" (*op.B), "m" (*rgba8_b255_64), "m" (*rgba8_w1_64), "m" (*rgba8_w255_64), "m" (*rgba8_alpha_mask_64)
                    : "0", "1", "2", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7");
    }
318

319 320 321 322 323
  asm("emms");
}


void
324
gimp_composite_darken_rgba8_rgba8_rgba8_sse (GimpCompositeContext *_op)
325 326
{
  GimpCompositeContext op = *_op;
327

328
  for (; op.n_pixels >= 2; op.n_pixels -= 2)
329 330 331 332 333 334 335 336 337 338 339
    {
      asm volatile ("  movq       %1, %%mm2\n"
                    "\tpminub     %2, %%mm2\n"
                    "\tmovq    %%mm2, %0\n"
                    : "=m" (*op.D)
                    : "m" (*op.A), "m" (*op.B)
                    : "%mm1", "%mm2", "%mm3", "%mm4");
      op.A += 8;
      op.B += 8;
      op.D += 8;
    }
340

341
  if (op.n_pixels)
342 343 344 345 346 347 348 349 350
    {
      asm volatile ("  movd       %1, %%mm2\n"
                    "\tmovd       %2, %%mm3\n"
                    "\tpminub  %%mm3, %%mm2\n"
                    "\tmovd    %%mm2, %0\n"
                    : "=m" (*op.D)
                    : "m" (*op.A), "m" (*op.B)
                    : "%mm2", "%mm3", "%mm4");
    }
351

352 353 354 355
  asm("emms");
}

void
356
gimp_composite_difference_rgba8_rgba8_rgba8_sse (GimpCompositeContext *_op)
357 358
{
  GimpCompositeContext op = *_op;
359

360
  asm volatile ("movq    %0,%%mm0"     :  : "m" (*rgba8_alpha_mask_64) : "%mm0");
361

362
  for (; op.n_pixels >= 2; op.n_pixels -= 2)
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
    {
      asm volatile ("  movq       %1, %%mm2\n"
                    "\tmovq       %2, %%mm3\n"
                    "\tmovq    %%mm2, %%mm4\n"
                    "\tmovq    %%mm3, %%mm5\n"
                    "\tpsubusb %%mm3, %%mm4\n"
                    "\tpsubusb %%mm2, %%mm5\n"
                    "\tpaddb   %%mm5, %%mm4\n"
                    "\tmovq    %%mm0, %%mm1\n"
                    "\tpandn   %%mm4, %%mm1\n"
                    "\tpminub  %%mm3, %%mm2\n"
                    "\tpand    %%mm0, %%mm2\n"
                    "\tpor     %%mm2, %%mm1\n"
                    "\tmovq    %%mm1, %0\n"
                    : "=m" (*op.D)
                    : "m" (*op.A), "m" (*op.B)
                    : "%mm1", "%mm2", "%mm3", "%mm4");
      op.A += 8;
      op.B += 8;
      op.D += 8;
    }
384

385
  if (op.n_pixels)
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    {
      asm volatile ("  movd       %1, %%mm2\n"
                    "\tmovd       %2, %%mm3\n"
                    "\tmovq    %%mm2, %%mm4\n"
                    "\tmovq    %%mm3, %%mm5\n"
                    "\tpsubusb %%mm3, %%mm4\n"
                    "\tpsubusb %%mm2, %%mm5\n"
                    "\tpaddb   %%mm5, %%mm4\n"
                    "\tmovq    %%mm0, %%mm1\n"
                    "\tpandn   %%mm4, %%mm1\n"
                    "\tpminub  %%mm3, %%mm2\n"
                    "\tpand    %%mm0, %%mm2\n"
                    "\tpor     %%mm2, %%mm1\n"
                    "\tmovd    %%mm1, %0\n"
                    : "=m" (*op.D)
                    : "m" (*op.A), "m" (*op.B), "m" (*op.D)
                    : "%mm1", "%mm2", "%mm3", "%mm4");
    }
404

405 406 407 408
  asm("emms");
}

void
409
gimp_composite_divide_rgba8_rgba8_rgba8_sse (GimpCompositeContext *_op)
410 411
{
  GimpCompositeContext op = *_op;
412

413 414 415 416 417
  asm volatile ("  movq    %0, %%mm0\n"
                "\tmovq    %1, %%mm7\n"
                :
                : "m" (*rgba8_alpha_mask_64), "m" (*rgba8_w1_64)
                : "%mm0", "%mm7");
418

419
  for (; op.n_pixels >= 2; op.n_pixels -= 2)
420 421 422 423 424
    {
      asm volatile ("  movq         %0,%%mm0\n"
                    "\tmovq         %1,%%mm1\n"
                    "\tpxor      %%mm2,%%mm2\n"
                    "\tpunpcklbw %%mm0,%%mm2\n" /* mm2 = A*256 */
425

426 427 428 429
                    "\tmovq      %%mm1,%%mm3\n"
                    "\tpxor      %%mm5,%%mm5\n"
                    "\tpunpcklbw %%mm5,%%mm3\n"
                    "\tpaddw     %%mm7,%%mm3\n" /* mm3 = B+1 */
430

431
                    "\t" pdivwuqX(mm2,mm3,mm5) "\n" /* mm5 = (A*256)/(B+1) */
432

433 434
                    "\tpxor      %%mm2,%%mm2\n"
                    "\tpunpckhbw %%mm0,%%mm2\n" /* mm2 = A*256 */
435

436 437 438 439
                    "\tmovq      %%mm1,%%mm3\n"
                    "\tpxor      %%mm6,%%mm6\n"
                    "\tpunpckhbw %%mm6,%%mm3\n"
                    "\tpaddw     %%mm7,%%mm3\n" /* mm3 = B+1 */
440

441
                    "\t" pdivwuqX(mm2,mm3,mm4) "\n" /* mm4 = (A*256)/(B+1) */
442

443
                    "\tpackuswb  %%mm4,%%mm5\n" /* expects mm4 and mm5 to be signed values */
444

445 446 447
                    "\tpminub    %%mm0,%%mm1\n"
                    "\tmovq         %3,%%mm3\n"
                    "\tmovq      %%mm3,%%mm2\n"
448

449
                    "\tpandn     %%mm5,%%mm3\n"
450

451 452
                    "\tpand      %%mm2,%%mm1\n"
                    "\tpor       %%mm1,%%mm3\n"
453

454 455 456 457 458 459 460 461
                    "\tmovq      %%mm3,%2\n"
                    : /* empty */
                    : "m" (*op.A), "m" (*op.B), "m" (*op.D), "m" (*rgba8_alpha_mask_64)
                    : "%eax", "%ecx", "%edx", "0", "1", "2", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5");
      op.A += 8;
      op.B += 8;
      op.D += 8;
    }
462

463
  if (op.n_pixels)
464 465 466
    {
      asm volatile ("  movd         %0,%%mm0\n"
                    "\tmovd         %1,%%mm1\n"
467

468 469
                    "\tpxor      %%mm2,%%mm2\n"
                    "\tpunpcklbw %%mm0,%%mm2\n" /* mm2 = A*256 */
470

471 472 473 474
                    "\tmovq      %%mm1,%%mm3\n"
                    "\tpxor      %%mm5,%%mm5\n"
                    "\tpunpcklbw %%mm5,%%mm3\n"
                    "\tpaddw     %%mm7,%%mm3\n" /* mm3 = B+1 */
475

476
                    "\t" pdivwuqX(mm2,mm3,mm5) "\n" /* mm5 = (A*256)/(B+1) */
477

478 479
                    "\tpxor      %%mm2,%%mm2\n"
                    "\tpunpckhbw %%mm0,%%mm2\n" /* mm2 = A*256 */
480

481 482 483 484
                    "\tmovq      %%mm1,%%mm3\n"
                    "\tpxor      %%mm6,%%mm6\n"
                    "\tpunpckhbw %%mm6,%%mm3\n"
                    "\tpaddw     %%mm7,%%mm3\n" /* mm3 = B+1 */
485

486
                    "\t" pdivwuqX(mm2,mm3,mm4) "\n" /* mm4 = (A*256)/(B+1) */
487

488
                    "\tpackuswb  %%mm4,%%mm5\n" /* expects mm4 and mm5 to be signed values */
489

490 491 492
                    "\tpminub    %%mm0,%%mm1\n"
                    "\tmovq         %3,%%mm3\n"
                    "\tmovq      %%mm3,%%mm2\n"
493

494
                    "\tpandn     %%mm5,%%mm3\n"
495

496 497
                    "\tpand      %%mm2,%%mm1\n"
                    "\tpor       %%mm1,%%mm3\n"
498

499 500 501 502 503
                    "\tmovd      %%mm3,%2\n"
                    : /* empty */
                    : "m" (*op.A), "m" (*op.B), "m" (*op.D), "m" (*rgba8_alpha_mask_64)
                    : "%eax", "%ecx", "%edx", "0", "1", "2", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5");
    }
504

505 506 507 508
  asm("emms");
}

void
509
gimp_composite_dodge_rgba8_rgba8_rgba8_sse (GimpCompositeContext *_op)
510 511
{
  GimpCompositeContext op = *_op;
512

513
  for (; op.n_pixels >= 2; op.n_pixels -= 2)
514 515 516 517 518 519 520
    {
      asm volatile ("  movq         %0,%%mm0\n"
                    "\tmovq         %1,%%mm1\n"
                    "\tmovq      %%mm1,%%mm3\n"
                    "\tpxor      %%mm2,%%mm2\n"
                    "\tpunpcklbw %%mm2,%%mm3\n"
                    "\tpunpcklbw %%mm0,%%mm2\n"
521

522 523
                    "\tmovq         %3,%%mm4\n"
                    "\tpsubw     %%mm3,%%mm4\n"
524

525
                    "\t" pdivwuqX(mm2,mm4,mm5) "\n"
526

527 528 529 530
                    "\tmovq      %%mm1,%%mm3\n"
                    "\tpxor      %%mm2,%%mm2\n"
                    "\tpunpckhbw %%mm2,%%mm3\n"
                    "\tpunpckhbw %%mm0,%%mm2\n"
531

532 533
                    "\tmovq         %3,%%mm4\n"
                    "\tpsubw     %%mm3,%%mm4\n"
534

535
                    "\t" pdivwuqX(mm2,mm4,mm6) "\n"
536

537
                    "\tpackuswb  %%mm6,%%mm5\n"
538

539 540 541 542 543
                    "\tmovq         %4,%%mm6\n"
                    "\tmovq      %%mm1,%%mm7\n"
                    "\t" pminub(mm0,mm7,mm2) "\n"
                    "\tpand      %%mm6,%%mm7\n"
                    "\tpandn     %%mm5,%%mm6\n"
544

545
                    "\tpor       %%mm6,%%mm7\n"
546

547 548 549 550 551 552 553 554
                    "\tmovq      %%mm7,%2\n"
                    : /* empty */
                    : "m" (*op.A), "m" (*op.B), "m" (*op.D), "m" (*rgba8_w256_64), "m" (*rgba8_alpha_mask_64)
                    : "0", "1", "2", "%eax", "%ecx", "%edx", "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7");
      op.A += 8;
      op.B += 8;
      op.D += 8;
    }
555

556
  if (op.n_pixels)
557 558 559 560 561 562 563
    {
      asm volatile ("  movd         %0,%%mm0\n"
                    "\tmovq         %1,%%mm1\n"
                    "\tmovq      %%mm1,%%mm3\n"
                    "\tpxor      %%mm2,%%mm2\n"
                    "\tpunpcklbw %%mm2,%%mm3\n"
                    "\tpunpcklbw %%mm0,%%mm2\n"
564

565 566
                    "\tmovq         %3,%%mm4\n"
                    "\tpsubw     %%mm3,%%mm4\n"
567

568
                    "\t" pdivwuqX(mm2,mm4,mm5) "\n"
569

570 571 572 573
                    "\tmovq      %%mm1,%%mm3\n"
                    "\tpxor      %%mm2,%%mm2\n"
                    "\tpunpckhbw %%mm2,%%mm3\n"
                    "\tpunpckhbw %%mm0,%%mm2\n"
574

575 576
                    "\tmovq         %3,%%mm4\n"
                    "\tpsubw     %%mm3,%%mm4\n"
577

578
                    "\t" pdivwuqX(mm2,mm4,mm6) "\n"
579

580
                    "\tpackuswb  %%mm6,%%mm5\n"
581

582 583 584 585 586
                    "\tmovq         %4,%%mm6\n"
                    "\tmovq      %%mm1,%%mm7\n"
                    "\tpminub    %%mm0,%%mm7\n"
                    "\tpand      %%mm6,%%mm7\n"
                    "\tpandn     %%mm5,%%mm6\n"
587

588
                    "\tpor       %%mm6,%%mm7\n"
589

590 591 592 593 594
                    "\tmovd      %%mm7,%2\n"
                    : /* empty */
                    : "m" (*op.A), "m" (*op.B), "m" (*op.D), "m" (*rgba8_w256_64), "m" (*rgba8_alpha_mask_64)
                    : "0", "1", "2", "%eax", "%ecx", "%edx", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5");
    }
595

596 597 598 599
  asm("emms");
}

void
600
gimp_composite_grain_extract_rgba8_rgba8_rgba8_sse (GimpCompositeContext *_op)
601
{
602
  GimpCompositeContext op = *_op;
603

604 605 606 607 608 609
  asm volatile ("  movq       %0,%%mm0\n"
                "\tpxor    %%mm6,%%mm6\n"
                "\tmovq       %1,%%mm7\n"
                : /* empty */
                : "m" (*rgba8_alpha_mask_64), "m" (*rgba8_w128_64)
                : "%mm0", "%mm6", "%mm7");
610

611

612
  for (; op.n_pixels >= 2; op.n_pixels -= 2)
613 614 615 616 617 618 619 620
    {
      asm volatile ("  movq         %1,%%mm2\n"
                    "\tmovq         %2,%%mm3\n"
                    mmx_low_bytes_to_words(mm2,mm4,mm6)
                    mmx_low_bytes_to_words(mm3,mm5,mm6)
                    "\tpsubw     %%mm5,%%mm4\n"
                    "\tpaddw     %%mm7,%%mm4\n"
                    "\tmovq      %%mm4,%%mm1\n"
621

622 623
                    mmx_high_bytes_to_words(mm2,mm4,mm6)
                    mmx_high_bytes_to_words(mm3,mm5,mm6)
624

625 626
                    "\tpsubw     %%mm5,%%mm4\n"
                    "\tpaddw     %%mm7,%%mm4\n"
627

628 629
                    "\tpackuswb  %%mm4,%%mm1\n"
                    "\tmovq      %%mm1,%%mm4\n"
630

631 632
                    "\tmovq      %%mm0,%%mm1\n"
                    "\tpandn     %%mm4,%%mm1\n"
633

634 635
                    "\tpminub    %%mm3,%%mm2\n"
                    "\tpand      %%mm0,%%mm2\n"
636

637 638 639 640 641 642 643 644 645
                    "\tpor       %%mm2,%%mm1\n"
                    "\tmovq      %%mm1,%0\n"
                    : "+m" (*op.D)
                    : "m" (*op.A), "m" (*op.B)
                    : "%mm1", "%mm2", "%mm3", "%mm4");
      op.A += 8;
      op.B += 8;
      op.D += 8;
    }
646

647
  if (op.n_pixels)
648 649 650
    {
      asm volatile ("  movd         %1, %%mm2\n"
                    "\tmovd         %2, %%mm3\n"
651

652 653
                    mmx_low_bytes_to_words(mm2,mm4,mm6)
                    mmx_low_bytes_to_words(mm3,mm5,mm6)
654

655 656 657
                    "\tpsubw     %%mm5, %%mm4\n"
                    "\tpaddw     %%mm7, %%mm4\n"
                    "\tmovq      %%mm4, %%mm1\n"
658

659
                    "\tpackuswb  %%mm6, %%mm1\n"
660

661
                    "\tmovq      %%mm1, %%mm4\n"
662

663 664
                    "\tmovq      %%mm0, %%mm1\n"
                    "\tpandn     %%mm4, %%mm1\n"
665

666 667
                    "\tpminub    %%mm3, %%mm2\n"
                    "\tpand      %%mm0, %%mm2\n"
668

669 670 671 672 673 674
                    "\tpor       %%mm2, %%mm1\n"
                    "\tmovd      %%mm1, %0\n"
                    : "+m" (*op.D)
                    : "m" (*op.A), "m" (*op.B)
                    : "%mm1", "%mm2", "%mm3", "%mm4");
    }
675

676 677 678 679
  asm("emms");
}

void
680
gimp_composite_grain_merge_rgba8_rgba8_rgba8_sse (GimpCompositeContext *_op)
681 682
{
  GimpCompositeContext op = *_op;
683

684 685 686 687
  asm volatile ("movq    %0, %%mm0\n"
                "pxor    %%mm6, %%mm6\n"
                "movq    %1, %%mm7\n"
                : /* empty */
688
                : "m" (*rgba8_alpha_mask_64), "m" (*rgba8_w128_64)
689
                : "%mm0", "%mm6", "%mm7");
690

691
  for (; op.n_pixels >= 2; op.n_pixels -= 2)
692 693 694
    {
      asm volatile ("  movq         %1, %%mm2\n"
                    "\tmovq         %2, %%mm3\n"
695

696 697 698 699
                    mmx_low_bytes_to_words(mm2,mm4,mm6)
                    mmx_low_bytes_to_words(mm3,mm5,mm6)
                    "\tpaddw     %%mm5, %%mm4\n"
                    "\tpsubw     %%mm7, %%mm4\n"
700

701 702 703 704
                    mmx_high_bytes_to_words(mm2,mm1,mm6)
                    mmx_high_bytes_to_words(mm3,mm5,mm6)
                    "\tpaddw     %%mm5, %%mm1\n"
                    "\tpsubw     %%mm7, %%mm1\n"
705

706
                    "\tpackuswb  %%mm1, %%mm4\n"
707

708 709
                    "\t" pminub(mm3,mm2,mm5) "\n"
                    "\tpand      %%mm0, %%mm2\n"
710

711 712 713 714 715 716 717 718 719 720 721
                    "\tmovq      %%mm0, %%mm1\n"
                    "\tpandn     %%mm4, %%mm1\n"
                    "\tpor       %%mm2, %%mm1\n"
                    "\tmovq      %%mm1, %0\n"
                    : "+m" (*op.D)
                    : "m" (*op.A), "m" (*op.B)
                    : "0", "1", "2", "%mm1", "%mm2", "%mm3", "%mm4");
      op.A += 8;
      op.B += 8;
      op.D += 8;
    }
722

723
  if (op.n_pixels)
724 725 726
    {
      asm volatile ("  movd         %1, %%mm2\n"
                    "\tmovd         %2, %%mm3\n"
727

728 729
                    mmx_low_bytes_to_words(mm2,mm4,mm6)
                    mmx_low_bytes_to_words(mm3,mm5,mm6)
730

731 732 733 734
                    "\tpaddw     %%mm5, %%mm4\n"
                    "\tpsubw     %%mm7, %%mm4\n"
                    "\tmovq      %%mm4, %%mm1\n"
                    "\tpackuswb  %%mm6, %%mm1\n"
735

736
                    "\tmovq      %%mm1, %%mm4\n"
737

738 739
                    "\tmovq      %%mm0, %%mm1\n"
                    "\tpandn     %%mm4, %%mm1\n"
740

741 742
                    "\tpminub    %%mm3, %%mm2\n"
                    "\tpand      %%mm0, %%mm2\n"
743

744 745 746 747 748 749
                    "\tpor       %%mm2, %%mm1\n"
                    "\tmovd      %%mm1, %0\n"
                    : "+m" (*op.D)
                    : "m" (*op.A), "m" (*op.B)
                    : "0", "1", "2", "%mm1", "%mm2", "%mm3", "%mm4");
    }
750

751 752 753 754
  asm("emms");
}

void
755
gimp_composite_lighten_rgba8_rgba8_rgba8_sse (GimpCompositeContext *_op)
756 757
{
  GimpCompositeContext op = *_op;
758

759
  asm volatile ("movq    %0,%%mm0"     :  : "m" (*rgba8_alpha_mask_64) : "%mm0");
760

761
  for (; op.n_pixels >= 2; op.n_pixels -= 2)
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
    {
      asm volatile ("  movq       %1, %%mm2\n"
                    "\tmovq       %2, %%mm3\n"
                    "\tmovq    %%mm2, %%mm4\n"
                    "\tpmaxub  %%mm3, %%mm4\n"
                    "\tmovq    %%mm0, %%mm1\n"
                    "\tpandn   %%mm4, %%mm1\n"
                    "\tpminub  %%mm2, %%mm3\n"
                    "\tpand    %%mm0, %%mm3\n"
                    "\tpor     %%mm3, %%mm1\n"
                    "\tmovq    %%mm1, %0\n"
                    : "=m" (*op.D)
                    : "m" (*op.A), "m" (*op.B)
                    : "%mm1", "%mm2", "%mm3", "%mm4", "%mm5");
      op.A += 8;
      op.B += 8;
      op.D += 8;
    }
780

781
  if (op.n_pixels)
782 783 784 785 786
    {
      asm volatile ("  movd       %1, %%mm2\n"
                    "\tmovd       %2, %%mm3\n"
                    "\tmovq    %%mm2, %%mm4\n"
                    "\tpmaxub  %%mm3, %%mm4\n"
787

788 789
                    "\tmovq    %%mm0, %%mm1\n"
                    "\tpandn   %%mm4, %%mm1\n"
790

791
                    "\tpminub  %%mm2,%%mm3\n"
792

793 794 795 796 797 798 799
                    "\tpand    %%mm0, %%mm3\n"
                    "\tpor     %%mm3, %%mm1\n"
                    "\tmovd    %%mm1, %0\n"
                    : "=m" (*op.D)
                    : "m" (*op.A), "m" (*op.B)
                    : "%mm1", "%mm2", "%mm3", "%mm4", "%mm5");
    }
800

801 802 803 804
  asm("emms");
}

void
805
gimp_composite_multiply_rgba8_rgba8_rgba8_sse (GimpCompositeContext *_op)
806 807
{
  GimpCompositeContext op = *_op;
808

809 810
  asm volatile ("movq    %0,%%mm0"     :  : "m" (*rgba8_alpha_mask_64) : "%mm0");
  asm volatile ("movq    %0,%%mm7"     :  : "m" (*rgba8_w128_64) : "%mm7");
811
  asm volatile ("pxor    %%mm6,%%mm6"  :  :  : "%mm6");
812

813
  for (; op.n_pixels >= 2; op.n_pixels -= 2)
814 815 816
    {
      asm volatile ("  movq         %0, %%mm2\n"
                    "\tmovq         %1, %%mm3\n"
817

818 819 820
                    mmx_low_bytes_to_words(mm2,mm1,mm6)
                    mmx_low_bytes_to_words(mm3,mm5,mm6)
                    mmx_int_mult(mm5,mm1,mm7)
821

822 823 824
                    mmx_high_bytes_to_words(mm2,mm4,mm6)
                    mmx_high_bytes_to_words(mm3,mm5,mm6)
                    mmx_int_mult(mm5,mm4,mm7)
825

826
                    "\tpackuswb  %%mm4, %%mm1\n"
827

828 829 830 831 832 833
                    "\tmovq      %%mm0, %%mm4\n"
                    "\tpandn     %%mm1, %%mm4\n"
                    "\tmovq      %%mm4, %%mm1\n"
                    "\t" pminub(mm3,mm2,mm4) "\n"
                    "\tpand      %%mm0, %%mm2\n"
                    "\tpor       %%mm2, %%mm1\n"
834

835 836 837 838 839 840 841 842
                    "\tmovq      %%mm1, %2\n"
                    : /* empty */
                    : "m" (*op.A), "m" (*op.B), "m" (*op.D)
                    : "0", "1", "2", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5");
      op.A += 8;
      op.B += 8;
      op.D += 8;
    }
843

844
  if (op.n_pixels)
845 846 847
    {
      asm volatile ("  movd         %0, %%mm2\n"
                    "\tmovd         %1, %%mm3\n"
848

849 850 851
                    mmx_low_bytes_to_words(mm2,mm1,mm6)
                    mmx_low_bytes_to_words(mm3,mm5,mm6)
                    pmulwX(mm5,mm1,mm7)
852

853
                    "\tpackuswb  %%mm6, %%mm1\n"
854

855 856 857 858 859 860
                    "\tmovq      %%mm0, %%mm4\n"
                    "\tpandn     %%mm1, %%mm4\n"
                    "\tmovq      %%mm4, %%mm1\n"
                    "\t" pminub(mm3,mm2,mm4) "\n"
                    "\tpand      %%mm0, %%mm2\n"
                    "\tpor       %%mm2, %%mm1\n"
861

862 863 864 865 866
                    "\tmovd      %%mm1, %2\n"
                    : /* empty */
                    : "m" (*op.A), "m" (*op.B), "m" (*op.D)
                    : "0", "1", "2", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5");
    }
867

868 869 870
  asm("emms");
}

871
static void
Helvetix Victorinox's avatar
Helvetix Victorinox committed
872
sse_op_overlay(void)
873
{
874 875 876
  asm volatile (
                /* low bytes */
                mmx_low_bytes_to_words(mm3,mm5,mm0)
877 878 879 880 881
                "\tpcmpeqb   %%mm4,%%mm4\n"
                "\tpsubb     %%mm2,%%mm4\n" /* mm4 = 255 - A */
                "\tpunpcklbw %%mm0,%%mm4\n" /* mm4 = (low bytes as word) mm4 */
                "\tmovq         %0,%%mm6\n"  /* mm6 = words of value 2 */
                "\tpmullw    %%mm5,%%mm6\n" /* mm6 = 2 * low bytes of B */
882
                mmx_int_mult(mm6,mm4,mm7)    /* mm4 = INT_MULT(mm6, mm4) */
883

884 885
                /* high bytes */
                mmx_high_bytes_to_words(mm3,mm5,mm0)
886 887 888 889 890
                "\tpcmpeqb   %%mm1,%%mm1\n"
                "\tpsubb     %%mm2,%%mm1\n" /* mm1 = 255 - A */
                "\tpunpckhbw %%mm0,%%mm1\n" /* mm1 = (high bytes as word) mm1 */
                "\tmovq         %0,%%mm6\n"  /* mm6 = words of value 2 */
                "\tpmullw    %%mm5,%%mm6\n" /* mm6 = 2 * high bytes of B */
891
                mmx_int_mult(mm6,mm1,mm7)    /* mm1 = INT_MULT(mm6, mm1) */
892

893
                "\tpackuswb  %%mm1,%%mm4\n"  /* mm4 = intermediate value */
894

895 896 897 898 899 900 901 902 903 904 905 906
                mmx_low_bytes_to_words(mm4,mm5,mm0)
                mmx_low_bytes_to_words(mm2,mm6,mm0)
                "\tpaddw     %%mm6,%%mm5\n"
                mmx_int_mult(mm6,mm5,mm7)   /* mm5 = INT_MULT(mm6, mm5) low bytes */

                mmx_high_bytes_to_words(mm4,mm1,mm0)
                mmx_high_bytes_to_words(mm2,mm6,mm0)
                "\tpaddw     %%mm6,%%mm1\n"
                mmx_int_mult(mm6,mm1,mm7)   /* mm1 = INT_MULT(mm6, mm1) high bytes */

                "\tpackuswb  %%mm1,%%mm5\n"

907 908 909
                "\tmovq         %1,%%mm0\n"
                "\tmovq      %%mm0,%%mm1\n"
                "\tpandn     %%mm5,%%mm1\n"
910

911
                "\t" pminub(mm2,mm3,mm4) "\n"
912
                "\tpand      %%mm0,%%mm3\n"
913

914
                "\tpor       %%mm3,%%mm1\n"
915

916
                : /* empty */
917
                : "m" (*rgba8_w2_64), "m" (*rgba8_alpha_mask_64)
918
                );
919 920 921
}

void
922
xxxgimp_composite_overlay_rgba8_rgba8_rgba8_sse (GimpCompositeContext *_op)
923 924
{
  GimpCompositeContext op = *_op;
925

Helvetix Victorinox's avatar
Helvetix Victorinox committed
926
  asm volatile ("pxor    %%mm0,%%mm0\n"
927
                "movq       %0,%%mm7"
928
                : /* empty */
929
                : "m" (*rgba8_w128_64) : "%mm0");
930

931
  for (; op.n_pixels >= 2; op.n_pixels -= 2)
932 933 934
    {
      asm volatile ("  movq         %0,%%mm2\n"
                    "\tmovq         %1,%%mm3\n"
935

936 937 938 939 940 941 942 943
                    /* low bytes */
                    mmx_low_bytes_to_words(mm3,mm5,mm0)
                    "\tpcmpeqb   %%mm4,%%mm4\n"
                    "\tpsubb     %%mm2,%%mm4\n" /* mm4 = 255 - A */
                    "\tpunpcklbw %%mm0,%%mm4\n" /* mm4 = (low bytes as word) mm4 */
                    "\tmovq         %3,%%mm6\n"  /* mm6 = words of value 2 */
                    "\tpmullw    %%mm5,%%mm6\n" /* mm6 = 2 * low bytes of B */
                    mmx_int_mult(mm6,mm4,mm7)    /* mm4 = INT_MULT(mm6, mm4) */
944

945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
                    /* high bytes */
                    mmx_high_bytes_to_words(mm3,mm5,mm0)
                    "\tpcmpeqb   %%mm1,%%mm1\n"
                    "\tpsubb     %%mm2,%%mm1\n" /* mm1 = 255 - A */
                    "\tpunpckhbw %%mm0,%%mm1\n" /* mm1 = (high bytes as word) mm1 */
                    "\tmovq         %3,%%mm6\n"  /* mm6 = words of value 2 */
                    "\tpmullw    %%mm5,%%mm6\n" /* mm6 = 2 * high bytes of B */
                    mmx_int_mult(mm6,mm1,mm7)    /* mm1 = INT_MULT(mm6, mm1) */
                    "\tpackuswb  %%mm1,%%mm4\n"  /* mm4 = intermediate value */
                    mmx_low_bytes_to_words(mm4,mm5,mm0)
                    mmx_low_bytes_to_words(mm2,mm6,mm0)
                    "\tpaddw     %%mm6,%%mm5\n"
                    mmx_int_mult(mm6,mm5,mm7)   /* mm5 = INT_MULT(mm6, mm5) low bytes */
                    mmx_high_bytes_to_words(mm4,mm1,mm0)
                    mmx_high_bytes_to_words(mm2,mm6,mm0)
                    "\tpaddw     %%mm6,%%mm1\n"
                    mmx_int_mult(mm6,mm1,mm7)   /* mm1 = INT_MULT(mm6, mm1) high bytes */

                    "\tpackuswb  %%mm1,%%mm5\n"

                    "\tmovq         %4,%%mm0\n"
                    "\tmovq      %%mm0,%%mm1\n"
                    "\tpandn     %%mm5,%%mm1\n"

                    "\t" pminub(mm2,mm3,mm4) "\n"
                    "\tpand      %%mm0,%%mm3\n"

                    "\tpor       %%mm3,%%mm1\n"

                    "\tmovq      %%mm1,%2\n"
                    : "+m" (*op.A), "+m" (*op.B), "+m" (*op.D)
                    : "m" (*rgba8_w2_64), "m" (*rgba8_alpha_mask_64)
                    : "0", "1", "2", "%mm1", "%mm2", "%mm3", "%mm4");
      op.A += 8;
      op.B += 8;
      op.D += 8;
    }
982 983

  if (op.n_pixels)
984 985 986
    {
      asm volatile ("  movd         %0,%%mm2\n"
                    "\tmovd         %1,%%mm3\n"
987

988 989 990 991 992 993 994 995
                    /* low bytes */
                    mmx_low_bytes_to_words(mm3,mm5,mm0)
                    "\tpcmpeqb   %%mm4,%%mm4\n"
                    "\tpsubb     %%mm2,%%mm4\n" /* mm4 = 255 - A */
                    "\tpunpcklbw %%mm0,%%mm4\n" /* mm4 = (low bytes as word) mm4 */
                    "\tmovq         %3,%%mm6\n"  /* mm6 = words of value 2 */
                    "\tpmullw    %%mm5,%%mm6\n" /* mm6 = 2 * low bytes of B */
                    mmx_int_mult(mm6,mm4,mm7)    /* mm4 = INT_MULT(mm6, mm4) */
996

997 998 999 1000 1001 1002 1003 1004
                    /* high bytes */
                    mmx_high_bytes_to_words(mm3,mm5,mm0)
                    "\tpcmpeqb   %%mm1,%%mm1\n"
                    "\tpsubb     %%mm2,%%mm1\n" /* mm1 = 255 - A */
                    "\tpunpckhbw %%mm0,%%mm1\n" /* mm1 = (high bytes as word) mm1 */
                    "\tmovq         %3,%%mm6\n"  /* mm6 = words of value 2 */
                    "\tpmullw    %%mm5,%%mm6\n" /* mm6 = 2 * high bytes of B */
                    mmx_int_mult(mm6,mm1,mm7)    /* mm1 = INT_MULT(mm6, mm1) */
1005

1006
                    "\tpackuswb  %%mm1,%%mm4\n"  /* mm4 = intermediate value */
1007

1008 1009 1010 1011
                    mmx_low_bytes_to_words(mm4,mm5,mm0)
                    mmx_low_bytes_to_words(mm2,mm6,mm0)
                    "\tpaddw     %%mm6,%%mm5\n"
                    mmx_int_mult(mm6,mm5,mm7)   /* mm5 = INT_MULT(mm6, mm5) low bytes */
1012

1013 1014 1015 1016
                    mmx_high_bytes_to_words(mm4,mm1,mm0)
                    mmx_high_bytes_to_words(mm2,mm6,mm0)
                    "\tpaddw     %%mm6,%%mm1\n"
                    mmx_int_mult(mm6,mm1,mm7)   /* mm1 = INT_MULT(mm6, mm1) high bytes */
1017

1018
                    "\tpackuswb  %%mm1,%%mm5\n"
1019

1020 1021 1022
                    "\tmovq         %4,%%mm0\n"
                    "\tmovq      %%mm0,%%mm1\n"
                    "\tpandn     %%mm5,%%mm1\n"
1023

1024 1025
                    "\t" pminub(mm2,mm3,mm4) "\n"
                    "\tpand      %%mm0,%%mm3\n"
1026

1027
                    "\tpor       %%mm3,%%mm1\n"
1028

1029 1030 1031 1032 1033 1034
                    "\tmovd      %%mm1,%2\n"
                    : /* empty */
                    : "m" (*op.A), "m" (*op.B), "m" (*op.D), "m" (*rgba8_w2_64), "m" (*rgba8_alpha_mask_64)
                    : "0", "1", "2", "%mm1", "%mm2", "%mm3", "%mm4");
    }

1035 1036 1037 1038 1039
  asm("emms");
}


void
1040
gimp_composite_scale_rgba8_rgba8_rgba8_sse (GimpCompositeContext *_op)
1041 1042 1043 1044
{
  GimpCompositeContext op = *_op;

  asm volatile ("pxor    %%mm0,%%mm0\n"
1045
                "\tmovl     %0,%%eax\n"
1046
                "\tmovl  %%eax,%%ebx\n"
1047
                "\tshl     $16,%%ebx\n"
1048 1049 1050
                "\torl   %%ebx,%%eax\n"
                "\tmovd  %%eax,%%mm5\n"
                "\tmovd  %%eax,%%mm3\n"
1051
                "\tpsllq   $32,%%mm5\n"
1052
                "\tpor   %%mm5,%%mm3\n"
1053
                "\tmovq     %1,%%mm7\n"
1054
                : /* empty */
1055
                : "m" (op.scale.scale), "m" (*rgba8_w128_64)
1056
                : "%eax", "%ebx", "%mm0", "%mm3", "%mm5", "%mm6", "%mm7");
1057

1058
  for (; op.n_pixels >= 2; op.n_pixels -= 2)
1059 1060 1061 1062 1063
    {
      asm volatile ("movq           %1,%%mm2\n"
                    "\tmovq      %%mm2,%%mm1\n"
                    "\tpunpcklbw %%mm0,%%mm1\n"
                    "\tmovq      %%mm3,%%mm5\n"
1064

1065
                    "\t" pmulwX(mm5,mm1,mm7) "\n"
1066

1067 1068 1069
                    "\tmovq      %%mm2,%%mm4\n"
                    "\tpunpckhbw %%mm0,%%mm4\n"
                    "\tmovq      %%mm3,%%mm5\n"
1070

1071
                    "\t" pmulwX(mm5,mm4,mm7) "\n"
1072

1073
                    "\tpackuswb  %%mm4,%%mm1\n"
1074

1075 1076 1077 1078 1079 1080 1081
                    "\tmovq      %%mm1,%0\n"
                    : "+m" (*op.D)
                    : "m" (*op.A)
                    : "%mm1", "%mm2", "%mm4", "%mm5", "%mm7");
      op.A += 8;
      op.D += 8;
    }
1082

1083
  if (op.n_pixels)
1084 1085 1086 1087 1088
    {
      asm volatile ("  movd         %1,%%mm2\n"
                    "\tmovq      %%mm2,%%mm1\n"
                    "\tpunpcklbw %%mm0,%%mm1\n"
                    "\tmovq      %%mm3,%%mm5\n"
1089

1090
                    "\t" pmulwX(mm5,mm1,mm7) "\n"
1091

1092 1093 1094 1095 1096 1097
                    "\tpackuswb  %%mm0,%%mm1\n"
                    "\tmovd      %%mm1,%0\n"
                    : "+m" (*op.D)
                    : "m" (*op.A)
                    : "%mm1", "%mm2", "%mm4", "%mm5", "%mm6", "%mm7");
    }
1098 1099 1100 1101 1102

  asm("emms");
}

void
1103
gimp_composite_screen_rgba8_rgba8_rgba8_sse (GimpCompositeContext *_op)
1104 1105
{
  GimpCompositeContext op = *_op;
1106

1107 1108
  asm volatile ("movq    %0,%%mm0"     :  : "m" (*rgba8_alpha_mask_64) : "%mm0");
  asm volatile ("movq    %0,%%mm7"     :  : "m" (*rgba8_w128_64)  : "%mm7");
1109
  asm volatile ("pxor    %mm6, %mm6");
1110

1111
  for (; op.n_pixels >= 2; op.n_pixels -= 2)
1112 1113 1114
    {
      asm volatile ("  movq         %0,%%mm2\n"
                    "\tmovq         %1,%%mm3\n"
1115

1116 1117 1118 1119
                    "\tpcmpeqb   %%mm4,%%mm4\n"
                    "\tpsubb     %%mm2,%%mm4\n"
                    "\tpcmpeqb   %%mm5,%%mm5\n"
                    "\tpsubb     %%mm3,%%mm5\n"
1120

1121 1122 1123 1124 1125 1126 1127 1128
                    "\tpunpcklbw %%mm6,%%mm4\n"
                    "\tpunpcklbw %%mm6,%%mm5\n"
                    "\tpmullw    %%mm4,%%mm5\n"
                    "\tpaddw     %%mm7,%%mm5\n"
                    "\tmovq      %%mm5,%%mm1\n"
                    "\tpsrlw       $ 8,%%mm1\n"
                    "\tpaddw     %%mm5,%%mm1\n"
                    "\tpsrlw       $ 8,%%mm1\n"
1129

1130 1131 1132 1133
                    "\tpcmpeqb   %%mm4,%%mm4\n"
                    "\tpsubb     %%mm2,%%mm4\n"
                    "\tpcmpeqb   %%mm5,%%mm5\n"
                    "\tpsubb     %%mm3,%%mm5\n"
1134

1135 1136 1137 1138 1139 1140 1141 1142
                    "\tpunpckhbw %%mm6,%%mm4\n"
                    "\tpunpckhbw %%mm6,%%mm5\n"
                    "\tpmullw    %%mm4,%%mm5\n"
                    "\tpaddw     %%mm7,%%mm5\n"
                    "\tmovq      %%mm5,%%mm4\n"
                    "\tpsrlw       $ 8,%%mm4\n"
                    "\tpaddw     %%mm5,%%mm4\n"
                    "\tpsrlw       $ 8,%%mm4\n"
1143

1144
                    "\tpackuswb  %%mm4,%%mm1\n"
1145

1146 1147
                    "\tpcmpeqb   %%mm4,%%mm4\n"
                    "\tpsubb     %%mm1,%%mm4\n"
1148

1149 1150
                    "\tmovq      %%mm0,%%mm1\n"
                    "\tpandn     %%mm4,%%mm1\n"
1151

1152 1153
                    "\t" pminub(mm2,mm3,mm4) "\n"
                    "\tpand      %%mm0,%%mm3\n"
1154

1155
                    "\tpor       %%mm3,%%mm1\n"
1156

1157 1158 1159 1160 1161 1162 1163 1164
                    "\tmovq      %%mm1,%2\n"
                    : /* empty */
                    : "m" (*op.A), "m" (*op.B), "m" (*op.D)
                    : "0", "1", "2", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5");
      op.A += 8;
      op.B += 8;
      op.D += 8;
    }
1165

1166
  if (op.n_pixels)
1167 1168 1169 1170 1171 1172 1173 1174
    {
      asm volatile ("  movd         %0,%%mm2\n"
                    "\tmovd         %1,%%mm3\n"

                    "\tpcmpeqb   %%mm4,%%mm4\n"
                    "\tpsubb     %%mm2,%%mm4\n"
                    "\tpcmpeqb   %%mm5,%%mm5\n"
                    "\tpsubb     %%mm3,%%mm5\n"
1175

1176 1177 1178 1179 1180 1181 1182 1183
                    "\tpunpcklbw %%mm6,%%mm4\n"
                    "\tpunpcklbw %%mm6,%%mm5\n"
                    "\tpmullw    %%mm4,%%mm5\n"
                    "\tpaddw     %%mm7,%%mm5\n"
                    "\tmovq      %%mm5,%%mm1\n"
                    "\tpsrlw       $ 8,%%mm1\n"
                    "\tpaddw     %%mm5,%%mm1\n"
                    "\tpsrlw       $ 8,%%mm1\n"
1184

1185 1186 1187 1188
                    "\tpcmpeqb   %%mm4,%%mm4\n"
                    "\tpsubb     %%mm2,%%mm4\n"
                    "\tpcmpeqb   %%mm5,%%mm5\n"
                    "\tpsubb     %%mm3,%%mm5\n"
1189

1190 1191 1192 1193 1194 1195 1196 1197
                    "\tpunpckhbw %%mm6,%%mm4\n"
                    "\tpunpckhbw %%mm6,%%mm5\n"
                    "\tpmullw    %%mm4,%%mm5\n"
                    "\tpaddw     %%mm7,%%mm5\n"
                    "\tmovq      %%mm5,%%mm4\n"
                    "\tpsrlw       $ 8,%%mm4\n"
                    "\tpaddw     %%mm5,%%mm4\n"
                    "\tpsrlw       $ 8,%%mm4\n"
1198

1199
                    "\tpackuswb  %%mm4,%%mm1\n"
1200

1201 1202
                    "\tpcmpeqb   %%mm4,%%mm4\n"
                    "\tpsubb     %%mm1,%%mm4\n"
1203

1204 1205
                    "\tmovq      %%mm0,%%mm1\n"
                    "\tpandn     %%mm4,%%mm1\n"
1206

1207 1208
                    "\t" pminub(mm2,mm3,mm4) "\n"
                    "\tpand      %%mm0,%%mm3\n"
1209

1210
                    "\tpor       %%mm3,%%mm1\n"
1211

1212 1213 1214 1215 1216
                    "\tmovd      %%mm1,%2\n"
                    : /* empty */
                    : "m" (*op.A), "m" (*op.B), "m" (*op.D)
                    : "0", "1", "2", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5");
    }
1217

1218 1219 1220 1221 1222
  asm("emms");
}


void
1223
gimp_composite_subtract_rgba8_rgba8_rgba8_sse (GimpCompositeContext *_op)
1224 1225
{
  GimpCompositeContext op = *_op;
1226

1227
  asm volatile ("movq    %0,%%mm0"     :  : "m" (*rgba8_alpha_mask_64) : "%mm0");
1228

1229
  for (; op.n_pixels >= 2; op.n_pixels -= 2)
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
    {
      asm volatile ("  movq       %1,%%mm2\n"
                    "\tmovq       %2,%%mm3\n"
                    "\tmovq    %%mm2,%%mm4\n"
                    "\tpsubusb %%mm3,%%mm4\n"
                    "\tmovq    %%mm0,%%mm1\n"
                    "\tpandn   %%mm4,%%mm1\n"
                    "\tpminub  %%mm3,%%mm2\n"
                    "\tpand    %%mm0,%%mm2\n"
                    "\tpor     %%mm2,%%mm1\n"
                    "\tmovq    %%mm1,%0\n"
                    : "+m" (*op.D)
                    : "m" (*op.A), "m" (*op.B)
                    : "%mm1", "%mm2", "%mm3", "%mm4");
      op.A += 8;
      op.B += 8;
      op.D += 8;
    }
1248

1249
  if (op.n_pixels)
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
    {
      asm volatile ("  movd       %1,%%mm2\n"
                    "\tmovd       %2,%%mm3\n"
                    "\tmovq    %%mm2,%%mm4\n"
                    "\tpsubusb %%mm3,%%mm4\n"
                    "\tmovq    %%mm0,%%mm1\n"
                    "\tpandn   %%mm4,%%mm1\n"
                    "\tpminub  %%mm3,%%mm2\n"
                    "\tpand    %%mm0,%%mm2\n"
                    "\tpor     %%mm2,%%mm1\n"
                    "\tmovd    %%mm1,%0\n"
                    : "+m" (*op.D)
                    : "m" (*op.A), "m" (*op.B)
                    : "%mm1", "%mm2", "%mm3", "%mm4");
    }
1265

1266 1267 1268 1269
  asm("emms");
}

void
1270
gimp_composite_swap_rgba8_rgba8_rgba8_sse (GimpCompositeContext *_op)
1271 1272
{
  GimpCompositeContext op = *_op;