swarfir.c 40.9 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 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 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 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 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 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 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
/* -----------------------------------------------------------------------------
 *  Copyright (C) 2021,2022 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    : swarfir.c
 *  Authors     : Roman Bartosinski
 *  Description : FIR filter with SWAR operations
 *  Release     :
 *  Version     : 1.0
 *  Date        : 27.4.2021
 * -----------------------------------------------------------------------------
 */

#include <stdint.h>
#include <stdlib.h>

#include "swarconf.h"

#include "../common_sys_header.inc"

//#define PRINT_TYPES     1
//#define PRINT_RESULTS   1
//#define PRINT_FAILED    1


#define TEST_OFFLINE 1
#define TEST_ONLINE  1

// SWARBASESIZE != 32 or 64
#ifndef SWARBASESIZE
  #warning Default SWAR basic type u64 is used.
  #define SWARBASESIZE          64
#endif /* SWARBASESIZE BASESIZE */

// example definitions - swar with 8bit elements
#define BITSZ             16

#ifndef CONFIG_DATA_LENGTH
  #define DATA_LENGTH       50
#else
  #define DATA_LENGTH       CONFIG_DATA_LENGTH
#endif /* CONFIG_DATA_LENGTH */

#ifndef CONFIG_FIR_LENGTH
  #define FIR_LENGTH        5    /* more accurately it is a number of FIR coefficients [c0,c1,c2,c3,c4,...]  */
#else
  #define FIR_LENGTH        CONFIG_FIR_LENGTH
#endif /* CONFIG_FIR_LENGTH */

//#ifdef CONFIG_ONLINE_MULTI
  //#define ONLINE_MULTIPLE   CONFIG_ONLINE_MULTI
//#else
  //#define ONLINE_MULTIPLE   1
//#endif /* CONFIG_ONLINE_MULTI */

#ifdef CONFIG_ONLINE_FIRLEN
  #define ONLINE_FIRLEN   CONFIG_ONLINE_FIRLEN
#else
  #define ONLINE_FIRLEN   (TEST_FIRCOEF_SIZE)
#endif /* CONFIG_ONLINE_MULTI */
#ifdef CONFIG_DATA_LENGTH
  #define ONLINE_DATALEN  (CONFIG_DATA_LENGTH)
#else
  #define ONLINE_DATALEN  TEST_INDATA_SIZE
#endif

#define MAXRAND           20

#if SWARBASESIZE==32
  typedef int32_t basetype;
  #define PRNFMTHEX  "%X"
  #define PRNFMTDEC  "%u"
#else
  typedef int64_t basetype;
  #define PRNFMTHEX  "%lX"
  #define PRNFMTDEC  "%lu"
#endif /* SWARBASESIZE */

typedef basetype swpack __attribute__((subword(BITSZ)));
typedef basetype swelm __attribute__((subword(BITSZ,1)));

#if BITSZ==8
  typedef int8_t dataelm;
  #define SWTP  SW_VIDEO
#elif BITSZ==16
  typedef int16_t dataelm;
  #define SWTP  SW_AUDIO
#elif BITSZ==32
  typedef int32_t dataelm;
  #define SWTP  SW_ALU
#endif /* BITSZ */

typedef basetype resultelm;

typedef union {
  swpack    s;
#if SWARBASESIZE==32
  int       i;
  unsigned  u;
#else
  long      i;
  unsigned long u;
#endif
} multi_t;

#define MAXNUM            ((1<<BITSZ))
#define MINVAL            0
#define MAXVAL            ((1<<BITSZ)-1)

#define PACKING           (SWARBASESIZE/BITSZ)
#define DATA_ARRAY_SIZE   ((DATA_LENGTH+PACKING-1)/PACKING)   /* array for packed data */
#define COEF_ARRAY_SIZE   ((FIR_LENGTH+PACKING-1)/PACKING)    /* array for packed coefficients */

#define MAXFIRLENGTH      128
/* -------------------------------------------------------------------------- */
void print_dir_data(const char *prefix, dataelm *pd, int len);
void print_swar_data(const char *prefix, swpack *ps, int len);
void print_counters(const char *prefix, unsigned *tm, unsigned *ins);

