testswar.c 20.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 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 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 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
/* -----------------------------------------------------------------------------
 *  Copyright (C) 2019-2021 daiteq s.r.o.                http://www.daiteq.com
 *
 *  This program is distributed WITHOUT ANY WARRANTY; without even
 *  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 *  PURPOSE.
 *
 * -----------------------------------------------------------------------------
 *  Filename    : testswar.c
 *  Authors     : Martin Danek, Roman Bartosinski
 *  Description : LEON2 test of SWAR instructions
 *  Release     :
 *  Version     : 2.0
 *  Date        : 27.4.2021
 * -----------------------------------------------------------------------------
 */

#include <stdint.h>
#include "my_printf.h"
#include "leon-myuart.h"
#include "swar.h"
#include "ise.h"
#include "vectors_swar.h"
#include "sysregs.h"
#include "lconf.h"

// swar type definition
typedef unsigned int su4x8b  __attribute__((subword(8)));
typedef signed int ss4x8b  __attribute__((subword(8)));
typedef unsigned int su2x16b  __attribute__((subword(16)));
typedef signed int ss2x16b  __attribute__((subword(16)));

typedef unsigned int su32x1b  __attribute__((subword(1)));
typedef signed int ss32x1b  __attribute__((subword(1)));
typedef unsigned int su16x2b  __attribute__((subword(2)));
typedef signed int ss16x2b  __attribute__((subword(2)));
typedef unsigned int su10x3b  __attribute__((subword(3)));
typedef signed int ss10x3b  __attribute__((subword(3)));
typedef unsigned int su8x4b  __attribute__((subword(4)));
typedef signed int ss8x4b  __attribute__((subword(4)));
//#define swar(x,y) __builtin_swar(x,y)
//#define swarcc(x,y) __builtin_swarcc(x,y)
//#define swarctrl(x) __builtin_swarctrl(x)
//#define swaraccum(x) __builtin_swaraccum(x)

typedef union {
  unsigned  i;
  su4x8b    u8;
  ss4x8b    s8;
  su2x16b   u16;
  ss2x16b   s16;
  su32x1b   u1;
  ss32x1b   s1;
  su16x2b   u2;
  ss16x2b   s2;
  su10x3b   u3;
  ss10x3b   s3;
  su8x4b    u4;
  ss8x4b    s4;
} swdata_t;

#define PGMNAME "testswar"

#ifndef ACC_BITS
  #define ACC_BITS 22
#endif

#ifndef MAX_ACC
  #define MAX_ACC 16  // MGSIM: 4 , BSTzedboard: 2
#endif /* MAX_ACC */

#define ACCMSK ((1<<ACC_BITS)-1)

// selection of enabled code
//#define SWAR_CORR
//#define SWAR_SINCOS
//#define SWAR_ALU

#define SYSREGS

#define PRINT_DETAILS 0 /* Print all test vectors and results when computed */

#ifndef SWAR_UNIT_TYPE
  #warning Swar Video Unit !
  #define SWAR_UNIT_TYPE 2 /* 1=4x8b 2=2x16b 4=ALU */
#endif /* SWAR_UNIT_TYPE */

#define ALU_NUMEL   CORR_MAX


#define MYHEAPSIZE (128 * 1024)

extern unsigned long __bss_start;  /* start address for the .bss section. defined in linker script */
extern unsigned long _end;         /* end address for the .bss section. defined in linker script */

void report(unsigned *failcnt, unsigned ref, unsigned act) {
  printf(" ERROR - EXP %08X ACT %08X\n", ref, act);
  (*failcnt)++;
}

void report_acc(unsigned *failcnt, unsigned ref, unsigned act) {
  printf(" ERROR - ACC - EXP %08X ACT %08X\n", ref, act);
  (*failcnt)++;
}

