Last-Modified: Mon, 11 May 2026 04:29:05 GMT Expires: Thu, 08 May 2036 04:29:05 GMT dircache.h\include\firmware - rockbox - My Rockbox tree
summaryrefslogtreecommitdiff
path: root/firmware/include/dircache.h
blob: b5b1b72c40e908dfcbddd17447f83b398a437aac (plain)
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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 by Miika Pekkarinen
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#ifndef _DIRCACHE_H
#define _DIRCACHE_H

#include "dir_uncached.h"

#ifdef HAVE_DIRCACHE

#define DIRCACHE_RESERVE  (1024*64)
#define DIRCACHE_LIMIT    (1024*1024*6)
#define DIRCACHE_FILE     ROCKBOX_DIR"/dircache.dat"

#define DIRCACHE_APPFLAG_TAGCACHE  0x0001

/* Internal structures. */
struct travel_data {
    struct dircache_entry *first;
    struct dircache_entry *ce;
    struct dircache_entry *down_entry;
#ifdef SIMULATOR
    DIR_UNCACHED *dir, *newdir;
    struct dirent_uncached *entry;
#else
    struct fat_dir *dir;
    struct fat_dir newdir;
    struct fat_direntry entry;
#endif
    int pathpos;
};

#define DIRCACHE_MAGIC  0x00d0c0a0
struct dircache_maindata {
    long magic;
    long size;
    long entry_count;
    long appflags;
    struct dircache_entry *root_entry;
};

#define MAX_PENDING_BINDINGS 2
struct fdbind_queue {
    char path[MAX_PATH];
    int fd;
};

/* Exported structures. */
struct dircache_entry {
    struct dircache_entry *next;
    struct dircache_entry *up;
    struct dircache_entry *down;
    int attribute;
    long size;
    long startcluster;
    unsigned short wrtdate;
    unsigned short wrttime;
    unsigned long name_len;
    char *d_name;
};

typedef struct {
    bool busy;
    struct dircache_entry *entry;
    struct dircache_entry *internal_entry;
    struct dircache_entry secondary_entry;
    DIR_UNCACHED *regulardir;
} DIR_CACHED;

void dircache_init(void);
int dircache_load(void);
int dircache_save(void);
int dircache_build(int last_size);
void* dircache_steal_buffer(long *size);
bool dircache_is_enabled(void);
bool dircache_is_initializing(void);
void dircache_set_appflag(long mask);
bool dircache_get_appflag(long mask);
int dircache_get_entry_count(void);
int dircache_get_cache_size(void);
int dircache_get_reserve_used(void);
int dircache_get_build_ticks(void);
void dircache_disable(void);
const struct dircache_entry *dircache_get_entry_ptr(const char *filename);
void dircache_copy_path(const struct dircache_entry *entry, char *buf, int size);

void dircache_bind(int fd, const char *path);
void dircache_update_filesize(int fd, long newsize, long startcluster);
void dircache_update_filetime(int fd);
void dircache_mkdir(const char *path);
void dircache_rmdir(const char *path);
void dircache_remove(const char *name);
void dircache_rename(const char *oldpath, const char *newpath);
void dircache_add_file(const char *path, long startcluster);

DIR_CACHED* opendir_cached(const char* name);
struct dircache_entry* readdir_cached(DIR_CACHED* dir);
int closedir_cached(DIR_CACHED *dir);
int mkdir_cached(const char *name);
int rmdir_cached(const char* name);
#endif /* !HAVE_DIRCACHE */

#endif
' href='#n492'>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 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
/*
 * libmad - MPEG audio decoder library
 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id$
 */

# ifdef HAVE_CONFIG_H
#  include "config.h"
# endif

# include "global.h"

# include "fixed.h"
# include "frame.h"
# include "synth.h"

/*
 * NAME:        synth->init()
 * DESCRIPTION: initialize synth struct
 */
void mad_synth_init(struct mad_synth *synth)
{
  mad_synth_mute(synth);

  synth->phase = 0;

  synth->pcm.samplerate = 0;
  synth->pcm.channels   = 0;
  synth->pcm.length     = 0;
  #if defined(CPU_COLDFIRE)
  /* init the emac unit here, since this function should always be called
     before using libmad */
  coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE | EMAC_ROUND);
  #endif
}

/*
 * NAME:        synth->mute()
 * DESCRIPTION: zero all polyphase filterbank values, resetting synthesis
 */
void mad_synth_mute(struct mad_synth *synth)
{
  memset(synth->filter, 0, sizeof(synth->filter));
}

#if 0 /* dct32 asm implementation is slower on current arm systems */
/* #ifdef FPM_ARM */

void dct32(mad_fixed_t const in[32], unsigned int slot,
           mad_fixed_t lo[16][8], mad_fixed_t hi[16][8]);

#else

/*
 * An optional optimization called here the Subband Synthesis Optimization
 * (SSO) improves the performance of subband synthesis at the expense of
 * accuracy.
 *
 * The idea is to simplify 32x32->64-bit multiplication to 32x32->32 such
 * that extra scaling and rounding are not necessary. This often allows the
 * compiler to use faster 32-bit multiply-accumulate instructions instead of
 * explicit 64-bit multiply, shift, and add instructions.
 *
 * SSO works like this: a full 32x32->64-bit multiply of two mad_fixed_t
 * values requires the result to be right-shifted 28 bits to be properly
 * scaled to the same fixed-point format. Right shifts can be applied at any
 * time to either operand or to the result, so the optimization involves
 * careful placement of these shifts to minimize the loss of accuracy.
 *
 * First, a 14-bit shift is applied with rounding at compile-time to the D[]
 * table of coefficients for the subband synthesis window. This only loses 2
 * bits of accuracy because the lower 12 bits are always zero. A second
 * 12-bit shift occurs after the DCT calculation. This loses 12 bits of
 * accuracy. Finally, a third 2-bit shift occurs just before the sample is
 * saved in the PCM buffer. 14 + 12 + 2 == 28 bits.
 */

/* FPM_DEFAULT without OPT_SSO will actually not work. */
# if defined(FPM_DEFAULT) && !defined(OPT_SSO)
#  define OPT_SSO
# endif

/* second SSO shift, with rounding */
# if defined(OPT_SSO)
#  define SHIFT(x)  (((x) + (1L << 11)) >> 12)
# else
#  define SHIFT(x)  (x)
# endif

/* possible DCT speed optimization */
# if defined(FPM_COLDFIRE_EMAC)
/* This is a Coldfire version of the OPT_SPEED optimisation below, but in the
   case of Coldfire it does not loose additional precision. */
#  define MUL(x, y) \
    ({ \
       mad_fixed64hi_t hi; \
       asm volatile("mac.l %[a], %[b], %%acc0\n\t" \
                    "movclr.l %%acc0, %[hi]" \
                    : [hi] "=r" (hi) \
                    : [a] "r" ((x)), [b] "r" ((y))); \
       hi; \
     })
# elif defined(FPM_ARM)
/* This is an ARM version of the OPT_SPEED optimisation below. This
   implementation will loose 1 bit of accuracy. */