/* -------------------------------------------------------------------------- */
// experimental data
// signal (sinus 1kHz + sinus 15kHz, sampl.f=48kHz
const dataelm test_input[] = {
  0,55,-9,18,92,38,33,115,80,42,121,109,46,109,121,42,80,115,33,38,92,18,-9,55,0,
  -55,9,-18,-92,-38,-33,-115,-80,-42,-121,-109,-46,-109,-121,-42,-80,-115,-33,-38,
  -92,-18,9,-55,0,55,-9,18,92,38,33,115,80,42,121,109,46,109,121,42,80,115,33,38,
  92,18,-9,55,0,-55,9,-18,-92,-38,-33,-115,-80,-42,-121,-109,-46,-109,-121,-42,-80,
  -115,-33,-38,-92,-18,9,-55,0,55,-9,18,92,38,33,115,80,42,121,109,46,109,121,42,
  80,115,33,38,92,18,-9,55,0,-55,9,-18,-92,-38,-33,-115,-80,-42,-121,-109,-46,-109,
  -121,-42,-80,-115,-33,-38,-92,-18,9,-55,0,55,-9,18,92,38,33,115,80,42,121,109,
  46,109,121,42,80,115,33,38,92,18,-9,55,0,-55,9,-18,-92,-38,-33,-115,-80,-42,-121,
  -109,-46,-109,-121,-42,-80,-115,-33,-38,-92,-18,9,-55,0,55,-9,18,92,38,33,115,
  80,42,121,109,46,109,121,42,80,115,33,38,92,18,-9,55,0,-55,9,-18,-92,-38,-33,-115,
  -80,-42,-121,-109,-46,-109,-121,-42,-80,-115,-33,-38,-92,-18,9,-55,0,55,-9,18,
  92,38,33,115,80,42,121,109,46,109,121,42,80,115,33,38,92,18,-9,55,0,-55,9,-18,
  -92,-38,-33,-115,-80,-42,-121,-109,-46,-109,-121,-42,-80,-115,-33,-38,-92,-18,
  9,-55,0,55,-9,18,92,38,33,115,80,42,121,109,46,109,121,42,80,115,33,38,92,18,
  -9,55,0,-55,9,-18,-92,-38,-33,-115,-80,-42,-121,-109,-46,-109,-121,-42,-80,-115,
  -33,-38,-92,-18,9,-55,0,55,-9,18,92,38,33,115,80,42,121,109,46,109,121,42,80,115,
  33,38,92,18,-9,55,0,-55,9,-18,-92,-38,-33,-115,-80,-42,-121,-109,-46,-109,-121,
  -42,-80,-115,-33,-38,-92,-18,9,-55,0,55,-9,18,92,38,33,115,80,42,121,109,46,109,
  121,42,80,115,33,38,92,18,-9,55,0,-55,9,-18,-92,-38,-33,-115,-80,-42,-121,-109,
  -46,-109,-121,-42,-80,-115,-33,-38,-92,-18,9,-55,0,55,-9,18,92,38,33,115,80,42,
  121,109,46,109,121,42,80,115,33,38,92,18,-9,55,0,-55,9,-18,-92,-38,-33,-115,-80,
  -42,-121,-109,-46,-109,-121,-42,-80,-115,-33,-38,-92,-18,9,-55,0,55,-9,18,92,38,
  33,115,80,42,121,109,46,109,121,42,80,115,33,38,92,18,-9,55,0,-55,9,-18,-92,-38,
  -33,-115,-80,-42,-121,-109,-46,-109,-121,-42,-80,-115,-33,-38,-92,-18,9,-55,0,55,
  -9,18,92,38,33,115,80,42,121,109,46,109,121,42,80,115,33,38,92,18,-9,55,0,-55,9,
  -18,-92,-38,-33,-115,-80,-42,-121,-109,-46,-109,-121,-42,-80,-115,-33,-38,-92,-18,
  9,-55,0,55,-9,18,92,38,33,115,80,42,121,109,46,109,121,42,80,115,33,38,92,18,-9,
  55,0,-55,9,-18,-92,-38,-33,-115,-80,-42,-121,-109,-46,-109,-121,-42,-80,-115,-33,
  -38,-92,-18,9,-55,0,55,-9,18,92,38,33,115,80,42,121,109,46,109,121,42,80,115,33,
  38,92,18,-9,55,0,-55,9,-18,-92,-38,-33,-115,-80,-42,-121,-109,-46,-109,-121,-42,
  -80,-115,-33,-38,-92,-18,9,-55,0,55,-9,18,92,38,33,115,80,42,121,109,46,109,121,
  42,80,115,33,38,92,18,-9,55,0,-55,9,-18,-92,-38,-33,-115,-80,-42,-121,-109,-46,
  -109,-121,-42,-80,-115,-33,-38,-92,-18,9,-55,0,55,-9,18,92,38,33,115,80,42,121,
  109,46,109,121,42,80,115,33,38,92,18,-9,55,0,-55,9,-18,-92,-38,-33,-115,-80,-42,
  -121,-109,-46,-109,-121,-42,-80,-115,-33,-38,-92,-18,9,-55,0,55,-9,18,92,38,33,
  115,80,42,121,109,46,109,121,42,80,115,33,38,92,18,-9,55,0,-55,9,-18,-92,-38,-33,
  -115,-80,-42,-121,-109,-46,-109,-121,-42,-80,-115,-33,-38,-92,-18,9,-55,0,55,-9,
  18,92,38,33,115,80,42,121,109,46,109,121,42,80,115,33,38,92,18,-9,55,0,-55,9,-18,
  -92,-38,-33,-115,-80,-42,-121,-109,-46,-109,-121,-42,-80,-115,-33,-38,-92,-18,9,
  -55,0,55,-9,18,92,38,33,115,80,42,121,109,46,109,121,42,80,115,33,38,92,18,-9,55,
  0,-55,9,-18,-92,-38,-33,-115,-80,-42,-121,-109,-46,-109,-121,-42,-80,-115,-33,-38,
  -92,-18,9,-55,0,55,-9,18,92,38,33,115,80,42,121,109,46,109,121,42,80,115,33,38,92,
  18,-9,55,0,-55,9,-18,-92,-38,-33,-115,-80,-42,-121,-109,-46,-109,-121,-42,-80,
  -115,-33,-38,-92,-18,9,-55,0,55,-9,18,92,38,33,115,80,42,121,109,46,109,121,42,
  80,115,33,38,92,18,-9,55,0,-55,9,-18,-92,-38,-33,-115,-80,-42,-121,-109,-46,-109,-121,-42
};
#define TEST_INDATA_SIZE  (sizeof(test_input)/sizeof(dataelm))
// coefficients of FIR filter
const dataelm test_fircoef[] = {
  0,0,0,0,1,1,0,-2,-3,-3,0,6,14,20,23,20,14,6,0,-3,-3,-2,0,1,1
};
#define TEST_FIRCOEF_SIZE  (sizeof(test_fircoef)/sizeof(dataelm))
// extra longer vector for testing only
const dataelm test_fircoef_extra[] = {
  0,0,0,0,1,1,0,-2,-3,-3,0,6,14,20,23,20,14,6,0,-3,-3,-2,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};

