tgsi_exec.c 196 KB
Newer Older
<
Brian's avatar
Brian committed
1 2
/**************************************************************************
 * 
Jose Fonseca's avatar
Jose Fonseca committed
3
 * Copyright 2007-2008 VMware, Inc.
Brian's avatar
Brian committed
4
 * All Rights Reserved.
5
 * Copyright 2009-2010 VMware, Inc.  All rights Reserved.
Brian's avatar
Brian committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
Jose Fonseca's avatar
Jose Fonseca committed
22
 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
Brian's avatar
Brian committed
23 24 25 26 27 28 29
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 **************************************************************************/

/**
30
 * TGSI interpreter/executor.
Brian's avatar
Brian committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 *
 * Flow control information:
 *
 * Since we operate on 'quads' (4 pixels or 4 vertices in parallel)
 * flow control statements (IF/ELSE/ENDIF, LOOP/ENDLOOP) require special
 * care since a condition may be true for some quad components but false
 * for other components.
 *
 * We basically execute all statements (even if they're in the part of
 * an IF/ELSE clause that's "not taken") and use a special mask to
 * control writing to destination registers.  This is the ExecMask.
 * See store_dest().
 *
 * The ExecMask is computed from three other masks (CondMask, LoopMask and
 * ContMask) which are controlled by the flow control instructions (namely:
 * (IF/ELSE/ENDIF, LOOP/ENDLOOP and CONT).
 *
 *
 * Authors:
Michal Krol's avatar
Michal Krol committed
50
 *   Michal Krol
Brian's avatar
Brian committed
51 52 53
 *   Brian Paul
 */

Michal Krol's avatar
Michal Krol committed
54
#include "pipe/p_compiler.h"
55
#include "pipe/p_state.h"
Michal Krol's avatar
Michal Krol committed
56
#include "pipe/p_shader_tokens.h"
57
#include "tgsi/tgsi_dump.h"
58 59
#include "tgsi/tgsi_parse.h"
#include "tgsi/tgsi_util.h"
Michal Krol's avatar
Michal Krol committed
60
#include "tgsi_exec.h"
61
#include "util/u_half.h"
62
#include "util/u_memory.h"
63
#include "util/u_math.h"
64
#include "util/rounding.h"
65

66

67 68 69
#define DEBUG_EXECUTION 0


Dave Airlie's avatar
Dave Airlie committed
70
#define FAST_MATH 0
71

72 73 74 75 76
#define TILE_TOP_LEFT     0
#define TILE_TOP_RIGHT    1
#define TILE_BOTTOM_LEFT  2
#define TILE_BOTTOM_RIGHT 3

77 78 79
union tgsi_double_channel {
   double d[TGSI_QUAD_SIZE];
   unsigned u[TGSI_QUAD_SIZE][2];
80 81
   uint64_t u64[TGSI_QUAD_SIZE];
   int64_t i64[TGSI_QUAD_SIZE];
82 83 84 85 86 87 88
};

struct tgsi_double_vector {
   union tgsi_double_channel xy;
   union tgsi_double_channel zw;
};

89 90 91 92 93 94 95 96 97 98
static void
micro_abs(union tgsi_exec_channel *dst,
          const union tgsi_exec_channel *src)
{
   dst->f[0] = fabsf(src->f[0]);
   dst->f[1] = fabsf(src->f[1]);
   dst->f[2] = fabsf(src->f[2]);
   dst->f[3] = fabsf(src->f[3]);
}

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
static void
micro_arl(union tgsi_exec_channel *dst,
          const union tgsi_exec_channel *src)
{
   dst->i[0] = (int)floorf(src->f[0]);
   dst->i[1] = (int)floorf(src->f[1]);
   dst->i[2] = (int)floorf(src->f[2]);
   dst->i[3] = (int)floorf(src->f[3]);
}

static void
micro_arr(union tgsi_exec_channel *dst,
          const union tgsi_exec_channel *src)
{
   dst->i[0] = (int)floorf(src->f[0] + 0.5f);
   dst->i[1] = (int)floorf(src->f[1] + 0.5f);
   dst->i[2] = (int)floorf(src->f[2] + 0.5f);
   dst->i[3] = (int)floorf(src->f[3] + 0.5f);
}

119 120 121 122 123 124 125 126 127 128
static void
micro_ceil(union tgsi_exec_channel *dst,
           const union tgsi_exec_channel *src)
{
   dst->f[0] = ceilf(src->f[0]);
   dst->f[1] = ceilf(src->f[1]);
   dst->f[2] = ceilf(src->f[2]);
   dst->f[3] = ceilf(src->f[3]);
}

129 130 131 132 133 134 135 136 137 138 139 140
static void
micro_cmp(union tgsi_exec_channel *dst,
          const union tgsi_exec_channel *src0,
          const union tgsi_exec_channel *src1,
          const union tgsi_exec_channel *src2)
{
   dst->f[0] = src0->f[0] < 0.0f ? src1->f[0] : src2->f[0];
   dst->f[1] = src0->f[1] < 0.0f ? src1->f[1] : src2->f[1];
   dst->f[2] = src0->f[2] < 0.0f ? src1->f[2] : src2->f[2];
   dst->f[3] = src0->f[3] < 0.0f ? src1->f[3] : src2->f[3];
}

141 142 143 144 145 146 147 148 149 150
static void
micro_cos(union tgsi_exec_channel *dst,
          const union tgsi_exec_channel *src)
{
   dst->f[0] = cosf(src->f[0]);
   dst->f[1] = cosf(src->f[1]);
   dst->f[2] = cosf(src->f[2]);
   dst->f[3] = cosf(src->f[3]);
}

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
static void
micro_d2f(union tgsi_exec_channel *dst,
          const union tgsi_double_channel *src)
{
   dst->f[0] = (float)src->d[0];
   dst->f[1] = (float)src->d[1];
   dst->f[2] = (float)src->d[2];
   dst->f[3] = (float)src->d[3];
}

static void
micro_d2i(union tgsi_exec_channel *dst,
          const union tgsi_double_channel *src)
{
   dst->i[0] = (int)src->d[0];
   dst->i[1] = (int)src->d[1];
   dst->i[2] = (int)src->d[2];
   dst->i[3] = (int)src->d[3];
}

static void
micro_d2u(union tgsi_exec_channel *dst,
          const union tgsi_double_channel *src)
{
   dst->u[0] = (unsigned)src->d[0];
   dst->u[1] = (unsigned)src->d[1];
   dst->u[2] = (unsigned)src->d[2];
   dst->u[3] = (unsigned)src->d[3];
}
static void
micro_dabs(union tgsi_double_channel *dst,
           const union tgsi_double_channel *src)
{
   dst->d[0] = src->d[0] >= 0.0 ? src->d[0] : -src->d[0];
   dst->d[1] = src->d[1] >= 0.0 ? src->d[1] : -src->d[1];
   dst->d[2] = src->d[2] >= 0.0 ? src->d[2] : -src->d[2];
   dst->d[3] = src->d[3] >= 0.0 ? src->d[3] : -src->d[3];
}