void check_acc(unsigned *failcnt, int *acc) {
unsigned i;
volatile unsigned accval,accref;
unsigned neg[MAX_ACC];
  for (i=0;i<MAX_ACC; i++) {
    accval=__builtin_swaraccum(i<<1)&ACCMSK;
    accref=(*(acc+i))&ACCMSK;
    if (PRINT_DETAILS) printf("acc[%2d] LOWER %08X (hw %08X)\n", i, acc[i], accval);
    if (accref!=accval)
      report_acc(failcnt, *(acc+i), accval);
    if (accval<0) neg[i]=1;
    else neg[i]=0;
  }
  for (i=0;i<MAX_ACC; i++) {
    accval=__builtin_swaraccum((i<<1)+1);
    if (PRINT_DETAILS) printf("acc[%2d] UPPER HW %08X\n",i, accval);
// 32-bit integer arithmetic only, do not check the upper 32 bits
//     if (accval!=(neg[i]?-1:0))
//       report_acc(failcnt, 0, accval);
  }
}

void report_alu_arith(unsigned sgnd,unsigned reduce, unsigned saturate) {
    if (!sgnd) printf("UN");
    printf("SIGNED");
    if (reduce) printf(", REDUCE");
    if (saturate) printf(", SATURATE");
    printf("\n");
}

/* ========================================================================== */
#ifdef SWAR_ALU
// ADD, bits is one from 8,-8,16,-16, 1,-1,2,-2,3,-3,4,-4
unsigned alu_swar_add(int *data, unsigned len, int bits, unsigned saturate, unsigned reduce)
{
#pragma swar manual
  unsigned err = 0;
  int acc[MAX_ACC], accval;
  unsigned sgnd = (bits<0) ? 1 : 0;
  unsigned bwidth = (bits<0) ? -bits : bits;
  unsigned pack = 32/bwidth;
  unsigned expres;
  swdata_t a, b, res;

  for (int i=0;i<MAX_ACC; i++) acc[i]=0;

  __builtin_swarctrl(
        SW_OP_ADD | 
        ((bwidth==8) ? SW_VIDEO : ((bwidth==16) ? SW_AUDIO : SW_ALU)) | 
        (sgnd ? (SW_SGND) : 0) | 
        (reduce ? (SW_RED) : 0) | 
        (saturate ? (SW_SAT) : 0)
      );

  printf("SWAR ADD %d x %d bits ", pack, bwidth);
  report_alu_arith(sgnd, reduce, saturate);
  printf("  SWAR CONFIG: 0x%08X\n",swar_readconfig());

  for (int i=0; i<len; i+=3) {
    a.i = data[0];
    b.i = data[1];
    switch (bits) {
      case 8:
        res.u8 = a.u8 + b.u8;
        break;
       
      case -8:
        res.s8 = a.s8 + b.s8;
        break;

      case 16:
        res.u16 = a.u16 + b.u16;
        break;
        
      case -16:
        res.s16 = a.s16 + b.s16;
        break;

      case 1:
        res.u1 = a.u1 + b.u1;
        break;
        
      case -1:
        res.s1 = a.s1 + b.s1;
        break;

      case 2:
        res.u2 = a.u2 + b.u2;
        break;
        
      case -2:
        res.s2 = a.s2 + b.s2;
        break;

      case 3:
        res.u3 = a.u3 + b.u3;
        break;
        
      case -3:
        res.s3 = a.s3 + b.s3;
        break;

      case 4:
        res.u4 = a.u4 + b.u4;
        break;
        
      case -4:
        res.s4 = a.s4 + b.s4;
        break;

      default:
        break;
    }
//    res = *((unsigned *)&sdata.u2.r);

    expres = swar_alu(0, (int *)&a.i, (int *)&b.i, pack, bwidth, ADD, sgnd, saturate, reduce, acc);

    if (PRINT_DETAILS) 
      printf("  a %08X b %08X res %08X expres %08X\n", a.i, b.i, res.i, expres);

    if (res.i!=expres) report(&err, expres, res.i);
    data += 3;
  }
  check_acc(&err, acc);
  printf("------\n\n");

  return err;
}


