kiss_fft_tm.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /* Copyright (C) 2007 Hong Zhiqian */
  2. /**
  3. @file kiss_fft_tm.h
  4. @author Hong Zhiqian
  5. @brief Various compatibility routines for Speex (TriMedia version)
  6. */
  7. /*
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions
  10. are met:
  11. - Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. - Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. - Neither the name of the Xiph.org Foundation nor the names of its
  17. contributors may be used to endorse or promote products derived from
  18. this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  23. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "_kiss_fft_guts_tm.h"
  32. #ifdef TM_ASM
  33. #include "profile_tm.h"
  34. #ifdef FIXED_POINT
  35. #define OVERRIDE_KFBFLY2
  36. static void kf_bfly2(
  37. kiss_fft_cpx *Fout,
  38. const int fstride,
  39. const kiss_fft_cfg st,
  40. int m
  41. )
  42. {
  43. register int * restrict Fout2;
  44. register int * restrict tw1 = (int*)st->twiddles;
  45. register int i, j;
  46. register int _inv = !st->inverse;
  47. Fout2 = (int*)Fout + m;
  48. for ( i=0,j=0 ; i<m ; ++i,j+=4,tw1+=fstride )
  49. { register int tw_10, ff_10, f2_10;
  50. ff_10 = ld32x(Fout, i);
  51. f2_10 = ld32x(Fout2, i);
  52. tw_10 = ld32(tw1);
  53. if ( _inv )
  54. { TM_SHR(f2_10, f2_10, 1);
  55. TM_SHR(ff_10, ff_10, 1);
  56. }
  57. TM_MUL(tw_10, tw_10, f2_10);
  58. TM_SUB(f2_10, ff_10, tw_10);
  59. TM_ADD(ff_10, ff_10, tw_10);
  60. st32d(j, Fout2, f2_10);
  61. st32d(j, Fout, ff_10);
  62. }
  63. }
  64. #define OVERRIDE_KFBFLY4
  65. static void kf_bfly4(
  66. kiss_fft_cpx *Fout,
  67. const int fstride,
  68. const kiss_fft_cfg st,
  69. const int m
  70. )
  71. {
  72. register int * restrict tw1;
  73. register int * restrict tw2;
  74. register int * restrict tw3;
  75. register int * restrict Fout1;
  76. register int * restrict Fout2;
  77. register int * restrict Fout3;
  78. register int i, j;
  79. register int fstride2, fstride3;
  80. register int _inv = !st->inverse;
  81. tw3 = tw2 = tw1 = (int*)st->twiddles;
  82. fstride2 = fstride << 1;
  83. fstride3 = fstride * 3;
  84. Fout1 = (int*)Fout + m;
  85. Fout2 = (int*)Fout + (m << 1);
  86. Fout3 = (int*)Fout + (m * 3);
  87. for ( i=0,j=0 ; i<m ; ++i,j+=4,tw1+=fstride,tw2+=fstride2,tw3+=fstride3 )
  88. { register int sc0, sc1, sc2, sc3, sc4, sc5;
  89. register int ff0;
  90. sc0 = ld32x(Fout1,i);
  91. sc3 = ld32(tw1);
  92. sc1 = ld32x(Fout2, i);
  93. sc4 = ld32(tw2);
  94. sc2 = ld32x(Fout3, i);
  95. sc5 = ld32(tw3);
  96. ff0 = ld32x(Fout,i);
  97. if ( _inv )
  98. {
  99. TM_ADD(sc0, sc0, 0x00020002);
  100. TM_ADD(sc1, sc1, 0x00020002);
  101. TM_ADD(sc2, sc2, 0x00020002);
  102. TM_ADD(ff0, ff0, 0x00020002);
  103. TM_SHR(sc0, sc0, 2);
  104. TM_SHR(sc1, sc1, 2);
  105. TM_SHR(sc2, sc2, 2);
  106. TM_SHR(ff0, ff0, 2);
  107. }
  108. TM_MUL(sc0, sc0, sc3);
  109. TM_MUL(sc1, sc1, sc4);
  110. TM_MUL(sc2, sc2, sc5);
  111. TM_SUB(sc5, ff0, sc1);
  112. TM_ADD(ff0, ff0, sc1);
  113. TM_ADD(sc3, sc0, sc2);
  114. TM_SUB(sc4, sc0, sc2);
  115. TM_SUB(sc1, ff0, sc3);
  116. TM_ADD(ff0, ff0, sc3);
  117. st32d(j, Fout2, sc1);
  118. st32d(j, Fout, ff0);
  119. sc5 = funshift2(sc5, sc5);
  120. if ( _inv )
  121. { TM_ADD(ff0, sc5, sc4);
  122. TM_SUB(sc1, sc5, sc4);
  123. } else
  124. { TM_ADD(sc1, sc5, sc4);
  125. TM_SUB(ff0, sc5, sc4);
  126. }
  127. sc0 = funshift2(sc1, ff0);
  128. sc2 = funshift2(ff0, sc1);
  129. st32d(j, Fout1, sc0);
  130. st32d(j, Fout3, sc2);
  131. }
  132. }
  133. #define OVERRIDE_KFBFLY3
  134. static void kf_bfly3(
  135. kiss_fft_cpx *Fout,
  136. const int fstride,
  137. const kiss_fft_cfg st,
  138. int m
  139. )
  140. {
  141. register int * restrict tw1;
  142. register int * restrict tw2;
  143. register int * restrict Fout1;
  144. register int * restrict Fout2;
  145. register int epi;
  146. register int i, j;
  147. register int fstride2;
  148. register int _inv = !st->inverse;
  149. tw1 = tw2 = (int*)st->twiddles;
  150. Fout1 = (int*)Fout + m;
  151. Fout2 = (int*)Fout + (m << 1);
  152. epi = tw1[fstride*m];
  153. epi = pack16lsb(epi,epi);
  154. fstride2 = fstride << 1;
  155. for ( i=0,j=0 ; i<m ; ++i,j+=4,tw1+=fstride,tw2+=fstride2 )
  156. { register int sc0, sc1, sc2, sc3, sc4, sc5;
  157. register int ff0;
  158. sc1 = ld32x(Fout1,i);
  159. sc2 = ld32x(Fout2,i);
  160. sc3 = ld32(tw1);
  161. sc4 = ld32(tw2);
  162. ff0 = ld32x(Fout,i);
  163. if ( _inv )
  164. {
  165. TM_DIV(sc1, sc1, 3);
  166. TM_DIV(sc2, sc2, 3);
  167. TM_DIV(ff0, ff0, 3);
  168. }
  169. TM_MUL(sc1, sc1, sc3);
  170. TM_MUL(sc2, sc2, sc4);
  171. TM_ADD(sc3, sc1, sc2);
  172. TM_SUB(sc0, sc1, sc2);
  173. TM_SHR(sc4, sc3, 1);
  174. TM_SUB(sc1, ff0, sc4);
  175. sc0 = dspidualmul(sc0, epi);
  176. sc0 = funshift2(sc0, sc0);
  177. TM_ADD(ff0, ff0, sc3);
  178. TM_ADD(sc4, sc1, sc0);
  179. TM_SUB(sc5, sc1, sc0);
  180. sc1 = funshift2(sc4, sc5);
  181. sc2 = funshift2(sc5, sc4);
  182. sc2 = funshift2(sc2, sc2);
  183. st32d(j, Fout1, sc1);
  184. st32d(j, Fout, ff0);
  185. st32d(j, Fout2, sc2);
  186. }
  187. }
  188. #define OVERRIDE_KFBFLY5
  189. static void kf_bfly5(
  190. kiss_fft_cpx *Fout,
  191. const int fstride,
  192. const kiss_fft_cfg st,
  193. int m
  194. )
  195. {
  196. register int * restrict tw1;
  197. register int * restrict tw2;
  198. register int * restrict tw3;
  199. register int * restrict tw4;
  200. register int * restrict Fout1;
  201. register int * restrict Fout2;
  202. register int * restrict Fout3;
  203. register int * restrict Fout4;
  204. register int fstride2, fstride3, fstride4;
  205. register int i, j;
  206. register int yab_msb, yab_lsb, yba_msb, yba_lsb;
  207. register int _inv = !st->inverse;
  208. Fout1=(int*)Fout+m;
  209. Fout2=(int*)Fout+(m<<1);
  210. Fout3=(int*)Fout+(3 *m);
  211. Fout4=(int*)Fout+(m<<2);
  212. tw1 = tw2 = tw3 = tw4 = (int*)st->twiddles;
  213. i = tw1[fstride*m];
  214. yab_lsb = tw1[fstride*(m<<1)];
  215. yab_msb = pack16msb(i, yab_lsb);
  216. yab_lsb = pack16lsb(i, yab_lsb);
  217. yba_msb = funshift2(-sex16(yab_msb), yab_msb);
  218. yba_lsb = funshift2(yab_lsb, yab_lsb);
  219. fstride2 = fstride << 1;
  220. fstride3 = fstride * 3;
  221. fstride4 = fstride << 2;
  222. for ( i=0,j=0 ; i<m ; ++i,j+=4,tw1+=fstride,tw2+=fstride2,tw3+=fstride3,tw4+=fstride4 )
  223. { register int sc0, sc1, sc2, sc3, sc4, sc5, sc6;
  224. register int sc7, sc8, sc9, sc10, sc11, sc12;
  225. register int ff0, sc78_msb, sc78_lsb, sc90_msb, sc90_lsb;
  226. sc0 = ld32x(Fout,i);
  227. sc1 = ld32x(Fout1,i);
  228. sc2 = ld32x(Fout2,i);
  229. sc3 = ld32x(Fout3,i);
  230. sc4 = ld32x(Fout4,i);
  231. sc5 = ld32(tw1);
  232. sc6 = ld32(tw2);
  233. sc7 = ld32(tw3);
  234. sc8 = ld32(tw4);
  235. if ( _inv )
  236. {
  237. TM_DIV(sc0, sc0, 5);
  238. TM_DIV(sc1, sc1, 5);
  239. TM_DIV(sc2, sc2, 5);
  240. TM_DIV(sc3, sc3, 5);
  241. TM_DIV(sc4, sc4, 5);
  242. }
  243. ff0 = sc0;
  244. TM_MUL(sc1, sc1, sc5);
  245. TM_MUL(sc2, sc2, sc6);
  246. TM_MUL(sc3, sc3, sc7);
  247. TM_MUL(sc4, sc4, sc8);
  248. TM_ADD(sc7, sc1, sc4);
  249. TM_SUB(sc10,sc1, sc4);
  250. TM_ADD(sc8, sc2, sc3);
  251. TM_SUB(sc9, sc2, sc3);
  252. TM_ADD(ff0, ff0, sc7);
  253. TM_ADD(ff0, ff0, sc8);
  254. st32d(j, Fout, ff0);
  255. sc78_msb = pack16msb(sc7,sc8);
  256. sc78_lsb = pack16lsb(sc7,sc8);
  257. sc90_msb = pack16msb(sc10,sc9);
  258. sc90_lsb = pack16lsb(sc10,sc9);
  259. sc5 = pack16lsb( sround(ifir16(sc78_msb,yab_lsb)), sround(ifir16(sc78_lsb,yab_lsb)));
  260. sc6 = pack16lsb(-sround(ifir16(sc90_lsb,yab_msb)), sround(ifir16(sc90_msb,yab_msb)));
  261. TM_ADD(sc5, sc5, sc0);
  262. TM_SUB(sc1, sc5, sc6);
  263. TM_ADD(sc4, sc5, sc6);
  264. st32d(j, Fout1, sc1);
  265. st32d(j, Fout4, sc4);
  266. sc11 = pack16lsb( sround(ifir16(sc78_msb,yba_lsb)), sround(ifir16(sc78_lsb,yba_lsb)));
  267. sc12 = pack16lsb(-sround(ifir16(sc90_lsb,yba_msb)), sround(ifir16(sc90_msb,yba_msb)));
  268. TM_ADD(sc11, sc11, sc0);
  269. TM_ADD(sc2, sc11, sc12);
  270. TM_SUB(sc3, sc11, sc12);
  271. st32d(j, Fout2, sc2);
  272. st32d(j, Fout3, sc3);
  273. }
  274. }
  275. #define OVERRIDE_KF_BFLY_GENERIC
  276. static void kf_bfly_generic(
  277. kiss_fft_cpx * restrict Fout,
  278. const size_t fstride,
  279. const kiss_fft_cfg st,
  280. int m,
  281. int p
  282. )
  283. {
  284. register int _inv = !st->inverse;
  285. register int i, j, k, l;
  286. register int * restrict twiddles = (int*)st->twiddles;
  287. register int Norig = st->nfft;
  288. CHECKBUF(scratchbuf,nscratchbuf,p);
  289. for ( i=0; i<m; ++i )
  290. { register int sc10;
  291. for ( j=0,k=i ; j<p ; ++j,k+=m )
  292. { register int f10;
  293. f10 = ld32x(Fout,k);
  294. if ( _inv )
  295. { TM_DIV(f10, f10, p);
  296. }
  297. st32d(j<<2, scratchbuf, f10);
  298. }
  299. for ( j=0,k=i,sc10=ld32(scratchbuf) ; j<p ; ++j,k+=m )
  300. {
  301. register int twidx = 0;
  302. register int f10;
  303. for ( l=1,f10 = sc10 ; l<p ; ++l )
  304. { register int tw, sc;
  305. twidx += fstride * k;
  306. if ( twidx>=Norig )
  307. { twidx -= Norig;
  308. }
  309. sc = ld32x(scratchbuf,l);
  310. tw = ld32x(twiddles,twidx);
  311. TM_MUL(sc, sc, tw);
  312. TM_ADD(f10, f10, sc);
  313. }
  314. st32d(k<<2, Fout, f10);
  315. }
  316. }
  317. }
  318. #else
  319. #define OVERRIDE_KFBFLY2
  320. static void kf_bfly2(
  321. kiss_fft_cpx * Fout,
  322. const size_t fstride,
  323. const kiss_fft_cfg st,
  324. int m
  325. )
  326. {
  327. register kiss_fft_cpx * restrict Fout2;
  328. register kiss_fft_cpx * restrict tw1 = st->twiddles;
  329. Fout2 = Fout + m;
  330. do
  331. {
  332. register kiss_fft_cpx _fout2, _fout, t;
  333. _fout2 = *Fout2;
  334. _fout = *Fout;
  335. C_MUL ( t, _fout2, *tw1);
  336. C_SUB (_fout2, _fout, t);
  337. C_ADD (_fout, _fout, t);
  338. *Fout2 = _fout2;
  339. *Fout = _fout;
  340. tw1 += fstride;
  341. ++Fout2;
  342. ++Fout;
  343. } while ( --m );
  344. }
  345. #define OVERRIDE_KFBFLY4
  346. static void kf_bfly4(
  347. kiss_fft_cpx * Fout,
  348. const int fstride,
  349. const kiss_fft_cfg st,
  350. int m
  351. )
  352. {
  353. register kiss_fft_cpx * restrict tw1,* restrict tw2,* restrict tw3;
  354. register kiss_fft_cpx * restrict Fout1, * restrict Fout2, * restrict Fout3;
  355. register int _inv = !st->inverse;
  356. tw3 = tw2 = tw1 = st->twiddles;
  357. Fout1 = Fout + m;
  358. Fout2 = Fout + (m << 1);
  359. Fout3 = Fout + (m * 3);
  360. do {
  361. register kiss_fft_cpx _fout;
  362. register kiss_fft_cpx sc0, sc1, sc2, sc3, sc4, sc5;
  363. _fout = *Fout;
  364. C_MUL( sc0,*Fout1, *tw1);
  365. C_MUL( sc1,*Fout2, *tw2);
  366. C_MUL( sc2,*Fout3, *tw3);
  367. C_SUB( sc5, _fout, sc1);
  368. C_ADD( _fout, _fout, sc1);
  369. C_ADD( sc3, sc0, sc2);
  370. C_SUB( sc4, sc0, sc2);
  371. C_SUB(*Fout2, _fout, sc3);
  372. C_ADD( *Fout, _fout, sc3);
  373. tw1 += fstride;
  374. tw2 += (fstride << 1);
  375. tw3 += (fstride * 3);
  376. if ( _inv )
  377. {
  378. Fout1->r = sc5.r + sc4.i;
  379. Fout1->i = sc5.i - sc4.r;
  380. Fout3->r = sc5.r - sc4.i;
  381. Fout3->i = sc5.i + sc4.r;
  382. }
  383. else
  384. { Fout1->r = sc5.r - sc4.i;
  385. Fout1->i = sc5.i + sc4.r;
  386. Fout3->r = sc5.r + sc4.i;
  387. Fout3->i = sc5.i - sc4.r;
  388. }
  389. ++Fout; ++Fout1; ++Fout2; ++Fout3;
  390. } while(--m);
  391. }
  392. #define OVERRIDE_KFBFLY3
  393. static void kf_bfly3(
  394. kiss_fft_cpx * Fout,
  395. const int fstride,
  396. const kiss_fft_cfg st,
  397. int m
  398. )
  399. {
  400. register kiss_fft_cpx * restrict Fout1, * restrict Fout2;
  401. register kiss_fft_cpx * restrict tw1,* restrict tw2;
  402. register float epi;
  403. tw1 = tw2 = st->twiddles;
  404. epi = st->twiddles[fstride*m].i;
  405. Fout1 = Fout + m;
  406. Fout2 = Fout + (m << 1);
  407. do {
  408. register kiss_fft_cpx _fout;
  409. register kiss_fft_cpx sc0, sc1, sc2, sc3;
  410. _fout = *Fout;
  411. C_MUL( sc1, *Fout1, *tw1);
  412. C_MUL( sc2, *Fout2, *tw2);
  413. C_ADD( sc3, sc1, sc2);
  414. C_SUB( sc0, sc1, sc2);
  415. tw1 += fstride;
  416. tw2 += (fstride << 1);
  417. sc1.r = _fout.r - HALF_OF(sc3.r);
  418. sc1.i = _fout.i - HALF_OF(sc3.i);
  419. C_MULBYSCALAR(sc0, epi);
  420. C_ADD(*Fout, _fout, sc3);
  421. Fout2->r = sc1.r + sc0.i;
  422. Fout2->i = sc1.i - sc0.r;
  423. Fout1->r = sc1.i - sc0.i;
  424. Fout1->i = sc1.r + sc0.r;
  425. ++Fout; ++Fout1; ++Fout2;
  426. } while(--m);
  427. }
  428. #define OVERRIDE_KFBFLY5
  429. static void kf_bfly5(
  430. kiss_fft_cpx * Fout,
  431. const size_t fstride,
  432. const kiss_fft_cfg st,
  433. int m
  434. )
  435. {
  436. register kiss_fft_cpx * restrict Fout1,* restrict Fout2,* restrict Fout3,* restrict Fout4;
  437. register int u;
  438. register kiss_fft_cpx *tw;
  439. register float yar, yai, ybr, ybi;
  440. Fout1=Fout+m;
  441. Fout2=Fout+(m<<1);
  442. Fout3=Fout+(m*3);
  443. Fout4=Fout+(m<<2);
  444. tw = st->twiddles;
  445. yar = tw[fstride*m].r;
  446. yai = tw[fstride*m].i;
  447. ybr = tw[fstride*2*m].r;
  448. ybi = tw[fstride*2*m].i;
  449. for ( u=0; u<m; ++u )
  450. {
  451. register kiss_fft_cpx sc0, sc1, sc2, sc3, sc4, sc5, sc6, sc7, sc8, sc9, sc10, sc11, sc12;
  452. sc0 = *Fout;
  453. C_MUL( sc1,*Fout1, tw[u*fstride]);
  454. C_MUL( sc2,*Fout2, tw[2*u*fstride]);
  455. C_MUL( sc3,*Fout3, tw[3*u*fstride]);
  456. C_MUL( sc4,*Fout4, tw[4*u*fstride]);
  457. C_ADD( sc7, sc1, sc4);
  458. C_SUB( sc10, sc1, sc4);
  459. C_ADD( sc8, sc2, sc3);
  460. C_SUB( sc9, sc2, sc3);
  461. Fout->r = sc0.r + sc7.r + sc8.r;
  462. Fout->i = sc0.i + sc7.i + sc8.i;
  463. sc5.r = sc0.r + S_MUL(sc7.r,yar) + S_MUL(sc8.r,ybr);
  464. sc5.i = sc0.i + S_MUL(sc7.i,yar) + S_MUL(sc8.i,ybr);
  465. sc6.r = S_MUL(sc10.i,yai) + S_MUL(sc9.i,ybi);
  466. sc6.i = -S_MUL(sc10.r,yai) - S_MUL(sc9.r,ybi);
  467. C_SUB(*Fout1,sc5,sc6);
  468. C_ADD(*Fout4,sc5,sc6);
  469. sc11.r = sc0.r + S_MUL(sc7.r,ybr) + S_MUL(sc8.r,yar);
  470. sc11.i = sc0.i + S_MUL(sc7.i,ybr) + S_MUL(sc8.i,yar);
  471. sc12.r = - S_MUL(sc10.i,ybi) + S_MUL(sc9.i,yai);
  472. sc12.i = S_MUL(sc10.r,ybi) - S_MUL(sc9.r,yai);
  473. C_ADD(*Fout2,sc11,sc12);
  474. C_SUB(*Fout3,sc11,sc12);
  475. ++Fout1; ++Fout2; ++Fout3; ++Fout4;
  476. }
  477. }
  478. #endif
  479. #endif