static void
micro_dadd(union tgsi_double_channel *dst,
          const union tgsi_double_channel *src)
{
   dst->d[0] = src[0].d[0] + src[1].d[0];
   dst->d[1] = src[0].d[1] + src[1].d[1];
   dst->d[2] = src[0].d[2] + src[1].d[2];
   dst->d[3] = src[0].d[3] + src[1].d[3];
}

200 201 202 203 204 205 206 207 208 209
static void
micro_ddiv(union tgsi_double_channel *dst,
          const union tgsi_double_channel *src)
{
   dst->d[0] = src[0].d[0] / src[1].d[0];
   dst->d[1] = src[0].d[1] / src[1].d[1];
   dst->d[2] = src[0].d[2] / src[1].d[2];
   dst->d[3] = src[0].d[3] / src[1].d[3];
}

210 211 212 213 214 215 216 217 218 219
static void
micro_ddx(union tgsi_exec_channel *dst,
          const union tgsi_exec_channel *src)
{
   dst->f[0] =
   dst->f[1] =
   dst->f[2] =
   dst->f[3] = src->f[TILE_BOTTOM_RIGHT] - src->f[TILE_BOTTOM_LEFT];
}

220 221 222 223 224 225 226 227 228 229 230
static void
micro_ddx_fine(union tgsi_exec_channel *dst,
          const union tgsi_exec_channel *src)
{
   dst->f[0] =
   dst->f[1] = src->f[TILE_TOP_RIGHT] - src->f[TILE_TOP_LEFT];
   dst->f[2] =
   dst->f[3] = src->f[TILE_BOTTOM_RIGHT] - src->f[TILE_BOTTOM_LEFT];
}


231 232 233 234 235 236 237 238 239 240
static void
micro_ddy(union tgsi_exec_channel *dst,
          const union tgsi_exec_channel *src)
{
   dst->f[0] =
   dst->f[1] =
   dst->f[2] =
   dst->f[3] = src->f[TILE_BOTTOM_LEFT] - src->f[TILE_TOP_LEFT];
}

241 242 243 244 245 246 247 248 249 250
static void
micro_ddy_fine(union tgsi_exec_channel *dst,
          const union tgsi_exec_channel *src)
{
   dst->f[0] =
   dst->f[2] = src->f[TILE_BOTTOM_LEFT] - src->f[TILE_TOP_LEFT];
   dst->f[1] =
   dst->f[3] = src->f[TILE_BOTTOM_RIGHT] - src->f[TILE_TOP_RIGHT];
}

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
static void
micro_dmul(union tgsi_double_channel *dst,
           const union tgsi_double_channel *src)
{
   dst->d[0] = src[0].d[0] * src[1].d[0];
   dst->d[1] = src[0].d[1] * src[1].d[1];
   dst->d[2] = src[0].d[2] * src[1].d[2];
   dst->d[3] = src[0].d[3] * src[1].d[3];
}

static void
micro_dmax(union tgsi_double_channel *dst,
           const union tgsi_double_channel *src)
{
   dst->d[0] = src[0].d[0] > src[1].d[0] ? src[0].d[0] : src[1].d[0];
   dst->d[1] = src[0].d[1] > src[1].d[1] ? src[0].d[1] : src[1].d[1];
   dst->d[2] = src[0].d[2] > src[1].d[2] ? src[0].d[2] : src[1].d[2];
   dst->d[3] = src[0].d[3] > src[1].d[3] ? src[0].d[3] : src[1].d[3];
}

static void
micro_dmin(union tgsi_double_channel *dst,
           const union tgsi_double_channel *src)
{
   dst->d[0] = src[0].d[0] < src[1].d[0] ? src[0].d[0] : src[1].d[0];
   dst->d[1] = src[0].d[1] < src[1].d[1] ? src[0].d[1] : src[1].d[1];
   dst->d[2] = src[0].d[2] < src[1].d[2] ? src[0].d[2] : src[1].d[2];
   dst->d[3] = src[0].d[3] < src[1].d[3] ? src[0].d[3] : src[1].d[3];
}

static void
micro_dneg(union tgsi_double_channel *dst,
           const union tgsi_double_channel *src)
{
   dst->d[0] = -src->d[0];
   dst->d[1] = -src->d[1];
   dst->d[2] = -src->d[2];
   dst->d[3] = -src->d[3];
}

static void
micro_dslt(union tgsi_double_channel *dst,
           const union tgsi_double_channel *src)
{
   dst->u[0][0] = src[0].d[0] < src[1].d[0] ? ~0U : 0U;
   dst->u[1][0] = src[0].d[1] < src[1].d[1] ? ~0U : 0U;
   dst->u[2][0] = src[0].d[2] < src[1].d[2] ? ~0U : 0U;
   dst->u[3][0] = src[0].d[3] < src[1].d[3] ? ~0U : 0U;
}

static void
micro_dsne(union tgsi_double_channel *dst,
           const union tgsi_double_channel *src)
{
   dst->u[0][0] = src[0].d[0] != src[1].d[0] ? ~0U : 0U;
   dst->u[1][0] = src[0].d[1] != src[1].d[1] ? ~0U : 0U;
   dst->u[2][0] = src[0].d[2] != src[1].d[2] ? ~0U : 0U;
   dst->u[3][0] = src[0].d[3] != src[1].d[3] ? ~0U : 0U;
}

static void
micro_dsge(union tgsi_double_channel *dst,
           const union tgsi_double_channel *src)
{
   dst->u[0][0] = src[0].d[0] >= src[1].d[0] ? ~0U : 0U;
   dst->u[1][0] = src[0].d[1] >= src[1].d[1] ? ~0U : 0U;
   dst->u[2][0] = src[0].d[2] >= src[1].d[2] ? ~0U : 0U;
   dst->u[3][0] = src[0].d[3] >= src[1].d[3] ? ~0U : 0U;
}

static void
micro_dseq(union tgsi_double_channel *dst,
           const union tgsi_double_channel *src)
{
   dst->u[0][0] = src[0].d[0] == src[1].d[0] ? ~0U : 0U;
   dst->u[1][0] = src[0].d[1] == src[1].d[1] ? ~0U : 0U;
   dst->u[2][0] = src[0].d[2] == src[1].d[2] ? ~0U : 0U;
   dst->u[3][0] = src[0].d[3] == src[1].d[3] ? ~0U : 0U;
}

static void
micro_drcp(union tgsi_double_channel *dst,
           const union tgsi_double_channel *src)
{
   dst->d[0] = 1.0 / src->d[0];
   dst->d[1] = 1.0 / src->d[1];
   dst->d[2] = 1.0 / src->d[2];
   dst->d[3] = 1.0 / src->d[3];
}