// filtered data for checking
const resultelm test_output[] = {
  0,0,0,0,0,0,-55,-156,-156,-119,-38,293,754,1365,2142,3087,3994,4926,5848,6579,
  7204,7706,8022,8244,8346,8244,8022,7706,7204,6579,5903,5082,4150,3206,2180,1072,
  0,-1072,-2180,-3206,-4150,-5082,-5903,-6579,-7204,-7706,-8022,-8244,-8346,-8244,
  -8022,-7706,-7204,-6579,-5903,-5082,-4150,-3206,-2180,-1072,0,1072,2180,3206,4150,
  5082,5903,6579,7204,7706,8022,8244,8346,8244,8022,7706,7204,6579,5903,5082,4150,
  3206,2180,1072,0,-1072,-2180,-3206,-4150,-5082,-5903,-6579,-7204,-7706,-8022,-8244,
  -8346,-8244,-8022,-7706,-7204,-6579,-5903,-5082,-4150,-3206,-2180,-1072,0,1072,
  2180,3206,4150,5082,5903,6579,7204,7706,8022,8244,8346,8244,8022,7706,7204,6579,
  5903,5082,4150,3206,2180,1072,0,-1072,-2180,-3206,-4150,-5082,-5903,-6579,-7204,
  -7706,-8022,-8244,-8346,-8244,-8022,-7706,-7204,-6579,-5903,-5082,-4150,-3206,
  -2180,-1072,0,1072,2180,3206,4150,5082,5903,6579,7204,7706,8022,8244,8346,8244,
  8022,7706,7204,6579,5903,5082,4150,3206,2180,1072,0,-1072,-2180,-3206,-4150,-5082,
  -5903,-6579,-7204,-7706,-8022,-8244,-8346,-8244,-8022,-7706,-7204,-6579,-5903,
  -5082,-4150,-3206,-2180,-1072,0,1072,2180,3206,4150,5082,5903,6579,7204,7706,8022,
  8244,8346,8244,8022,7706,7204,6579,5903,5082,4150,3206,2180,1072,0,-1072,-2180,
  -3206,-4150,-5082,-5903,-6579,-7204,-7706,-8022,-8244,-8346,-8244,-8022,-7706,
  -7204,-6579,-5903,-5082,-4150,-3206,-2180,-1072,0,1072,2180,3206,4150,5082,5903,
  6579,7204,7706,8022,8244,8346,8244,8022,7706,7204,6579,5903,5082,4150,3206,2180,
  1072,0,-1072,-2180,-3206,-4150,-5082,-5903,-6579,-7204,-7706,-8022,-8244,-8346,
  -8244,-8022,-7706,-7204,-6579,-5903,-5082,-4150,-3206,-2180,-1072,0,1072,2180,
  3206,4150,5082,5903,6579,7204,7706,8022,8244,8346,8244,8022,7706,7204,6579,5903,
  5082,4150,3206,2180,1072,0,-1072,-2180,-3206,-4150,-5082,-5903,-6579,-7204,-7706,
  -8022,-8244,-8346,-8244,-8022,-7706,-7204,-6579,-5903,-5082,-4150,-3206,-2180,
  -1072,0,1072,2180,3206,4150,5082,5903,6579,7204,7706,8022,8244,8346,8244,8022,
  7706,7204,6579,5903,5082,4150,3206,2180,1072,0,-1072,-2180,-3206,-4150,-5082,-5903,
  -6579,-7204,-7706,-8022,-8244,-8346,-8244,-8022,-7706,-7204,-6579,-5903,-5082,-4150,
  -3206,-2180,-1072,0,1072,2180,3206,4150,5082,5903,6579,7204,7706,8022,8244,8346,
  8244,8022,7706,7204,6579,5903,5082,4150,3206,2180,1072,0,-1072,-2180,-3206,-4150,
  -5082,-5903,-6579,-7204,-7706,-8022,-8244,-8346,-8244,-8022,-7706,-7204,-6579,-5903,
  -5082,-4150,-3206,-2180,-1072,0,1072,2180,3206,4150,5082,5903,6579,7204,7706,8022,
  8244,8346,8244,8022,7706,7204,6579,5903,5082,4150,3206,2180,1072,0,-1072,-2180,
  -3206,-4150,-5082,-5903,-6579,-7204,-7706,-8022,-8244,-8346,-8244,-8022,-7706,
  -7204,-6579,-5903,-5082,-4150,-3206,-2180,-1072,0,1072,2180,3206,4150,5082,5903,
  6579,7204,7706,8022,8244,8346,8244,8022,7706,7204,6579,5903,5082,4150,3206,2180,
  1072,0,-1072,-2180,-3206,-4150,-5082,-5903,-6579,-7204,-7706,-8022,-8244,-8346,
  -8244,-8022,-7706,-7204,-6579,-5903,-5082,-4150,-3206,-2180,-1072,0,1072,2180,
  3206,4150,5082,5903,6579,7204,7706,8022,8244,8346,8244,8022,7706,7204,6579,5903,
  5082,4150,3206,2180,1072,0,-1072,-2180,-3206,-4150,-5082,-5903,-6579,-7204,-7706,
  -8022,-8244,-8346,-8244,-8022,-7706,-7204,-6579,-5903,-5082,-4150,-3206,-2180,
  -1072,0,1072,2180,3206,4150,5082,5903,6579,7204,7706,8022,8244,8346,8244,8022,
  7706,7204,6579,5903,5082,4150,3206,2180,1072,0,-1072,-2180,-3206,-4150,-5082,-5903,
  -6579,-7204,-7706,-8022,-8244,-8346,-8244,-8022,-7706,-7204,-6579,-5903,-5082,
  -4150,-3206,-2180,-1072,0,1072,2180,3206,4150,5082,5903,6579,7204,7706,8022,8244,
  8346,8244,8022,7706,7204,6579,5903,5082,4150,3206,2180,1072,0,-1072,-2180,-3206,
  -4150,-5082,-5903,-6579,-7204,-7706,-8022,-8244,-8346,-8244,-8022,-7706,-7204,
  -6579,-5903,-5082,-4150,-3206,-2180,-1072,0,1072,2180,3206,4150,5082,5903,6579,
  7204,7706,8022,8244,8346,8244,8022,7706,7204,6579,5903,5082,4150,3206,2180,1072,
  0,-1072,-2180,-3206,-4150,-5082,-5903,-6579,-7204,-7706,-8022,-8244,-8346,-8244,
  -8022,-7706,-7204,-6579,-5903,-5082,-4150,-3206,-2180,-1072,0,1072,2180,3206,4150,
  5082,5903,6579,7204,7706,8022,8244,8346,8244,8022,7706,7204,6579,5903,5082,4150,
  3206,2180,1072,0,-1072,-2180,-3206,-4150,-5082,-5903,-6579,-7204,-7706,-8022,-8244,
  -8346,-8244,-8022,-7706,-7204,-6579,-5903,-5082,-4150,-3206,-2180,-1072,0,1072,
  2180,3206,4150,5082,5903,6579,7204,7706,8022,8244,8346,8244,8022,7706,7204,6579,
  5903,5082,4150,3206,2180,1072,0,-1072,-2180,-3206,-4150,-5082,-5903,-6579,-7204,
  -7706,-8022,-8244,-8346,-8244,-8022,-7706,-7204,-6579,-5903,-5082,-4150,-3206,
  -2180,-1072,0,1072,2180,3206,4150,5082,5903,6579,7204,7706,8022,8244,8346,8244,
  8022,7706,7204,6579,5903,5082,4150,3206,2180,1072,0,-1072,-2180,-3206,-4150,-5082,
  -5903,-6579,-7204,-7706,-8022,-8244,-8346,-8244,-8022,-7706,-7204,-6579,-5903,
  -5082,-4150,-3206,-2180,-1072,0,1072,2180,3206,4150,5082,5903,6579,7204,7706,8022,
  8244,8346,8244,8022,7706,7204,6579,5903,5082,4150,3206,2180,1072,0,-1072,-2180,
  -3206,-4150,-5082,-5903,-6579,-7204,-7706,-8022,-8244,-8346,-8244,-8022,-7706,
  -7204,-6579,-5903,-5082,-4150,-3206,-2180,-1072,0,1072,2180,3206,4150,5082,5903,
  6579,7204,7706,8022,8244,8346,8244,8022,7706,7204,6579,5903,5082,4150,3206,2180,
  1072,0,-1072,-2180,-3206,-4150,-5082,-5903,-6579,-7204,-7706,-8022,-8244,-8346,
  -8244,-8022,-7706,-7204,-6579,-5903,-5082,-4150,-3206,-2180,-1072,0,1072,2180,
  3206,4150,5082,5903,6579,7204,7706,8022,8244,8346,8244,8022,7706,7204,6579,5903,
  5082,4150,3206,2180,1072,0,-1072,-2180,-3206
};
#define TEST_OUTPUT_SIZE  (sizeof(test_output)/sizeof(resultelm))

resultelm test_dir_output[TEST_OUTPUT_SIZE];
resultelm test_swar_output[TEST_OUTPUT_SIZE];

/* -------------------------------------------------------------------------- */
// data for classical FIR computation
dataelm dir_in_x[DATA_LENGTH+FIR_LENGTH];  /* INPUT DATA + zeros */
dataelm dir_in_c[FIR_LENGTH];             /* COEFFICIENTS */
resultelm dir_out_y[DATA_LENGTH];            /* OUTPUT DATA */


// data for FIR computation with SWAR operations
swpack swar_in_x[(DATA_LENGTH+FIR_LENGTH+(PACKING-1))/PACKING]; /* space for data ramp up */
swpack swar_in_c[COEF_ARRAY_SIZE];

resultelm swar_out_y_red[DATA_LENGTH];
resultelm swar_out_y_accum[DATA_LENGTH];
resultelm swar_out_y_add[DATA_LENGTH];

/* array of extended vectors of coefficients */
#define EXTCOEF_LEN   (FIR_LENGTH+PACKING-1+(PACKING-1))/PACKING  /* array of packed coefficients */
//multi_t extcoefs[PACKING][EXTCOEF_LEN];
swpack swar_ecoefs[PACKING][EXTCOEF_LEN];