// SUB, bits is one from 8,-8,16,-16,1,-1,2,-2,3,-3,4,-4
unsigned alu_swar_sub(int *data, unsigned len, signed bits, unsigned saturate, unsigned reduce)
{
  unsigned err = 0;
  int acc[MAX_ACC], accval;
  unsigned sgnd = (bits<0) ? 1 : 0;
  unsigned bwidth = (bits<0) ? -bits : bits;
  unsigned pack = 32/bwidth;
  unsigned expres;
  swdata_t a, b, res;

  for (int i=0;i<MAX_ACC; i++) acc[i]=0;

  __builtin_swarctrl(
        SW_OP_SUB | 
        ((bwidth==8) ? SW_VIDEO : ((bwidth==16) ? SW_AUDIO : SW_ALU)) | 
        (sgnd ? (SW_SGND) : 0) | 
        (reduce ? (SW_RED) : 0) | 
        (saturate ? (SW_SAT) : 0)
      );

  printf("SWAR SUB %d x %d bits ", pack, bwidth);
  report_alu_arith(sgnd, reduce, saturate);
  printf("  SWAR CONFIG: 0x%08X\n",swar_readconfig());

  for (int i=0; i<len; i+=3) {
    a.i = data[0];
    b.i = data[1];
    switch (bits) {
      case 8:
        res.u8 = a.u8 - b.u8;
        break;
        
      case -8:
        res.s8 = a.s8 - b.s8;
        break;

      case 16:
        res.u16 = a.u16 - b.u16;
        break;
        
      case -16:
        res.s16 = a.s16 - b.s16;
        break;

      case 1:
        res.u1 = a.u1 - b.u1;
        break;
        
      case -1:
        res.s1 = a.s1 - b.s1;
        break;

      case 2:
        res.u2 = a.u2 - b.u2;
        break;
        
      case -2:
        res.s2 = a.s2 - b.s2;
        break;

      case 3:
        res.u3 = a.u3 - b.u3;
        break;
        
      case -3:
        res.s3 = a.s3 - b.s3;
        break;

      case 4:
        res.u4 = a.u4 - b.u4;
        break;
        
      case -4:
        res.s4 = a.s4 - b.s4;
        break;

      default:
        break;
    }
    expres = swar_alu(0, (int *)&a.i, (int *)&b.i, pack, bwidth, SUB, sgnd, saturate, reduce, acc);

    if (PRINT_DETAILS) 
      printf("  a %08X b %08X res %08X expres %08X\n", a.i, b.i, res.i, expres);

    if (res.i!=expres) report(&err, expres, res.i);
    data += 3;
  }
  check_acc(&err, acc);
  printf("------\n\n");

  return err;
}


// MUL, bits is one from 8,-8,16,-16,2,-2,3,-3,4,-4
unsigned alu_swar_mul(int *data, unsigned len, signed bits, unsigned saturate, unsigned reduce)
{
  unsigned err = 0;
  int acc[MAX_ACC], accval;
  unsigned sgnd = (bits<0) ? 1 : 0;
  unsigned bwidth = (bits<0) ? -bits : bits;
  unsigned pack = 32/bwidth;
  unsigned expres;
  swdata_t a, b, res;

  for (int i=0;i<MAX_ACC; i++) acc[i]=0;

  __builtin_swarctrl(
        SW_OP_MUL | 
        ((bwidth==8) ? SW_VIDEO : ((bwidth==16) ? SW_AUDIO : SW_ALU)) | 
        (sgnd ? (SW_SGND) : 0) | 
        (reduce ? (SW_RED) : 0) | 
        (saturate ? (SW_SAT) : 0)
      );

  printf("SWAR MUL %d x %d bits ", pack, bwidth);
  report_alu_arith(sgnd, reduce, saturate);
  printf("  SWAR CONFIG: 0x%08X\n",swar_readconfig());

  for (int i=0; i<len; i+=3) {
    a.i = data[0];
    b.i = data[1];

    switch (bits) {
      case 8:
        res.u8 = a.u8 * b.u8;
        break;
        
      case -8:
        res.s8 = a.s8 * b.s8;
        break;

      case 16:
        res.u16 = a.u16 * b.u16;
        break;
        
      case -16:
        res.s16 = a.s16 * b.s16;
        break;

      case 1:
        res.u1 = a.u1 * b.u1;
        break;
        
      case -1:
        res.s1 = a.s1 * b.s1;
        break;

      case 2:
        res.u2 = a.u2 * b.u2;
        break;
        
      case -2:
        res.s2 = a.s2 * b.s2;
        break;

      case 3:
        res.u3 = a.u3 * b.u3;
        break;
        
      case -3:
        res.s3 = a.s3 * b.s3;
        break;

      case 4:
        res.u4 = a.u4 * b.u4;
        break;
        
      case -4:
        res.s4 = a.s4 * b.s4;
        break;

      default:
        break;
    }
    expres = swar_alu(0, (int *)&a.i, (int *)&b.i, pack, bwidth, MUL, sgnd, saturate, reduce, acc);

    if (PRINT_DETAILS) 
      printf("  a %08X b %08X res %08X expres %08X\n", a.i, b.i, res.i, expres);

    if (res.i!=expres) report(&err, expres, res.i);
    data += 3;
  }
  check_acc(&err, acc);
  printf("------\n\n");

  return err;
}

