| 1 |
/* |
| 2 |
* Mesa 3-D graphics library |
| 3 |
* Version: 6.3 |
| 4 |
* |
| 5 |
* Copyright (C) 1999-2004 Brian Paul All Rights Reserved. |
| 6 |
* |
| 7 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 8 |
* copy of this software and associated documentation files (the "Software"), |
| 9 |
* to deal in the Software without restriction, including without limitation |
| 10 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 12 |
* Software is furnished to do so, subject to the following conditions: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be included |
| 15 |
* in all copies or substantial portions of the Software. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 18 |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 |
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 21 |
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 |
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
/** |
| 26 |
* \file t_vb_arb_program_sse.c |
| 27 |
* |
| 28 |
* Translate simplified vertex_program representation to |
| 29 |
* x86/x87/SSE/SSE2 machine code using mesa's rtasm runtime assembler. |
| 30 |
* |
| 31 |
* This is very much a first attempt - build something that works. |
| 32 |
* There are probably better approaches for applying SSE to vertex |
| 33 |
* programs, and the whole thing is crying out for static analysis of |
| 34 |
* the programs to avoid redundant operations. |
| 35 |
* |
| 36 |
* \author Keith Whitwell |
| 37 |
*/ |
| 38 |
|
| 39 |
#include "glheader.h" |
| 40 |
#include "context.h" |
| 41 |
#include "imports.h" |
| 42 |
#include "macros.h" |
| 43 |
#include "mtypes.h" |
| 44 |
#include "arbprogparse.h" |
| 45 |
#include "program.h" |
| 46 |
#include "math/m_matrix.h" |
| 47 |
#include "math/m_translate.h" |
| 48 |
#include "t_context.h" |
| 49 |
#include "t_vb_arbprogram.h" |
| 50 |
|
| 51 |
#if defined(USE_SSE_ASM) |
| 52 |
|
| 53 |
#include "x86/rtasm/x86sse.h" |
| 54 |
#include "x86/common_x86_asm.h" |
| 55 |
|
| 56 |
#define X 0 |
| 57 |
#define Y 1 |
| 58 |
#define Z 2 |
| 59 |
#define W 3 |
| 60 |
|
| 61 |
/* Reg usage: |
| 62 |
* |
| 63 |
* EAX - temp |
| 64 |
* EBX - point to 'm->File[0]' |
| 65 |
* ECX - point to 'm->File[3]' |
| 66 |
* EDX - holds 'm' |
| 67 |
* EBP, |
| 68 |
* ESI, |
| 69 |
* EDI |
| 70 |
*/ |
| 71 |
|
| 72 |
#define DISASSEM 0 |
| 73 |
|
| 74 |
#define FAIL \ |
| 75 |
do { \ |
| 76 |
_mesa_printf("x86 translation failed in %s\n", __FUNCTION__); \ |
| 77 |
return GL_FALSE; \ |
| 78 |
} while (0) |
| 79 |
|
| 80 |
struct compilation { |
| 81 |
struct x86_function func; |
| 82 |
struct tnl_compiled_program *p; |
| 83 |
GLuint insn_counter; |
| 84 |
|
| 85 |
struct { |
| 86 |
GLuint file:2; |
| 87 |
GLuint idx:7; |
| 88 |
GLuint dirty:1; |
| 89 |
GLuint last_used:10; |
| 90 |
} xmm[8]; |
| 91 |
|
| 92 |
struct { |
| 93 |
struct x86_reg base; |
| 94 |
} file[4]; |
| 95 |
|
| 96 |
GLboolean have_sse2; |
| 97 |
GLshort fpucntl; |
| 98 |
}; |
| 99 |
|
| 100 |
static INLINE GLboolean eq( struct x86_reg a, |
| 101 |
struct x86_reg b ) |
| 102 |
{ |
| 103 |
return (a.file == b.file && |
| 104 |
a.idx == b.idx && |
| 105 |
a.mod == b.mod && |
| 106 |
a.disp == b.disp); |
| 107 |
} |
| 108 |
|
| 109 |
static GLint get_offset( const void *a, const void *b ) |
| 110 |
{ |
| 111 |
return (const char *)b - (const char *)a; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
static struct x86_reg get_reg_ptr(GLuint file, |
| 116 |
GLuint idx ) |
| 117 |
{ |
| 118 |
struct x86_reg reg; |
| 119 |
|
| 120 |
switch (file) { |
| 121 |
case FILE_REG: |
| 122 |
reg = x86_make_reg(file_REG32, reg_BX); |
| 123 |
assert(idx != REG_UNDEF); |
| 124 |
break; |
| 125 |
case FILE_STATE_PARAM: |
| 126 |
reg = x86_make_reg(file_REG32, reg_CX); |
| 127 |
break; |
| 128 |
default: |
| 129 |
assert(0); |
| 130 |
} |
| 131 |
|
| 132 |
return x86_make_disp(reg, 16 * idx); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
static void spill( struct compilation *cp, GLuint idx ) |
| 137 |
{ |
| 138 |
struct x86_reg oldval = get_reg_ptr(cp->xmm[idx].file, |
| 139 |
cp->xmm[idx].idx); |
| 140 |
|
| 141 |
assert(cp->xmm[idx].dirty); |
| 142 |
sse_movups(&cp->func, oldval, x86_make_reg(file_XMM, idx)); |
| 143 |
cp->xmm[idx].dirty = 0; |
| 144 |
} |
| 145 |
|
| 146 |
static struct x86_reg get_xmm_reg( struct compilation *cp ) |
| 147 |
{ |
| 148 |
GLuint i; |
| 149 |
GLuint oldest = 0; |
| 150 |
|
| 151 |
for (i = 0; i < 8; i++) |
| 152 |
if (cp->xmm[i].last_used < cp->xmm[oldest].last_used) |
| 153 |
oldest = i; |
| 154 |
|
| 155 |
/* Need to write out the old value? |
| 156 |
*/ |
| 157 |
if (cp->xmm[oldest].dirty) |
| 158 |
spill(cp, oldest); |
| 159 |
|
| 160 |
assert(cp->xmm[oldest].last_used != cp->insn_counter); |
| 161 |
|
| 162 |
cp->xmm[oldest].file = FILE_REG; |
| 163 |
cp->xmm[oldest].idx = REG_UNDEF; |
| 164 |
cp->xmm[oldest].last_used = cp->insn_counter; |
| 165 |
return x86_make_reg(file_XMM, oldest); |
| 166 |
} |
| 167 |
|
| 168 |
static void invalidate_xmm( struct compilation *cp, |
| 169 |
GLuint file, GLuint idx ) |
| 170 |
{ |
| 171 |
GLuint i; |
| 172 |
|
| 173 |
/* Invalidate any old copy of this register in XMM0-7. |
| 174 |
*/ |
| 175 |
for (i = 0; i < 8; i++) { |
| 176 |
if (cp->xmm[i].file == file && cp->xmm[i].idx == idx) { |
| 177 |
cp->xmm[i].file = FILE_REG; |
| 178 |
cp->xmm[i].idx = REG_UNDEF; |
| 179 |
cp->xmm[i].dirty = 0; |
| 180 |
break; |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
/* Return an XMM reg to receive the results of an operation. |
| 187 |
*/ |
| 188 |
static struct x86_reg get_dst_xmm_reg( struct compilation *cp, |
| 189 |
GLuint file, GLuint idx ) |
| 190 |
{ |
| 191 |
struct x86_reg reg; |
| 192 |
|
| 193 |
/* Invalidate any old copy of this register in XMM0-7. Don't reuse |
| 194 |
* as this may be one of the arguments. |
| 195 |
*/ |
| 196 |
invalidate_xmm( cp, file, idx ); |
| 197 |
|
| 198 |
reg = get_xmm_reg( cp ); |
| 199 |
cp->xmm[reg.idx].file = file; |
| 200 |
cp->xmm[reg.idx].idx = idx; |
| 201 |
cp->xmm[reg.idx].dirty = 1; |
| 202 |
return reg; |
| 203 |
} |
| 204 |
|
| 205 |
/* As above, but return a pointer. Note - this pointer may alias |
| 206 |
* those returned by get_arg_ptr(). |
| 207 |
*/ |
| 208 |
static struct x86_reg get_dst_ptr( struct compilation *cp, |
| 209 |
GLuint file, GLuint idx ) |
| 210 |
{ |
| 211 |
/* Invalidate any old copy of this register in XMM0-7. Don't reuse |
| 212 |
* as this may be one of the arguments. |
| 213 |
*/ |
| 214 |
invalidate_xmm( cp, file, idx ); |
| 215 |
|
| 216 |
return get_reg_ptr(file, idx); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
/* Return an XMM reg if the argument is resident, otherwise return a |
| 222 |
* base+offset pointer to the saved value. |
| 223 |
*/ |
| 224 |
static struct x86_reg get_arg( struct compilation *cp, GLuint file, GLuint idx ) |
| 225 |
{ |
| 226 |
GLuint i; |
| 227 |
|
| 228 |
for (i = 0; i < 8; i++) { |
| 229 |
if (cp->xmm[i].file == file && |
| 230 |
cp->xmm[i].idx == idx) { |
| 231 |
cp->xmm[i].last_used = cp->insn_counter; |
| 232 |
return x86_make_reg(file_XMM, i); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
return get_reg_ptr(file, idx); |
| 237 |
} |
| 238 |
|
| 239 |
/* As above, but always return a pointer: |
| 240 |
*/ |
| 241 |
static struct x86_reg get_arg_ptr( struct compilation *cp, GLuint file, GLuint idx ) |
| 242 |
{ |
| 243 |
GLuint i; |
| 244 |
|
| 245 |
/* If there is a modified version of this register in one of the |
| 246 |
* XMM regs, write it out to memory. |
| 247 |
*/ |
| 248 |
for (i = 0; i < 8; i++) { |
| 249 |
if (cp->xmm[i].file == file && |
| 250 |
cp->xmm[i].idx == idx && |
| 251 |
cp->xmm[i].dirty) |
| 252 |
spill(cp, i); |
| 253 |
} |
| 254 |
|
| 255 |
return get_reg_ptr(file, idx); |
| 256 |
} |
| 257 |
|
| 258 |
/* Emulate pshufd insn in regular SSE, if necessary: |
| 259 |
*/ |
| 260 |
static void emit_pshufd( struct compilation *cp, |
| 261 |
struct x86_reg dst, |
| 262 |
struct x86_reg arg0, |
| 263 |
GLubyte shuf ) |
| 264 |
{ |
| 265 |
if (cp->have_sse2) { |
| 266 |
sse2_pshufd(&cp->func, dst, arg0, shuf); |
| 267 |
cp->func.fn = 0; |
| 268 |
} |
| 269 |
else { |
| 270 |
if (!eq(dst, arg0)) |
| 271 |
sse_movups(&cp->func, dst, arg0); |
| 272 |
|
| 273 |
sse_shufps(&cp->func, dst, dst, shuf); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
static void set_fpu_round_neg_inf( struct compilation *cp ) |
| 278 |
{ |
| 279 |
if (cp->fpucntl != RND_NEG_FPU) { |
| 280 |
struct x86_reg regEDX = x86_make_reg(file_REG32, reg_DX); |
| 281 |
struct arb_vp_machine *m = NULL; |
| 282 |
|
| 283 |
cp->fpucntl = RND_NEG_FPU; |
| 284 |
x87_fnclex(&cp->func); |
| 285 |
x87_fldcw(&cp->func, x86_make_disp(regEDX, get_offset(m, &m->fpucntl_rnd_neg))); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
/* Perform a reduced swizzle. |
| 291 |
*/ |
| 292 |
static GLboolean emit_RSW( struct compilation *cp, union instruction op ) |
| 293 |
{ |
| 294 |
struct x86_reg arg0 = get_arg(cp, op.rsw.file0, op.rsw.idx0); |
| 295 |
struct x86_reg dst = get_dst_xmm_reg(cp, FILE_REG, op.rsw.dst); |
| 296 |
GLuint swz = op.rsw.swz; |
| 297 |
GLuint neg = op.rsw.neg; |
| 298 |
|
| 299 |
emit_pshufd(cp, dst, arg0, swz); |
| 300 |
|
| 301 |
if (neg) { |
| 302 |
struct x86_reg negs = get_arg(cp, FILE_REG, REG_SWZ); |
| 303 |
struct x86_reg tmp = get_xmm_reg(cp); |
| 304 |
/* Load 1,-1,0,0 |
| 305 |
* Use neg as arg to pshufd |
| 306 |
* Multiply |
| 307 |
*/ |
| 308 |
emit_pshufd(cp, tmp, negs, |
| 309 |
SHUF((neg & 1) ? 1 : 0, |
| 310 |
(neg & 2) ? 1 : 0, |
| 311 |
(neg & 4) ? 1 : 0, |
| 312 |
(neg & 8) ? 1 : 0)); |
| 313 |
sse_mulps(&cp->func, dst, tmp); |
| 314 |
} |
| 315 |
|
| 316 |
return GL_TRUE; |
| 317 |
} |
| 318 |
|
| 319 |
/* Helper for writemask: |
| 320 |
*/ |
| 321 |
static GLboolean emit_shuf_copy1( struct compilation *cp, |
| 322 |
struct x86_reg dst, |
| 323 |
struct x86_reg arg0, |
| 324 |
struct x86_reg arg1, |
| 325 |
GLubyte shuf ) |
| 326 |
{ |
| 327 |
struct x86_reg tmp = get_xmm_reg(cp); |
| 328 |
sse_movups(&cp->func, dst, arg1); |
| 329 |
emit_pshufd(cp, dst, dst, shuf); |
| 330 |
emit_pshufd(cp, tmp, arg0, shuf); |
| 331 |
|
| 332 |
sse_movss(&cp->func, dst, tmp); |
| 333 |
|
| 334 |
emit_pshufd(cp, dst, dst, shuf); |
| 335 |
return GL_TRUE; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
/* Helper for writemask: |
| 340 |
*/ |
| 341 |
static GLboolean emit_shuf_copy2( struct compilation *cp, |
| 342 |
struct x86_reg dst, |
| 343 |
struct x86_reg arg0, |
| 344 |
struct x86_reg arg1, |
| 345 |
GLubyte shuf ) |
| 346 |
{ |
| 347 |
struct x86_reg tmp = get_xmm_reg(cp); |
| 348 |
emit_pshufd(cp, dst, arg1, shuf); |
| 349 |
emit_pshufd(cp, tmp, arg0, shuf); |
| 350 |
|
| 351 |
sse_shufps(&cp->func, dst, tmp, SHUF(X, Y, Z, W)); |
| 352 |
|
| 353 |
emit_pshufd(cp, dst, dst, shuf); |
| 354 |
return GL_TRUE; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
static void emit_x87_ex2( struct compilation *cp ) |
| 359 |
{ |
| 360 |
struct x86_reg st0 = x86_make_reg(file_x87, 0); |
| 361 |
struct x86_reg st1 = x86_make_reg(file_x87, 1); |
| 362 |
struct x86_reg st3 = x86_make_reg(file_x87, 3); |
| 363 |
|
| 364 |
set_fpu_round_neg_inf( cp ); |
| 365 |
|
| 366 |
x87_fld(&cp->func, st0); /* a a */ |
| 367 |
x87_fprndint( &cp->func ); /* int(a) a */ |
| 368 |
x87_fld(&cp->func, st0); /* int(a) int(a) a */ |
| 369 |
x87_fstp(&cp->func, st3); /* int(a) a int(a)*/ |
| 370 |
x87_fsubp(&cp->func, st1); /* frac(a) int(a) */ |
| 371 |
x87_f2xm1(&cp->func); /* (2^frac(a))-1 int(a)*/ |
| 372 |
x87_fld1(&cp->func); /* 1 (2^frac(a))-1 int(a)*/ |
| 373 |
x87_faddp(&cp->func, st1); /* 2^frac(a) int(a) */ |
| 374 |
x87_fscale(&cp->func); /* 2^a */ |
| 375 |
} |
| 376 |
|
| 377 |
#if 0 |
| 378 |
static GLboolean emit_MSK2( struct compilation *cp, union instruction op ) |
| 379 |
{ |
| 380 |
struct x86_reg arg0 = get_arg(cp, op.msk.file, op.msk.arg); |
| 381 |
struct x86_reg arg1 = get_arg(cp, FILE_REG, op.msk.dst); /* NOTE! */ |
| 382 |
struct x86_reg dst = get_dst_xmm_reg(cp, FILE_REG, op.msk.dst); |
| 383 |
|
| 384 |
/* make full width bitmask in tmp |
| 385 |
* dst = ~tmp |
| 386 |
* tmp &= arg0 |
| 387 |
* dst &= arg1 |
| 388 |
* dst |= tmp |
| 389 |
*/ |
| 390 |
emit_pshufd(cp, tmp, get_arg(cp, FILE_REG, REG_NEGS), |
| 391 |
SHUF((op.msk.mask & 1) ? 2 : 0, |
| 392 |
(op.msk.mask & 2) ? 2 : 0, |
| 393 |
(op.msk.mask & 4) ? 2 : 0, |
| 394 |
(op.msk.mask & 8) ? 2 : 0)); |
| 395 |
sse2_pnot(&cp->func, dst, tmp); |
| 396 |
sse2_pand(&cp->func, arg0, tmp); |
| 397 |
sse2_pand(&cp->func, arg1, dst); |
| 398 |
sse2_por(&cp->func, tmp, dst); |
| 399 |
return GL_TRUE; |
| 400 |
} |
| 401 |
#endif |
| 402 |
|
| 403 |
|
| 404 |
/* Used to implement write masking. This and most of the other instructions |
| 405 |
* here would be easier to implement if there had been a translation |
| 406 |
* to a 2 argument format (dst/arg0, arg1) at the shader level before |
| 407 |
* attempting to translate to x86/sse code. |
| 408 |
*/ |
| 409 |
static GLboolean emit_MSK( struct compilation *cp, union instruction op ) |
| 410 |
{ |
| 411 |
struct x86_reg arg = get_arg(cp, op.msk.file, op.msk.idx); |
| 412 |
struct x86_reg dst0 = get_arg(cp, FILE_REG, op.msk.dst); /* NOTE! */ |
| 413 |
struct x86_reg dst = get_dst_xmm_reg(cp, FILE_REG, op.msk.dst); |
| 414 |
|
| 415 |
/* Note that dst and dst0 refer to the same program variable, but |
| 416 |
* will definitely be different XMM registers. We're effectively |
| 417 |
* treating this as a 2 argument SEL now, just one of which happens |
| 418 |
* always to be the same register as the destination. |
| 419 |
*/ |
| 420 |
|
| 421 |
switch (op.msk.mask) { |
| 422 |
case 0: |
| 423 |
sse_movups(&cp->func, dst, dst0); |
| 424 |
return GL_TRUE; |
| 425 |
|
| 426 |
case WRITEMASK_X: |
| 427 |
if (arg.file == file_XMM) { |
| 428 |
sse_movups(&cp->func, dst, dst0); |
| 429 |
sse_movss(&cp->func, dst, arg); |
| 430 |
} |
| 431 |
else { |
| 432 |
struct x86_reg tmp = get_xmm_reg(cp); |
| 433 |
sse_movups(&cp->func, dst, dst0); |
| 434 |
sse_movss(&cp->func, tmp, arg); |
| 435 |
sse_movss(&cp->func, dst, tmp); |
| 436 |
} |
| 437 |
return GL_TRUE; |
| 438 |
|
| 439 |
case WRITEMASK_XY: |
| 440 |
sse_movups(&cp->func, dst, dst0); |
| 441 |
sse_shufps(&cp->func, dst, arg, SHUF(X, Y, Z, W)); |
| 442 |
return GL_TRUE; |
| 443 |
|
| 444 |
case WRITEMASK_ZW: |
| 445 |
sse_movups(&cp->func, dst, arg); |
| 446 |
sse_shufps(&cp->func, dst, dst0, SHUF(X, Y, Z, W)); |
| 447 |
return GL_TRUE; |
| 448 |
|
| 449 |
case WRITEMASK_YZW: |
| 450 |
if (dst0.file == file_XMM) { |
| 451 |
sse_movups(&cp->func, dst, arg); |
| 452 |
sse_movss(&cp->func, dst, dst0); |
| 453 |
} |
| 454 |
else { |
| 455 |
struct x86_reg tmp = get_xmm_reg(cp); |
| 456 |
sse_movups(&cp->func, dst, arg); |
| 457 |
sse_movss(&cp->func, tmp, dst0); |
| 458 |
sse_movss(&cp->func, dst, tmp); |
| 459 |
} |
| 460 |
return GL_TRUE; |
| 461 |
|
| 462 |
case WRITEMASK_Y: |
| 463 |
emit_shuf_copy1(cp, dst, arg, dst0, SHUF(Y,X,Z,W)); |
| 464 |
return GL_TRUE; |
| 465 |
|
| 466 |
case WRITEMASK_Z: |
| 467 |
emit_shuf_copy1(cp, dst, arg, dst0, SHUF(Z,Y,X,W)); |
| 468 |
return GL_TRUE; |
| 469 |
|
| 470 |
case WRITEMASK_W: |
| 471 |
emit_shuf_copy1(cp, dst, arg, dst0, SHUF(W,Y,Z,X)); |
| 472 |
return GL_TRUE; |
| 473 |
|
| 474 |
case WRITEMASK_XZ: |
| 475 |
emit_shuf_copy2(cp, dst, arg, dst0, SHUF(X,Z,Y,W)); |
| 476 |
return GL_TRUE; |
| 477 |
|
| 478 |
case WRITEMASK_XW: |
| 479 |
emit_shuf_copy2(cp, dst, arg, dst0, SHUF(X,W,Z,Y)); |
| 480 |
|
| 481 |
case WRITEMASK_YZ: |
| 482 |
emit_shuf_copy2(cp, dst, arg, dst0, SHUF(Z,Y,X,W)); |
| 483 |
return GL_TRUE; |
| 484 |
|
| 485 |
case WRITEMASK_YW: |
| 486 |
emit_shuf_copy2(cp, dst, arg, dst0, SHUF(W,Y,Z,X)); |
| 487 |
return GL_TRUE; |
| 488 |
|
| 489 |
case WRITEMASK_XZW: |
| 490 |
emit_shuf_copy1(cp, dst, dst0, arg, SHUF(Y,X,Z,W)); |
| 491 |
return GL_TRUE; |
| 492 |
|
| 493 |
case WRITEMASK_XYW: |
| 494 |
emit_shuf_copy1(cp, dst, dst0, arg, SHUF(Z,Y,X,W)); |
| 495 |
return GL_TRUE; |
| 496 |
|
| 497 |
case WRITEMASK_XYZ: |
| 498 |
emit_shuf_copy1(cp, dst, dst0, arg, SHUF(W,Y,Z,X)); |
| 499 |
return GL_TRUE; |
| 500 |
|
| 501 |
case WRITEMASK_XYZW: |
| 502 |
sse_movups(&cp->func, dst, arg); |
| 503 |
return GL_TRUE; |
| 504 |
|
| 505 |
default: |
| 506 |
assert(0); |
| 507 |
break; |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
static GLboolean emit_PRT( struct compilation *cp, union instruction op ) |
| 514 |
{ |
| 515 |
FAIL; |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
/** |
| 520 |
* The traditional instructions. All operate on internal registers |
| 521 |
* and ignore write masks and swizzling issues. |
| 522 |
*/ |
| 523 |
|
| 524 |
static GLboolean emit_ABS( struct compilation *cp, union instruction op ) |
| 525 |
{ |
| 526 |
struct x86_reg arg0 = get_arg(cp, op.alu.file0, op.alu.idx0); |
| 527 |
struct x86_reg dst = get_dst_xmm_reg(cp, FILE_REG, op.alu.dst); |
| 528 |
struct x86_reg neg = get_reg_ptr(FILE_REG, REG_NEG); |
| 529 |
|
| 530 |
sse_movups(&cp->func, dst, arg0); |
| 531 |
sse_mulps(&cp->func, dst, neg); |
| 532 |
sse_maxps(&cp->func, dst, arg0); |
| 533 |
return GL_TRUE; |
| 534 |
} |
| 535 |
|
| 536 |
static GLboolean emit_ADD( struct compilation *cp, union instruction op ) |
| 537 |
{ |
| 538 |
struct x86_reg arg0 = get_arg(cp, op.alu.file0, op.alu.idx0); |
| 539 |
struct x86_reg arg1 = get_arg(cp, op.alu.file1, op.alu.idx1); |
| 540 |
struct x86_reg dst = get_dst_xmm_reg(cp, FILE_REG, op.alu.dst); |
| 541 |
|
| 542 |
sse_movups(&cp->func, dst, arg0); |
| 543 |
sse_addps(&cp->func, dst, arg1); |
| 544 |
return GL_TRUE; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
/* The dotproduct instructions don't really do that well in sse: |
| 549 |
*/ |
| 550 |
static GLboolean emit_DP3( struct compilation *cp, union instruction op ) |
| 551 |
{ |
| 552 |
struct x86_reg arg0 = get_arg(cp, op.alu.file0, op.alu.idx0); |
| 553 |
struct x86_reg arg1 = get_arg(cp, op.alu.file1, op.alu.idx1); |
| 554 |
struct x86_reg dst = get_dst_xmm_reg(cp, FILE_REG, op.alu.dst); |
| 555 |
struct x86_reg tmp = get_xmm_reg(cp); |
| 556 |
|
| 557 |
sse_movups(&cp->func, dst, arg0); |
| 558 |
sse_mulps(&cp->func, dst, arg1); |
| 559 |
|
| 560 |
/* Now the hard bit: sum the first 3 values: |
| 561 |
*/ |
| 562 |
sse_movhlps(&cp->func, tmp, dst); |
| 563 |
sse_addss(&cp->func, dst, tmp); /* a*x+c*z, b*y, ?, ? */ |
| 564 |
emit_pshufd(cp, tmp, dst, SHUF(Y,X,W,Z)); |
| 565 |
sse_addss(&cp->func, dst, tmp); |
| 566 |
sse_shufps(&cp->func, dst, dst, SHUF(X, X, X, X)); |
| 567 |
return GL_TRUE; |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
static GLboolean emit_DP4( struct compilation *cp, union instruction op ) |
| 573 |
{ |
| 574 |
struct x86_reg arg0 = get_arg(cp, op.alu.file0, op.alu.idx0); |
| 575 |
struct x86_reg arg1 = get_arg(cp, op.alu.file1, op.alu.idx1); |
| 576 |
struct x86_reg dst = get_dst_xmm_reg(cp, FILE_REG, op.alu.dst); |
| 577 |
struct x86_reg tmp = get_xmm_reg(cp); |
| 578 |
|
| 579 |
sse_movups(&cp->func, dst, arg0); |
| 580 |
sse_mulps(&cp->func, dst, arg1); |
| 581 |
|
| 582 |
/* Now the hard bit: sum the values: |
| 583 |
*/ |
| 584 |
sse_movhlps(&cp->func, tmp, dst); |
| 585 |
sse_addps(&cp->func, dst, tmp); /* a*x+c*z, b*y+d*w, a*x+c*z, b*y+d*w */ |
| 586 |
emit_pshufd(cp, tmp, dst, SHUF(Y,X,W,Z)); |
| 587 |
sse_addss(&cp->func, dst, tmp); |
| 588 |
sse_shufps(&cp->func, dst, dst, SHUF(X, X, X, X)); |
| 589 |
return GL_TRUE; |
| 590 |
} |
| 591 |
|
| 592 |
static GLboolean emit_DPH( struct compilation *cp, union instruction op ) |
| 593 |
{ |
| 594 |
struct x86_reg arg0 = get_arg(cp, op.alu.file0, op.alu.idx0); |
| 595 |
struct x86_reg arg1 = get_arg(cp, op.alu.file1, op.alu.idx1); |
| 596 |
struct x86_reg dst = get_dst_xmm_reg(cp, FILE_REG, op.alu.dst); |
| 597 |
struct x86_reg ones = get_reg_ptr(FILE_REG, REG_ONES); |
| 598 |
struct x86_reg tmp = get_xmm_reg(cp); |
| 599 |
|
| 600 |
emit_pshufd(cp, dst, arg0, SHUF(W,X,Y,Z)); |
| 601 |
sse_movss(&cp->func, dst, ones); |
| 602 |
emit_pshufd(cp, dst, dst, SHUF(W,X,Y,Z)); |
| 603 |
sse_mulps(&cp->func, dst, arg1); |
| 604 |
|
| 605 |
/* Now the hard bit: sum the values (from DP4): |
| 606 |
*/ |
| 607 |
sse_movhlps(&cp->func, tmp, dst); |
| 608 |
sse_addps(&cp->func, dst, tmp); /* a*x+c*z, b*y+d*w, a*x+c*z, b*y+d*w */ |
| 609 |
emit_pshufd(cp, tmp, dst, SHUF(Y,X,W,Z)); |
| 610 |
sse_addss(&cp->func, dst, tmp); |
| 611 |
sse_shufps(&cp->func, dst, dst, SHUF(X, X, X, X)); |
| 612 |
return GL_TRUE; |
| 613 |
} |
| 614 |
|
| 615 |
#if 0 |
| 616 |
static GLboolean emit_DST( struct compilation *cp, union instruction op ) |
| 617 |
{ |
| 618 |
struct x86_reg arg0 = get_arg_ptr(cp, op.alu.file0, op.alu.idx0); |
| 619 |
struct x86_reg arg1 = get_arg_ptr(cp, op.alu.file1, op.alu.idx1); |
| 620 |
struct x86_reg dst = get_dst_ptr(cp, FILE_REG, op.alu.dst); |
| 621 |
|
| 622 |
/* dst[0] = 1.0 * 1.0F; */ |
| 623 |
/* dst[1] = arg0[1] * arg1[1]; */ |
| 624 |
/* dst[2] = arg0[2] * 1.0; */ |
| 625 |
/* dst[3] = 1.0 * arg1[3]; */ |
| 626 |
|
| 627 |
/* Would rather do some of this with integer regs, but: |
| 628 |
* 1) No proper support for immediate values yet |
| 629 |
* 2) I'd need to push/pop somewhere to get a free reg. |
| 630 |
*/ |
| 631 |
x87_fld1(&cp->func); |
| 632 |
x87_fstp(&cp->func, dst); /* would rather do an immediate store... */ |
| 633 |
x87_fld(&cp->func, x86_make_disp(arg0, 4)); |
| 634 |
x87_fmul(&cp->func, x86_make_disp(arg1, 4)); |
| 635 |
x87_fstp(&cp->func, x86_make_disp(dst, 4)); |
| 636 |
|
| 637 |
if (!eq(arg0, dst)) { |
| 638 |
x86_fld(&cp->func, x86_make_disp(arg0, 8)); |
| 639 |
x86_stp(&cp->func, x86_make_disp(dst, 8)); |
| 640 |
} |
| 641 |
|
| 642 |
if (!eq(arg1, dst)) { |
| 643 |
x86_fld(&cp->func, x86_make_disp(arg0, 12)); |
| 644 |
x86_stp(&cp->func, x86_make_disp(dst, 12)); |
| 645 |
} |
| 646 |
|
| 647 |
return GL_TRUE; |
| 648 |
} |
| 649 |
#else |
| 650 |
static GLboolean emit_DST( struct compilation *cp, union instruction op ) |
| 651 |
{ |
| 652 |
struct x86_reg arg0 = get_arg(cp, op.alu.file0, op.alu.idx0); |
| 653 |
struct x86_reg arg1 = get_arg(cp, op.alu.file1, op.alu.idx1); |
| 654 |
struct x86_reg dst = get_dst_xmm_reg(cp, FILE_REG, op.alu.dst); |
| 655 |
struct x86_reg tmp = get_xmm_reg(cp); |
| 656 |
struct x86_reg ones = get_reg_ptr(FILE_REG, REG_ONES); |
| 657 |
|
| 658 |
emit_shuf_copy2(cp, dst, arg0, ones, SHUF(X,W,Z,Y)); |
| 659 |
emit_shuf_copy2(cp, tmp, arg1, ones, SHUF(X,Z,Y,W)); |
| 660 |
sse_mulps(&cp->func, dst, tmp); |
| 661 |
|
| 662 |
/* dst[0] = 1.0 * 1.0F; */ |
| 663 |
/* dst[1] = arg0[1] * arg1[1]; */ |
| 664 |
/* dst[2] = arg0[2] * 1.0; */ |
| 665 |
/* dst[3] = 1.0 * arg1[3]; */ |
| 666 |
|
| 667 |
return GL_TRUE; |
| 668 |
} |
| 669 |
#endif |
| 670 |
|
| 671 |
static GLboolean emit_LG2( struct compilation *cp, union instruction op ) |
| 672 |
{ |
| 673 |
struct x86_reg arg0 = get_arg_ptr(cp, op.alu.file0, op.alu.idx0); |
| 674 |
struct x86_reg dst = get_dst_ptr(cp, FILE_REG, op.alu.dst); |
| 675 |
|
| 676 |
x87_fld1(&cp->func); /* 1 */ |
| 677 |
x87_fld(&cp->func, arg0); /* a0 1 */ |
| 678 |
x87_fyl2x(&cp->func); /* log2(a0) */ |
| 679 |
x87_fst(&cp->func, x86_make_disp(dst, 0)); |
| 680 |
x87_fst(&cp->func, x86_make_disp(dst, 4)); |
| 681 |
x87_fst(&cp->func, x86_make_disp(dst, 8)); |
| 682 |
x87_fstp(&cp->func, x86_make_disp(dst, 12)); |
| 683 |
|
| 684 |
return GL_TRUE; |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
static GLboolean emit_EX2( struct compilation *cp, union instruction op ) |
| 689 |
{ |
| 690 |
struct x86_reg arg0 = get_arg_ptr(cp, op.alu.file0, op.alu.idx0); |
| 691 |
struct x86_reg dst = get_dst_ptr(cp, FILE_REG, op.alu.dst); |
| 692 |
|
| 693 |
/* CAUTION: dst may alias arg0! |
| 694 |
*/ |
| 695 |
x87_fld(&cp->func, arg0); |
| 696 |
|
| 697 |
emit_x87_ex2(cp); |
| 698 |
|
| 699 |
x87_fst(&cp->func, x86_make_disp(dst, 0)); |
| 700 |
x87_fst(&cp->func, x86_make_disp(dst, 4)); |
| 701 |
x87_fst(&cp->func, x86_make_disp(dst, 8)); |
| 702 |
x87_fst(&cp->func, x86_make_disp(dst, 12)); |
| 703 |
return GL_TRUE; |
| 704 |
} |
| 705 |
|
| 706 |
static GLboolean emit_EXP( struct compilation *cp, union instruction op ) |
| 707 |
{ |
| 708 |
struct x86_reg arg0 = get_arg_ptr(cp, op.alu.file0, op.alu.idx0); |
| 709 |
struct x86_reg dst = get_dst_ptr(cp, FILE_REG, op.alu.dst); |
| 710 |
struct x86_reg st0 = x86_make_reg(file_x87, 0); |
| 711 |
struct x86_reg st1 = x86_make_reg(file_x87, 1); |
| 712 |
struct x86_reg st3 = x86_make_reg(file_x87, 3); |
| 713 |
|
| 714 |
/* CAUTION: dst may alias arg0! |
| 715 |
*/ |
| 716 |
x87_fld(&cp->func, arg0); /* arg0.x */ |
| 717 |
x87_fld(&cp->func, st0); /* arg arg */ |
| 718 |
|
| 719 |
/* by default, fpu is setup to round-to-nearest. We want to |
| 720 |
* change this now, and track the state through to the end of the |
| 721 |
* generated function so that it isn't repeated unnecessarily. |
| 722 |
* Alternately, could subtract .5 to get round to -inf behaviour. |
| 723 |
*/ |
| 724 |
set_fpu_round_neg_inf( cp ); |
| 725 |
x87_fprndint( &cp->func ); /* flr(a) a */ |
| 726 |
x87_fld(&cp->func, st0); /* flr(a) flr(a) a */ |
| 727 |
x87_fld1(&cp->func); /* 1 floor(a) floor(a) a */ |
| 728 |
x87_fst(&cp->func, x86_make_disp(dst, 12)); /* stack unchanged */ |
| 729 |
x87_fscale(&cp->func); /* 2^floor(a) floor(a) a */ |
| 730 |
x87_fst(&cp->func, st3); /* 2^floor(a) floor(a) a 2^floor(a)*/ |
| 731 |
x87_fstp(&cp->func, x86_make_disp(dst, 0)); /* flr(a) a 2^flr(a) */ |
| 732 |
x87_fsubrp(&cp->func, st1); /* frac(a) 2^flr(a) */ |
| 733 |
x87_fst(&cp->func, x86_make_disp(dst, 4)); /* frac(a) 2^flr(a) */ |
| 734 |
x87_f2xm1(&cp->func); /* (2^frac(a))-1 2^flr(a)*/ |
| 735 |
x87_fld1(&cp->func); /* 1 (2^frac(a))-1 2^flr(a)*/ |
| 736 |
x87_faddp(&cp->func, st1); /* 2^frac(a) 2^flr(a) */ |
| 737 |
x87_fmulp(&cp->func, st1); /* 2^a */ |
| 738 |
x87_fst(&cp->func, x86_make_disp(dst, 8)); |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
/* dst[0] = 2^floor(tmp); */ |
| 743 |
/* dst[1] = frac(tmp); */ |
| 744 |
/* dst[2] = 2^floor(tmp) * 2^frac(tmp); */ |
| 745 |
/* dst[3] = 1.0F; */ |
| 746 |
return GL_TRUE; |
| 747 |
} |
| 748 |
|
| 749 |
static GLboolean emit_LOG( struct compilation *cp, union instruction op ) |
| 750 |
{ |
| 751 |
struct x86_reg arg0 = get_arg_ptr(cp, op.alu.file0, op.alu.idx0); |
| 752 |
struct x86_reg dst = get_dst_ptr(cp, FILE_REG, op.alu.dst); |
| 753 |
struct x86_reg st0 = x86_make_reg(file_x87, 0); |
| 754 |
struct x86_reg st1 = x86_make_reg(file_x87, 1); |
| 755 |
struct x86_reg st2 = x86_make_reg(file_x87, 2); |
| 756 |
|
| 757 |
/* CAUTION: dst may alias arg0! |
| 758 |
*/ |
| 759 |
x87_fld(&cp->func, arg0); /* arg0.x */ |
| 760 |
x87_fabs(&cp->func); /* |arg0.x| */ |
| 761 |
x87_fxtract(&cp->func); /* mantissa(arg0.x), exponent(arg0.x) */ |
| 762 |
x87_fst(&cp->func, st2); /* mantissa, exponent, mantissa */ |
| 763 |
x87_fld1(&cp->func); /* 1, mantissa, exponent, mantissa */ |
| 764 |
x87_fyl2x(&cp->func); /* log2(mantissa), exponent, mantissa */ |
| 765 |
x87_fadd(&cp->func, st0, st1); /* e+l2(m), e, m */ |
| 766 |
x87_fstp(&cp->func, x86_make_disp(dst, 8)); /* e, m */ |
| 767 |
|
| 768 |
x87_fld1(&cp->func); /* 1, e, m */ |
| 769 |
x87_fsub(&cp->func, st1, st0); /* 1, e-1, m */ |
| 770 |
x87_fstp(&cp->func, x86_make_disp(dst, 12)); /* e-1,m */ |
| 771 |
x87_fstp(&cp->func, dst); /* m */ |
| 772 |
|
| 773 |
x87_fadd(&cp->func, st0, st0); /* 2m */ |
| 774 |
x87_fstp(&cp->func, x86_make_disp(dst, 4)); |
| 775 |
|
| 776 |
return GL_TRUE; |
| 777 |
} |
| 778 |
|
| 779 |
static GLboolean emit_FLR( struct compilation *cp, union instruction op ) |
| 780 |
{ |
| 781 |
struct x86_reg arg0 = get_arg_ptr(cp, op.alu.file0, op.alu.idx0); |
| 782 |
struct x86_reg dst = get_dst_ptr(cp, FILE_REG, op.alu.dst); |
| 783 |
int i; |
| 784 |
|
| 785 |
set_fpu_round_neg_inf( cp ); |
| 786 |
|
| 787 |
for (i = 0; i < 4; i++) { |
| 788 |
x87_fld(&cp->func, x86_make_disp(arg0, i*4)); |
| 789 |
x87_fprndint( &cp->func ); |
| 790 |
x87_fstp(&cp->func, x86_make_disp(dst, i*4)); |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
return GL_TRUE; |
| 795 |
} |
| 796 |
|
| 797 |
static GLboolean emit_FRC( struct compilation *cp, union instruction op ) |
| 798 |
{ |
| 799 |
struct x86_reg arg0 = get_arg_ptr(cp, op.alu.file0, op.alu.idx0); |
| 800 |
struct x86_reg dst = get_dst_ptr(cp, FILE_REG, op.alu.dst); |
| 801 |
struct x86_reg st0 = x86_make_reg(file_x87, 0); |
| 802 |
struct x86_reg st1 = x86_make_reg(file_x87, 1); |
| 803 |
int i; |
| 804 |
|
| 805 |
set_fpu_round_neg_inf( cp ); |
| 806 |
|
| 807 |
/* Knowing liveness info or even just writemask would be useful |
| 808 |
* here: |
| 809 |
*/ |
| 810 |
for (i = 0; i < 4; i++) { |
| 811 |
x87_fld(&cp->func, x86_make_disp(arg0, i*4)); |
| 812 |
x87_fld(&cp->func, st0); /* a a */ |
| 813 |
x87_fprndint( &cp->func ); /* flr(a) a */ |
| 814 |
x87_fsubrp(&cp->func, st1); /* frc(a) */ |
| 815 |
x87_fstp(&cp->func, x86_make_disp(dst, i*4)); |
| 816 |
} |
| 817 |
|
| 818 |
return GL_TRUE; |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
static GLboolean emit_LIT( struct compilation *cp, union instruction op ) |
| 824 |
{ |
| 825 |
#if 1 |
| 826 |
struct x86_reg arg0 = get_arg_ptr(cp, op.alu.file0, op.alu.idx0); |
| 827 |
struct x86_reg dst = get_dst_ptr(cp, FILE_REG, op.alu.dst); |
| 828 |
struct x86_reg lit = get_arg(cp, FILE_REG, REG_LIT); |
| 829 |
struct x86_reg tmp = get_xmm_reg(cp); |
| 830 |
struct x86_reg st1 = x86_make_reg(file_x87, 1); |
| 831 |
struct x86_reg regEAX = x86_make_reg(file_REG32, reg_AX); |
| 832 |
GLubyte *fixup1, *fixup2; |
| 833 |
|
| 834 |
|
| 835 |
/* Load the interesting parts of arg0: |
| 836 |
*/ |
| 837 |
x87_fld(&cp->func, x86_make_disp(arg0, 12)); /* a3 */ |
| 838 |
x87_fld(&cp->func, x86_make_disp(arg0, 4)); /* a1 a3 */ |
| 839 |
x87_fld(&cp->func, x86_make_disp(arg0, 0)); /* a0 a1 a3 */ |
| 840 |
|
| 841 |
/* Intialize dst: |
| 842 |
*/ |
| 843 |
sse_movaps(&cp->func, tmp, lit); |
| 844 |
sse_movaps(&cp->func, dst, tmp); |
| 845 |
|
| 846 |
/* Check arg0[0]: |
| 847 |
*/ |
| 848 |
x87_fldz(&cp->func); /* 0 a0 a1 a3 */ |
| 849 |
x87_fucomp(&cp->func, st1); /* a0 a1 a3 */ |
| 850 |
x87_fnstsw(&cp->func, regEAX); |
| 851 |
x86_sahf(&cp->func); |
| 852 |
fixup1 = x86_jcc_forward(&cp->func, cc_AE); |
| 853 |
|
| 854 |
x87_fstp(&cp->func, x86_make_disp(dst, 4)); /* a1 a3 */ |
| 855 |
|
| 856 |
/* Check arg0[1]: |
| 857 |
*/ |
| 858 |
x87_fldz(&cp->func); /* 0 a1 a3 */ |
| 859 |
x87_fucomp(&cp->func, st1); /* a1 a3 */ |
| 860 |
x87_fnstsw(&cp->func, regEAX); |
| 861 |
x86_sahf(&cp->func); |
| 862 |
fixup2 = x86_jcc_forward(&cp->func, cc_AE); |
| 863 |
|
| 864 |
/* Compute pow(a1, a3) |
| 865 |
*/ |
| 866 |
x87_fyl2x(&cp->func); /* a3*log2(a1) */ |
| 867 |
|
| 868 |
emit_x87_ex2( cp ); /* 2^(a3*log2(a1)) */ |
| 869 |
|
| 870 |
x87_fstp(&cp->func, x86_make_disp(dst, 8)); |
| 871 |
|
| 872 |
/* Land jumps: |
| 873 |
*/ |
| 874 |
x86_fixup_fwd_jump(&cp->func, fixup1); |
| 875 |
x86_fixup_fwd_jump(&cp->func, fixup2); |
| 876 |
#else |
| 877 |
struct x86_reg dst = get_dst_xmm_reg(cp, FILE_REG, op.alu.dst); |
| 878 |
struct x86_reg ones = get_reg_ptr(FILE_REG, REG_LIT); |
| 879 |
sse_movups(&cp->func, dst, ones); |
| 880 |
#endif |
| 881 |
return GL_TRUE; |
| 882 |
} |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
static GLboolean emit_MAX( struct compilation *cp, union instruction op ) |
| 887 |
{ |
| 888 |
struct x86_reg arg0 = get_arg(cp, op.alu.file0, op.alu.idx0); |
| 889 |
struct x86_reg arg1 = get_arg(cp, op.alu.file1, op.alu.idx1); |
| 890 |
struct x86_reg dst = get_dst_xmm_reg(cp, FILE_REG, op.alu.dst); |
| 891 |
|
| 892 |
sse_movups(&cp->func, dst, arg0); |
| 893 |
sse_maxps(&cp->func, dst, arg1); |
| 894 |
return GL_TRUE; |
| 895 |
} |
| 896 |
|
| 897 |
|
| 898 |
static GLboolean emit_MIN( struct compilation *cp, union instruction op ) |
| 899 |
{ |
| 900 |
struct x86_reg arg0 = get_arg(cp, op.alu.file0, op.alu.idx0); |
| 901 |
struct x86_reg arg1 = get_arg(cp, op.alu.file1, op.alu.idx1); |
| 902 |
struct x86_reg dst = get_dst_xmm_reg(cp, FILE_REG, op.alu.dst); |
| 903 |
|
| 904 |
sse_movups(&cp->func, dst, arg0); |
| 905 |
sse_minps(&cp->func, dst, arg1); |
| 906 |
return GL_TRUE; |
| 907 |
} |
| 908 |
|
| 909 |
static GLboolean emit_MOV( struct compilation *cp, union instruction op ) |
| 910 |
{ |
| 911 |
struct x86_reg arg0 = get_arg(cp, op.alu.file0, op.alu.idx0); |
| 912 |
struct x86_reg dst = get_dst_xmm_reg(cp, FILE_REG, op.alu.dst); |
| 913 |
|
| 914 |
sse_movups(&cp->func, dst, arg0); |
| 915 |
return GL_TRUE; |
| 916 |
} |
| 917 |
|
| 918 |
static GLboolean emit_MUL( struct compilation *cp, union instruction op ) |
| 919 |
{ |
| 920 |
struct x86_reg arg0 = get_arg(cp, op.alu.file0, op.alu.idx0); |
| 921 |
struct x86_reg arg1 = get_arg(cp, op.alu.file1, op.alu.idx1); |
| 922 |
struct x86_reg dst = get_dst_xmm_reg(cp, FILE_REG, op.alu.dst); |
| 923 |
|
| 924 |
sse_movups(&cp->func, dst, arg0); |
| 925 |
sse_mulps(&cp->func, dst, arg1); |
| 926 |
return GL_TRUE; |
| 927 |
} |
| 928 |
|
| 929 |
|
| 930 |
static GLboolean emit_POW( struct compilation *cp, union instruction op ) |
| 931 |
{ |
| 932 |
struct x86_reg arg0 = get_arg_ptr(cp, op.alu.file0, op.alu.idx0); |
| 933 |
struct x86_reg arg1 = get_arg_ptr(cp, op.alu.file1, op.alu.idx1); |
| 934 |
struct x86_reg dst = get_dst_ptr(cp, FILE_REG, op.alu.dst); |
| 935 |
|
| 936 |
x87_fld(&cp->func, arg1); /* a1 */ |
| 937 |
x87_fld(&cp->func, arg0); /* a0 a1 */ |
| 938 |
x87_fyl2x(&cp->func); /* a1*log2(a0) */ |
| 939 |
|
| 940 |
emit_x87_ex2( cp ); /* 2^(a1*log2(a0)) */ |
| 941 |
|
| 942 |
x87_fst(&cp->func, x86_make_disp(dst, 0)); |
| 943 |
x87_fst(&cp->func, x86_make_disp(dst, 4)); |
| 944 |
x87_fst(&cp->func, x86_make_disp(dst, 8)); |
| 945 |
x87_fstp(&cp->func, x86_make_disp(dst, 12)); |
| 946 |
|
| 947 |
return GL_TRUE; |
| 948 |
} |
| 949 |
|
| 950 |
static GLboolean emit_REL( struct compilation *cp, union instruction op ) |
| 951 |
{ |
| 952 |
/* GLuint idx = (op.alu.idx0 + (GLint)cp->File[0][REG_ADDR][0]) & (MAX_NV_VERTEX_PROGRAM_PARAMS-1); */ |
| 953 |
/* GLuint idx = 0; */ |
| 954 |
/* struct x86_reg arg0 = get_arg(cp, op.alu.file0, idx); */ |
| 955 |
/* struct x86_reg dst = get_dst_xmm_reg(cp, FILE_REG, op.alu.dst); */ |
| 956 |
|
| 957 |
/* dst[0] = arg0[0]; */ |
| 958 |
/* dst[1] = arg0[1]; */ |
| 959 |
/* dst[2] = arg0[2]; */ |
| 960 |
/* dst[3] = arg0[3]; */ |
| 961 |
|
| 962 |
FAIL; |
| 963 |
} |
| 964 |
|
| 965 |
static GLboolean emit_RCP( struct compilation *cp, union instruction op ) |
| 966 |
{ |
| 967 |
struct x86_reg arg0 = get_arg(cp, op.alu.file0, op.alu.idx0); |
| 968 |
struct x86_reg dst = get_dst_xmm_reg(cp, FILE_REG, op.alu.dst); |
| 969 |
|
| 970 |
if (cp->have_sse2) { |
| 971 |
sse2_rcpss(&cp->func, dst, arg0); |
| 972 |
} |
| 973 |
else { |
| 974 |
struct x86_reg ones = get_reg_ptr(FILE_REG, REG_ONES); |
| 975 |
sse_movss(&cp->func, dst, ones); |
| 976 |
sse_divss(&cp->func, dst, arg0); |
| 977 |
} |
| 978 |
|
| 979 |
sse_shufps(&cp->func, dst, dst, SHUF(X, X, X, X)); |
| 980 |
return GL_TRUE; |
| 981 |
} |
| 982 |
|
| 983 |
static GLboolean emit_RSQ( struct compilation *cp, union instruction op ) |
| 984 |
{ |
| 985 |
struct x86_reg arg0 = get_arg(cp, op.alu.file0, op.alu.idx0); |
| 986 |
struct x86_reg dst = get_dst_xmm_reg(cp, FILE_REG, op.alu.dst); |
| 987 |
|
| 988 |
/* TODO: Calculate absolute value |
| 989 |
*/ |
| 990 |
#if 0 |
| 991 |
sse_movss(&cp->func, dst, arg0); |
| 992 |
sse_mulss(&cp->func, dst, neg); |
| 993 |
sse_maxss(&cp->func, dst, arg0); |
| 994 |
#endif |
| 995 |
|
| 996 |
sse_rsqrtss(&cp->func, dst, arg0); |
| 997 |
sse_shufps(&cp->func, dst, dst, SHUF(X, X, X, X)); |
| 998 |
return GL_TRUE; |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
static GLboolean emit_SGE( struct compilation *cp, union instruction op ) |
| 1003 |
{ |
| 1004 |
struct x86_reg arg0 = get_arg(cp, op.alu.file0, op.alu.idx0); |
| 1005 |
struct x86_reg arg1 = get_arg(cp, op.alu.file1, op.alu.idx1); |
| 1006 |
struct x86_reg dst = get_dst_xmm_reg(cp, FILE_REG, op.alu.dst); |
| 1007 |
struct x86_reg ones = get_reg_ptr(FILE_REG, REG_ONES); |
| 1008 |
|
| 1009 |
sse_movups(&cp->func, dst, arg0); |
| 1010 |
sse_cmpps(&cp->func, dst, arg1, cc_NotLessThan); |
| 1011 |
sse_andps(&cp->func, dst, ones); |
| 1012 |
return GL_TRUE; |
| 1013 |
} |
| 1014 |
|
| 1015 |
|
| 1016 |
static GLboolean emit_SLT( struct compilation *cp, union instruction op ) |
| 1017 |
{ |
| 1018 |
struct x86_reg arg0 = get_arg(cp, op.alu.file0, op.alu.idx0); |
| 1019 |
struct x86_reg arg1 = get_arg(cp, op.alu.file1, op.alu.idx1); |
| 1020 |
struct x86_reg dst = get_dst_xmm_reg(cp, FILE_REG, op.alu.dst); |
| 1021 |
struct x86_reg ones = get_reg_ptr(FILE_REG, REG_ONES); |
| 1022 |
|
| 1023 |
sse_movups(&cp->func, dst, arg0); |
| 1024 |
sse_cmpps(&cp->func, dst, arg1, cc_LessThan); |
| 1025 |
sse_andps(&cp->func, dst, ones); |
| 1026 |
return GL_TRUE; |
| 1027 |
} |
| 1028 |
|
| 1029 |
static GLboolean emit_SUB( struct compilation *cp, union instruction op ) |
| 1030 |
{ |
| 1031 |
struct x86_reg arg0 = get_arg(cp, op.alu.file0, op.alu.idx0); |
| 1032 |
struct x86_reg arg1 = get_arg(cp, op.alu.file1, op.alu.idx1); |
| 1033 |
struct x86_reg dst = get_dst_xmm_reg(cp, FILE_REG, op.alu.dst); |
| 1034 |
|
| 1035 |
sse_movups(&cp->func, dst, arg0); |
| 1036 |
sse_subps(&cp->func, dst, arg1); |
| 1037 |
return GL_TRUE; |
| 1038 |
} |
| 1039 |
|
| 1040 |
|
| 1041 |
static GLboolean emit_XPD( struct compilation *cp, union instruction op ) |
| 1042 |
{ |
| 1043 |
struct x86_reg arg0 = get_arg(cp, op.alu.file0, op.alu.idx0); |
| 1044 |
struct x86_reg arg1 = get_arg(cp, op.alu.file1, op.alu.idx1); |
| 1045 |
struct x86_reg dst = get_dst_xmm_reg(cp, FILE_REG, op.alu.dst); |
| 1046 |
struct x86_reg tmp0 = get_xmm_reg(cp); |
| 1047 |
struct x86_reg tmp1 = get_xmm_reg(cp); |
| 1048 |
|
| 1049 |
/* Could avoid tmp0, tmp1 if we overwrote arg0, arg1. Need a way |
| 1050 |
* to invalidate registers. This will come with better analysis |
| 1051 |
* (liveness analysis) of the incoming program. |
| 1052 |
*/ |
| 1053 |
emit_pshufd(cp, dst, arg0, SHUF(Y, Z, X, W)); |
| 1054 |
emit_pshufd(cp, tmp1, arg1, SHUF(Z, X, Y, W)); |
| 1055 |
sse_mulps(&cp->func, dst, tmp1); |
| 1056 |
emit_pshufd(cp, tmp0, arg0, SHUF(Z, X, Y, W)); |
| 1057 |
emit_pshufd(cp, tmp1, arg1, SHUF(Y, Z, X, W)); |
| 1058 |
sse_mulps(&cp->func, tmp0, tmp1); |
| 1059 |
sse_subps(&cp->func, dst, tmp0); |
| 1060 |
|
| 1061 |
/* dst[0] = arg0[1] * arg1[2] - arg0[2] * arg1[1]; */ |
| 1062 |
/* dst[1] = arg0[2] * arg1[0] - arg0[0] * arg1[2]; */ |
| 1063 |
/* dst[2] = arg0[0] * arg1[1] - arg0[1] * arg1[0]; */ |
| 1064 |
/* dst[3] is undef */ |
| 1065 |
|
| 1066 |
return GL_TRUE; |
| 1067 |
} |
| 1068 |
|
| 1069 |
static GLboolean emit_NOP( struct compilation *cp, union instruction op ) |
| 1070 |
{ |
| 1071 |
return GL_TRUE; |
| 1072 |
} |
| 1073 |
|
| 1074 |
|
| 1075 |
static GLboolean (* const emit_func[])(struct compilation *, union instruction) = |
| 1076 |
{ |
| 1077 |
emit_ABS, |
| 1078 |
emit_ADD, |
| 1079 |
emit_NOP, |
| 1080 |
emit_DP3, |
| 1081 |
emit_DP4, |
| 1082 |
emit_DPH, |
| 1083 |
emit_DST, |
| 1084 |
emit_NOP, |
| 1085 |
emit_EX2, |
| 1086 |
emit_EXP, |
| 1087 |
emit_FLR, |
| 1088 |
emit_FRC, |
| 1089 |
emit_LG2, |
| 1090 |
emit_LIT, |
| 1091 |
emit_LOG, |
| 1092 |
emit_NOP, |
| 1093 |
emit_MAX, |
| 1094 |
emit_MIN, |
| 1095 |
emit_MOV, |
| 1096 |
emit_MUL, |
| 1097 |
emit_POW, |
| 1098 |
emit_PRT, |
| 1099 |
emit_NOP, |
| 1100 |
emit_RCP, |
| 1101 |
emit_RSQ, |
| 1102 |
emit_SGE, |
| 1103 |
emit_SLT, |
| 1104 |
emit_SUB, |
| 1105 |
emit_RSW, |
| 1106 |
emit_XPD, |
| 1107 |
emit_RSW, |
| 1108 |
emit_MSK, |
| 1109 |
emit_REL, |
| 1110 |
}; |
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
static GLboolean build_vertex_program( struct compilation *cp ) |
| 1115 |
{ |
| 1116 |
struct arb_vp_machine *m = NULL; |
| 1117 |
GLuint j; |
| 1118 |
|
| 1119 |
struct x86_reg regEBX = x86_make_reg(file_REG32, reg_BX); |
| 1120 |
struct x86_reg regECX = x86_make_reg(file_REG32, reg_CX); |
| 1121 |
struct x86_reg regEDX = x86_make_reg(file_REG32, reg_DX); |
| 1122 |
|
| 1123 |
x86_push(&cp->func, regEBX); |
| 1124 |
|
| 1125 |
x86_mov(&cp->func, regEDX, x86_fn_arg(&cp->func, 1)); |
| 1126 |
x86_mov(&cp->func, regEBX, x86_make_disp(regEDX, get_offset(m, m->File + FILE_REG))); |
| 1127 |
x86_mov(&cp->func, regECX, x86_make_disp(regEDX, get_offset(m, m->File + FILE_STATE_PARAM))); |
| 1128 |
|
| 1129 |
for (j = 0; j < cp->p->nr_instructions; j++) { |
| 1130 |
union instruction inst = cp->p->instructions[j]; |
| 1131 |
cp->insn_counter = j+1; /* avoid zero */ |
| 1132 |
|
| 1133 |
if (DISASSEM) { |
| 1134 |
_mesa_printf("%p: ", cp->func.csr); |
| 1135 |
_tnl_disassem_vba_insn( inst ); |
| 1136 |
} |
| 1137 |
cp->func.fn = NULL; |
| 1138 |
|
| 1139 |
if (!emit_func[inst.alu.opcode]( cp, inst )) { |
| 1140 |
return GL_FALSE; |
| 1141 |
} |
| 1142 |
} |
| 1143 |
|
| 1144 |
/* TODO: only for outputs: |
| 1145 |
*/ |
| 1146 |
for (j = 0; j < 8; j++) { |
| 1147 |
if (cp->xmm[j].dirty) |
| 1148 |
spill(cp, j); |
| 1149 |
} |
| 1150 |
|
| 1151 |
|
| 1152 |
/* Exit mmx state? |
| 1153 |
*/ |
| 1154 |
if (cp->func.need_emms) |
| 1155 |
mmx_emms(&cp->func); |
| 1156 |
|
| 1157 |
/* Restore FPU control word? |
| 1158 |
*/ |
| 1159 |
if (cp->fpucntl != RESTORE_FPU) { |
| 1160 |
x87_fnclex(&cp->func); |
| 1161 |
x87_fldcw(&cp->func, x86_make_disp(regEDX, get_offset(m, &m->fpucntl_restore))); |
| 1162 |
} |
| 1163 |
|
| 1164 |
x86_pop(&cp->func, regEBX); |
| 1165 |
x86_ret(&cp->func); |
| 1166 |
|
| 1167 |
return GL_TRUE; |
| 1168 |
} |
| 1169 |
|
| 1170 |
/** |
| 1171 |
* Execute the given vertex program. |
| 1172 |
* |
| 1173 |
* TODO: Integrate the t_vertex.c code here, to build machine vertices |
| 1174 |
* directly at this point. |
| 1175 |
* |
| 1176 |
* TODO: Eliminate the VB struct entirely and just use |
| 1177 |
* struct arb_vertex_machine. |
| 1178 |
*/ |
| 1179 |
GLboolean |
| 1180 |
_tnl_sse_codegen_vertex_program(struct tnl_compiled_program *p) |
| 1181 |
{ |
| 1182 |
struct compilation cp; |
| 1183 |
|
| 1184 |
/* sanity checks */ |
| 1185 |
assert(emit_func[VP_OPCODE_ABS] == emit_ABS); |
| 1186 |
assert(emit_func[VP_OPCODE_MUL] == emit_MUL); |
| 1187 |
assert(emit_func[VP_OPCODE_XPD] == emit_XPD); |
| 1188 |
|
| 1189 |
memset(&cp, 0, sizeof(cp)); |
| 1190 |
cp.p = p; |
| 1191 |
cp.have_sse2 = 1; |
| 1192 |
|
| 1193 |
if (p->compiled_func) { |
| 1194 |
_mesa_free((void *)p->compiled_func); |
| 1195 |
p->compiled_func = NULL; |
| 1196 |
} |
| 1197 |
|
| 1198 |
x86_init_func(&cp.func); |
| 1199 |
|
| 1200 |
cp.fpucntl = RESTORE_FPU; |
| 1201 |
|
| 1202 |
|
| 1203 |
/* Note ctx state is not referenced in building the function, so it |
| 1204 |
* depends only on the list of instructions: |
| 1205 |
*/ |
| 1206 |
if (!build_vertex_program(&cp)) { |
| 1207 |
x86_release_func( &cp.func ); |
| 1208 |
return GL_FALSE; |
| 1209 |
} |
| 1210 |
|
| 1211 |
|
| 1212 |
p->compiled_func = (void (*)(struct arb_vp_machine *))x86_get_func( &cp.func ); |
| 1213 |
return GL_TRUE; |
| 1214 |
} |
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
#else |
| 1219 |
|
| 1220 |
GLboolean |
| 1221 |
_tnl_sse_codegen_vertex_program(struct tnl_compiled_program *p) |
| 1222 |
{ |
| 1223 |
/* Dummy version for when USE_SSE_ASM not defined */ |
| 1224 |
return GL_FALSE; |
| 1225 |
} |
| 1226 |
|
| 1227 |
#endif |