/* ========================================================================== */
// data preparation
void prepare_data(void)
{
  for (unsigned i=0;i<DATA_LENGTH;++i) {
    int d = rand();
//printf("!x %d -> ", d);
    d = ((d&0x1000)?-1:1) * (d % (MAXRAND));
//printf(" %d\n", d);
    dir_in_x[i] = d;
    swar_in_x[i/PACKING][i%PACKING] = d;
  }

  for (unsigned i=DATA_LENGTH;i<(DATA_LENGTH+FIR_LENGTH);++i)
    dir_in_x[i] = 0;

  for (unsigned i=0;i<FIR_LENGTH;++i) {
    int c = rand();
//printf("!c %d -> ", c);
    c = ((c&0x1000)?-1:1) * (c % (MAXRAND));
//printf(" %d\n", c);
    dir_in_c[i] = c;
    swar_in_c[i/PACKING][i%PACKING] = c;
  }

  //// prepare coefficients with different aligning
  //for (unsigned  p=0; p<PACKING; ++p) { // for each alignment
    //unsigned rem = 0;
    //for(unsigned w=0; w<EXTCOEF_LEN; ++w) {
      //unsigned nv = ((unsigned *)swar_in_c)[w];
      //extcoefs[p][w].u = rem | (nv<<(p*BITSZ));
      //rem = (nv>>((PACKING-p)*BITSZ)) & ((1<<(BITSZ*p))-1);
    //}
    //extcoefs[p][EXTCOEF_LEN].u = rem;
  //}
//#if 1
  //// debug print array of coefficients
  //for(unsigned p=0;p<PACKING;++p) {
    //m_print(" #"); i_print(p); m_print(": ");
    //print_swar_data("extcoef ", (swpack*)&extcoefs[p][0], EXTCOEF_LEN*PACKING);
  //}
//#endif

  //// copy extended coefficients to standalone swar array
  //{
    //unsigned *pi = (unsigned *)extcoefs;
    //unsigned *po = (unsigned *)swar_ecoefs;
    //for (unsigned p=0;p<PACKING*EXTCOEF_LEN;++p)
      //*po++ = *pi++;
  //}

  for (unsigned p=0; p<PACKING; ++p) {
    for (unsigned w=0; w<EXTCOEF_LEN; ++w) {
      *(resultelm *)&swar_ecoefs[p][w] = 0;
    }
    for (unsigned c=0; c<FIR_LENGTH;++c) {
      unsigned iw = (c+p) / PACKING;
      unsigned ie = (c+p) % PACKING;
      swar_ecoefs[p][iw][ie] = dir_in_c[c];
    }
  }
  
#if 0
  // debug print array of coefficients
  for(unsigned p=0;p<PACKING;++p) {
    printf(" #%u: ", p);
    print_swar_data("DIRecoef ", (swpack*)&swar_ecoefs[p][0], EXTCOEF_LEN*PACKING);
  }
#endif
#if 0
  for (unsigned i=0;i<(DATA_LENGTH+FIR_LENGTH);++i)
    printf(" * %d\n", dir_in_x[i]);
#endif
}


/* ========================================================================== */
// classical computation element by element
// data must be correctly long (with zeroes after real data)
// datalen = number of input data (or length of required output vector)
// firlen = number of coefficients (b0,b1,b2,b3,...)
void fir_offline(resultelm *out_y, dataelm *in_x, int len_data, dataelm *in_c)
{
  for (unsigned j=0; j<len_data; ++j) {
    resultelm s = 0;
    for (unsigned i=0; i<FIR_LENGTH; ++i) {
      s += in_c[i] * in_x[i+j];
    }
    out_y[j] = s;
  }
}


dataelm dir_x_mem[MAXFIRLENGTH];  // [x(n),x(n-1),x(n-2),x(n-3),...]
dataelm dir_c_mem[MAXFIRLENGTH];  // [c0,c1,c2,c3,c4,...]
unsigned mem_firlen;
unsigned rbuf_next;

void fir_online_init(unsigned firlen, const dataelm *in_c)
{
  if (firlen>MAXFIRLENGTH) {
    printf("!!! Too long FIR filter\n");
    firlen = MAXFIRLENGTH;
  }
  for (unsigned n=0;n<firlen;++n) {
    dir_x_mem[n] = 0;
    dir_c_mem[n] = in_c[n]; //firlen-n-1];
//printf("#%u: c %d (x%X) x %d (x%X)\n", n, dir_c_mem[n], dir_c_mem[n], dir_x_mem[n], dir_x_mem[n]);
  }
  mem_firlen = firlen;
//NL;
}

resultelm fir_online(dataelm in_x)
{
  resultelm y = 0;
  for (unsigned i=mem_firlen-1;i>0;--i) {
    dir_x_mem[i] = dir_x_mem[i-1];  // x(n-1) = x'(n)
//printf("#%u: %u (x%X)\n", i, dir_x_mem[i], dir_x_mem[i]);
    y += dir_x_mem[i]*dir_c_mem[i];
  }
  dir_x_mem[0] = in_x;
  y+= dir_x_mem[0]*dir_c_mem[0];
//printf("#0: %u (x%X) ---> %u (x%X)\n", dir_x_mem[0], dir_x_mem[0], y, y);
  return y;
}

/* ---------------------------------- */

void fir_online_rbuf_init(unsigned firlen, const dataelm *in_c)
{
  if (firlen>MAXFIRLENGTH) {
    printf("!!! Too long FIR filter\n");
    firlen = MAXFIRLENGTH;
  }
  rbuf_next = firlen-1; // fill from back to front
  for (unsigned n=0;n<firlen;++n) {
    dir_x_mem[n] = 0;
    dir_c_mem[n] = in_c[n];
//m_print("#"); i_print(n); m_print(":"); i_print(dir_c_mem[n]); m_print("  ");
  }
  mem_firlen = firlen;
//NL;
}

resultelm fir_online_rbuf(dataelm in_x)
{
  resultelm y = 0;
  unsigned xi = rbuf_next;
  dir_x_mem[rbuf_next] = in_x;
  
  for (unsigned i=0;i<mem_firlen;++i) {
//m_print("#"); i_print(i); m_print(":"); i_print(dir_x_mem[i]); m_print("  ");
    y += dir_c_mem[i] * dir_x_mem[xi];
    if (++xi>=mem_firlen) xi=0;
  }
  if (rbuf_next) rbuf_next--;
  else rbuf_next = mem_firlen-1;
  return y;
}

/* ---------------------------------- */
unsigned bufsize;
unsigned bufmask;

void fir_online_align_rbuf_init(unsigned firlen, const dataelm *in_c)
{
  if (firlen>MAXFIRLENGTH) {
    printf("!!! Too long FIR filter\n");
    firlen = MAXFIRLENGTH;
  }
  unsigned p = 8*sizeof(unsigned) - __builtin_clz(firlen);
  mem_firlen = firlen;
  bufsize = 1<<p;
  bufmask = bufsize-1;
//  printf("FIRLEN = %u %u, mask=%u\n", mem_firlen, bufsize, bufmask);

  rbuf_next = bufsize-1; // fill from back to front
  for (unsigned n=0;n<bufsize;++n) {
    dir_x_mem[n] = 0;
  }
  for (unsigned n=0;n<firlen;++n) {
    dir_c_mem[n] = in_c[n];
//printf("c # %u : %d x%X\n", n, dir_c_mem[n], dir_c_mem[n]);
  }
}

resultelm fir_online_align_rbuf(dataelm in_x)
{
  resultelm y = 0;
  unsigned xi = rbuf_next;
  dir_x_mem[rbuf_next] = in_x;
  
  for (unsigned i=0;i<mem_firlen;++i) {
//printf("  x # %u : %d x%X\n", i, dir_x_mem[i], dir_x_mem[i]);
    y += dir_c_mem[i] * dir_x_mem[xi];
    xi = (xi+1)&bufmask;
//printf("   y = %d x%X | xi = %u\n", y, y, xi);
  }
  rbuf_next = (rbuf_next-1)&bufmask;
  return y;
}