#  define MUL(x, y) \
    ({ \
       mad_fixed64lo_t lo; \
       mad_fixed64hi_t hi; \
       asm volatile ( \
          "smull %[lo], %[hi], %[a], %[b]\n\t" /* hi = result */ \
          "mov %[hi], %[hi], lsl #1"   /* hi <<= 1 */ \
          : [lo]"=&r"(lo), [hi]"=&r"(hi) \
          : [a]"r"(x), [b]"r"(y)); \
       hi; \
    })
# elif defined(OPT_SPEED) && defined(MAD_F_MLX)
#  define MUL(x, y)  \
    ({ mad_fixed64hi_t hi;  \
       mad_fixed64lo_t lo;  \
       MAD_F_MLX(hi, lo, (x), (y));  \
       hi << (32 - MAD_F_SCALEBITS - 3);  \
    })
# else
#  define MUL(x, y)  mad_f_mul((x), (y>>3))
# endif

/*
 * NAME:        dct32()
 * DESCRIPTION: perform fast in[32]->out[32] DCT
 */
static
void dct32(mad_fixed_t const in[32], unsigned int slot,
           mad_fixed_t lo[16][8], mad_fixed_t hi[16][8])
{
  mad_fixed_t t0,   t1,   t2,   t3,   t4,   t5,   t6,   t7;
  mad_fixed_t t8,   t9,   t10,  t11,  t12,  t13,  t14,  t15;
  mad_fixed_t t16,  t17,  t18,  t19,  t20,  t21,  t22,  t23;
  mad_fixed_t t24,  t25,  t26,  t27,  t28,  t29,  t30,  t31;
  mad_fixed_t t32,  t33,  t34,  t35,  t36,  t37,  t38,  t39;
  mad_fixed_t t40,  t41,  t42,  t43,  t44,  t45,  t46,  t47;
  mad_fixed_t t48,  t49,  t50,  t51,  t52,  t53,  t54,  t55;
  mad_fixed_t t56,  t57,  t58,  t59,  t60,  t61,  t62,  t63;
  mad_fixed_t t64,  t65,  t66,  t67,  t68,  t69,  t70,  t71;
  mad_fixed_t t72,  t73,  t74,  t75,  t76,  t77,  t78,  t79;
  mad_fixed_t t80,  t81,  t82,  t83,  t84,  t85,  t86,  t87;
  mad_fixed_t t88,  t89,  t90,  t91,  t92,  t93,  t94,  t95;
  mad_fixed_t t96,  t97,  t98,  t99,  t100, t101, t102, t103;
  mad_fixed_t t104, t105, t106, t107, t108, t109, t110, t111;
  mad_fixed_t t112, t113, t114, t115, t116, t117, t118, t119;
  mad_fixed_t t120, t121, t122, t123, t124, t125, t126, t127;
  mad_fixed_t t128, t129, t130, t131, t132, t133, t134, t135;
  mad_fixed_t t136, t137, t138, t139, t140, t141, t142, t143;
  mad_fixed_t t144, t145, t146, t147, t148, t149, t150, t151;
  mad_fixed_t t152, t153, t154, t155, t156, t157, t158, t159;
  mad_fixed_t t160, t161, t162, t163, t164, t165, t166, t167;
  mad_fixed_t t168, t169, t170, t171, t172, t173, t174, t175;
  mad_fixed_t t176;

  /* costab[i] = cos(PI / (2 * 32) * i) */
#define costab1   MAD_F(0x7fd8878e) /* 0.998795456 */
#define costab2   MAD_F(0x7f62368f) /* 0.995184727 */
#define costab3   MAD_F(0x7e9d55fc) /* 0.989176510 */
#define costab4   MAD_F(0x7d8a5f40) /* 0.980785280 */
#define costab5   MAD_F(0x7c29fbee) /* 0.970031253 */
#define costab6   MAD_F(0x7a7d055b) /* 0.956940336 */
#define costab7   MAD_F(0x78848414) /* 0.941544065 */
#define costab8   MAD_F(0x7641af3d) /* 0.923879533 */
#define costab9   MAD_F(0x73b5ebd1) /* 0.903989293 */
#define costab10  MAD_F(0x70e2cbc6) /* 0.881921264 */
#define costab11  MAD_F(0x6dca0d14) /* 0.857728610 */
#define costab12  MAD_F(0x6a5d98a4) /* 0.831469612 */
#define costab13  MAD_F(0x66cf8120) /* 0.803207531 */
#define costab14  MAD_F(0x62f201ac) /* 0.773010453 */
#define costab15  MAD_F(0x5ed77c8a) /* 0.740951125 */
#define costab16  MAD_F(0x5a82799a) /* 0.707106781 */
#define costab17  MAD_F(0x55f5a4d2) /* 0.671558955 */
#define costab18  MAD_F(0x5133cc94) /* 0.634393284 */
#define costab19  MAD_F(0x4c3fdff4) /* 0.595699304 */
#define costab20  MAD_F(0x471cece7) /* 0.555570233 */
#define costab21  MAD_F(0x41ce1e65) /* 0.514102744 */
#define costab22  MAD_F(0x3c56ba70) /* 0.471396737 */
#define costab23  MAD_F(0x36ba2014) /* 0.427555093 */
#define costab24  MAD_F(0x30fbc54d) /* 0.382683432 */
#define costab25  MAD_F(0x2b1f34eb) /* 0.336889853 */
#define costab26  MAD_F(0x25280c5e) /* 0.290284677 */
#define costab27  MAD_F(0x1f19f97b) /* 0.242980180 */
#define costab28  MAD_F(0x18f8b83c) /* 0.195090322 */
#define costab29  MAD_F(0x12c8106f) /* 0.146730474 */
#define costab30  MAD_F(0x0c8bd35e) /* 0.098017140 */
#define costab31  MAD_F(0x0647d97c) /* 0.049067674 */

  t0   = in[0]  + in[31];  t16  = MUL(in[0]  - in[31], costab1);
  t1   = in[15] + in[16];  t17  = MUL(in[15] - in[16], costab31);

  t41  = t16 + t17;
  t59  = MUL(t16 - t17, costab2);
  t33  = t0  + t1;
  t50  = MUL(t0  - t1,  costab2);

  t2   = in[7]  + in[24];  t18  = MUL(in[7]  - in[24], costab15);
  t3   = in[8]  + in[23];  t19  = MUL(in[8]  - in[23], costab17);

  t42  = t18 + t19;
  t60  = MUL(t18 - t19, costab30);
  t34  = t2  + t3;
  t51  = MUL(t2  - t3,  costab30);

  t4   = in[3]  + in[28];  t20  = MUL(in[3]  - in[28], costab7);
  t5   = in[12] + in[19];  t21  = MUL(in[12] - in[19], costab25);

  t43  = t20 + t21;
  t61  = MUL(t20 - t21, costab14);
  t35  = t4  + t5;
  t52  = MUL(t4  - t5,  costab14);

  t6   = in[4]  + in[27];  t22  = MUL(in[4]  - in[27], costab9);
  t7   = in[11] + in[20];  t23  = MUL(in[11] - in[20], costab23);

  t44  = t22 + t23;
  t62  = MUL(t22 - t23, costab18);
  t36  = t6  + t7;
  t53  = MUL(t6  - t7,  costab18);

  t8   = in[1]  + in[30];  t24  = MUL(in[1]  - in[30], costab3);
  t9   = in[14] + in[17];  t25  = MUL(in[14] - in[17], costab29);

  t45  = t24 + t25;
  t63  = MUL(t24 - t25, costab6);
  t37  = t8  + t9;
  t54  = MUL(t8  - t9,  costab6);

  t10  = in[6]  + in[25];  t26  = MUL(in[6]  - in[25], costab13);
  t11  = in[9]  + in[22];  t27  = MUL(in[9]  - in[22], costab19);

  t46  = t26 + t27;
  t64  = MUL(t26 - t27, costab26);
  t38  = t10 + t11;
  t55  = MUL(t10 - t11, costab26);

  t12  = in[2]  + in[29];  t28  = MUL(in[2]  - in[29], costab5);
  t13  = in[13] + in[18];  t29  = MUL(in[13] - in[18], costab27);

  t47  = t28 + t29;
  t65  = MUL(t28 - t29, costab10);
  t39  = t12 + t13;
  t56  = MUL(t12 - t13, costab10);

  t14  = in[5]  + in[26];  t30  = MUL(in[5]  - in[26], costab11);
  t15  = in[10] + in[21];  t31  = MUL(in[10] - in[21], costab21);

  t48  = t30 + t31;
  t66  = MUL(t30 - t31, costab22);
  t40  = t14 + t15;
  t57  = MUL(t14 - t15, costab22);

  t69  = t33 + t34;  t89  = MUL(t33 - t34, costab4);
  t70  = t35 + t36;  t90  = MUL(t35 - t36, costab28);
  t71  = t37 + t38;  t91  = MUL(t37 - t38, costab12);
  t72  = t39 + t40;  t92  = MUL(t39 - t40, costab20);
  t73  = t41 + t42;  t94  = MUL(t41 - t42, costab4);
  t74  = t43 + t44;  t95  = MUL(t43 - t44, costab28);
  t75  = t45 + t46;  t96  = MUL(t45 - t46, costab12);
  t76  = t47 + t48;  t97  = MUL(t47 - t48, costab20);

  t78  = t50 + t51;  t100 = MUL(t50 - t51, costab4);
  t79  = t52 + t53;  t101 = MUL(t52 - t53, costab28);
  t80  = t54 + t55;  t102 = MUL(t54 - t55, costab12);
  t81  = t56 + t57;  t103 = MUL(t56 - t57, costab20);

  t83  = t59 + t60;  t106 = MUL(t59 - t60, costab4);
  t84  = t61 + t62;  t107 = MUL(t61 - t62, costab28);
  t85  = t63 + t64;  t108 = MUL(t63 - t64, costab12);
  t86  = t65 + t66;  t109 = MUL(t65 - t66, costab20);

  t113 = t69  + t70;
  t114 = t71  + t72;

  /*  0 */ hi[15][slot] = SHIFT(t113 + t114);
  /* 16 */ lo[ 0][slot] = SHIFT(MUL(t113 - t114, costab16));

  t115 = t73  + t74;
  t116 = t75  + t76;

  t32  = t115 + t116;

  /*  1 */ hi[14][slot] = SHIFT(t32);

  t118 = t78  + t79;
  t119 = t80  + t81;

  t58  = t118 + t119;

  /*  2 */ hi[13][slot] = SHIFT(t58);

  t121 = t83  + t84;
  t122 = t85  + t86;

  t67  = t121 + t122;

  t49  = (t67 * 2) - t32;

  /*  3 */ hi[12][slot] = SHIFT(t49);

  t125 = t89  + t90;
  t126 = t91  + t92;

  t93  = t125 + t126;

  /*  4 */ hi[11][slot] = SHIFT(t93);

  t128 = t94  + t95;
  t129 = t96  + t97;

  t98  = t128 + t129;

  t68  = (t98 * 2) - t49;

  /*  5 */ hi[10][slot] = SHIFT(t68);

  t132 = t100 + t101;
  t133 = t102 + t103;

  t104 = t132 + t133;

  t82  = (t104 * 2) - t58;

  /*  6 */ hi[ 9][slot] = SHIFT(t82);

  t136 = t106 + t107;
  t137 = t108 + t109;

  t110 = t136 + t137;

  t87  = (t110 * 2) - t67;

  t77  = (t87 * 2) - t68;

  /*  7 */ hi[ 8][slot] = SHIFT(t77);

  t141 = MUL(t69 - t70, costab8);
  t142 = MUL(t71 - t72, costab24);
  t143 = t141 + t142;

  /*  8 */ hi[ 7][slot] = SHIFT(t143);
  /* 24 */ lo[ 8][slot] =
             SHIFT((MUL(t141 - t142, costab16) * 2) - t143);

  t144 = MUL(t73 - t74, costab8);
  t145 = MUL(t75 - t76, costab24);
  t146 = t144 + t145;

  t88  = (t146 * 2) - t77;

  /*  9 */ hi[ 6][slot] = SHIFT(t88);

  t148 = MUL(t78 - t79, costab8);
  t149 = MUL(t80 - t81, costab24);
  t150 = t148 + t149;

  t105 = (t150 * 2) - t82;

  /* 10 */ hi[ 5][slot] = SHIFT(t105);

  t152 = MUL(t83 - t84, costab8);
  t153 = MUL(t85 - t86, costab24);
  t154 = t152 + t153;

  t111 = (t154 * 2) - t87;

  t99  = (t111 * 2) - t88;

  /* 11 */ hi[ 4][slot] = SHIFT(t99);

  t157 = MUL(t89 - t90, costab8);
  t158 = MUL(t91 - t92, costab24);
  t159 = t157 + t158;

  t127 = (t159 * 2) - t93;

  /* 12 */ hi[ 3][slot] = SHIFT(t127);

  t160 = (MUL(t125 - t126, costab16) * 2) - t127;

  /* 20 */ lo[ 4][slot] = SHIFT(t160);
  /* 28 */ lo[12][slot] =
             SHIFT((((MUL(t157 - t158, costab16) * 2) - t159) * 2) - t160);

  t161 = MUL(t94 - t95, costab8);
  t162 = MUL(t96 - t97, costab24);
  t163 = t161 + t162;

  t130 = (t163 * 2) - t98;

  t112 = (t130 * 2) - t99;

  /* 13 */ hi[ 2][slot] = SHIFT(t112);

  t164 = (MUL(t128 - t129, costab16) * 2) - t130;

  t166 = MUL(t100 - t101, costab8);
  t167 = MUL(t102 - t103, costab24);
  t168 = t166 + t167;

  t134 = (t168 * 2) - t104;

  t120 = (t134 * 2) - t105;

  /* 14 */ hi[ 1][slot] = SHIFT(t120);

  t135 = (MUL(t118 - t119, costab16) * 2) - t120;

  /* 18 */ lo[ 2][slot] = SHIFT(t135);

  t169 = (MUL(t132 - t133, costab16) * 2) - t134;

  t151 = (t169 * 2) - t135;

  /* 22 */ lo[ 6][slot] = SHIFT(t151);

  t170 = (((MUL(t148 - t149, costab16) * 2) - t150) * 2) - t151;

  /* 26 */ lo[10][slot] = SHIFT(t170);
  /* 30 */ lo[14][slot] =
             SHIFT((((((MUL(t166 - t167, costab16) * 2) -
                       t168) * 2) - t169) * 2) - t170);

  t171 = MUL(t106 - t107, costab8);
  t172 = MUL(t108 - t109, costab24);
  t173 = t171 + t172;

  t138 = (t173 * 2) - t110;

  t123 = (t138 * 2) - t111;

  t139 = (MUL(t121 - t122, costab16) * 2) - t123;

  t117 = (t123 * 2) - t112;

  /* 15 */ hi[ 0][slot] = SHIFT(t117);

  t124 = (MUL(t115 - t116, costab16) * 2) - t117;

  /* 17 */ lo[ 1][slot] = SHIFT(t124);

  t131 = (t139 * 2) - t124;

  /* 19 */ lo[ 3][slot] = SHIFT(t131);

  t140 = (t164 * 2) - t131;

  /* 21 */ lo[ 5][slot] = SHIFT(t140);

  t174 = (MUL(t136 - t137, costab16) * 2) - t138;

  t155 = (t174 * 2) - t139;

  t147 = (t155 * 2) - t140;

  /* 23 */ lo[ 7][slot] = SHIFT(t147);

  t156 = (((MUL(t144 - t145, costab16) * 2) - t146) * 2) - t147;

  /* 25 */ lo[ 9][slot] = SHIFT(t156);

  t175 = (((MUL(t152 - t153, costab16) * 2) - t154) * 2) - t155;

  t165 = (t175 * 2) - t156;

  /* 27 */ lo[11][slot] = SHIFT(t165);

  t176 = (((((MUL(t161 - t162, costab16) * 2) -
             t163) * 2) - t164) * 2) - t165;

  /* 29 */ lo[13][slot] = SHIFT(t176);
  /* 31 */ lo[15][slot] =
             SHIFT((((((((MUL(t171 - t172, costab16) * 2) -
                         t173) * 2) - t174) * 2) - t175) * 2) - t176);

  /*
   * Totals:
   *  80 multiplies
   *  80 additions
   * 119 subtractions
   *  49 shifts (not counting SSO)
   */
}