static void
micro_dsqrt(union tgsi_double_channel *dst,
            const union tgsi_double_channel *src)
{
   dst->d[0] = sqrt(src->d[0]);
   dst->d[1] = sqrt(src->d[1]);
   dst->d[2] = sqrt(src->d[2]);
   dst->d[3] = sqrt(src->d[3]);
}

static void
micro_drsq(union tgsi_double_channel *dst,
          const union tgsi_double_channel *src)
{
   dst->d[0] = 1.0 / sqrt(src->d[0]);
   dst->d[1] = 1.0 / sqrt(src->d[1]);
   dst->d[2] = 1.0 / sqrt(src->d[2]);
   dst->d[3] = 1.0 / sqrt(src->d[3]);
}

static void
micro_dmad(union tgsi_double_channel *dst,
           const union tgsi_double_channel *src)
{
   dst->d[0] = src[0].d[0] * src[1].d[0] + src[2].d[0];
   dst->d[1] = src[0].d[1] * src[1].d[1] + src[2].d[1];
   dst->d[2] = src[0].d[2] * src[1].d[2] + src[2].d[2];
   dst->d[3] = src[0].d[3] * src[1].d[3] + src[2].d[3];
}

static void
micro_dfrac(union tgsi_double_channel *dst,
            const union tgsi_double_channel *src)
{
   dst->d[0] = src->d[0] - floor(src->d[0]);
   dst->d[1] = src->d[1] - floor(src->d[1]);
   dst->d[2] = src->d[2] - floor(src->d[2]);
   dst->d[3] = src->d[3] - floor(src->d[3]);
}

381 382 383 384 385 386 387 388 389 390
static void
micro_dflr(union tgsi_double_channel *dst,
           const union tgsi_double_channel *src)
{
   dst->d[0] = floor(src->d[0]);
   dst->d[1] = floor(src->d[1]);
   dst->d[2] = floor(src->d[2]);
   dst->d[3] = floor(src->d[3]);
}

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
static void
micro_dldexp(union tgsi_double_channel *dst,
             const union tgsi_double_channel *src0,
             union tgsi_exec_channel *src1)
{
   dst->d[0] = ldexp(src0->d[0], src1->i[0]);
   dst->d[1] = ldexp(src0->d[1], src1->i[1]);
   dst->d[2] = ldexp(src0->d[2], src1->i[2]);
   dst->d[3] = ldexp(src0->d[3], src1->i[3]);
}

static void
micro_dfracexp(union tgsi_double_channel *dst,
               union tgsi_exec_channel *dst_exp,
               const union tgsi_double_channel *src)
{
   dst->d[0] = frexp(src->d[0], &dst_exp->i[0]);
   dst->d[1] = frexp(src->d[1], &dst_exp->i[1]);
   dst->d[2] = frexp(src->d[2], &dst_exp->i[2]);
   dst->d[3] = frexp(src->d[3], &dst_exp->i[3]);
}

413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
static void
micro_exp2(union tgsi_exec_channel *dst,
           const union tgsi_exec_channel *src)
{
#if FAST_MATH
   dst->f[0] = util_fast_exp2(src->f[0]);
   dst->f[1] = util_fast_exp2(src->f[1]);
   dst->f[2] = util_fast_exp2(src->f[2]);
   dst->f[3] = util_fast_exp2(src->f[3]);
#else
#if DEBUG
   /* Inf is okay for this instruction, so clamp it to silence assertions. */
   uint i;
   union tgsi_exec_channel clamped;

   for (i = 0; i < 4; i++) {
      if (src->f[i] > 127.99999f) {
         clamped.f[i] = 127.99999f;
      } else if (src->f[i] < -126.99999f) {
         clamped.f[i] = -126.99999f;
      } else {
         clamped.f[i] = src->f[i];
      }
   }
   src = &clamped;
#endif /* DEBUG */

   dst->f[0] = powf(2.0f, src->f[0]);
   dst->f[1] = powf(2.0f, src->f[1]);
   dst->f[2] = powf(2.0f, src->f[2]);
   dst->f[3] = powf(2.0f, src->f[3]);
#endif /* FAST_MATH */
}

447 448 449 450 451 452 453 454 455 456
static void
micro_f2d(union tgsi_double_channel *dst,
          const union tgsi_exec_channel *src)
{
   dst->d[0] = (double)src->f[0];
   dst->d[1] = (double)src->f[1];
   dst->d[2] = (double)src->f[2];
   dst->d[3] = (double)src->f[3];
}

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
static void
micro_flr(union tgsi_exec_channel *dst,
          const union tgsi_exec_channel *src)
{
   dst->f[0] = floorf(src->f[0]);
   dst->f[1] = floorf(src->f[1]);
   dst->f[2] = floorf(src->f[2]);
   dst->f[3] = floorf(src->f[3]);
}

static void
micro_frc(union tgsi_exec_channel *dst,
          const union tgsi_exec_channel *src)
{
   dst->f[0] = src->f[0] - floorf(src->f[0]);
   dst->f[1] = src->f[1] - floorf(src->f[1]);
   dst->f[2] = src->f[2] - floorf(src->f[2]);
   dst->f[3] = src->f[3] - floorf(src->f[3]);
}

477 478 479 480 481 482 483 484 485 486
static void
micro_i2d(union tgsi_double_channel *dst,
          const union tgsi_exec_channel *src)
{
   dst->d[0] = (double)src->i[0];
   dst->d[1] = (double)src->i[1];
   dst->d[2] = (double)src->i[2];
   dst->d[3] = (double)src->i[3];
}

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
static void
micro_iabs(union tgsi_exec_channel *dst,
           const union tgsi_exec_channel *src)
{
   dst->i[0] = src->i[0] >= 0 ? src->i[0] : -src->i[0];
   dst->i[1] = src->i[1] >= 0 ? src->i[1] : -src->i[1];
   dst->i[2] = src->i[2] >= 0 ? src->i[2] : -src->i[2];
   dst->i[3] = src->i[3] >= 0 ? src->i[3] : -src->i[3];
}

static void
micro_ineg(union tgsi_exec_channel *dst,
           const union tgsi_exec_channel *src)
{
   dst->i[0] = -src->i[0];
   dst->i[1] = -src->i[1];
   dst->i[2] = -src->i[2];
   dst->i[3] = -src->i[3];
}

507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
static void
micro_lg2(union tgsi_exec_channel *dst,
          const union tgsi_exec_channel *src)
{
#if FAST_MATH
   dst->f[0] = util_fast_log2(src->f[0]);
   dst->f[1] = util_fast_log2(src->f[1]);
   dst->f[2] = util_fast_log2(src->f[2]);
   dst->f[3] = util_fast_log2(src->f[3]);
#else
   dst->f[0] = logf(src->f[0]) * 1.442695f;
   dst->f[1] = logf(src->f[1]) * 1.442695f;
   dst->f[2] = logf(src->f[2]) * 1.442695f;
   dst->f[3] = logf(src->f[3]) * 1.442695f;
#endif
}