/* ========================================================================== */
// swar computation
// use swar multiplication with reduction
void fir_swar_mul_red(resultelm *out_y, swpack *in_x, int len_data) //, swpack *in_c)
{
  #pragma swar reduce
  // loop over all len_data data
  for (unsigned j=0; j<len_data; ++j) {
    //multi_t iny;
    swpack swz[1]; /* workaround to generate swarctrl automatically */
//    swpack sa[1],sb[1],sz[1]; /* workaround to generate swarctrl automatically */
    unsigned pi = j%PACKING;
    unsigned pw = j/PACKING;
    // reset accumulator
    int acc = 0;
    // loop 1:((len_fir/PACKING)+1)
    for (unsigned w=0;w<EXTCOEF_LEN;++w) {
      // swar mul xi,bi => accum  [bi=select coefs with correct alignment]
      swz[0] = in_x[pw+w] * swar_ecoefs[pi][w];
      acc += *((int *)&swz);
    }
    // save accum to yi
    out_y[j] = acc;
  }
}

/* -------------------------------------------------------------------------- */
// use swar multiplication with lane accumulators
void fir_swar_mul_accum(resultelm *out_y, swpack *in_x, int len_data) //, swpack *in_c)
{
  for (unsigned j=0; j<len_data; ++j) {
    swpack swz[1];
    unsigned pi = j%PACKING;
    unsigned pw = j/PACKING;
    // internal accumulators are reset automatically with settings swar control word
    for (unsigned w=0;w<EXTCOEF_LEN;++w) {
      swz[0] = in_x[pw+w] * swar_ecoefs[pi][w];
    }
    resultelm sum = 0;
    for (unsigned a=0;a<PACKING;++a) {
      sum += __builtin_swaraccum(a<<1);
    }
    out_y[j] = sum;
  }
}

/* -------------------------------------------------------------------------- */
// use multiplication without lane accumulators or reduction
swpack swar_tmp_mem[EXTCOEF_LEN]; /* space for internal subtotals */

/* this version needs elements with sufficient bitwidth - saturation is more possible */
void fir_swar_mul_add(resultelm *out_y, swpack *in_x, int len_data) //, swpack *in_c)
{
  #pragma swar saturate
  for (unsigned j=0; j<len_data; ++j) {
    unsigned pi = j%PACKING;
    unsigned pw = j/PACKING;
    // tmp[i] = x[n-i]*c[i]
    for (unsigned w=0;w<EXTCOEF_LEN;++w) {
      swar_tmp_mem[w] = in_x[pw+w] * swar_ecoefs[pi][w];
    }
    // sum in packed data
    swpack sum;
    sum = swar_tmp_mem[0];
    for (unsigned w=1;w<EXTCOEF_LEN;++w) {
      swpack tmp = swar_tmp_mem[w];
      sum = sum + tmp;
    }
    // sum of packed data
    resultelm out = 0;
    for (unsigned p=0;p<PACKING;++p) {
      dataelm oval = (((*(basetype*)&sum) >> (p*BITSZ)) & MAXVAL);
      out += oval;
    }
    out_y[j] = out;
  }
}

/* -------------------------------------------------------------------------- */
swpack swar_x_mem[MAXFIRLENGTH/PACKING+1]; /* space for the latest data vector */
swpack swar_c_mem[MAXFIRLENGTH/PACKING+1]; /* space for the coefficients (for testing another alignment) */
unsigned swar_mem_coefwrds = 0;

void fir_swar_online_init(unsigned firlen, const dataelm *in_c)
{
  if (firlen>MAXFIRLENGTH) {
    printf("!!! Too long FIR filter.\n");
    firlen = MAXFIRLENGTH;
  }
  unsigned cw = ((firlen+PACKING-1)/PACKING);
  for (unsigned w=0;w<cw;++w) {
    *(resultelm *)&swar_c_mem[w] = 0; // swar_in_c[w];
    *(resultelm *)&swar_x_mem[w] = 0;
  }
  for (unsigned c=0;c<firlen;++c) {
    resultelm w = c / PACKING;
    resultelm e = c % PACKING;
    swar_c_mem[w][e] = in_c[c];
//printf(" !c %u (%d) w=%u/e=%u -> x%lX\n",c,in_c[c],w,e, *(resultelm *)&swar_c_mem[w]);
  }

#if 0
  // debug print coefficients
  print_swar_data("coeffs ", swar_c_mem, firlen);
#endif

  swar_mem_coefwrds = cw;
}

resultelm fir_swar_online(dataelm in_x)
{
  resultelm y = 0;
  // update data vector
  unsigned rem = in_x & MAXVAL;
  for(unsigned w = 0;w<swar_mem_coefwrds;++w) {
//printf("UPx #%u: from x%lX\n", w, *((resultelm *)&swar_x_mem[w]));
    resultelm new = (*((resultelm *)&swar_x_mem[w]) << BITSZ) | (rem & MAXVAL);
    rem = *((resultelm *)&swar_x_mem[w]) >> (SWARBASESIZE-BITSZ);
    swar_x_mem[w] = new;
//printf("        to x%lX\n", *((resultelm *)&swar_x_mem[w]));
  }
  // compute result for the latest data vector
  #pragma swar manual
//#pragma swar reduce
  __builtin_swarctrl(SWTP | SW_SGND | SW_RED | SW_OP_MUL);
  for(unsigned w=0;w<swar_mem_coefwrds;++w) {
//printf("SO#%u:x=x%lX c=x%lX\n", w, *(basetype *)&swar_x_mem[w], *(basetype *)&swar_c_mem[w]);
    swpack yc = __builtin_swar(swar_x_mem[w], swar_c_mem[w]);
//printf(" ----------- YC = x%lx\n", *(basetype *)&yc);
    y += *(resultelm *)&yc;
//printf(" ----------- Y = x%lx\n", *(basetype *)&y);
  }
//printf(" =============== Y = x%lx\n", *(basetype *)&y);
  return y;
}

/* ---------------------------------- */
// 
swpack swar_c_arr[PACKING][MAXFIRLENGTH/PACKING+1]; /* space for aligned vectors of coefficients */
unsigned swar_ca_len;
unsigned swar_pidx, swar_widx;

void fir_swar_online_rbuf_init(unsigned firlen, const dataelm *in_c)
{
  swar_ca_len = (firlen + PACKING-1 + PACKING-1) / PACKING; // pocet slov
//m_print("flen="); i_print(firlen); m_print("s_ca_l="); i_print(swar_ca_len); NL;
  swar_pidx = 0;
  swar_widx = 0;
  // prepare swar_c_arr
  // reset swar_x_mem
  for (unsigned w=0; w<swar_ca_len; ++w) {
    *(resultelm *)&swar_x_mem[w] = 0;
    for (unsigned p; p<PACKING; ++p)
      *(resultelm *)&swar_c_arr[p][w] = 0;
  }
  for (unsigned p=0; p<PACKING; ++p) {
    for (unsigned c=0; c<firlen; ++c) {
      unsigned w = (c+PACKING-p-1) / PACKING;
      unsigned e = (c+PACKING-p-1) % PACKING;
//m_print("cc p="); i_print(p); m_print(" w="); i_print(w); m_print(" e="); i_print(e); NL;
      swar_c_arr[p][w][e] = in_c[c];
    }
  }
//  print_dir_data("in-c", in_c, firlen);
//  for(unsigned p=0;p<PACKING;++p) {
//    m_print(" #"); i_print(p); m_print(": ");
//    print_swar_data("ac0", &swar_c_arr[p][0], swar_ca_len*PACKING);
//  }
//  print_swar_data("x0", &swar_x_mem[0], swar_ca_len*PACKING);
}