#endif /* SWAR_ALU */

/* ========================================================================== */
int main(void) {

  volatile unsigned a, b, res, res_sw;
  unsigned i, j;
  unsigned bits, cfg, sgnd, reduce, saturate;
  int acc[MAX_ACC], accval;
  int sop;
  unsigned failcnt;

#ifdef SYSREGS
  unsigned ticks_lo, ticks_hi, insns_lo, insns_hi;
  lconf_type lconf;
  lconf_fpu_type lconf_fpu;
  lconf_swar_type lconf_swar;
#endif /* SYSREGS */

  /* reset BSS section to zero */
  uint32_t *pMem;
  for(pMem = &__bss_start; pMem < &_end;) {
    *(pMem++) = 0;
  }

  init_uart();

#ifdef SYSREGS
  get_lconf(&lconf);
  get_lconf_fpu(&lconf_fpu);
  get_lconf_swar(&lconf_swar);

  report_lconf(&lconf);
  if ((lconf.fpu==FPU_DAIFPU)||(lconf.fpu==FPU_MEIKO))
    report_lconf_fpu(&lconf_fpu);
  report_lconf_swar(&lconf_swar);

  sysregs_init();
#endif

  failcnt = 0;

  printf("\n\nLEON2FT SWAR TEST\n");
  printf("=================\n\n");


#ifdef SWAR_CORR
  printf("CORRELATION - 2b SIGNED\n"); // a..mask, b..signal
  __builtin_swarctrl(SW_SGND | SW_OP_COR2b);
  printf("SWAR CONFIG: 0x%08X\n",swar_readconfig());
  bits = 2;
  sop = 0;

  res=0;

  for (i=0; i<CORR_MAX;i+=3) {
    a = corr[i];
    b = corr[i+1];
    res = __builtin_swar(a,b);
    if (PRINT_DETAILS) printf("a %08X b %08X res %08X\n",a,b,res);
    if (res!=corr[i+2])
      report(&failcnt, corr[i+2], res);
  }

  printf("------\n\n");

  printf("CORRELATION - 1b UNSIGNED\n"); // a..mask, b..signal
  __builtin_swarctrl(SW_OP_COR1b);
  printf("SWAR CONFIG: 0x%08X\n",swar_readconfig());
  bits = 1;
  sop = 0;

  res=0;

  for (i=0; i<CORR_MAX;i+=3) {
    a = corr[i];
    b = corr[i+1];
    res = __builtin_swar(a,b);
    res_sw = swar_corr(0, &a, &b, 32/bits, bits, 0, 0, &sop);
    if (PRINT_DETAILS) printf("a %08X b %08X res %08X\n",a,b,res);
    if (res!=res_sw)
      report(&failcnt, res_sw, res);
  }

  printf("------\n\n");

  printf("CORRELATION - 3b UNSIGNED, REDUCE\n"); // a..mask, b..signal
  __builtin_swarctrl(SW_OP_COR3b | SW_RED);
  printf("SWAR CONFIG: 0x%08X\n",swar_readconfig());
  bits = 3;
  sop = 0;

  res=0;

  for (i=0; i<CORR_MAX;i+=3) {
    a = corr[i];
    b = corr[i+1];
    res = __builtin_swar(a,b);
    res_sw = swar_corr(0, &a, &b, 32/bits, bits, 0, 1, &sop);
    if (PRINT_DETAILS) printf("a %08X b %08X res %08X\n",a,b,res);
    if (res!=res_sw)
      report(&failcnt, res_sw, res);
  }

  printf("------\n\n");

  printf("CORRELATION - 4b SIGNED, REDUCE\n"); // a..mask, b..signal
  __builtin_swarctrl(SW_OP_COR4b | SW_SGND | SW_RED);
  printf("SWAR CONFIG: 0x%08X\n",swar_readconfig());
  bits = 4;
  sop = 0;

  res=0;

  for (i=0; i<CORR_MAX;i+=3) {
    a = corr[i];
    b = corr[i+1];
    res = __builtin_swar(a,b);
    res_sw = swar_corr(0, &a, &b, 32/bits, bits, 1, 1, &sop);
    if (PRINT_DETAILS) printf("a %08X b %08X res %08X\n",a,b,res);
    if (res!=res_sw)
      report(&failcnt, res_sw, res);
  }

  printf("------\n\n");

  printf("DEMODULATION - 2b COMPLEX GROUPED\n"); // a..carrier, b..signal
  __builtin_swarctrl(SW_OP_DEMC2bG);
  printf("SWAR CONFIG: 0x%08X\n",swar_readconfig());

  for (i=0; i<DEMOD_MAX;i+=3) {
    a = demod[i];
    b = demod[i+1];
    res = __builtin_swar(a,b);
    if (PRINT_DETAILS) printf("a %08X b %08X res %08X\n",a,b,res);
    if (res!=demod[i+2])
      report(&failcnt, demod[i+2], res);
  }

  printf("------\n\n");

  printf("DEMODULATION - 2b COMPLEX\n"); // a..carrier, b..signal
  __builtin_swarctrl(SW_OP_DEMC2b);
  printf("SWAR CONFIG: 0x%08X\n",swar_readconfig());
  bits = 2;

  for (i=0; i<DEMOD_MAX;i+=3) {
    a = demod[i];
    b = demod[i+1];
    res = __builtin_swar(a,b);
    res_sw = swar_dem(0, &a, &b, 32/bits, bits, 1, 1);
    if (PRINT_DETAILS) printf("a %08X b %08X res %08X\n",a,b,res);
    if (res!=res_sw)
      report(&failcnt, res_sw, res);
  }

  printf("------\n\n");

  printf("DEMODULATION - 4b COMPLEX\n"); // a..carrier, b..signal
  __builtin_swarctrl(SW_OP_DEMC4b);
  printf("SWAR CONFIG: 0x%08X\n",swar_readconfig());
  bits = 4;

  for (i=0; i<DEMOD_MAX;i+=3) {
    a = demod[i];
    b = demod[i+1];
    res = __builtin_swar(a,b);
    res_sw = swar_dem(0, &a, &b, 32/bits, bits, 1, 1);
    if (PRINT_DETAILS) printf("a %08X b %08X res %08X\n",a,b,res);
    if (res!=res_sw)
      report(&failcnt, res_sw, res);
  }

  printf("------\n\n");

  printf("DEMODULATION - 3b REAL\n"); // a..carrier, b..signal
  __builtin_swarctrl(SW_OP_DEMR3b);
  printf("SWAR CONFIG: 0x%08X\n",swar_readconfig());
  bits = 3;

  for (i=0; i<DEMOD_MAX;i+=3) {
    a = demod[i];
    b = demod[i+1];
    res = __builtin_swar(a,b);
    res_sw = swar_dem(0, &a, &b, 32/bits, bits, 1, 0);
    if (PRINT_DETAILS) printf("a %08X b %08X res %08X\n",a,b,res);
    if (res!=res_sw)
      report(&failcnt, res_sw, res);
  }

#endif /* SWAR_CORR */

#ifdef SWAR_SINCOS
  printf("------\n\n");

  printf("SINCOS - 4b\n"); // a..arg b..N.U.
  __builtin_swarctrl(SW_OP_SC4b);
  printf("SWAR CONFIG: 0x%08X\n",swar_readconfig());
  bits = 4;

  res=0;

  for (i=0; i<DEMOD_MAX;i+=3) {
    a = demod[i];
    b = demod[i+1];
    res = __builtin_swar(a,b);
    res_sw = swar_sincos(0, a, bits);
    if (PRINT_DETAILS) printf("arg %08X res %08X\n",a,res);
    if (res!=res_sw)
      report(&failcnt, res_sw, res);
  }



  printf("------\n\n");

  printf("SINCOS - 3b\n"); // a..arg b..N.U.
  __builtin_swarctrl(SW_OP_SC3b);
  printf("SWAR CONFIG: 0x%08X\n",swar_readconfig());
  bits = 3;

  res=0;

  for (i=0; i<DEMOD_MAX;i+=3) {
    a = demod[i];
    b = demod[i+1];
    res = __builtin_swar(a,b);
    res_sw = swar_sincos(0, a, bits);
    if (PRINT_DETAILS) printf("arg %08X res %08X\n",a,res);
    if (res!=res_sw)
      report(&failcnt, res_sw, res);
  }

  printf("------\n\n");

  printf("SINCOS - 2b\n"); // a..arg b..N.U.
  __builtin_swarctrl(SW_OP_SC2b);
  printf("SWAR CONFIG: 0x%08X\n",swar_readconfig());
  bits = 2;

  res=0;

  for (i=0; i<DEMOD_MAX;i+=3) {
    a = demod[i];
    b = demod[i+1];
    res = __builtin_swar(a,b);
    res_sw = swar_sincos(0, a, bits);
    if (PRINT_DETAILS) printf("arg %08X res %08X\n",a,res);
    if (res!=res_sw)
      report(&failcnt, res_sw, res);
  }


  printf("------\n\n");

  printf("SINCOS - 1b\n"); // a..arg b..N.U.
  __builtin_swarctrl(SW_OP_SC1b);
  printf("SWAR CONFIG: 0x%08X\n",swar_readconfig());
  bits = 1;

  res=0;

  for (i=0; i<DEMOD_MAX;i+=3) {
    a = demod[i];
    b = demod[i+1];
    res = __builtin_swar(a,b);
    res_sw = swar_sincos(0, a, bits);
    if (PRINT_DETAILS) printf("arg %08X res %08X\n",a,res);
    if (res!=res_sw)
      report(&failcnt, res_sw, res);
  }

#endif /* SWAR_SINCOS */



#ifdef SWAR_ALU

  saturate = 1;
  do {

  reduce = 1;
  do {

  sgnd = 1;
  do {
    j = SWAR_UNIT_TYPE;

    if (j==1) {           /* SWAR 4x8b */
      // add 4x8b
      //cfg |= SW_VIDEO;
      if (sgnd) {
        failcnt += alu_swar_add((int *)corr, ALU_NUMEL, -8, saturate, reduce);
        failcnt += alu_swar_sub((int *)corr, ALU_NUMEL, -8, saturate, reduce);
        failcnt += alu_swar_mul((int *)corr, ALU_NUMEL, -8, saturate, reduce);
      } else {
        failcnt += alu_swar_add((int *)corr, ALU_NUMEL, 8, saturate, reduce);
        failcnt += alu_swar_sub((int *)corr, ALU_NUMEL, 8, saturate, reduce);
        failcnt += alu_swar_mul((int *)corr, ALU_NUMEL, 8, saturate, reduce);
      }

    } else if (j==2) {    /* SWAR 2x16b */
      //bits = 16;
      //cfg |= SW_AUDIO;
      if (sgnd) {
        failcnt += alu_swar_add((int *)corr, ALU_NUMEL, -16, saturate, reduce);
        failcnt += alu_swar_sub((int *)corr, ALU_NUMEL, -16, saturate, reduce);
        failcnt += alu_swar_mul((int *)corr, ALU_NUMEL, -16, saturate, reduce);
      } else {
        failcnt += alu_swar_add((int *)corr, ALU_NUMEL, 16, saturate, reduce);
        failcnt += alu_swar_sub((int *)corr, ALU_NUMEL, 16, saturate, reduce);
        failcnt += alu_swar_mul((int *)corr, ALU_NUMEL, 16, saturate, reduce);
      }
      
      
    } else if (j==4) {    /* SWAR 32x1b,16x2b,10x3b,8x4b */
      //cfg |= SW_ALU;
      //bits = 1;
      if (sgnd) {
        failcnt += alu_swar_add((int *)corr, ALU_NUMEL, -1, saturate, reduce);
        failcnt += alu_swar_sub((int *)corr, ALU_NUMEL, -1, saturate, reduce);
        failcnt += alu_swar_mul((int *)corr, ALU_NUMEL, -1, saturate, reduce);
      } else {
        failcnt += alu_swar_add((int *)corr, ALU_NUMEL, 1, saturate, reduce);
        failcnt += alu_swar_sub((int *)corr, ALU_NUMEL, 1, saturate, reduce);
        failcnt += alu_swar_mul((int *)corr, ALU_NUMEL, 1, saturate, reduce);
      }
      //bits = 2;
      if (sgnd) {
        failcnt += alu_swar_add((int *)corr, ALU_NUMEL, -2, saturate, reduce);
        failcnt += alu_swar_sub((int *)corr, ALU_NUMEL, -2, saturate, reduce);
        failcnt += alu_swar_mul((int *)corr, ALU_NUMEL, -2, saturate, reduce);
      } else {
        failcnt += alu_swar_add((int *)corr, ALU_NUMEL, 2, saturate, reduce);
        failcnt += alu_swar_sub((int *)corr, ALU_NUMEL, 2, saturate, reduce);
        failcnt += alu_swar_mul((int *)corr, ALU_NUMEL, 2, saturate, reduce);
      }
      //bits = 3;
      if (sgnd) {
        failcnt += alu_swar_add((int *)corr, ALU_NUMEL, -3, saturate, reduce);
        failcnt += alu_swar_sub((int *)corr, ALU_NUMEL, -3, saturate, reduce);
        failcnt += alu_swar_mul((int *)corr, ALU_NUMEL, -3, saturate, reduce);
      } else {
        failcnt += alu_swar_add((int *)corr, ALU_NUMEL, 3, saturate, reduce);
        failcnt += alu_swar_sub((int *)corr, ALU_NUMEL, 3, saturate, reduce);
        failcnt += alu_swar_mul((int *)corr, ALU_NUMEL, 3, saturate, reduce);
      }
      //bits = 4;
      if (sgnd) {
        failcnt += alu_swar_add((int *)corr, ALU_NUMEL, -4, saturate, reduce);
        failcnt += alu_swar_sub((int *)corr, ALU_NUMEL, -4, saturate, reduce);
        failcnt += alu_swar_mul((int *)corr, ALU_NUMEL, -4, saturate, reduce);
      } else {
        failcnt += alu_swar_add((int *)corr, ALU_NUMEL, 4, saturate, reduce);
        failcnt += alu_swar_sub((int *)corr, ALU_NUMEL, 4, saturate, reduce);
        failcnt += alu_swar_mul((int *)corr, ALU_NUMEL, 4, saturate, reduce);
      }

    }

  } while (sgnd--);
  } while (reduce--);
  } while (saturate--);


#endif /* SWAR_ALU */

  printf("------\n\n");

#ifdef SYSREGS
  sysregs_stop();
  sysregs_read(&ticks_lo, &ticks_hi, &insns_lo, &insns_hi);

  if (ticks_hi)
    printf("TEST EXECUTED IN (%d * 2^32 + %d) TICKS AND (%d * 2^32 + %d) INSTRUCTIONS\n\n", ticks_hi, ticks_lo, insns_lo, insns_hi);
  else
    printf("TEST EXECUTED IN %d TICKS AND %d INSTRUCTIONS\n\n", ticks_lo, insns_lo);
#endif

  if (!failcnt)
    printf("END OF TEST\n\n");
  else
    printf("TEST COMPLETED WITH ERRORS\n\n");

  __asm__ volatile (
    "\t ta 0\n"
    "\t nop\n"
    "\t nop\n"
  );

  return 0;
}