static void
micro_lrp(union tgsi_exec_channel *dst,
526 527 528
          const union tgsi_exec_channel *src0,
          const union tgsi_exec_channel *src1,
          const union tgsi_exec_channel *src2)
529
{
530 531 532 533
   dst->f[0] = src0->f[0] * (src1->f[0] - src2->f[0]) + src2->f[0];
   dst->f[1] = src0->f[1] * (src1->f[1] - src2->f[1]) + src2->f[1];
   dst->f[2] = src0->f[2] * (src1->f[2] - src2->f[2]) + src2->f[2];
   dst->f[3] = src0->f[3] * (src1->f[3] - src2->f[3]) + src2->f[3];
534 535 536 537
}

static void
micro_mad(union tgsi_exec_channel *dst,
538 539 540
          const union tgsi_exec_channel *src0,
          const union tgsi_exec_channel *src1,
          const union tgsi_exec_channel *src2)
541
{
542 543 544 545
   dst->f[0] = src0->f[0] * src1->f[0] + src2->f[0];
   dst->f[1] = src0->f[1] * src1->f[1] + src2->f[1];
   dst->f[2] = src0->f[2] * src1->f[2] + src2->f[2];
   dst->f[3] = src0->f[3] * src1->f[3] + src2->f[3];
546 547
}

548 549 550 551 552 553 554 555 556 557
static void
micro_mov(union tgsi_exec_channel *dst,
          const union tgsi_exec_channel *src)
{
   dst->u[0] = src->u[0];
   dst->u[1] = src->u[1];
   dst->u[2] = src->u[2];
   dst->u[3] = src->u[3];
}

558 559 560 561
static void
micro_rcp(union tgsi_exec_channel *dst,
          const union tgsi_exec_channel *src)
{
562 563 564 565 566 567
#if 0 /* for debugging */
   assert(src->f[0] != 0.0f);
   assert(src->f[1] != 0.0f);
   assert(src->f[2] != 0.0f);
   assert(src->f[3] != 0.0f);
#endif
568 569 570 571 572 573 574 575 576 577
   dst->f[0] = 1.0f / src->f[0];
   dst->f[1] = 1.0f / src->f[1];
   dst->f[2] = 1.0f / src->f[2];
   dst->f[3] = 1.0f / src->f[3];
}

static void
micro_rnd(union tgsi_exec_channel *dst,
          const union tgsi_exec_channel *src)
{
578 579 580 581
   dst->f[0] = _mesa_roundevenf(src->f[0]);
   dst->f[1] = _mesa_roundevenf(src->f[1]);
   dst->f[2] = _mesa_roundevenf(src->f[2]);
   dst->f[3] = _mesa_roundevenf(src->f[3]);
582 583 584 585 586 587
}

static void
micro_rsq(union tgsi_exec_channel *dst,
          const union tgsi_exec_channel *src)
{
588 589 590 591 592 593
#if 0 /* for debugging */
   assert(src->f[0] != 0.0f);
   assert(src->f[1] != 0.0f);
   assert(src->f[2] != 0.0f);
   assert(src->f[3] != 0.0f);
#endif
594 595 596 597
   dst->f[0] = 1.0f / sqrtf(src->f[0]);
   dst->f[1] = 1.0f / sqrtf(src->f[1]);
   dst->f[2] = 1.0f / sqrtf(src->f[2]);
   dst->f[3] = 1.0f / sqrtf(src->f[3]);
598 599
}

600 601 602 603
static void
micro_sqrt(union tgsi_exec_channel *dst,
           const union tgsi_exec_channel *src)
{
604 605 606 607
   dst->f[0] = sqrtf(src->f[0]);
   dst->f[1] = sqrtf(src->f[1]);
   dst->f[2] = sqrtf(src->f[2]);
   dst->f[3] = sqrtf(src->f[3]);
608 609
}

610 611
static void
micro_seq(union tgsi_exec_channel *dst,
612 613
          const union tgsi_exec_channel *src0,
          const union tgsi_exec_channel *src1)
614
{
615 616 617 618
   dst->f[0] = src0->f[0] == src1->f[0] ? 1.0f : 0.0f;
   dst->f[1] = src0->f[1] == src1->f[1] ? 1.0f : 0.0f;
   dst->f[2] = src0->f[2] == src1->f[2] ? 1.0f : 0.0f;
   dst->f[3] = src0->f[3] == src1->f[3] ? 1.0f : 0.0f;
619 620 621 622
}

static void
micro_sge(union tgsi_exec_channel *dst,
623 624
          const union tgsi_exec_channel *src0,
          const union tgsi_exec_channel *src1)
625
{
626 627 628 629
   dst->f[0] = src0->f[0] >= src1->f[0] ? 1.0f : 0.0f;
   dst->f[1] = src0->f[1] >= src1->f[1] ? 1.0f : 0.0f;
   dst->f[2] = src0->f[2] >= src1->f[2] ? 1.0f : 0.0f;
   dst->f[3] = src0->f[3] >= src1->f[3] ? 1.0f : 0.0f;
630 631 632 633 634 635 636 637 638 639 640 641
}

static void
micro_sgn(union tgsi_exec_channel *dst,
          const union tgsi_exec_channel *src)
{
   dst->f[0] = src->f[0] < 0.0f ? -1.0f : src->f[0] > 0.0f ? 1.0f : 0.0f;
   dst->f[1] = src->f[1] < 0.0f ? -1.0f : src->f[1] > 0.0f ? 1.0f : 0.0f;
   dst->f[2] = src->f[2] < 0.0f ? -1.0f : src->f[2] > 0.0f ? 1.0f : 0.0f;
   dst->f[3] = src->f[3] < 0.0f ? -1.0f : src->f[3] > 0.0f ? 1.0f : 0.0f;
}

Dave Airlie's avatar
Dave Airlie committed
642 643 644 645 646 647 648 649 650 651
static void
micro_isgn(union tgsi_exec_channel *dst,
          const union tgsi_exec_channel *src)
{
   dst->i[0] = src->i[0] < 0 ? -1 : src->i[0] > 0 ? 1 : 0;
   dst->i[1] = src->i[1] < 0 ? -1 : src->i[1] > 0 ? 1 : 0;
   dst->i[2] = src->i[2] < 0 ? -1 : src->i[2] > 0 ? 1 : 0;
   dst->i[3] = src->i[3] < 0 ? -1 : src->i[3] > 0 ? 1 : 0;
}

652 653
static void
micro_sgt(union tgsi_exec_channel *dst,
654 655
          const union tgsi_exec_channel *src0,
          const union tgsi_exec_channel *src1)
656
{
657 658 659 660
   dst->f[0] = src0->f[0] > src1->f[0] ? 1.0f : 0.0f;
   dst->f[1] = src0->f[1] > src1->f[1] ? 1.0f : 0.0f;
   dst->f[2] = src0->f[2] > src1->f[2] ? 1.0f : 0.0f;
   dst->f[3] = src0->f[3] > src1->f[3] ? 1.0f : 0.0f;
661 662 663 664 665 666 667 668 669 670 671 672 673 674
}

