1 // 2 // Copyright (C) 2002-2005 3Dlabs Inc. Ltd. 3 // Copyright (C) 2013 LunarG, Inc. 4 // Copyright (C) 2017 ARM Limited. 5 // 6 // All rights reserved. 7 // 8 // Redistribution and use in source and binary forms, with or without 9 // modification, are permitted provided that the following conditions 10 // are met: 11 // 12 // Redistributions of source code must retain the above copyright 13 // notice, this list of conditions and the following disclaimer. 14 // 15 // Redistributions in binary form must reproduce the above 16 // copyright notice, this list of conditions and the following 17 // disclaimer in the documentation and/or other materials provided 18 // with the distribution. 19 // 20 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its 21 // contributors may be used to endorse or promote products derived 22 // from this software without specific prior written permission. 23 // 24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 27 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 28 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 29 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 30 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 31 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 32 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 34 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 // POSSIBILITY OF SUCH DAMAGE. 36 // 37 38 #ifndef _CONSTANT_UNION_INCLUDED_ 39 #define _CONSTANT_UNION_INCLUDED_ 40 41 #include "../Include/Common.h" 42 #include "../Include/BaseTypes.h" 43 44 namespace glslang { 45 46 class TConstUnion { 47 public: POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator ())48 POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator()) 49 50 TConstUnion() : iConst(0), type(EbtInt) { } 51 setI8Const(signed char i)52 void setI8Const(signed char i) 53 { 54 i8Const = i; 55 type = EbtInt8; 56 } 57 setU8Const(unsigned char u)58 void setU8Const(unsigned char u) 59 { 60 u8Const = u; 61 type = EbtUint8; 62 } 63 setI16Const(signed short i)64 void setI16Const(signed short i) 65 { 66 i16Const = i; 67 type = EbtInt16; 68 } 69 setU16Const(unsigned short u)70 void setU16Const(unsigned short u) 71 { 72 u16Const = u; 73 type = EbtUint16; 74 } 75 setIConst(int i)76 void setIConst(int i) 77 { 78 iConst = i; 79 type = EbtInt; 80 } 81 setUConst(unsigned int u)82 void setUConst(unsigned int u) 83 { 84 uConst = u; 85 type = EbtUint; 86 } 87 setI64Const(long long i64)88 void setI64Const(long long i64) 89 { 90 i64Const = i64; 91 type = EbtInt64; 92 } 93 setU64Const(unsigned long long u64)94 void setU64Const(unsigned long long u64) 95 { 96 u64Const = u64; 97 type = EbtUint64; 98 } 99 setDConst(double d)100 void setDConst(double d) 101 { 102 dConst = d; 103 type = EbtDouble; 104 } 105 setBConst(bool b)106 void setBConst(bool b) 107 { 108 bConst = b; 109 type = EbtBool; 110 } 111 setSConst(const TString * s)112 void setSConst(const TString* s) 113 { 114 sConst = s; 115 type = EbtString; 116 } 117 getI8Const()118 signed char getI8Const() const { return i8Const; } getU8Const()119 unsigned char getU8Const() const { return u8Const; } getI16Const()120 signed short getI16Const() const { return i16Const; } getU16Const()121 unsigned short getU16Const() const { return u16Const; } getIConst()122 int getIConst() const { return iConst; } getUConst()123 unsigned int getUConst() const { return uConst; } getI64Const()124 long long getI64Const() const { return i64Const; } getU64Const()125 unsigned long long getU64Const() const { return u64Const; } getDConst()126 double getDConst() const { return dConst; } getBConst()127 bool getBConst() const { return bConst; } getSConst()128 const TString* getSConst() const { return sConst; } 129 130 bool operator==(const signed char i) const 131 { 132 if (i == i8Const) 133 return true; 134 135 return false; 136 } 137 138 bool operator==(const unsigned char u) const 139 { 140 if (u == u8Const) 141 return true; 142 143 return false; 144 } 145 146 bool operator==(const signed short i) const 147 { 148 if (i == i16Const) 149 return true; 150 151 return false; 152 } 153 154 bool operator==(const unsigned short u) const 155 { 156 if (u == u16Const) 157 return true; 158 159 return false; 160 } 161 162 bool operator==(const int i) const 163 { 164 if (i == iConst) 165 return true; 166 167 return false; 168 } 169 170 bool operator==(const unsigned int u) const 171 { 172 if (u == uConst) 173 return true; 174 175 return false; 176 } 177 178 bool operator==(const long long i64) const 179 { 180 if (i64 == i64Const) 181 return true; 182 183 return false; 184 } 185 186 bool operator==(const unsigned long long u64) const 187 { 188 if (u64 == u64Const) 189 return true; 190 191 return false; 192 } 193 194 bool operator==(const double d) const 195 { 196 if (d == dConst) 197 return true; 198 199 return false; 200 } 201 202 bool operator==(const bool b) const 203 { 204 if (b == bConst) 205 return true; 206 207 return false; 208 } 209 210 bool operator==(const TConstUnion& constant) const 211 { 212 if (constant.type != type) 213 return false; 214 215 switch (type) { 216 case EbtInt: 217 if (constant.iConst == iConst) 218 return true; 219 220 break; 221 case EbtUint: 222 if (constant.uConst == uConst) 223 return true; 224 225 break; 226 case EbtBool: 227 if (constant.bConst == bConst) 228 return true; 229 230 break; 231 case EbtDouble: 232 if (constant.dConst == dConst) 233 return true; 234 235 break; 236 237 case EbtInt16: 238 if (constant.i16Const == i16Const) 239 return true; 240 241 break; 242 case EbtUint16: 243 if (constant.u16Const == u16Const) 244 return true; 245 246 break; 247 case EbtInt8: 248 if (constant.i8Const == i8Const) 249 return true; 250 251 break; 252 case EbtUint8: 253 if (constant.u8Const == u8Const) 254 return true; 255 256 break; 257 case EbtInt64: 258 if (constant.i64Const == i64Const) 259 return true; 260 261 break; 262 case EbtUint64: 263 if (constant.u64Const == u64Const) 264 return true; 265 266 break; 267 default: 268 assert(false && "Default missing"); 269 } 270 271 return false; 272 } 273 274 bool operator!=(const signed char i) const 275 { 276 return !operator==(i); 277 } 278 279 bool operator!=(const unsigned char u) const 280 { 281 return !operator==(u); 282 } 283 284 bool operator!=(const signed short i) const 285 { 286 return !operator==(i); 287 } 288 289 bool operator!=(const unsigned short u) const 290 { 291 return !operator==(u); 292 } 293 294 bool operator!=(const int i) const 295 { 296 return !operator==(i); 297 } 298 299 bool operator!=(const unsigned int u) const 300 { 301 return !operator==(u); 302 } 303 304 bool operator!=(const long long i) const 305 { 306 return !operator==(i); 307 } 308 309 bool operator!=(const unsigned long long u) const 310 { 311 return !operator==(u); 312 } 313 314 bool operator!=(const float f) const 315 { 316 return !operator==(f); 317 } 318 319 bool operator!=(const bool b) const 320 { 321 return !operator==(b); 322 } 323 324 bool operator!=(const TConstUnion& constant) const 325 { 326 return !operator==(constant); 327 } 328 329 bool operator>(const TConstUnion& constant) const 330 { 331 assert(type == constant.type); 332 switch (type) { 333 case EbtInt: 334 if (iConst > constant.iConst) 335 return true; 336 337 return false; 338 case EbtUint: 339 if (uConst > constant.uConst) 340 return true; 341 342 return false; 343 case EbtDouble: 344 if (dConst > constant.dConst) 345 return true; 346 347 return false; 348 case EbtInt8: 349 if (i8Const > constant.i8Const) 350 return true; 351 352 return false; 353 case EbtUint8: 354 if (u8Const > constant.u8Const) 355 return true; 356 357 return false; 358 case EbtInt16: 359 if (i16Const > constant.i16Const) 360 return true; 361 362 return false; 363 case EbtUint16: 364 if (u16Const > constant.u16Const) 365 return true; 366 367 return false; 368 case EbtInt64: 369 if (i64Const > constant.i64Const) 370 return true; 371 372 return false; 373 case EbtUint64: 374 if (u64Const > constant.u64Const) 375 return true; 376 377 return false; 378 default: 379 assert(false && "Default missing"); 380 return false; 381 } 382 } 383 384 bool operator<(const TConstUnion& constant) const 385 { 386 assert(type == constant.type); 387 switch (type) { 388 case EbtInt8: 389 if (i8Const < constant.i8Const) 390 return true; 391 392 return false; 393 case EbtUint8: 394 if (u8Const < constant.u8Const) 395 return true; 396 397 return false; 398 case EbtInt16: 399 if (i16Const < constant.i16Const) 400 return true; 401 402 return false; 403 case EbtUint16: 404 if (u16Const < constant.u16Const) 405 return true; 406 return false; 407 case EbtInt64: 408 if (i64Const < constant.i64Const) 409 return true; 410 411 return false; 412 case EbtUint64: 413 if (u64Const < constant.u64Const) 414 return true; 415 416 return false; 417 case EbtDouble: 418 if (dConst < constant.dConst) 419 return true; 420 421 return false; 422 case EbtInt: 423 if (iConst < constant.iConst) 424 return true; 425 426 return false; 427 case EbtUint: 428 if (uConst < constant.uConst) 429 return true; 430 431 return false; 432 default: 433 assert(false && "Default missing"); 434 return false; 435 } 436 } 437 438 TConstUnion operator+(const TConstUnion& constant) const 439 { 440 TConstUnion returnValue; 441 assert(type == constant.type); 442 switch (type) { 443 case EbtInt: returnValue.setIConst(iConst + constant.iConst); break; 444 case EbtUint: returnValue.setUConst(uConst + constant.uConst); break; 445 case EbtDouble: returnValue.setDConst(dConst + constant.dConst); break; 446 case EbtInt8: returnValue.setI8Const(i8Const + constant.i8Const); break; 447 case EbtInt16: returnValue.setI16Const(i16Const + constant.i16Const); break; 448 case EbtInt64: returnValue.setI64Const(i64Const + constant.i64Const); break; 449 case EbtUint8: returnValue.setU8Const(u8Const + constant.u8Const); break; 450 case EbtUint16: returnValue.setU16Const(u16Const + constant.u16Const); break; 451 case EbtUint64: returnValue.setU64Const(u64Const + constant.u64Const); break; 452 default: assert(false && "Default missing"); 453 } 454 455 return returnValue; 456 } 457 458 TConstUnion operator-(const TConstUnion& constant) const 459 { 460 TConstUnion returnValue; 461 assert(type == constant.type); 462 switch (type) { 463 case EbtInt: returnValue.setIConst(iConst - constant.iConst); break; 464 case EbtUint: returnValue.setUConst(uConst - constant.uConst); break; 465 case EbtDouble: returnValue.setDConst(dConst - constant.dConst); break; 466 case EbtInt8: returnValue.setI8Const(i8Const - constant.i8Const); break; 467 case EbtInt16: returnValue.setI16Const(i16Const - constant.i16Const); break; 468 case EbtInt64: returnValue.setI64Const(i64Const - constant.i64Const); break; 469 case EbtUint8: returnValue.setU8Const(u8Const - constant.u8Const); break; 470 case EbtUint16: returnValue.setU16Const(u16Const - constant.u16Const); break; 471 case EbtUint64: returnValue.setU64Const(u64Const - constant.u64Const); break; 472 default: assert(false && "Default missing"); 473 } 474 475 return returnValue; 476 } 477 478 TConstUnion operator*(const TConstUnion& constant) const 479 { 480 TConstUnion returnValue; 481 assert(type == constant.type); 482 switch (type) { 483 case EbtInt: returnValue.setIConst(iConst * constant.iConst); break; 484 case EbtUint: returnValue.setUConst(uConst * constant.uConst); break; 485 case EbtDouble: returnValue.setDConst(dConst * constant.dConst); break; 486 case EbtInt8: returnValue.setI8Const(i8Const * constant.i8Const); break; 487 case EbtInt16: returnValue.setI16Const(i16Const * constant.i16Const); break; 488 case EbtInt64: returnValue.setI64Const(i64Const * constant.i64Const); break; 489 case EbtUint8: returnValue.setU8Const(u8Const * constant.u8Const); break; 490 case EbtUint16: returnValue.setU16Const(u16Const * constant.u16Const); break; 491 case EbtUint64: returnValue.setU64Const(u64Const * constant.u64Const); break; 492 default: assert(false && "Default missing"); 493 } 494 495 return returnValue; 496 } 497 498 TConstUnion operator%(const TConstUnion& constant) const 499 { 500 TConstUnion returnValue; 501 assert(type == constant.type); 502 switch (type) { 503 case EbtInt: returnValue.setIConst(iConst % constant.iConst); break; 504 case EbtUint: returnValue.setUConst(uConst % constant.uConst); break; 505 case EbtInt8: returnValue.setI8Const(i8Const % constant.i8Const); break; 506 case EbtInt16: returnValue.setI16Const(i16Const % constant.i16Const); break; 507 case EbtInt64: returnValue.setI64Const(i64Const % constant.i64Const); break; 508 case EbtUint8: returnValue.setU8Const(u8Const % constant.u8Const); break; 509 case EbtUint16: returnValue.setU16Const(u16Const % constant.u16Const); break; 510 case EbtUint64: returnValue.setU64Const(u64Const % constant.u64Const); break; 511 default: assert(false && "Default missing"); 512 } 513 514 return returnValue; 515 } 516 517 TConstUnion operator>>(const TConstUnion& constant) const 518 { 519 TConstUnion returnValue; 520 switch (type) { 521 case EbtInt8: 522 switch (constant.type) { 523 case EbtInt8: returnValue.setI8Const(i8Const >> constant.i8Const); break; 524 case EbtUint8: returnValue.setI8Const(i8Const >> constant.u8Const); break; 525 case EbtInt16: returnValue.setI8Const(i8Const >> constant.i16Const); break; 526 case EbtUint16: returnValue.setI8Const(i8Const >> constant.u16Const); break; 527 case EbtInt: returnValue.setI8Const(i8Const >> constant.iConst); break; 528 case EbtUint: returnValue.setI8Const(i8Const >> constant.uConst); break; 529 case EbtInt64: returnValue.setI8Const(i8Const >> constant.i64Const); break; 530 case EbtUint64: returnValue.setI8Const(i8Const >> constant.u64Const); break; 531 default: assert(false && "Default missing"); 532 } 533 break; 534 case EbtUint8: 535 switch (constant.type) { 536 case EbtInt8: returnValue.setU8Const(u8Const >> constant.i8Const); break; 537 case EbtUint8: returnValue.setU8Const(u8Const >> constant.u8Const); break; 538 case EbtInt16: returnValue.setU8Const(u8Const >> constant.i16Const); break; 539 case EbtUint16: returnValue.setU8Const(u8Const >> constant.u16Const); break; 540 case EbtInt: returnValue.setU8Const(u8Const >> constant.iConst); break; 541 case EbtUint: returnValue.setU8Const(u8Const >> constant.uConst); break; 542 case EbtInt64: returnValue.setU8Const(u8Const >> constant.i64Const); break; 543 case EbtUint64: returnValue.setU8Const(u8Const >> constant.u64Const); break; 544 default: assert(false && "Default missing"); 545 } 546 break; 547 case EbtInt16: 548 switch (constant.type) { 549 case EbtInt8: returnValue.setI16Const(i16Const >> constant.i8Const); break; 550 case EbtUint8: returnValue.setI16Const(i16Const >> constant.u8Const); break; 551 case EbtInt16: returnValue.setI16Const(i16Const >> constant.i16Const); break; 552 case EbtUint16: returnValue.setI16Const(i16Const >> constant.u16Const); break; 553 case EbtInt: returnValue.setI16Const(i16Const >> constant.iConst); break; 554 case EbtUint: returnValue.setI16Const(i16Const >> constant.uConst); break; 555 case EbtInt64: returnValue.setI16Const(i16Const >> constant.i64Const); break; 556 case EbtUint64: returnValue.setI16Const(i16Const >> constant.u64Const); break; 557 default: assert(false && "Default missing"); 558 } 559 break; 560 case EbtUint16: 561 switch (constant.type) { 562 case EbtInt8: returnValue.setU16Const(u16Const >> constant.i8Const); break; 563 case EbtUint8: returnValue.setU16Const(u16Const >> constant.u8Const); break; 564 case EbtInt16: returnValue.setU16Const(u16Const >> constant.i16Const); break; 565 case EbtUint16: returnValue.setU16Const(u16Const >> constant.u16Const); break; 566 case EbtInt: returnValue.setU16Const(u16Const >> constant.iConst); break; 567 case EbtUint: returnValue.setU16Const(u16Const >> constant.uConst); break; 568 case EbtInt64: returnValue.setU16Const(u16Const >> constant.i64Const); break; 569 case EbtUint64: returnValue.setU16Const(u16Const >> constant.u64Const); break; 570 default: assert(false && "Default missing"); 571 } 572 break; 573 case EbtInt: 574 switch (constant.type) { 575 case EbtInt: returnValue.setIConst(iConst >> constant.iConst); break; 576 case EbtUint: returnValue.setIConst(iConst >> constant.uConst); break; 577 case EbtInt8: returnValue.setIConst(iConst >> constant.i8Const); break; 578 case EbtUint8: returnValue.setIConst(iConst >> constant.u8Const); break; 579 case EbtInt16: returnValue.setIConst(iConst >> constant.i16Const); break; 580 case EbtUint16: returnValue.setIConst(iConst >> constant.u16Const); break; 581 case EbtInt64: returnValue.setIConst(iConst >> constant.i64Const); break; 582 case EbtUint64: returnValue.setIConst(iConst >> constant.u64Const); break; 583 default: assert(false && "Default missing"); 584 } 585 break; 586 case EbtUint: 587 switch (constant.type) { 588 case EbtInt: returnValue.setUConst(uConst >> constant.iConst); break; 589 case EbtUint: returnValue.setUConst(uConst >> constant.uConst); break; 590 case EbtInt8: returnValue.setUConst(uConst >> constant.i8Const); break; 591 case EbtUint8: returnValue.setUConst(uConst >> constant.u8Const); break; 592 case EbtInt16: returnValue.setUConst(uConst >> constant.i16Const); break; 593 case EbtUint16: returnValue.setUConst(uConst >> constant.u16Const); break; 594 case EbtInt64: returnValue.setUConst(uConst >> constant.i64Const); break; 595 case EbtUint64: returnValue.setUConst(uConst >> constant.u64Const); break; 596 default: assert(false && "Default missing"); 597 } 598 break; 599 case EbtInt64: 600 switch (constant.type) { 601 case EbtInt8: returnValue.setI64Const(i64Const >> constant.i8Const); break; 602 case EbtUint8: returnValue.setI64Const(i64Const >> constant.u8Const); break; 603 case EbtInt16: returnValue.setI64Const(i64Const >> constant.i16Const); break; 604 case EbtUint16: returnValue.setI64Const(i64Const >> constant.u16Const); break; 605 case EbtInt: returnValue.setI64Const(i64Const >> constant.iConst); break; 606 case EbtUint: returnValue.setI64Const(i64Const >> constant.uConst); break; 607 case EbtInt64: returnValue.setI64Const(i64Const >> constant.i64Const); break; 608 case EbtUint64: returnValue.setI64Const(i64Const >> constant.u64Const); break; 609 default: assert(false && "Default missing"); 610 } 611 break; 612 case EbtUint64: 613 switch (constant.type) { 614 case EbtInt8: returnValue.setU64Const(u64Const >> constant.i8Const); break; 615 case EbtUint8: returnValue.setU64Const(u64Const >> constant.u8Const); break; 616 case EbtInt16: returnValue.setU64Const(u64Const >> constant.i16Const); break; 617 case EbtUint16: returnValue.setU64Const(u64Const >> constant.u16Const); break; 618 case EbtInt: returnValue.setU64Const(u64Const >> constant.iConst); break; 619 case EbtUint: returnValue.setU64Const(u64Const >> constant.uConst); break; 620 case EbtInt64: returnValue.setU64Const(u64Const >> constant.i64Const); break; 621 case EbtUint64: returnValue.setU64Const(u64Const >> constant.u64Const); break; 622 default: assert(false && "Default missing"); 623 } 624 break; 625 default: assert(false && "Default missing"); 626 } 627 628 return returnValue; 629 } 630 631 TConstUnion operator<<(const TConstUnion& constant) const 632 { 633 TConstUnion returnValue; 634 switch (type) { 635 case EbtInt8: 636 switch (constant.type) { 637 case EbtInt8: returnValue.setI8Const(i8Const << constant.i8Const); break; 638 case EbtUint8: returnValue.setI8Const(i8Const << constant.u8Const); break; 639 case EbtInt16: returnValue.setI8Const(i8Const << constant.i16Const); break; 640 case EbtUint16: returnValue.setI8Const(i8Const << constant.u16Const); break; 641 case EbtInt: returnValue.setI8Const(i8Const << constant.iConst); break; 642 case EbtUint: returnValue.setI8Const(i8Const << constant.uConst); break; 643 case EbtInt64: returnValue.setI8Const(i8Const << constant.i64Const); break; 644 case EbtUint64: returnValue.setI8Const(i8Const << constant.u64Const); break; 645 default: assert(false && "Default missing"); 646 } 647 break; 648 case EbtUint8: 649 switch (constant.type) { 650 case EbtInt8: returnValue.setU8Const(u8Const << constant.i8Const); break; 651 case EbtUint8: returnValue.setU8Const(u8Const << constant.u8Const); break; 652 case EbtInt16: returnValue.setU8Const(u8Const << constant.i16Const); break; 653 case EbtUint16: returnValue.setU8Const(u8Const << constant.u16Const); break; 654 case EbtInt: returnValue.setU8Const(u8Const << constant.iConst); break; 655 case EbtUint: returnValue.setU8Const(u8Const << constant.uConst); break; 656 case EbtInt64: returnValue.setU8Const(u8Const << constant.i64Const); break; 657 case EbtUint64: returnValue.setU8Const(u8Const << constant.u64Const); break; 658 default: assert(false && "Default missing"); 659 } 660 break; 661 case EbtInt16: 662 switch (constant.type) { 663 case EbtInt8: returnValue.setI16Const(i16Const << constant.i8Const); break; 664 case EbtUint8: returnValue.setI16Const(i16Const << constant.u8Const); break; 665 case EbtInt16: returnValue.setI16Const(i16Const << constant.i16Const); break; 666 case EbtUint16: returnValue.setI16Const(i16Const << constant.u16Const); break; 667 case EbtInt: returnValue.setI16Const(i16Const << constant.iConst); break; 668 case EbtUint: returnValue.setI16Const(i16Const << constant.uConst); break; 669 case EbtInt64: returnValue.setI16Const(i16Const << constant.i64Const); break; 670 case EbtUint64: returnValue.setI16Const(i16Const << constant.u64Const); break; 671 default: assert(false && "Default missing"); 672 } 673 break; 674 case EbtUint16: 675 switch (constant.type) { 676 case EbtInt8: returnValue.setU16Const(u16Const << constant.i8Const); break; 677 case EbtUint8: returnValue.setU16Const(u16Const << constant.u8Const); break; 678 case EbtInt16: returnValue.setU16Const(u16Const << constant.i16Const); break; 679 case EbtUint16: returnValue.setU16Const(u16Const << constant.u16Const); break; 680 case EbtInt: returnValue.setU16Const(u16Const << constant.iConst); break; 681 case EbtUint: returnValue.setU16Const(u16Const << constant.uConst); break; 682 case EbtInt64: returnValue.setU16Const(u16Const << constant.i64Const); break; 683 case EbtUint64: returnValue.setU16Const(u16Const << constant.u64Const); break; 684 default: assert(false && "Default missing"); 685 } 686 break; 687 case EbtInt64: 688 switch (constant.type) { 689 case EbtInt8: returnValue.setI64Const(i64Const << constant.i8Const); break; 690 case EbtUint8: returnValue.setI64Const(i64Const << constant.u8Const); break; 691 case EbtInt16: returnValue.setI64Const(i64Const << constant.i16Const); break; 692 case EbtUint16: returnValue.setI64Const(i64Const << constant.u16Const); break; 693 case EbtInt: returnValue.setI64Const(i64Const << constant.iConst); break; 694 case EbtUint: returnValue.setI64Const(i64Const << constant.uConst); break; 695 case EbtInt64: returnValue.setI64Const(i64Const << constant.i64Const); break; 696 case EbtUint64: returnValue.setI64Const(i64Const << constant.u64Const); break; 697 default: assert(false && "Default missing"); 698 } 699 break; 700 case EbtUint64: 701 switch (constant.type) { 702 case EbtInt8: returnValue.setU64Const(u64Const << constant.i8Const); break; 703 case EbtUint8: returnValue.setU64Const(u64Const << constant.u8Const); break; 704 case EbtInt16: returnValue.setU64Const(u64Const << constant.i16Const); break; 705 case EbtUint16: returnValue.setU64Const(u64Const << constant.u16Const); break; 706 case EbtInt: returnValue.setU64Const(u64Const << constant.iConst); break; 707 case EbtUint: returnValue.setU64Const(u64Const << constant.uConst); break; 708 case EbtInt64: returnValue.setU64Const(u64Const << constant.i64Const); break; 709 case EbtUint64: returnValue.setU64Const(u64Const << constant.u64Const); break; 710 default: assert(false && "Default missing"); 711 } 712 break; 713 case EbtInt: 714 switch (constant.type) { 715 case EbtInt: returnValue.setIConst(iConst << constant.iConst); break; 716 case EbtUint: returnValue.setIConst(iConst << constant.uConst); break; 717 case EbtInt8: returnValue.setIConst(iConst << constant.i8Const); break; 718 case EbtUint8: returnValue.setIConst(iConst << constant.u8Const); break; 719 case EbtInt16: returnValue.setIConst(iConst << constant.i16Const); break; 720 case EbtUint16: returnValue.setIConst(iConst << constant.u16Const); break; 721 case EbtInt64: returnValue.setIConst(iConst << constant.i64Const); break; 722 case EbtUint64: returnValue.setIConst(iConst << constant.u64Const); break; 723 default: assert(false && "Default missing"); 724 } 725 break; 726 case EbtUint: 727 switch (constant.type) { 728 case EbtInt: returnValue.setUConst(uConst << constant.iConst); break; 729 case EbtUint: returnValue.setUConst(uConst << constant.uConst); break; 730 case EbtInt8: returnValue.setUConst(uConst << constant.i8Const); break; 731 case EbtUint8: returnValue.setUConst(uConst << constant.u8Const); break; 732 case EbtInt16: returnValue.setUConst(uConst << constant.i16Const); break; 733 case EbtUint16: returnValue.setUConst(uConst << constant.u16Const); break; 734 case EbtInt64: returnValue.setUConst(uConst << constant.i64Const); break; 735 case EbtUint64: returnValue.setUConst(uConst << constant.u64Const); break; 736 default: assert(false && "Default missing"); 737 } 738 break; 739 default: assert(false && "Default missing"); 740 } 741 742 return returnValue; 743 } 744 745 TConstUnion operator&(const TConstUnion& constant) const 746 { 747 TConstUnion returnValue; 748 assert(type == constant.type); 749 switch (type) { 750 case EbtInt: returnValue.setIConst(iConst & constant.iConst); break; 751 case EbtUint: returnValue.setUConst(uConst & constant.uConst); break; 752 case EbtInt8: returnValue.setI8Const(i8Const & constant.i8Const); break; 753 case EbtUint8: returnValue.setU8Const(u8Const & constant.u8Const); break; 754 case EbtInt16: returnValue.setI16Const(i16Const & constant.i16Const); break; 755 case EbtUint16: returnValue.setU16Const(u16Const & constant.u16Const); break; 756 case EbtInt64: returnValue.setI64Const(i64Const & constant.i64Const); break; 757 case EbtUint64: returnValue.setU64Const(u64Const & constant.u64Const); break; 758 default: assert(false && "Default missing"); 759 } 760 761 return returnValue; 762 } 763 764 TConstUnion operator|(const TConstUnion& constant) const 765 { 766 TConstUnion returnValue; 767 assert(type == constant.type); 768 switch (type) { 769 case EbtInt: returnValue.setIConst(iConst | constant.iConst); break; 770 case EbtUint: returnValue.setUConst(uConst | constant.uConst); break; 771 case EbtInt8: returnValue.setI8Const(i8Const | constant.i8Const); break; 772 case EbtUint8: returnValue.setU8Const(u8Const | constant.u8Const); break; 773 case EbtInt16: returnValue.setI16Const(i16Const | constant.i16Const); break; 774 case EbtUint16: returnValue.setU16Const(u16Const | constant.u16Const); break; 775 case EbtInt64: returnValue.setI64Const(i64Const | constant.i64Const); break; 776 case EbtUint64: returnValue.setU64Const(u64Const | constant.u64Const); break; 777 default: assert(false && "Default missing"); 778 } 779 780 return returnValue; 781 } 782 783 TConstUnion operator^(const TConstUnion& constant) const 784 { 785 TConstUnion returnValue; 786 assert(type == constant.type); 787 switch (type) { 788 case EbtInt: returnValue.setIConst(iConst ^ constant.iConst); break; 789 case EbtUint: returnValue.setUConst(uConst ^ constant.uConst); break; 790 case EbtInt8: returnValue.setI8Const(i8Const ^ constant.i8Const); break; 791 case EbtUint8: returnValue.setU8Const(u8Const ^ constant.u8Const); break; 792 case EbtInt16: returnValue.setI16Const(i16Const ^ constant.i16Const); break; 793 case EbtUint16: returnValue.setU16Const(u16Const ^ constant.u16Const); break; 794 case EbtInt64: returnValue.setI64Const(i64Const ^ constant.i64Const); break; 795 case EbtUint64: returnValue.setU64Const(u64Const ^ constant.u64Const); break; 796 default: assert(false && "Default missing"); 797 } 798 799 return returnValue; 800 } 801 802 TConstUnion operator~() const 803 { 804 TConstUnion returnValue; 805 switch (type) { 806 case EbtInt: returnValue.setIConst(~iConst); break; 807 case EbtUint: returnValue.setUConst(~uConst); break; 808 case EbtInt8: returnValue.setI8Const(~i8Const); break; 809 case EbtUint8: returnValue.setU8Const(~u8Const); break; 810 case EbtInt16: returnValue.setI16Const(~i16Const); break; 811 case EbtUint16: returnValue.setU16Const(~u16Const); break; 812 case EbtInt64: returnValue.setI64Const(~i64Const); break; 813 case EbtUint64: returnValue.setU64Const(~u64Const); break; 814 default: assert(false && "Default missing"); 815 } 816 817 return returnValue; 818 } 819 820 TConstUnion operator&&(const TConstUnion& constant) const 821 { 822 TConstUnion returnValue; 823 assert(type == constant.type); 824 switch (type) { 825 case EbtBool: returnValue.setBConst(bConst && constant.bConst); break; 826 default: assert(false && "Default missing"); 827 } 828 829 return returnValue; 830 } 831 832 TConstUnion operator||(const TConstUnion& constant) const 833 { 834 TConstUnion returnValue; 835 assert(type == constant.type); 836 switch (type) { 837 case EbtBool: returnValue.setBConst(bConst || constant.bConst); break; 838 default: assert(false && "Default missing"); 839 } 840 841 return returnValue; 842 } 843 getType()844 TBasicType getType() const { return type; } 845 846 private: 847 union { 848 signed char i8Const; // used for i8vec, scalar int8s 849 unsigned char u8Const; // used for u8vec, scalar uint8s 850 signed short i16Const; // used for i16vec, scalar int16s 851 unsigned short u16Const; // used for u16vec, scalar uint16s 852 int iConst; // used for ivec, scalar ints 853 unsigned int uConst; // used for uvec, scalar uints 854 long long i64Const; // used for i64vec, scalar int64s 855 unsigned long long u64Const; // used for u64vec, scalar uint64s 856 bool bConst; // used for bvec, scalar bools 857 double dConst; // used for vec, dvec, mat, dmat, scalar floats and doubles 858 const TString* sConst; // string constant 859 }; 860 861 TBasicType type; 862 }; 863 864 // Encapsulate having a pointer to an array of TConstUnion, 865 // which only needs to be allocated if its size is going to be 866 // bigger than 0. 867 // 868 // One convenience is being able to use [] to go inside the array, instead 869 // of C++ assuming it as an array of pointers to vectors. 870 // 871 // General usage is that the size is known up front, and it is 872 // created once with the proper size. 873 // 874 class TConstUnionArray { 875 public: POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator ())876 POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator()) 877 878 TConstUnionArray() : unionArray(nullptr) { } ~TConstUnionArray()879 virtual ~TConstUnionArray() { } 880 TConstUnionArray(int size)881 explicit TConstUnionArray(int size) 882 { 883 if (size == 0) 884 unionArray = nullptr; 885 else 886 unionArray = new TConstUnionVector(size); 887 } 888 TConstUnionArray(const TConstUnionArray& a) = default; TConstUnionArray(const TConstUnionArray & a,int start,int size)889 TConstUnionArray(const TConstUnionArray& a, int start, int size) 890 { 891 unionArray = new TConstUnionVector(size); 892 for (int i = 0; i < size; ++i) 893 (*unionArray)[i] = a[start + i]; 894 } 895 896 // Use this constructor for a smear operation TConstUnionArray(int size,const TConstUnion & val)897 TConstUnionArray(int size, const TConstUnion& val) 898 { 899 unionArray = new TConstUnionVector(size, val); 900 } 901 size()902 int size() const { return unionArray ? (int)unionArray->size() : 0; } 903 TConstUnion& operator[](size_t index) { return (*unionArray)[index]; } 904 const TConstUnion& operator[](size_t index) const { return (*unionArray)[index]; } 905 bool operator==(const TConstUnionArray& rhs) const 906 { 907 // this includes the case that both are unallocated 908 if (unionArray == rhs.unionArray) 909 return true; 910 911 if (! unionArray || ! rhs.unionArray) 912 return false; 913 914 return *unionArray == *rhs.unionArray; 915 } 916 bool operator!=(const TConstUnionArray& rhs) const { return ! operator==(rhs); } 917 dot(const TConstUnionArray & rhs)918 double dot(const TConstUnionArray& rhs) 919 { 920 assert(rhs.unionArray->size() == unionArray->size()); 921 double sum = 0.0; 922 923 for (size_t comp = 0; comp < unionArray->size(); ++comp) 924 sum += (*this)[comp].getDConst() * rhs[comp].getDConst(); 925 926 return sum; 927 } 928 empty()929 bool empty() const { return unionArray == nullptr; } 930 931 protected: 932 typedef TVector<TConstUnion> TConstUnionVector; 933 TConstUnionVector* unionArray; 934 }; 935 936 } // end namespace glslang 937 938 #endif // _CONSTANT_UNION_INCLUDED_ 939