# undef MUL
# undef SHIFT

#endif

/* third SSO shift and/or D[] optimization preshift */

# if defined(OPT_SSO)
#  if MAD_F_FRACBITS != 28
#   error "MAD_F_FRACBITS must be 28 to use OPT_SSO"
#  endif
#  define ML0(hi, lo, x, y)     ((lo)  = (x) * (y))
#  define MLA(hi, lo, x, y)     ((lo) += (x) * (y))
#  define MLN(hi, lo)           ((lo)  = -(lo))
#  define MLZ(hi, lo)           ((void) (hi), (mad_fixed_t) (lo))
#  define SHIFT(x)              ((x) >> 2)
#  define PRESHIFT(x)           ((MAD_F(x) + (1L << 13)) >> 14)
# else
#  define ML0(hi, lo, x, y)     MAD_F_ML0((hi), (lo), (x), (y))
#  define MLA(hi, lo, x, y)     MAD_F_MLA((hi), (lo), (x), (y))
#  define MLN(hi, lo)           MAD_F_MLN((hi), (lo))
#  define MLZ(hi, lo)           MAD_F_MLZ((hi), (lo))
#  define SHIFT(x)              (x)
#  if defined(MAD_F_SCALEBITS)
#   undef  MAD_F_SCALEBITS
#   define MAD_F_SCALEBITS      (MAD_F_FRACBITS - 12)
#   define PRESHIFT(x)          (MAD_F(x) >> 12)
#  else
#   define PRESHIFT(x)          MAD_F(x)
#  endif
# endif