static void
micro_sin(union tgsi_exec_channel *dst,
          const union tgsi_exec_channel *src)
{
   dst->f[0] = sinf(src->f[0]);
   dst->f[1] = sinf(src->f[1]);
   dst->f[2] = sinf(src->f[2]);
   dst->f[3] = sinf(src->f[3]);
}

static void
micro_sle(union tgsi_exec_channel *dst,
675 676
          const union tgsi_exec_channel *src0,
          const union tgsi_exec_channel *src1)
677
{
678 679 680 681
   dst->f[0] = src0->f[0] <= src1->f[0] ? 1.0f : 0.0f;
   dst->f[1] = src0->f[1] <= src1->f[1] ? 1.0f : 0.0f;
   dst->f[2] = src0->f[2] <= src1->f[2] ? 1.0f : 0.0f;
   dst->f[3] = src0->f[3] <= src1->f[3] ? 1.0f : 0.0f;
682 683 684 685
}

static void
micro_slt(union tgsi_exec_channel *dst,
686 687
          const union tgsi_exec_channel *src0,
          const union tgsi_exec_channel *src1)
688
{
689 690 691 692
   dst->f[0] = src0->f[0] < src1->f[0] ? 1.0f : 0.0f;
   dst->f[1] = src0->f[1] < src1->f[1] ? 1.0f : 0.0f;
   dst->f[2] = src0->f[2] < src1->f[2] ? 1.0f : 0.0f;
   dst->f[3] = src0->f[3] < src1->f[3] ? 1.0f : 0.0f;
693 694 695 696
}

static void
micro_sne(union tgsi_exec_channel *dst,
697 698
          const union tgsi_exec_channel *src0,
          const union tgsi_exec_channel *src1)
699
{
700 701 702 703
   dst->f[0] = src0->f[0] != src1->f[0] ? 1.0f : 0.0f;
   dst->f[1] = src0->f[1] != src1->f[1] ? 1.0f : 0.0f;
   dst->f[2] = src0->f[2] != src1->f[2] ? 1.0f : 0.0f;
   dst->f[3] = src0->f[3] != src1->f[3] ? 1.0f : 0.0f;
704 705 706 707 708 709
}

static void
micro_trunc(union tgsi_exec_channel *dst,
            const union tgsi_exec_channel *src)
{
Lars Hamre's avatar
Lars Hamre committed
710 711 712 713
   dst->f[0] = truncf(src->f[0]);
   dst->f[1] = truncf(src->f[1]);
   dst->f[2] = truncf(src->f[2]);
   dst->f[3] = truncf(src->f[3]);
714 715
}

716 717 718 719 720 721 722 723 724
static void
micro_u2d(union tgsi_double_channel *dst,
          const union tgsi_exec_channel *src)
{
   dst->d[0] = (double)src->u[0];
   dst->d[1] = (double)src->u[1];
   dst->d[2] = (double)src->u[2];
   dst->d[3] = (double)src->u[3];
}
725

726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
static void
micro_i64abs(union tgsi_double_channel *dst,
             const union tgsi_double_channel *src)
{
   dst->i64[0] = src->i64[0] >= 0.0 ? src->i64[0] : -src->i64[0];
   dst->i64[1] = src->i64[1] >= 0.0 ? src->i64[1] : -src->i64[1];
   dst->i64[2] = src->i64[2] >= 0.0 ? src->i64[2] : -src->i64[2];
   dst->i64[3] = src->i64[3] >= 0.0 ? src->i64[3] : -src->i64[3];
}

static void
micro_i64sgn(union tgsi_double_channel *dst,
             const union tgsi_double_channel *src)
{
   dst->i64[0] = src->i64[0] < 0 ? -1 : src->i64[0] > 0 ? 1 : 0;
   dst->i64[1] = src->i64[1] < 0 ? -1 : src->i64[1] > 0 ? 1 : 0;
   dst->i64[2] = src->i64[2] < 0 ? -1 : src->i64[2] > 0 ? 1 : 0;
   dst->i64[3] = src->i64[3] < 0 ? -1 : src->i64[3] > 0 ? 1 : 0;
}

static void
micro_i64neg(union tgsi_double_channel *dst,
             const union tgsi_double_channel *src)
{
   dst->i64[0] = -src->i64[0];
   dst->i64[1] = -src->i64[1];
   dst->i64[2] = -src->i64[2];
   dst->i64[3] = -src->i64[3];
}

static void
micro_u64seq(union tgsi_double_channel *dst,
           const union tgsi_double_channel *src)
{
   dst->u[0][0] = src[0].u64[0] == src[1].u64[0] ? ~0U : 0U;
   dst->u[1][0] = src[0].u64[1] == src[1].u64[1] ? ~0U : 0U;
   dst->u[2][0] = src[0].u64[2] == src[1].u64[2] ? ~0U : 0U;
   dst->u[3][0] = src[0].u64[3] == src[1].u64[3] ? ~0U : 0U;
}

static void
micro_u64sne(union tgsi_double_channel *dst,
             const union tgsi_double_channel *src)
{
   dst->u[0][0] = src[0].u64[0] != src[1].u64[0] ? ~0U : 0U;
   dst->u[1][0] = src[0].u64[1] != src[1].u64[1] ? ~0U : 0U;
   dst->u[2][0] = src[0].u64[2] != src[1].u64[2] ? ~0U : 0U;
   dst->u[3][0] = src[0].u64[3] != src[1].u64[3] ? ~0U : 0U;
}

static void
micro_i64slt(union tgsi_double_channel *dst,
             const union tgsi_double_channel *src)
{
   dst->u[0][0] = src[0].i64[0] < src[1].i64[0] ? ~0U : 0U;
   dst->u[1][0] = src[0].i64[1] < src[1].i64[1] ? ~0U : 0U;
   dst->u[2][0] = src[0].i64[2] < src[1].i64[2] ? ~0U : 0U;
   dst->u[3][0] = src[0].i64[3] < src[1].i64[3] ? ~0U : 0U;
}

static void
micro_u64slt(union tgsi_double_channel *dst,
             const union tgsi_double_channel *src)
{
   dst->u[0][0] = src[0].u64[0] < src[1].u64[0] ? ~0U : 0U;
   dst->u[1][0] = src[0].u64[1] < src[1].u64[1] ? ~0U : 0U;
   dst->u[2][0] = src[0].u64[2] < src[1].u64[2] ? ~0U : 0U;
   dst->u[3][0] = src[0].u64[3] < src[1].u64[3] ? ~0U : 0U;
}

static void
micro_i64sge(union tgsi_double_channel *dst,
           const union tgsi_double_channel *src)
{
   dst->u[0][0] = src[0].i64[0] >= src[1].i64[0] ? ~0U : 0U;
   dst->u[1][0] = src[0].i64[1] >= src[1].i64[1] ? ~0U : 0U;
   dst->u[2][0] = src[0].i64[2] >= src[1].i64[2] ? ~0U : 0U;
   dst->u[3][0] = src[0].i64[3] >= src[1].i64[3] ? ~0U : 0U;
}