resultelm fir_swar_online_rbuf(dataelm in_x)
{
  #pragma swar manual
  resultelm y = 0;
//m_print("<<< x = "); i_print(in_x); NL;
  // update X rbuf with a new value
  if (swar_pidx==0) *((unsigned *)&swar_x_mem[swar_widx]) = 0;
  swar_x_mem[swar_widx][PACKING-swar_pidx-1] = in_x;
  unsigned wi = swar_widx;
  __builtin_swarctrl(SWTP | SW_SGND | SW_RED | SW_OP_MUL);
  for (unsigned w=0;w<swar_ca_len;++w) {
//m_print("#"); i_print(w); m_print("(p:"); i_print(swar_pidx); m_print(",w:"); i_print(swar_widx); m_print(")");
//m_print(" => x:@"); i_print(wi); m_print("="); i_hexprint(swar_x_mem[w]);
//m_print(" x c:@"); i_print(swar_pidx); m_print(","); i_print(w); m_print("="); i_hexprint(swar_c_arr[swar_pidx][w]); NL;
    swpack ysw = __builtin_swar(swar_x_mem[wi],swar_c_arr[swar_pidx][w]); /* !!! */
    y += *(resultelm *)&ysw;
    if (++wi>=swar_ca_len) wi=0;
  }

  if (++swar_pidx>=PACKING) {
    swar_pidx=0;
    if (swar_widx==0) 
      swar_widx=swar_ca_len-1;
    else
      swar_widx--;
  }
//m_print(">>> y = "); i_print(y); NL;
  return y;
}

/* ---------------------------------- */
// 
unsigned swar_ca_len;
unsigned swar_pidx, swar_widx;
unsigned swar_xbufsize;
unsigned swar_xbufmask;

void fir_swar_online_align_rbuf_init(unsigned firlen, const dataelm *in_c)
{
  swar_ca_len = (firlen + PACKING-1 + PACKING-1) / PACKING; // minimal number of swar words
  unsigned mbspt = SWARBASESIZE - __builtin_clz(swar_ca_len);
  swar_xbufsize = 1<<mbspt;
  swar_xbufmask = swar_xbufsize-1;
//m_print("flen="); i_print(firlen); m_print("s_ca_l="); i_print(swar_ca_len); NL;
  swar_pidx = 0;
  swar_widx = 0;
  // prepare swar_c_arr
  // reset swar_x_mem
  for (unsigned w=0; w<swar_ca_len; ++w) {
    for (unsigned p; p<PACKING; ++p)
      *(resultelm *)&swar_c_arr[p][w] = 0;
  }
  for (unsigned w=0;w<swar_xbufsize;++w)
    *(resultelm *)&swar_x_mem[w] = 0;

  for (unsigned p=0; p<PACKING; ++p) {
    for (unsigned c=0; c<firlen; ++c) {
      unsigned w = (c+PACKING-p-1) / PACKING;
      unsigned e = (c+PACKING-p-1) % PACKING;
//m_print("cc p="); i_print(p); m_print(" w="); i_print(w); m_print(" e="); i_print(e); NL;
      swar_c_arr[p][w][e] = in_c[c];
    }
  }
//  print_dir_data("in-c", in_c, firlen);
//  for(unsigned p=0;p<PACKING;++p) {
//    m_print(" #"); i_print(p); m_print(": ");
//    print_swar_data("ac0", &swar_c_arr[p][0], swar_ca_len*PACKING);
//  }
//  print_swar_data("x0", &swar_x_mem[0], swar_ca_len*PACKING);
}


resultelm fir_swar_online_align_rbuf(dataelm in_x)
{
  resultelm y = 0;
 
//m_print("<<< x = "); i_print(in_x); NL;
  // update X rbuf with a new value
  if (swar_pidx==0) *((unsigned *)&swar_x_mem[swar_widx]) = 0;
  swar_x_mem[swar_widx][PACKING-swar_pidx-1] = in_x;
  unsigned wi = swar_widx;

  #pragma swar manual

  __builtin_swarctrl(SWTP | SW_SGND | SW_RED | SW_OP_MUL);
  for (unsigned w=0;w<swar_ca_len;++w) {
//m_print("#"); i_print(w); m_print("(p:"); i_print(swar_pidx); m_print(",w:"); i_print(swar_widx); m_print(")");
//m_print(" => x:@"); i_print(wi); m_print("(");i_hexprint(&swar_x_mem[wi]); m_print(") ="); i_hexprint(swar_x_mem[wi]);
//m_print(" x c:@"); i_print(swar_pidx); m_print(","); i_print(w); m_print("="); i_hexprint(swar_c_arr[swar_pidx][w]); NL;
#if 0
    volatile unsigned yc= __builtin_swar(swar_x_mem[wi],swar_c_arr[swar_pidx][w]);
    y+=yc;
//m_print("  >>> yc = "); i_print(yc); NL;
#else
    swpack ysw = __builtin_swar(swar_x_mem[wi],swar_c_arr[swar_pidx][w]);
    y += *(resultelm *)&ysw;
#endif
    wi = (wi + 1) & swar_xbufmask;
  }

  if (++swar_pidx>=PACKING) {
    swar_pidx=0;
    swar_widx = (swar_widx - 1) & swar_xbufmask;
  }
//m_print(">>> y = "); i_print(y); NL;
  return y;
}



/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* comparing */
int check_results(unsigned len, resultelm *in, resultelm *sw)
{
  int res = 0;
  printf("Check results: ");
  for (unsigned p=0;p<len;++p) {
    if (in[p]!=sw[p]) {
#if PRINT_FAILED
      printf("\n failed pack #%u (%lu @%p , %lu @%p)", p, in[p], &in[p], sw[p], &sw[p]);
#endif /* PRINT_FAILED */
      res++;
    }
  }
  if (!res) {
    printf("  OK\n\n");
  } else { 
    printf(" FAILED %u\n\n", res);
  }
  return res;
}


/* print data */
void print_dir_data(const char *prefix, dataelm *pd, int len)
{
  printf("%s data (%lu):\n", prefix, sizeof(dataelm));
  for(int i=0;i<len;++i) {
    printf("  %d %u(x%X)", *pd, *pd, *pd);
    pd++;
  }
  printf("\n\n");
}

void print_swar_data(const char *prefix, swpack *ps, int packlen)
{
  swpack *ptmp = ps;
  printf("%s swar data:\n", prefix);
  // print packed swar data
  printf("  PACK:");
  for(unsigned i=0;i<((packlen+PACKING-1)/PACKING);++i) {
    printf("  x" PRNFMTHEX, *((resultelm *)ps) );
    ps++;
  }
  printf("\n  ELM: ");
  // print swar element data
  ps = ptmp;
  for(unsigned i=0;i<packlen;++i) {
    multi_t v; v.s = *(ps+i/PACKING);
    int ii = i % PACKING;
//    dataelm e = ((dataelm *)&v)[ii]; /* !!! TODO: e=v[ii] */
    resultelm e = (v.u>>(BITSZ*ii))&((1<<BITSZ)-1);
    printf("  x" PRNFMTHEX, e);
  }
  printf("\n");
}