static
mad_fixed_t const D[17][32] ICONST_ATTR MEM_ALIGN_ATTR = {
# include "D.dat"
};

/*
 * NAME:        synth->full()
 * DESCRIPTION: perform full frequency PCM synthesis
 */
/* optimised version of synth_full */
# ifdef FPM_COLDFIRE_EMAC

#define SYNTH_EMAC1(res, f1, pD) \
    asm volatile( \
    "movem.l (%0), %%d0-%%d7               \n\t" \
    "move.l (%1), %%a5                     \n\t" \
    "mac.l %%d0, %%a5, 56(%1), %%a5, %%acc0\n\t" \
    "mac.l %%d1, %%a5, 48(%1), %%a5, %%acc0\n\t" \
    "mac.l %%d2, %%a5, 40(%1), %%a5, %%acc0\n\t" \
    "mac.l %%d3, %%a5, 32(%1), %%a5, %%acc0\n\t" \
    "mac.l %%d4, %%a5, 24(%1), %%a5, %%acc0\n\t" \
    "mac.l %%d5, %%a5, 16(%1), %%a5, %%acc0\n\t" \
    "mac.l %%d6, %%a5,  8(%1), %%a5, %%acc0\n\t" \
    "mac.l %%d7, %%a5,               %%acc0\n\t" \
    : \
    : "a" (*f1), "a" (*pD) \
    : "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "a5"); \
    asm volatile ( \
    "movclr.l %%acc0, %0                   \n\t" \
    : "=d" (res));
    
#define SYNTH_EMAC2(res, f1, f2, pD) \
    asm volatile( \
    "movem.l (%0), %%d0-%%d7                \n\t" \
    "move.l 4(%1), %%a5                     \n\t" \
    "msac.l %%d0, %%a5, 60(%1), %%a5, %%acc0\n\t" \
    "msac.l %%d1, %%a5, 52(%1), %%a5, %%acc0\n\t" \
    "msac.l %%d2, %%a5, 44(%1), %%a5, %%acc0\n\t" \
    "msac.l %%d3, %%a5, 36(%1), %%a5, %%acc0\n\t" \
    "msac.l %%d4, %%a5, 28(%1), %%a5, %%acc0\n\t" \
    "msac.l %%d5, %%a5, 20(%1), %%a5, %%acc0\n\t" \
    "msac.l %%d6, %%a5, 12(%1), %%a5, %%acc0\n\t" \
    "msac.l %%d7, %%a5,   (%1), %%a5, %%acc0\n\t" \
    "movem.l (%2), %%d0-%%d7                \n\t" \
    "mac.l  %%d0, %%a5, 56(%1), %%a5, %%acc0\n\t" \
    "mac.l  %%d1, %%a5, 48(%1), %%a5, %%acc0\n\t" \
    "mac.l  %%d2, %%a5, 40(%1), %%a5, %%acc0\n\t" \
    "mac.l  %%d3, %%a5, 32(%1), %%a5, %%acc0\n\t" \
    "mac.l  %%d4, %%a5, 24(%1), %%a5, %%acc0\n\t" \
    "mac.l  %%d5, %%a5, 16(%1), %%a5, %%acc0\n\t" \
    "mac.l  %%d6, %%a5,  8(%1), %%a5, %%acc0\n\t" \
    "mac.l  %%d7, %%a5,               %%acc0\n\t" \
    : \
    : "a" (*f1), "a" (*pD), "a" (*f2) \
    : "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "a5", "memory"); \
    asm volatile ( \
    "movclr.l %%acc0, %0                    \n\t" \
    : "=d" (res));
    
#define SYNTH_EMAC_ODD_SBSAMPLE(f1, f2, pD1, pD2, res1, res2) \
    asm volatile ( \
    "movem.l (%0), %%d0-%%d7                 \n\t" \
    "move.l 4(%2), %%a5                      \n\t" \
    "msac.l %%d0, %%a5,  60(%2), %%a5, %%acc0\n\t" \
    "msac.l %%d1, %%a5,  52(%2), %%a5, %%acc0\n\t" \
    "msac.l %%d2, %%a5,  44(%2), %%a5, %%acc0\n\t" \
    "msac.l %%d3, %%a5,  36(%2), %%a5, %%acc0\n\t" \
    "msac.l %%d4, %%a5,  28(%2), %%a5, %%acc0\n\t" \
    "msac.l %%d5, %%a5,  20(%2), %%a5, %%acc0\n\t" \
    "msac.l %%d6, %%a5,  12(%2), %%a5, %%acc0\n\t" \
    "msac.l %%d7, %%a5, 112(%3), %%a5, %%acc0\n\t" \
    "mac.l  %%d7, %%a5, 104(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d6, %%a5,  96(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d5, %%a5,  88(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d4, %%a5,  80(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d3, %%a5,  72(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d2, %%a5,  64(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d1, %%a5, 120(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d0, %%a5,   8(%2), %%a5, %%acc1\n\t" \
    "movem.l (%1), %%d0-%%d7                 \n\t" \
    "mac.l  %%d7, %%a5,  16(%2), %%a5, %%acc0\n\t" \
    "mac.l  %%d6, %%a5,  24(%2), %%a5, %%acc0\n\t" \
    "mac.l  %%d5, %%a5,  32(%2), %%a5, %%acc0\n\t" \
    "mac.l  %%d4, %%a5,  40(%2), %%a5, %%acc0\n\t" \
    "mac.l  %%d3, %%a5,  48(%2), %%a5, %%acc0\n\t" \
    "mac.l  %%d2, %%a5,  56(%2), %%a5, %%acc0\n\t" \
    "mac.l  %%d1, %%a5,    (%2), %%a5, %%acc0\n\t" \
    "mac.l  %%d0, %%a5,  60(%3), %%a5, %%acc0\n\t" \
    "mac.l  %%d0, %%a5,  68(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d1, %%a5,  76(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d2, %%a5,  84(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d3, %%a5,  92(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d4, %%a5, 100(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d5, %%a5, 108(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d6, %%a5, 116(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d7, %%a5,                %%acc1\n\t" \
    :                                              \
    : "a" (*f1), "a" (*f2), "a" (*pD1), "a" (*pD2) \
    : "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "a5", "memory"); \
    asm volatile( \
    "movclr.l %%acc0, %0\n\t" \
    "movclr.l %%acc1, %1\n\t" \
    : "=d" (res1), "=d" (res2) );
    
#define SYNTH_EMAC_EVEN_SBSAMPLE(f1, f2, pD1, pD2, res1, res2) \
    asm volatile ( \
    "movem.l (%0), %%d0-%%d7                 \n\t" \
    "move.l (%2), %%a5                       \n\t" \
    "msac.l %%d0, %%a5,  56(%2), %%a5, %%acc0\n\t" \
    "msac.l %%d1, %%a5,  48(%2), %%a5, %%acc0\n\t" \
    "msac.l %%d2, %%a5,  40(%2), %%a5, %%acc0\n\t" \
    "msac.l %%d3, %%a5,  32(%2), %%a5, %%acc0\n\t" \
    "msac.l %%d4, %%a5,  24(%2), %%a5, %%acc0\n\t" \
    "msac.l %%d5, %%a5,  16(%2), %%a5, %%acc0\n\t" \
    "msac.l %%d6, %%a5,   8(%2), %%a5, %%acc0\n\t" \
    "msac.l %%d7, %%a5, 116(%3), %%a5, %%acc0\n\t" \
    "mac.l  %%d7, %%a5, 108(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d6, %%a5, 100(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d5, %%a5,  92(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d4, %%a5,  84(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d3, %%a5,  76(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d2, %%a5,  68(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d1, %%a5,  60(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d0, %%a5,  12(%2), %%a5, %%acc1\n\t" \
    "movem.l (%1), %%d0-%%d7                 \n\t" \
    "mac.l  %%d7, %%a5,  20(%2), %%a5, %%acc0\n\t" \
    "mac.l  %%d6, %%a5,  28(%2), %%a5, %%acc0\n\t" \
    "mac.l  %%d5, %%a5,  36(%2), %%a5, %%acc0\n\t" \
    "mac.l  %%d4, %%a5,  44(%2), %%a5, %%acc0\n\t" \
    "mac.l  %%d3, %%a5,  52(%2), %%a5, %%acc0\n\t" \
    "mac.l  %%d2, %%a5,  60(%2), %%a5, %%acc0\n\t" \
    "mac.l  %%d1, %%a5,   4(%2), %%a5, %%acc0\n\t" \
    "mac.l  %%d0, %%a5, 120(%3), %%a5, %%acc0\n\t" \
    "mac.l  %%d0, %%a5,  64(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d1, %%a5,  72(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d2, %%a5,  80(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d3, %%a5,  88(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d4, %%a5,  96(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d5, %%a5, 104(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d6, %%a5, 112(%3), %%a5, %%acc1\n\t" \
    "mac.l  %%d7, %%a5,                %%acc1\n\t" \
    :                                              \
    : "a" (*f1), "a" (*f2), "a" (*pD1), "a" (*pD2) \
    : "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "a5", "memory"); \
    asm volatile( \
    "movclr.l %%acc0, %0\n\t" \
    "movclr.l %%acc1, %1\n\t" \
    : "=d" (res1), "=d" (res2) );

static
void synth_full(struct mad_synth *synth, struct mad_frame const *frame,
                unsigned int nch, unsigned int ns)
{
  int sb;
  unsigned int phase, ch, s, p;
  mad_fixed_t *pcm, (*filter)[2][2][16][8];
  mad_fixed_t (*sbsample)[36][32];
  mad_fixed_t (*fe)[8], (*fx)[8], (*fo)[8];
  mad_fixed_t const (*D0ptr)[32];
  mad_fixed_t const (*D1ptr)[32];
  mad_fixed64hi_t hi0, hi1;

  for (ch = 0; ch < nch; ++ch) {
    sbsample = &(*frame->sbsample_prev)[ch];
    filter   = &synth->filter[ch];
    phase    = synth->phase;
    pcm      = synth->pcm.samples[ch];

    for (s = 0; s < ns; ++s) {
      dct32((*sbsample)[s], phase >> 1,
            (*filter)[0][phase & 1], (*filter)[1][phase & 1]);

      p = (phase - 1) & 0xf;

      /* calculate 32 samples */
      fe = &(*filter)[0][ phase & 1][0];
      fx = &(*filter)[0][~phase & 1][0];
      fo = &(*filter)[1][~phase & 1][0];

      D0ptr = (void*)&D[0][ p];
      D1ptr = (void*)&D[0][-p];

      if(s & 1)
      {
        SYNTH_EMAC2(hi0, fx, fe, D0ptr);
        pcm[0] = hi0 << 3; /* shift result to libmad's fixed point format */
        pcm   += 16;

        for (sb = 15; sb; sb--, fo++) {
          ++fe;
          ++D0ptr;
          ++D1ptr;

          /* D[32 - sb][i] == -D[sb][31 - i] */
          SYNTH_EMAC_ODD_SBSAMPLE(fo, fe, D0ptr, D1ptr, hi0, hi1);
          pcm[-sb] = hi0 << 3;
          pcm[ sb] = hi1 << 3;
        }

        ++D0ptr;
        SYNTH_EMAC1(hi0, fo, D0ptr+1);
        pcm[0] = -(hi0 << 3);
      }
      else
      {
        SYNTH_EMAC2(hi0, fe, fx, D0ptr);
        pcm[0] = -(hi0 << 3); /* shift result to libmad's fixed point format */
        pcm   += 16;

        for (sb = 15; sb; sb--, fo++) {
          ++fe;
          ++D0ptr;
          ++D1ptr;

          /* D[32 - sb][i] == -D[sb][31 - i] */
          SYNTH_EMAC_EVEN_SBSAMPLE(fo, fe, D0ptr, D1ptr, hi0, hi1);
          pcm[-sb] = hi0 << 3;
          pcm[ sb] = hi1 << 3;
        }

        ++D0ptr;
        SYNTH_EMAC1(hi0, fo, D0ptr);
        pcm[0] = -(hi0 << 3);
      }
      pcm  += 16;
      phase = (phase + 1) % 16;
    }
  }
}

#elif defined(FPM_ARM)

#define PROD_O(hi, lo, f, ptr) \
  ({                             \
    mad_fixed_t *__p = (f);        \
    asm("ldmia   %2!, {r0, r1, r2, r3}\n\t" \
        "ldr     r4, [%3,  #0]\n\t"   \
        "ldr     r12, [%3, #56]\n\t"   \
        "smull   %0, %1, r0, r4\n\t"  \
        "ldr     r4, [%3, #48]\n\t"   \
        "smlal   %0, %1, r1, r12\n\t"  \
        "ldr     r12, [%3, #40]\n\t"   \
        "smlal   %0, %1, r2, r4\n\t"  \
        "smlal   %0, %1, r3, r12\n\t"  \
        "ldmia   %2, {r0, r1, r2, r3}\n\t" \
        "ldr     r4, [%3, #32]\n\t"   \
        "ldr     r12, [%3, #24]\n\t"   \
        "smlal   %0, %1, r0, r4\n\t"  \
        "ldr     r4, [%3, #16]\n\t"   \
        "smlal   %0, %1, r1, r12\n\t"  \
        "ldr     r12, [%3, #8]\n\t"    \
        "smlal   %0, %1, r2, r4\n\t"  \
        "smlal   %0, %1, r3, r12\n\t"  \
        : "=&r" (lo), "=&r" (hi), "+r" (__p) \
        : "r" (ptr)     \
        : "r0", "r1", "r2", "r3", "r4", "r12"); \
  })

#define PROD_A(hi, lo, f, ptr) \
  ({                             \
    mad_fixed_t *__p = (f);        \
    asm("ldmia   %2!, {r0, r1, r2, r3}\n\t" \
        "ldr     r4, [%3,  #0]\n\t"   \
        "ldr     r12, [%3, #56]\n\t"   \
        "smlal   %0, %1, r0, r4\n\t"  \
        "ldr     r4, [%3, #48]\n\t"   \
        "smlal   %0, %1, r1, r12\n\t"  \
        "ldr     r12, [%3, #40]\n\t"   \
        "smlal   %0, %1, r2, r4\n\t"  \
        "smlal   %0, %1, r3, r12\n\t"  \
        "ldmia   %2, {r0, r1, r2, r3}\n\t" \
        "ldr     r4, [%3, #32]\n\t"   \
        "ldr     r12, [%3, #24]\n\t"   \
        "smlal   %0, %1, r0, r4\n\t"  \
        "ldr     r4, [%3, #16]\n\t"   \
        "smlal   %0, %1, r1, r12\n\t"  \
        "ldr     r12, [%3, #8]\n\t"    \
        "smlal   %0, %1, r2, r4\n\t"  \
        "smlal   %0, %1, r3, r12\n\t"  \
        : "+r" (lo), "+r" (hi), "+r" (__p) \
        : "r" (ptr)     \
        : "r0", "r1", "r2", "r3", "r4", "r12"); \
  })

void synth_full_odd_sbsample (mad_fixed_t *pcm,
                              mad_fixed_t (*fo)[8],
                              mad_fixed_t (*fe)[8],
                              mad_fixed_t const (*D0ptr)[32],
                              mad_fixed_t const (*D1ptr)[32]);
void synth_full_even_sbsample(mad_fixed_t *pcm,
                              mad_fixed_t (*fo)[8],
                              mad_fixed_t (*fe)[8],
                              mad_fixed_t const (*D0ptr)[32],
                              mad_fixed_t const (*D1ptr)[32]);

static
void synth_full(struct mad_synth *synth, struct mad_frame const *frame,
                unsigned int nch, unsigned int ns) ICODE_ATTR_MPA_SYNTH;
static
void synth_full(struct mad_synth *synth, struct mad_frame const *frame,
                unsigned int nch, unsigned int ns)
{
  int          p;
  unsigned int phase, ch, s;
  mad_fixed_t *pcm, (*filter)[2][2][16][8];
  mad_fixed_t  (*sbsample)[36][32];
  mad_fixed_t (*fe)[8], (*fx)[8], (*fo)[8];
  mad_fixed_t const (*D0ptr)[32], *ptr;
  mad_fixed_t const (*D1ptr)[32];
  mad_fixed64hi_t hi;
  mad_fixed64lo_t lo;

  for (ch = 0; ch < nch; ++ch) {
    sbsample = &(*frame->sbsample_prev)[ch];
    filter   = &synth->filter[ch];
    phase    = synth->phase;
    pcm      = synth->pcm.samples[ch];

    for (s = 0; s < ns; ++s) {
      dct32((*sbsample)[s], phase >> 1,
            (*filter)[0][phase & 1], (*filter)[1][phase & 1]);

      p = (phase - 1) & 0xf;

      /* calculate 32 samples */
      fe = &(*filter)[0][ phase & 1][0];
      fx = &(*filter)[0][~phase & 1][0];
      fo = &(*filter)[1][~phase & 1][0];

      D0ptr = (void*)&D[0][ p];
      D1ptr = (void*)&D[0][-p];

      if(s & 1)
      {
        ptr = *D0ptr;
        PROD_O(hi, lo, *fx, ptr+1);
        MLN(hi, lo);
        PROD_A(hi, lo, *fe, ptr);
        pcm[0] = SHIFT(MLZ(hi, lo));
        pcm   += 16;

        synth_full_odd_sbsample(pcm, fo, fe, D0ptr, D1ptr);
        D0ptr += 15;
        D1ptr += 15;
        fo += 15;
        fe += 15;

        ptr = *(D0ptr + 1);
        PROD_O(hi, lo, *fo, ptr+1);
        pcm[0] = SHIFT(-MLZ(hi, lo));
      }
      else
      {
        ptr = *D0ptr;
        PROD_O(hi, lo, *fx, ptr);
        MLN(hi, lo);
        PROD_A(hi, lo, *fe, ptr+1);
        pcm[0] = SHIFT(MLZ(hi, lo));
        pcm   += 16;

        synth_full_even_sbsample(pcm, fo, fe, D0ptr, D1ptr);
        D0ptr += 15;
        D1ptr += 15;
        fo += 15;
        fe += 15;

        ptr = *(D0ptr + 1);
        PROD_O(hi, lo, *fo, ptr);
        pcm[0] = SHIFT(-MLZ(hi, lo));
      }

      pcm  += 16;
      phase = (phase + 1) % 16;
    }
  }
}

# else /* not FPM_COLDFIRE_EMAC and not FPM_ARM */

#define PROD_O(hi, lo, f, ptr, offset) \
        ML0(hi, lo, (*f)[0], ptr[ 0+offset]); \
        MLA(hi, lo, (*f)[1], ptr[14+offset]); \
        MLA(hi, lo, (*f)[2], ptr[12+offset]); \
        MLA(hi, lo, (*f)[3], ptr[10+offset]); \
        MLA(hi, lo, (*f)[4], ptr[ 8+offset]); \
        MLA(hi, lo, (*f)[5], ptr[ 6+offset]); \
        MLA(hi, lo, (*f)[6], ptr[ 4+offset]); \
        MLA(hi, lo, (*f)[7], ptr[ 2+offset]);
        
#define PROD_A(hi, lo, f, ptr, offset) \
        MLA(hi, lo, (*f)[0], ptr[ 0+offset]); \
        MLA(hi, lo, (*f)[1], ptr[14+offset]); \
        MLA(hi, lo, (*f)[2], ptr[12+offset]); \
        MLA(hi, lo, (*f)[3], ptr[10+offset]); \
        MLA(hi, lo, (*f)[4], ptr[ 8+offset]); \
        MLA(hi, lo, (*f)[5], ptr[ 6+offset]); \
        MLA(hi, lo, (*f)[6], ptr[ 4+offset]); \
        MLA(hi, lo, (*f)[7], ptr[ 2+offset]);
        
#define PROD_SB(hi, lo, ptr, offset, first_idx, last_idx) \
        ML0(hi, lo, (*fe)[0], ptr[first_idx]); \
        MLA(hi, lo, (*fe)[1], ptr[16+offset]); \
        MLA(hi, lo, (*fe)[2], ptr[18+offset]); \
        MLA(hi, lo, (*fe)[3], ptr[20+offset]); \
        MLA(hi, lo, (*fe)[4], ptr[22+offset]); \
        MLA(hi, lo, (*fe)[5], ptr[24+offset]); \
        MLA(hi, lo, (*fe)[6], ptr[26+offset]); \
        MLA(hi, lo, (*fe)[7], ptr[28+offset]); \
        MLA(hi, lo, (*fo)[7], ptr[29-offset]); \
        MLA(hi, lo, (*fo)[6], ptr[27-offset]); \
        MLA(hi, lo, (*fo)[5], ptr[25-offset]); \
        MLA(hi, lo, (*fo)[4], ptr[23-offset]); \
        MLA(hi, lo, (*fo)[3], ptr[21-offset]); \
        MLA(hi, lo, (*fo)[2], ptr[19-offset]); \
        MLA(hi, lo, (*fo)[1], ptr[17-offset]); \
        MLA(hi, lo, (*fo)[0], ptr[last_idx ]);

static
void synth_full(struct mad_synth *synth, struct mad_frame const *frame,
                unsigned int nch, unsigned int ns)
{
  int          p, sb;
  unsigned int phase, ch, s;
  mad_fixed_t *pcm, (*filter)[2][2][16][8];
  mad_fixed_t (*sbsample)[36][32];
  mad_fixed_t (*fe)[8], (*fx)[8], (*fo)[8];
  mad_fixed_t const (*D0ptr)[32], *ptr;
  mad_fixed_t const (*D1ptr)[32];
  mad_fixed64hi_t hi;
  mad_fixed64lo_t lo;

  for (ch = 0; ch < nch; ++ch) {
    sbsample = &(*frame->sbsample_prev)[ch];
    filter   = &synth->filter[ch];
    phase    = synth->phase;
    pcm      = synth->pcm.samples[ch];

    for (s = 0; s < ns; ++s) {
      dct32((*sbsample)[s], phase >> 1,
            (*filter)[0][phase & 1], (*filter)[1][phase & 1]);

      p = (phase - 1) & 0xf;

      /* calculate 32 samples */
      fe = &(*filter)[0][ phase & 1][0];
      fx = &(*filter)[0][~phase & 1][0];
      fo = &(*filter)[1][~phase & 1][0];

      D0ptr = (void*)&D[0][ p];
      D1ptr = (void*)&D[0][-p];

      if(s & 1)
      {
        ptr = *D0ptr;
        PROD_O(hi, lo, fx, ptr, 1)
        MLN(hi, lo);
        PROD_A(hi, lo, fe, ptr, 0)
        pcm[0] = SHIFT(MLZ(hi, lo));
        pcm   += 16;

        for (sb = 15; sb; sb--, fo++)
        {
          ++fe;
          ++D0ptr;
          ++D1ptr;

          /* D[32 - sb][i] == -D[sb][31 - i] */
          ptr = *D0ptr;
          PROD_O(hi, lo, fo, ptr, 1)
          MLN(hi, lo);
          PROD_A(hi, lo, fe, ptr, 0)
          pcm[-sb] = SHIFT(MLZ(hi, lo));

          ptr = *D1ptr;
          PROD_SB(hi, lo, ptr, 1, 15, 30)
          pcm[sb] = SHIFT(MLZ(hi, lo));
        }

        ptr = *(D0ptr + 1);
        PROD_O(hi, lo, fo, ptr, 1)
        pcm[0] = SHIFT(-MLZ(hi, lo));
      }
      else
      {
        ptr = *D0ptr;
        PROD_O(hi, lo, fx, ptr, 0)
        MLN(hi, lo);
        PROD_A(hi, lo, fe, ptr, 1)
        pcm[0] = SHIFT(MLZ(hi, lo));
        pcm   += 16;

        for (sb = 15; sb; sb--, fo++)
        {
          ++fe;
          ++D0ptr;
          ++D1ptr;

          /* D[32 - sb][i] == -D[sb][31 - i] */
          ptr = *D0ptr;
          PROD_O(hi, lo, fo, ptr, 0)
          MLN(hi, lo);
          PROD_A(hi, lo, fe, ptr, 1)
          pcm[-sb] = SHIFT(MLZ(hi, lo));

          ptr = *D1ptr;
          PROD_SB(hi, lo, ptr, 0, 30, 15)
          pcm[sb] = SHIFT(MLZ(hi, lo));
        }

        ptr = *(D0ptr + 1);
        PROD_O(hi, lo, fo, ptr, 0)
        pcm[0] = SHIFT(-MLZ(hi, lo));
      }

      pcm  += 16;
      phase = (phase + 1) % 16;
    }
  }
}
# endif /* FPM_COLDFIRE_EMAC, FPM_ARM */

#if 0 /* rockbox: unused */
/*
 * NAME:        synth->half()
 * DESCRIPTION: perform half frequency PCM synthesis
 */
static
void synth_half(struct mad_synth *synth, struct mad_frame const *frame,
                unsigned int nch, unsigned int ns)
{
  unsigned int phase, ch, s, sb, pe, po;
  mad_fixed_t *pcm1, *pcm2, (*filter)[2][2][16][8];
  mad_fixed_t  (*sbsample)[36][32];
  register mad_fixed_t (*fe)[8], (*fx)[8], (*fo)[8];
  register mad_fixed_t const (*Dptr)[32], *ptr;
  register mad_fixed64hi_t hi;
  register mad_fixed64lo_t lo;

  for (ch = 0; ch < nch; ++ch) {
    sbsample = &(*frame->sbsample_prev)[ch];
    filter   = &synth->filter[ch];
    phase    = synth->phase;
    pcm1     = synth->pcm.samples[ch];

    for (s = 0; s < ns; ++s) {
      dct32((*sbsample)[s], phase >> 1,
            (*filter)[0][phase & 1], (*filter)[1][phase & 1]);

      pe = phase & ~1;
      po = ((phase - 1) & 0xf) | 1;

      /* calculate 16 samples */

      fe = &(*filter)[0][ phase & 1][0];
      fx = &(*filter)[0][~phase & 1][0];
      fo = &(*filter)[1][~phase & 1][0];

      Dptr = &D[0];

      ptr = *Dptr + po;
      ML0(hi, lo, (*fx)[0], ptr[ 0]);
      MLA(hi, lo, (*fx)[1], ptr[14]);
      MLA(hi, lo, (*fx)[2], ptr[12]);
      MLA(hi, lo, (*fx)[3], ptr[10]);
      MLA(hi, lo, (*fx)[4], ptr[ 8]);
      MLA(hi, lo, (*fx)[5], ptr[ 6]);
      MLA(hi, lo, (*fx)[6], ptr[ 4]);
      MLA(hi, lo, (*fx)[7], ptr[ 2]);
      MLN(hi, lo);

      ptr = *Dptr + pe;
      MLA(hi, lo, (*fe)[0], ptr[ 0]);
      MLA(hi, lo, (*fe)[1], ptr[14]);
      MLA(hi, lo, (*fe)[2], ptr[12]);
      MLA(hi, lo, (*fe)[3], ptr[10]);
      MLA(hi, lo, (*fe)[4], ptr[ 8]);
      MLA(hi, lo, (*fe)[5], ptr[ 6]);
      MLA(hi, lo, (*fe)[6], ptr[ 4]);
      MLA(hi, lo, (*fe)[7], ptr[ 2]);

      *pcm1++ = SHIFT(MLZ(hi, lo));

      pcm2 = pcm1 + 14;

      for (sb = 1; sb < 16; ++sb) {
        ++fe;
        ++Dptr;

        /* D[32 - sb][i] == -D[sb][31 - i] */

        if (!(sb & 1)) {
          ptr = *Dptr + po;
          ML0(hi, lo, (*fo)[0], ptr[ 0]);
          MLA(hi, lo, (*fo)[1], ptr[14]);
          MLA(hi, lo, (*fo)[2], ptr[12]);
          MLA(hi, lo, (*fo)[3], ptr[10]);
          MLA(hi, lo, (*fo)[4], ptr[ 8]);
          MLA(hi, lo, (*fo)[5], ptr[ 6]);
          MLA(hi, lo, (*fo)[6], ptr[ 4]);
          MLA(hi, lo, (*fo)[7], ptr[ 2]);
          MLN(hi, lo);

          ptr = *Dptr + pe;
          MLA(hi, lo, (*fe)[7], ptr[ 2]);
          MLA(hi, lo, (*fe)[6], ptr[ 4]);
          MLA(hi, lo, (*fe)[5], ptr[ 6]);
          MLA(hi, lo, (*fe)[4], ptr[ 8]);
          MLA(hi, lo, (*fe)[3], ptr[10]);
          MLA(hi, lo, (*fe)[2], ptr[12]);
          MLA(hi, lo, (*fe)[1], ptr[14]);
          MLA(hi, lo, (*fe)[0], ptr[ 0]);

          *pcm1++ = SHIFT(MLZ(hi, lo));

          ptr = *Dptr - po;
          ML0(hi, lo, (*fo)[7], ptr[31 -  2]);
          MLA(hi, lo, (*fo)[6], ptr[31 -  4]);
          MLA(hi, lo, (*fo)[5], ptr[31 -  6]);
          MLA(hi, lo, (*fo)[4], ptr[31 -  8]);
          MLA(hi, lo, (*fo)[3], ptr[31 - 10]);
          MLA(hi, lo, (*fo)[2], ptr[31 - 12]);
          MLA(hi, lo, (*fo)[1], ptr[31 - 14]);
          MLA(hi, lo, (*fo)[0], ptr[31 - 16]);

          ptr = *Dptr - pe;
          MLA(hi, lo, (*fe)[0], ptr[31 - 16]);
          MLA(hi, lo, (*fe)[1], ptr[31 - 14]);
          MLA(hi, lo, (*fe)[2], ptr[31 - 12]);
          MLA(hi, lo, (*fe)[3], ptr[31 - 10]);
          MLA(hi, lo, (*fe)[4], ptr[31 -  8]);
          MLA(hi, lo, (*fe)[5], ptr[31 -  6]);
          MLA(hi, lo, (*fe)[6], ptr[31 -  4]);
          MLA(hi, lo, (*fe)[7], ptr[31 -  2]);

          *pcm2-- = SHIFT(MLZ(hi, lo));
        }

        ++fo;
      }

      ++Dptr;

      ptr = *Dptr + po;
      ML0(hi, lo, (*fo)[0], ptr[ 0]);
      MLA(hi, lo, (*fo)[1], ptr[14]);
      MLA(hi, lo, (*fo)[2], ptr[12]);
      MLA(hi, lo, (*fo)[3], ptr[10]);
      MLA(hi, lo, (*fo)[4], ptr[ 8]);
      MLA(hi, lo, (*fo)[5], ptr[ 6]);
      MLA(hi, lo, (*fo)[6], ptr[ 4]);
      MLA(hi, lo, (*fo)[7], ptr[ 2]);

      *pcm1 = SHIFT(-MLZ(hi, lo));
      pcm1 += 8;

      phase = (phase + 1) % 16;
    }
  }
}
#endif /* unused */

/*
 * NAME:        synth->frame()
 * DESCRIPTION: perform PCM synthesis of frame subband samples
 */
void mad_synth_frame(struct mad_synth *synth, struct mad_frame const *frame)
{
  unsigned int nch, ns;
#if 0 /* rockbox: unused */
  void (*synth_frame)(struct mad_synth *, struct mad_frame const *,
                      unsigned int, unsigned int);
#endif

  nch = MAD_NCHANNELS(&frame->header);
  ns  = MAD_NSBSAMPLES(&frame->header);

  synth->pcm.samplerate = frame->header.samplerate;
  synth->pcm.channels   = nch;
  synth->pcm.length     = 32 * ns;

#if 0 /* rockbox: unused */
  synth_frame = synth_full;

  if (frame->options & MAD_OPTION_HALFSAMPLERATE) {
    synth->pcm.samplerate /= 2;
    synth->pcm.length     /= 2;

    synth_frame = synth_half;
  }
  
  synth_frame(synth, frame, nch, ns);
#else
  synth_full(synth, frame, nch, ns);
#endif

  synth->phase = (synth->phase + ns) % 16;
}