static void
micro_u64sge(union tgsi_double_channel *dst,
             const union tgsi_double_channel *src)
{
   dst->u[0][0] = src[0].u64[0] >= src[1].u64[0] ? ~0U : 0U;
   dst->u[1][0] = src[0].u64[1] >= src[1].u64[1] ? ~0U : 0U;
   dst->u[2][0] = src[0].u64[2] >= src[1].u64[2] ? ~0U : 0U;
   dst->u[3][0] = src[0].u64[3] >= src[1].u64[3] ? ~0U : 0U;
}

static void
micro_u64max(union tgsi_double_channel *dst,
             const union tgsi_double_channel *src)
{
   dst->u64[0] = src[0].u64[0] > src[1].u64[0] ? src[0].u64[0] : src[1].u64[0];
   dst->u64[1] = src[0].u64[1] > src[1].u64[1] ? src[0].u64[1] : src[1].u64[1];
   dst->u64[2] = src[0].u64[2] > src[1].u64[2] ? src[0].u64[2] : src[1].u64[2];
   dst->u64[3] = src[0].u64[3] > src[1].u64[3] ? src[0].u64[3] : src[1].u64[3];
}

static void
micro_i64max(union tgsi_double_channel *dst,
             const union tgsi_double_channel *src)
{
   dst->i64[0] = src[0].i64[0] > src[1].i64[0] ? src[0].i64[0] : src[1].i64[0];
   dst->i64[1] = src[0].i64[1] > src[1].i64[1] ? src[0].i64[1] : src[1].i64[1];
   dst->i64[2] = src[0].i64[2] > src[1].i64[2] ? src[0].i64[2] : src[1].i64[2];
   dst->i64[3] = src[0].i64[3] > src[1].i64[3] ? src[0].i64[3] : src[1].i64[3];
}

static void
micro_u64min(union tgsi_double_channel *dst,
             const union tgsi_double_channel *src)
{
   dst->u64[0] = src[0].u64[0] < src[1].u64[0] ? src[0].u64[0] : src[1].u64[0];
   dst->u64[1] = src[0].u64[1] < src[1].u64[1] ? src[0].u64[1] : src[1].u64[1];
   dst->u64[2] = src[0].u64[2] < src[1].u64[2] ? src[0].u64[2] : src[1].u64[2];
   dst->u64[3] = src[0].u64[3] < src[1].u64[3] ? src[0].u64[3] : src[1].u64[3];
}

static void
micro_i64min(union tgsi_double_channel *dst,
             const union tgsi_double_channel *src)
{
   dst->i64[0] = src[0].i64[0] < src[1].i64[0] ? src[0].i64[0] : src[1].i64[0];
   dst->i64[1] = src[0].i64[1] < src[1].i64[1] ? src[0].i64[1] : src[1].i64[1];
   dst->i64[2] = src[0].i64[2] < src[1].i64[2] ? src[0].i64[2] : src[1].i64[2];
   dst->i64[3] = src[0].i64[3] < src[1].i64[3] ? src[0].i64[3] : src[1].i64[3];
}

static void
micro_u64add(union tgsi_double_channel *dst,
             const union tgsi_double_channel *src)
{
   dst->u64[0] = src[0].u64[0] + src[1].u64[0];
   dst->u64[1] = src[0].u64[1] + src[1].u64[1];
   dst->u64[2] = src[0].u64[2] + src[1].u64[2];
   dst->u64[3] = src[0].u64[3] + src[1].u64[3];
}

static void
micro_u64mul(union tgsi_double_channel *dst,
             const union tgsi_double_channel *src)
{
   dst->u64[0] = src[0].u64[0] * src[1].u64[0];
   dst->u64[1] = src[0].u64[1] * src[1].u64[1];
   dst->u64[2] = src[0].u64[2] * src[1].u64[2];
   dst->u64[3] = src[0].u64[3] * src[1].u64[3];
}

static void
micro_u64div(union tgsi_double_channel *dst,
             const union tgsi_double_channel *src)
{
880 881 882 883
   dst->u64[0] = src[1].u64[0] ? src[0].u64[0] / src[1].u64[0] : ~0ull;
   dst->u64[1] = src[1].u64[1] ? src[0].u64[1] / src[1].u64[1] : ~0ull;
   dst->u64[2] = src[1].u64[2] ? src[0].u64[2] / src[1].u64[2] : ~0ull;
   dst->u64[3] = src[1].u64[3] ? src[0].u64[3] / src[1].u64[3] : ~0ull;
884 885 886 887 888 889
}

static void
micro_i64div(union tgsi_double_channel *dst,
             const union tgsi_double_channel *src)
{
890 891 892 893
   dst->i64[0] = src[1].i64[0] ? src[0].i64[0] / src[1].i64[0] : 0;
   dst->i64[1] = src[1].i64[1] ? src[0].i64[1] / src[1].i64[1] : 0;
   dst->i64[2] = src[1].i64[2] ? src[0].i64[2] / src[1].i64[2] : 0;
   dst->i64[3] = src[1].i64[3] ? src[0].i64[3] / src[1].i64[3] : 0;
894 895 896 897 898 899
}

static void
micro_u64mod(union tgsi_double_channel *dst,
             const union tgsi_double_channel *src)
{
900 901 902 903
   dst->u64[0] = src[1].u64[0] ? src[0].u64[0] % src[1].u64[0] : ~0ull;
   dst->u64[1] = src[1].u64[1] ? src[0].u64[1] % src[1].u64[1] : ~0ull;
   dst->u64[2] = src[1].u64[2] ? src[0].u64[2] % src[1].u64[2] : ~0ull;
   dst->u64[3] = src[1].u64[3] ? src[0].u64[3] % src[1].u64[3] : ~0ull;
904 905 906 907 908 909
}

static void
micro_i64mod(union tgsi_double_channel *dst,
             const union tgsi_double_channel *src)
{
910 911 912 913
   dst->i64[0] = src[1].i64[0] ? src[0].i64[0] % src[1].i64[0] : ~0ll;
   dst->i64[1] = src[1].i64[1] ? src[0].i64[1] % src[1].i64[1] : ~0ll;
   dst->i64[2] = src[1].i64[2] ? src[0].i64[2] % src[1].i64[2] : ~0ll;
   dst->i64[3] = src[1].i64[3] ? src[0].i64[3] % src[1].i64[3] : ~0ll;
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
}

static void
micro_u64shl(union tgsi_double_channel *dst,
             const union tgsi_double_channel *src0,
             union tgsi_exec_channel *src1)
{
   unsigned masked_count;
   masked_count = src1->u[0] & 0x3f;
   dst->u64[0] = src0->u64[0] << masked_count;
   masked_count = src1->u[1] & 0x3f;
   dst->u64[1] = src0->u64[1] << masked_count;
   masked_count = src1->u[2] & 0x3f;
   dst->u64[2] = src0->u64[2] << masked_count;
   masked_count = src1->u[3] & 0x3f;
   dst->u64[3] = src0->u64[3] << masked_count;
}