/* print results */
void print_results(const char *prefix, resultelm *pd, int len)
{
  printf("%s data @%p:\n", prefix, pd);
  for(int i=0;i<len;++i) {
    printf("  " PRNFMTDEC, *pd++);
  }
  printf("\n\n");
}

/* print counters */
void print_counters(const char *prefix, unsigned *tm, unsigned *ins)
{
  unsigned ticks_lo, ticks_hi, insns_lo, insns_hi;
  sysregs_stop();
  sysregs_read(&ticks_lo, &ticks_hi, &insns_lo, &insns_hi);

  // use relative cycles (between cycle_start and cycle_stop)
  unsigned long dcyc = cycle_stop-cycle_start;
  ticks_hi = (unsigned)(dcyc>>32);
  ticks_lo = (unsigned)(dcyc);

  printf("%s", prefix);
  if (ticks_hi) {
    printf(" EXECUTED IN (%u * 2^32 + %u) TICKS AND (%u * 2^32 + %u) INSTRUCTIONS\n\n", ticks_hi, ticks_lo, insns_hi, insns_lo);
  } else {
    printf(" EXECUTED IN %u TICKS AND %u INSTRUCTIONS\n", ticks_lo, insns_lo);
  }
  if (tm) *tm = ticks_lo;
  if (ins) *ins = insns_lo;
}