static void
micro_i64shr(union tgsi_double_channel *dst,
             const union tgsi_double_channel *src0,
             union tgsi_exec_channel *src1)
{
   unsigned masked_count;
   masked_count = src1->u[0] & 0x3f;
   dst->i64[0] = src0->i64[0] >> masked_count;
   masked_count = src1->u[1] & 0x3f;
   dst->i64[1] = src0->i64[1] >> masked_count;
   masked_count = src1->u[2] & 0x3f;
   dst->i64[2] = src0->i64[2] >> masked_count;
   masked_count = src1->u[3] & 0x3f;
   dst->i64[3] = src0->i64[3] >> masked_count;
}

static void
micro_u64shr(union tgsi_double_channel *dst,
             const union tgsi_double_channel *src0,
             union tgsi_exec_channel *src1)
{
   unsigned masked_count;
   masked_count = src1->u[0] & 0x3f;
   dst->u64[0] = src0->u64[0] >> masked_count;
   masked_count = src1->u[1] & 0x3f;
   dst->u64[1] = src0->u64[1] >> masked_count;
   masked_count = src1->u[2] & 0x3f;
   dst->u64[2] = src0->u64[2] >> masked_count;
   masked_count = src1->u[3] & 0x3f;
   dst->u64[3] = src0->u64[3] >> masked_count;
}

964 965 966
enum tgsi_exec_datatype {
   TGSI_EXEC_DATA_FLOAT,
   TGSI_EXEC_DATA_INT,
967
   TGSI_EXEC_DATA_UINT,
968 969 970
   TGSI_EXEC_DATA_DOUBLE,
   TGSI_EXEC_DATA_INT64,
   TGSI_EXEC_DATA_UINT64,
971 972
};

Brian's avatar
Brian committed
973 974 975
/*
 * Shorthand locations of various utility registers (_I = Index, _C = Channel)
 */
976 977 978 979 980 981
#define TEMP_KILMASK_I     TGSI_EXEC_TEMP_KILMASK_I
#define TEMP_KILMASK_C     TGSI_EXEC_TEMP_KILMASK_C
#define TEMP_OUTPUT_I      TGSI_EXEC_TEMP_OUTPUT_I
#define TEMP_OUTPUT_C      TGSI_EXEC_TEMP_OUTPUT_C
#define TEMP_PRIMITIVE_I   TGSI_EXEC_TEMP_PRIMITIVE_I
#define TEMP_PRIMITIVE_C   TGSI_EXEC_TEMP_PRIMITIVE_C
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
#define TEMP_PRIMITIVE_S1_I   TGSI_EXEC_TEMP_PRIMITIVE_S1_I
#define TEMP_PRIMITIVE_S1_C   TGSI_EXEC_TEMP_PRIMITIVE_S1_C
#define TEMP_PRIMITIVE_S2_I   TGSI_EXEC_TEMP_PRIMITIVE_S2_I
#define TEMP_PRIMITIVE_S2_C   TGSI_EXEC_TEMP_PRIMITIVE_S2_C
#define TEMP_PRIMITIVE_S3_I   TGSI_EXEC_TEMP_PRIMITIVE_S3_I
#define TEMP_PRIMITIVE_S3_C   TGSI_EXEC_TEMP_PRIMITIVE_S3_C

static const struct {
   int idx;
   int chan;
} temp_prim_idxs[] = {
   { TEMP_PRIMITIVE_I, TEMP_PRIMITIVE_C },
   { TEMP_PRIMITIVE_S1_I, TEMP_PRIMITIVE_S1_C },
   { TEMP_PRIMITIVE_S2_I, TEMP_PRIMITIVE_S2_C },
   { TEMP_PRIMITIVE_S3_I, TEMP_PRIMITIVE_S3_C },
};
998 999 1000

/** The execution mask depends on the conditional mask and the loop mask */
#define UPDATE_EXEC_MASK(MACH) \
1001
      MACH->ExecMask = MACH->CondMask & MACH->LoopMask & MACH->ContMask & MACH->Switch.mask & MACH->FuncMask
1002

1003 1004 1005 1006

static const union tgsi_exec_channel ZeroVec =
   { { 0.0, 0.0, 0.0, 0.0 } };

1007 1008 1009 1010
static const union tgsi_exec_channel OneVec = {
   {1.0f, 1.0f, 1.0f, 1.0f}
};

1011 1012 1013 1014 1015 1016 1017 1018
static const union tgsi_exec_channel P128Vec = {
   {128.0f, 128.0f, 128.0f, 128.0f}
};

static const union tgsi_exec_channel M128Vec = {
   {-128.0f, -128.0f, -128.0f, -128.0f}
};

1019

1020 1021 1022 1023 1024 1025
/**
 * Assert that none of the float values in 'chan' are infinite or NaN.
 * NaN and Inf may occur normally during program execution and should
 * not lead to crashes, etc.  But when debugging, it's helpful to catch
 * them.
 */
1026
static inline void
1027 1028 1029 1030 1031 1032 1033
check_inf_or_nan(const union tgsi_exec_channel *chan)
{
   assert(!util_is_inf_or_nan((chan)->f[0]));
   assert(!util_is_inf_or_nan((chan)->f[1]));
   assert(!util_is_inf_or_nan((chan)->f[2]));
   assert(!util_is_inf_or_nan((chan)->f[3]));
}
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064


#ifdef DEBUG
static void
print_chan(const char *msg, const union tgsi_exec_channel *chan)
{
   debug_printf("%s = {%f, %f, %f, %f}\n",
                msg, chan->f[0], chan->f[1], chan->f[2], chan->f[3]);
}
#endif


#ifdef DEBUG
static void
print_temp(const struct tgsi_exec_machine *mach, uint index)
{
   const struct tgsi_exec_vector *tmp = &mach->Temps[index];
   int i;
   debug_printf("Temp[%u] =\n", index);
   for (i = 0; i < 4; i++) {
      debug_printf("  %c: { %f, %f, %f, %f }\n",
                   "XYZW"[i],
                   tmp->xyzw[i].f[0],
                   tmp->xyzw[i].f[1],
                   tmp->xyzw[i].f[2],
                   tmp->xyzw[i].f[3]);
   }
}
#endif


1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
void
tgsi_exec_set_constant_buffers(struct tgsi_exec_machine *mach,
                               unsigned num_bufs,
                               const void **bufs,
                               const unsigned *buf_sizes)
{
   unsigned i;

   for (i = 0; i < num_bufs; i++) {
      mach->Consts[i] = bufs[i];
      mach->ConstsSize[i] = buf_sizes[i];
   }
}

1079 1080 1081 1082 1083 1084 1085 1086 1087
/**
 * Initialize machine state by expanding tokens to full instructions,
 * allocating temporary storage, setting up constants, etc.
 * After this, we can call tgsi_exec_machine_run() many times.
 */
void 
tgsi_exec_machine_bind_shader(
   struct tgsi_exec_machine *mach,
   const struct tgsi_token *tokens,
1088
   struct tgsi_sampler *sampler,
1089 1090
   struct tgsi_image *image,
   struct tgsi_buffer *buffer)
1091
{
1092
   uint k;
1093
   struct tgsi_parse_context parse;
1094 1095 1096 1097 1098
   struct tgsi_full_instruction *instructions;
   struct tgsi_full_declaration *declarations;
   uint maxInstructions = 10, numInstructions = 0;
   uint maxDeclarations = 10, numDeclarations = 0;

1099 1100 1101 1102
#if 0
   tgsi_dump(tokens, 0);
#endif

1103 1104
   util_init_math();

1105

1106
   mach->Tokens = tokens;
1107
   mach->Sampler = sampler;
1108
   mach->Image = image;
1109
   mach->Buffer = buffer;
1110

1111 1112
   if (!tokens) {
      /* unbind and free all */
1113
      FREE(mach->Declarations);
1114 1115 1116
      mach->Declarations = NULL;
      mach->NumDeclarations = 0;

1117
      FREE(mach->Instructions);
1118 1119 1120 1121 1122 1123
      mach->Instructions = NULL;
      mach->NumInstructions = 0;

      return;
   }

1124 1125 1126 1127 1128 1129
   k = tgsi_parse_init (&parse, mach->Tokens);
   if (k != TGSI_PARSE_OK) {
      debug_printf( "Problem parsing!\n" );
      return;
   }

1130
   mach->ImmLimit = 0;
1131
   mach->NumOutputs = 0;
1132

1133 1134 1135
   for (k = 0; k < TGSI_SEMANTIC_COUNT; k++)
      mach->SysSemanticToIndex[k] = -1;

1136
   if (mach->ShaderType == PIPE_SHADER_GEOMETRY &&
1137
       !mach->UsedGeometryShader) {
1138 1139 1140 1141
      struct tgsi_exec_vector *inputs;
      struct tgsi_exec_vector *outputs;

      inputs = align_malloc(sizeof(struct tgsi_exec_vector) *
1142
                            TGSI_MAX_PRIM_VERTICES * PIPE_MAX_SHADER_INPUTS,
1143
                            16);
1144 1145 1146

      if (!inputs)
         return;
1147 1148 1149 1150

      outputs = align_malloc(sizeof(struct tgsi_exec_vector) *
                             TGSI_MAX_TOTAL_VERTICES, 16);

1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
      if (!outputs) {
         align_free(inputs);
         return;
      }

      align_free(mach->Inputs);
      align_free(mach->Outputs);

      mach->Inputs = inputs;
      mach->Outputs = outputs;
      mach->UsedGeometryShader = TRUE;
   }

1164
   declarations = (struct tgsi_full_declaration *)
Michal Krol's avatar
Michal Krol committed
1165
      MALLOC( maxDeclarations * sizeof(struct tgsi_full_declaration) );
1166

1167 1168 1169 1170
   if (!declarations) {
      return;
   }

1171
   instructions = (struct tgsi_full_instruction *)
Michal Krol's avatar
Michal Krol committed
1172
      MALLOC( maxInstructions * sizeof(struct tgsi_full_instruction) );
1173

1174 1175 1176 1177
   if (!instructions) {
      FREE( declarations );
      return;
   }
1178

1179
   while( !tgsi_parse_end_of_tokens( &parse ) ) {
Brian's avatar
Brian committed
1180
      uint i;
1181

1182 1183 1184
      tgsi_parse_token( &parse );
      switch( parse.FullToken.Token.Type ) {
      case TGSI_TOKEN_TYPE_DECLARATION:
1185
         /* save expanded declaration */
1186
         if (numDeclarations == maxDeclarations) {
Michal Krol's avatar
Michal Krol committed
1187
            declarations = REALLOC(declarations,
1188
                                   maxDeclarations
1189
                                   * sizeof(struct tgsi_full_declaration),
Michal Krol's avatar
Michal Krol committed
1190
                                   (maxDeclarations + 10)
1191
                                   * sizeof(struct tgsi_full_declaration));
Michal Krol's avatar
Michal Krol committed
1192
            maxDeclarations += 10;
1193
         }
1194 1195
         if (parse.FullToken.FullDeclaration.Declaration.File == TGSI_FILE_OUTPUT)
            mach->NumOutputs = MAX2(mach->NumOutputs, parse.FullToken.FullDeclaration.Range.Last + 1);
1196 1197 1198 1199 1200
         else if (parse.FullToken.FullDeclaration.Declaration.File == TGSI_FILE_SYSTEM_VALUE) {
            const struct tgsi_full_declaration *decl = &parse.FullToken.FullDeclaration;
            mach->SysSemanticToIndex[decl->Semantic.Name] = decl->Range.First;
         }

1201
         memcpy(declarations + numDeclarations,
1202
                &parse.FullToken.FullDeclaration,
1203 1204 1205
                sizeof(declarations[0]));
         numDeclarations++;
         break;
1206

1207
      case TGSI_TOKEN_TYPE_IMMEDIATE:
1208
         {
1209
            uint size = parse.FullToken.FullImmediate.Immediate.NrTokens - 1;
1210
            assert( size <= 4 );
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
            if (mach->ImmLimit >= mach->ImmsReserved) {
               unsigned newReserved = mach->ImmsReserved ? 2 * mach->ImmsReserved : 128;
               float4 *imms = REALLOC(mach->Imms, mach->ImmsReserved, newReserved * sizeof(float4));
               if (imms) {
                  mach->ImmsReserved = newReserved;
                  mach->Imms = imms;
               } else {
                  debug_printf("Unable to (re)allocate space for immidiate constants\n");
                  break;
               }
            }
1222 1223

            for( i = 0; i < size; i++ ) {
1224 1225
               mach->Imms[mach->ImmLimit][i] = 
		  parse.FullToken.FullImmediate.u[i].Float;
1226
            }
1227
            mach->ImmLimit += 1;
1228
         }
1229
         break;
1230

1231
      case TGSI_TOKEN_TYPE_INSTRUCTION:
1232 1233

         /* save expanded instruction */
1234
         if (numInstructions == maxInstructions) {
Michal Krol's avatar
Michal Krol committed
1235
            instructions = REALLOC(instructions,
1236
                                   maxInstructions
Michal Krol's avatar
Michal Krol committed
1237 1238
                                   * sizeof(struct tgsi_full_instruction),
                                   (maxInstructions + 10)
1239
                                   * sizeof(struct tgsi_full_instruction));
Michal Krol's avatar
Michal Krol committed
1240
            maxInstructions += 10;
1241
         }
1242