// main
int main(void)
{
  system_init();

  printf("TEST: SWARFIR\n");
#if 1
  printf(" - info:\n");
#if PRINT_TYPES
  printf("  - Types\n");
  printf("   - sizeof(unsigned)  : %lu B\n", sizeof(unsigned));
  printf("   - sizeof(uint32_t)  : %lu B\n", sizeof(uint32_t));
  printf("   - sizeof(uint64_t)  : %lu B\n", sizeof(uint64_t));
  printf("   - sizeof(swpack)    : %lu B\n", sizeof(swpack));
  printf("   - sizeof(swelm)     : %lu B\n", sizeof(swelm));
  printf("   - sizeof(dataelm)   : %lu B\n", sizeof(dataelm));
  printf("   - sizeof(resultelm) : %lu b\n", sizeof(resultelm));
  printf("   - sizeofswar(swpack): %lu b\n", sizeofswar(swpack));
  printf("   - sizeofswar(swelm) : %lu b\n", sizeofswar(swelm));
#endif /* PRINT_TYPES */
  printf("  - data length  : %u\n", DATA_LENGTH);
  printf("  - filter length: %u\n", FIR_LENGTH-1);
  printf("  - memory for direct FIR [Bytes]:\n");
  printf("   - input data  : %lu\n", sizeof(dir_in_x));
  printf("   - (max.number of input elements) : %lu)\n", TEST_INDATA_SIZE);
  printf("   - coefficients: %lu\n", sizeof(dir_in_c));
  printf("   - output data : %lu\n", sizeof(dir_out_y));
  printf("   - (max.number of output elements) : %lu)\n", TEST_OUTPUT_SIZE);
  printf("  - memory for SWAR FIR [Bytes]:\n");
  printf("   - input data  : %lu\n", sizeof(swar_in_x));
  printf("   - coefficients: %lu\n", sizeof(swar_in_c));
  printf("   - output data : %lu\n", sizeof(swar_out_y_add));
  printf("  - position in memory:\n");
  printf("   - dir_out_Y       : %p\n", &dir_out_y[0]);
  printf("   - swar_out_Y(add) : %p\n", &swar_out_y_add[0]);
#endif

  printf(" - preparing data\n");
  sysregs_init();
  prepare_data();
  print_counters("  ", NULL, NULL);

#if TEST_OFFLINE

#if 0
  print_dir_data("IN-X", dir_in_x, DATA_LENGTH);
  print_dir_data("IN-C", dir_in_c, FIR_LENGTH);
  print_swar_data("SWAR-IN-X", swar_in_x, DATA_LENGTH);
  print_swar_data("SWAR-IN-C", swar_in_c, FIR_LENGTH);
#endif

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

#if 1
  printf("  * FIR\n");
#if PRINT_RESULTS
  printf(" In-data:\n");
  for(unsigned u=0;u<DATA_LENGTH;++u)
    printf("  #%u : %u\n", u, dir_in_x[u]);
  printf("\n Coefs:\n");
  for(unsigned u=0;u<FIR_LENGTH;++u)
    printf("  #%u : %u\n", u, dir_in_c[u]);
  printf("\n");
#endif /* PRINT_RESULTS */
  sysregs_start();
  fir_offline(dir_out_y, dir_in_x, DATA_LENGTH, dir_in_c);
  print_counters("  ", NULL, NULL);
  #if PRINT_RESULTS
    print_results("OUT-Y", dir_out_y, DATA_LENGTH);
  #endif /* PRINT_RESULTS */
#endif

#if 1
  printf("  * SWAR FIR (mul,add)\n");
  sysregs_start();
  fir_swar_mul_add(swar_out_y_add, swar_in_x, DATA_LENGTH); //, swar_in_c);
  print_counters("  ", NULL, NULL);
  #if PRINT_RESULTS
    print_results("SWAR(add) Y", swar_out_y_add, DATA_LENGTH);
  #endif /* PRINT_RESULTS */

  check_results(DATA_LENGTH, dir_out_y, swar_out_y_add);
#endif

#if 1
  printf("  * SWAR FIR (reduction)\n");
  sysregs_start();
  fir_swar_mul_red(swar_out_y_red, swar_in_x, DATA_LENGTH); //, swar_in_c);
  print_counters("  ", NULL, NULL);
  #if PRINT_RESULTS
    print_results("SWAR(red) Y", swar_out_y_red, DATA_LENGTH);
  #endif /* PRINT_RESULTS */

  check_results(DATA_LENGTH, dir_out_y, swar_out_y_red);
#endif

#if 1
  printf("  * SWAR FIR (accumulators)\n");
  sysregs_start();
  fir_swar_mul_accum(swar_out_y_accum, swar_in_x, DATA_LENGTH); //, swar_in_c);
  print_counters("  ", NULL, NULL);
  #if PRINT_RESULTS
    print_results("SWAR(acc) Y", swar_out_y_accum, DATA_LENGTH);
  #endif /* PRINT_RESULTS */

  check_results(DATA_LENGTH, dir_out_y, swar_out_y_accum);
#endif

#endif /* TEST_OFFLINE */


/* ---------------------------------- */
/* ---------------------------------- */
/* ---------------------------------- */


#if TEST_ONLINE
  printf("\n - Compute online\n");

#if 1
  printf("  * FIR online\n");
  fir_online_init(FIR_LENGTH, dir_in_c);
  sysregs_start();
  for (unsigned i=0;i<DATA_LENGTH;++i) {
//printf("### %u : %u x%X", i, dir_in_x[i], dir_in_x[i]);
    dir_out_y[i] = fir_online(dir_in_x[i]);
//printf(" ->Y %u x%X\n", dir_out_y[i], dir_out_y[i]);
  }
  print_counters("  ", NULL, NULL);
  #if PRINT_RESULTS
    print_results("OUT-Y", dir_out_y, DATA_LENGTH);
  #endif /* PRINT_RESULTS */
#endif

#if 1
  printf("  * FIR swar online\n");
  fir_swar_online_init(FIR_LENGTH, dir_in_c);
  sysregs_start();
  for (unsigned i=0;i<DATA_LENGTH;++i) {
//printf("SO#%u : %u (x%X)\n", i, dir_in_x[i], dir_in_x[i]);
    swar_out_y_red[i] = fir_swar_online(dir_in_x[i]);
  }
  print_counters("  ", NULL, NULL);
  #if PRINT_RESULTS
    print_results("SWAR(online)", swar_out_y_red, DATA_LENGTH);
  #endif /* PRINT_RESULTS */

  check_results(DATA_LENGTH, dir_out_y, swar_out_y_red);
#endif


/* ---------------------------------- */
/* low pass FIR filter */
  printf("\nExample: on-line low pass FIR filtering\n");
  unsigned times[10], insts[10];
  unsigned nres = 0;

  unsigned id = 0;
  unsigned len = (TEST_INDATA_SIZE<TEST_OUTPUT_SIZE) ? TEST_INDATA_SIZE : TEST_OUTPUT_SIZE;
  if (ONLINE_DATALEN<len) len = ONLINE_DATALEN;

  printf("\n   - FIR length (# coeffs) = %lu\n", ONLINE_FIRLEN); /* sizeof(test_fircoef)/sizeof(dataelm) */
  printf("   - Data length (samples) = %u\n", len);

#if 1
  printf("  * FIR online\n");
  fir_online_init(ONLINE_FIRLEN, test_fircoef_extra); //test_fircoef);
//print_dir_data("-c-", test_fircoef_extra, ONLINE_FIRLEN);
  id = 0;
  sysregs_start();
  for (unsigned i=0;i<len;++i) {
    if (id>=len) id -= len;
//printf("#%u (id=%u: %d ", i, id, test_input[id]);
    test_dir_output[id] = fir_online(test_input[id]);
//printf("   -> %d\n", test_dir_output[id]);
    id++;
  }
  print_counters("  ", &times[0], &insts[0]);
  #if PRINT_RESULTS
    print_results("OUTPUT", test_dir_output, len);
  #endif /* PRINT_RESULTS */
//  check_results(len, test_dir_output, test_output);
#endif

#if 1
  printf("  * FIR swar online\n");
//print_dir_data("### -c-", test_fircoef_extra, ONLINE_FIRLEN);
  fir_swar_online_init(ONLINE_FIRLEN, test_fircoef_extra); //test_fircoef);
  id = 0;
  sysregs_start();
  for (unsigned i=0;i<len;++i) {
    if (id>=len) id -= len;
//printf("###%u: i= %d \n", i, test_input[id]);
    test_swar_output[id] = fir_swar_online(test_input[id]);
//printf("     -> o= %d (x%lx)\n", test_swar_output[id], test_swar_output[id]);
    id++;
  }
  print_counters("  ", &times[1], &insts[1]);
  #if PRINT_RESULTS
    print_results("OUTPUT", test_swar_output, len);
  #endif /* PRINT_RESULTS */
//  check_results(len, test_swar_output, test_output);
  check_results(len, test_swar_output, test_dir_output);
#endif

/* ---------------------------------- */
/* round buffer */

#if 1
  printf("  * FIR online rbuf\n");
  fir_online_rbuf_init(ONLINE_FIRLEN, test_fircoef_extra); //test_fircoef);
//print_dir_data("-c-", test_fircoef_extra, ONLINE_FIRLEN);
  id = 0;
  sysregs_start();
  for (unsigned i=0;i<len;++i) {
    if (id>=len) id -= len;
    //test_dir_output[id] 
//  m_print("#"); i_print(i); m_print(":"); i_print(test_input[id]);
    test_swar_output[id] = fir_online_rbuf(test_input[id]);
//  m_print(" -> "); i_print(test_swar_output[id]); NL;
    id++;
  }
  print_counters("  ", &times[2], &insts[2]);
  #if PRINT_RESULTS
    print_results("OUTPUT", test_swar_output, len);
  #endif /* PRINT_RESULTS */
//  check_results(len, test_dir_output, test_output);
  check_results(len, test_dir_output, test_swar_output);
#endif

#if 1
  printf("  * FIR swar online rbuf\n");
  fir_swar_online_rbuf_init(ONLINE_FIRLEN, test_fircoef_extra); // test_fircoef);
//print_dir_data("-c-", test_fircoef_extra, ONLINE_FIRLEN);
  id = 0;
  sysregs_start();
  for (unsigned i=0;i<len;++i) {
    if (id>=len) id -= len;
//  m_print("#"); i_print(i); m_print(":"); i_print(test_input[id]);
    test_swar_output[id] = fir_swar_online_rbuf(test_input[id]);
//  m_print(" -> "); i_print(test_swar_output[id]); NL;
    id++;
  }
  print_counters("  ", &times[3], &insts[3]);
  #if PRINT_RESULTS
    print_results("OUTPUT", test_swar_output, len);
  #endif /* PRINT_RESULTS */
//  check_results(len, test_dir_output, test_output);
  check_results(len, test_dir_output, test_swar_output);
#endif

/* ---------------------------------- */
/* aligned round buffer */

#if 1
  printf("  * FIR online aligned rbuf\n");
  fir_online_align_rbuf_init(ONLINE_FIRLEN, test_fircoef_extra); //test_fircoef);
//print_dir_data("-c-", test_fircoef_extra, ONLINE_FIRLEN);
  id = 0;
  sysregs_start();
  for (unsigned i=0;i<len;++i) {
    if (id>=len) id -= len;
    //test_dir_output[id] 
//printf("#%u (id=%u): %d x%lX\n", i, id, test_input[id], test_input[id]);
    test_swar_output[id] = fir_online_align_rbuf(test_input[id]);
//printf(" #-> %d x%lX\n", test_swar_output[id], test_swar_output[id]);
    id++;
  }
  print_counters("  ", &times[4], &insts[4]);
  #if PRINT_RESULTS
    print_results("OUTPUT", test_swar_output, len);
  #endif /* PRINT_RESULTS */
//  check_results(len, test_dir_output, test_output);
  check_results(len, test_dir_output, test_swar_output);
#endif

#if 1
  printf("  * FIR swar online aligned rbuf\n");
  fir_swar_online_align_rbuf_init(ONLINE_FIRLEN, test_fircoef_extra); // test_fircoef);
//print_dir_data("-c-", test_fircoef_extra, ONLINE_FIRLEN);
  id = 0;
  sysregs_start();
  for (unsigned i=0;i<len;++i) {
    if (id>=len) id -= len;
//  m_print("#"); i_print(i); m_print(":"); i_print(test_input[id]);
    test_swar_output[id] = fir_swar_online_align_rbuf(test_input[id]);
//  m_print(" -> "); i_print(test_swar_output[id]); NL;
    id++;
  }
  print_counters("  ", &times[5], &insts[5]);
  #if PRINT_RESULTS
    print_results("OUTPUT", test_swar_output, len);
  #endif /* PRINT_RESULTS */
//  check_results(len, test_dir_output, test_output);
  check_results(len, test_dir_output, test_swar_output);
#endif

/* ---------------------------------- */

#endif /* TEST_ONLINE */

  printf("\n*** END OF TEST ***\n");
  
  system_done();

  return 0;
}