mad_fixed.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * libmad - MPEG audio decoder library
  3. * Copyright (C) 2000-2004 Underbit Technologies, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * $Id: fixed.h,v 1.38 2004/02/17 02:02:03 rob Exp $
  20. */
  21. # ifndef LIBMAD_FIXED_H
  22. # define LIBMAD_FIXED_H
  23. # if SIZEOF_INT >= 4
  24. typedef signed int mad_fixed_t;
  25. typedef signed int mad_fixed64hi_t;
  26. typedef unsigned int mad_fixed64lo_t;
  27. # else
  28. typedef signed long mad_fixed_t;
  29. typedef signed long mad_fixed64hi_t;
  30. typedef unsigned long mad_fixed64lo_t;
  31. # endif
  32. # if defined(_MSC_VER)
  33. # define mad_fixed64_t signed __int64
  34. # elif 1 || defined(__GNUC__)
  35. # define mad_fixed64_t signed long long
  36. # endif
  37. # if defined(FPM_FLOAT)
  38. typedef double mad_sample_t;
  39. # else
  40. typedef mad_fixed_t mad_sample_t;
  41. # endif
  42. /*
  43. * Fixed-point format: 0xABBBBBBB
  44. * A == whole part (sign + 3 bits)
  45. * B == fractional part (28 bits)
  46. *
  47. * Values are signed two's complement, so the effective range is:
  48. * 0x80000000 to 0x7fffffff
  49. * -8.0 to +7.9999999962747097015380859375
  50. *
  51. * The smallest representable value is:
  52. * 0x00000001 == 0.0000000037252902984619140625 (i.e. about 3.725e-9)
  53. *
  54. * 28 bits of fractional accuracy represent about
  55. * 8.6 digits of decimal accuracy.
  56. *
  57. * Fixed-point numbers can be added or subtracted as normal
  58. * integers, but multiplication requires shifting the 64-bit result
  59. * from 56 fractional bits back to 28 (and rounding.)
  60. *
  61. * Changing the definition of MAD_F_FRACBITS is only partially
  62. * supported, and must be done with care.
  63. */
  64. # define MAD_F_FRACBITS 28
  65. # if MAD_F_FRACBITS == 28
  66. # define MAD_F(x) ((mad_fixed_t) (x##L))
  67. # else
  68. # if MAD_F_FRACBITS < 28
  69. # warning "MAD_F_FRACBITS < 28"
  70. # define MAD_F(x) ((mad_fixed_t) \
  71. (((x##L) + \
  72. (1L << (28 - MAD_F_FRACBITS - 1))) >> \
  73. (28 - MAD_F_FRACBITS)))
  74. # elif MAD_F_FRACBITS > 28
  75. # error "MAD_F_FRACBITS > 28 not currently supported"
  76. # define MAD_F(x) ((mad_fixed_t) \
  77. ((x##L) << (MAD_F_FRACBITS - 28)))
  78. # endif
  79. # endif
  80. # define MAD_F_MIN ((mad_fixed_t) -0x80000000L)
  81. # define MAD_F_MAX ((mad_fixed_t) +0x7fffffffL)
  82. # define MAD_F_ONE MAD_F(0x10000000)
  83. # define mad_f_tofixed(x) ((mad_fixed_t) \
  84. ((x) * (double) (1L << MAD_F_FRACBITS) + 0.5))
  85. # define mad_f_todouble(x) ((double) \
  86. ((x) / (double) (1L << MAD_F_FRACBITS)))
  87. # define mad_f_intpart(x) ((x) >> MAD_F_FRACBITS)
  88. # define mad_f_fracpart(x) ((x) & ((1L << MAD_F_FRACBITS) - 1))
  89. /* (x should be positive) */
  90. # define mad_f_fromint(x) ((x) << MAD_F_FRACBITS)
  91. # define mad_f_add(x, y) ((x) + (y))
  92. # define mad_f_sub(x, y) ((x) - (y))
  93. # if defined(FPM_FLOAT)
  94. # error "FPM_FLOAT not yet supported"
  95. # undef MAD_F
  96. # define MAD_F(x) mad_f_todouble(x)
  97. # define mad_f_mul(x, y) ((x) * (y))
  98. # define mad_f_scale64
  99. # undef ASO_ZEROCHECK
  100. # elif defined(FPM_64BIT)
  101. /*
  102. * This version should be the most accurate if 64-bit types are supported by
  103. * the compiler, although it may not be the most efficient.
  104. */
  105. # if defined(OPT_ACCURACY)
  106. # define mad_f_mul(x, y) \
  107. ((mad_fixed_t) \
  108. ((((mad_fixed64_t) (x) * (y)) + \
  109. (1L << (MAD_F_SCALEBITS - 1))) >> MAD_F_SCALEBITS))
  110. # else
  111. # define mad_f_mul(x, y) \
  112. ((mad_fixed_t) (((mad_fixed64_t) (x) * (y)) >> MAD_F_SCALEBITS))
  113. # endif
  114. # define MAD_F_SCALEBITS MAD_F_FRACBITS
  115. /* --- Intel --------------------------------------------------------------- */
  116. # elif defined(FPM_INTEL)
  117. # if defined(_MSC_VER)
  118. # pragma warning(push)
  119. # pragma warning(disable: 4035) /* no return value */
  120. static __forceinline
  121. mad_fixed_t mad_f_mul_inline(mad_fixed_t x, mad_fixed_t y)
  122. {
  123. enum {
  124. fracbits = MAD_F_FRACBITS
  125. };
  126. __asm {
  127. mov eax, x
  128. imul y
  129. shrd eax, edx, fracbits
  130. }
  131. /* implicit return of eax */
  132. }
  133. # pragma warning(pop)
  134. # define mad_f_mul mad_f_mul_inline
  135. # define mad_f_scale64
  136. # else
  137. /*
  138. * This Intel version is fast and accurate; the disposition of the least
  139. * significant bit depends on OPT_ACCURACY via mad_f_scale64().
  140. */
  141. # define MAD_F_MLX(hi, lo, x, y) \
  142. asm ("imull %3" \
  143. : "=a" (lo), "=d" (hi) \
  144. : "%a" (x), "rm" (y) \
  145. : "cc")
  146. # if defined(OPT_ACCURACY)
  147. /*
  148. * This gives best accuracy but is not very fast.
  149. */
  150. # define MAD_F_MLA(hi, lo, x, y) \
  151. ({ mad_fixed64hi_t __hi; \
  152. mad_fixed64lo_t __lo; \
  153. MAD_F_MLX(__hi, __lo, (x), (y)); \
  154. asm ("addl %2,%0\n\t" \
  155. "adcl %3,%1" \
  156. : "=rm" (lo), "=rm" (hi) \
  157. : "r" (__lo), "r" (__hi), "0" (lo), "1" (hi) \
  158. : "cc"); \
  159. })
  160. # endif /* OPT_ACCURACY */
  161. # if defined(OPT_ACCURACY)
  162. /*
  163. * Surprisingly, this is faster than SHRD followed by ADC.
  164. */
  165. # define mad_f_scale64(hi, lo) \
  166. ({ mad_fixed64hi_t __hi_; \
  167. mad_fixed64lo_t __lo_; \
  168. mad_fixed_t __result; \
  169. asm ("addl %4,%2\n\t" \
  170. "adcl %5,%3" \
  171. : "=rm" (__lo_), "=rm" (__hi_) \
  172. : "0" (lo), "1" (hi), \
  173. "ir" (1L << (MAD_F_SCALEBITS - 1)), "ir" (0) \
  174. : "cc"); \
  175. asm ("shrdl %3,%2,%1" \
  176. : "=rm" (__result) \
  177. : "0" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS) \
  178. : "cc"); \
  179. __result; \
  180. })
  181. # elif defined(OPT_INTEL)
  182. /*
  183. * Alternate Intel scaling that may or may not perform better.
  184. */
  185. # define mad_f_scale64(hi, lo) \
  186. ({ mad_fixed_t __result; \
  187. asm ("shrl %3,%1\n\t" \
  188. "shll %4,%2\n\t" \
  189. "orl %2,%1" \
  190. : "=rm" (__result) \
  191. : "0" (lo), "r" (hi), \
  192. "I" (MAD_F_SCALEBITS), "I" (32 - MAD_F_SCALEBITS) \
  193. : "cc"); \
  194. __result; \
  195. })
  196. # else
  197. # define mad_f_scale64(hi, lo) \
  198. ({ mad_fixed_t __result; \
  199. asm ("shrdl %3,%2,%1" \
  200. : "=rm" (__result) \
  201. : "0" (lo), "r" (hi), "I" (MAD_F_SCALEBITS) \
  202. : "cc"); \
  203. __result; \
  204. })
  205. # endif /* OPT_ACCURACY */
  206. # define MAD_F_SCALEBITS MAD_F_FRACBITS
  207. # endif
  208. /* --- ARM ----------------------------------------------------------------- */
  209. # elif defined(FPM_ARM)
  210. /*
  211. * This ARM V4 version is as accurate as FPM_64BIT but much faster. The
  212. * least significant bit is properly rounded at no CPU cycle cost!
  213. */
  214. # if 1
  215. /*
  216. * This is faster than the default implementation via MAD_F_MLX() and
  217. * mad_f_scale64().
  218. */
  219. # define mad_f_mul(x, y) \
  220. ({ mad_fixed64hi_t __hi; \
  221. mad_fixed64lo_t __lo; \
  222. mad_fixed_t __result; \
  223. asm ("smull %0, %1, %3, %4\n\t" \
  224. "movs %0, %0, lsr %5\n\t" \
  225. "adc %2, %0, %1, lsl %6" \
  226. : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
  227. : "%r" (x), "r" (y), \
  228. "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
  229. : "cc"); \
  230. __result; \
  231. })
  232. # endif
  233. # define MAD_F_MLX(hi, lo, x, y) \
  234. asm ("smull %0, %1, %2, %3" \
  235. : "=&r" (lo), "=&r" (hi) \
  236. : "%r" (x), "r" (y))
  237. # define MAD_F_MLA(hi, lo, x, y) \
  238. asm ("smlal %0, %1, %2, %3" \
  239. : "+r" (lo), "+r" (hi) \
  240. : "%r" (x), "r" (y))
  241. # define MAD_F_MLN(hi, lo) \
  242. asm ("rsbs %0, %2, #0\n\t" \
  243. "rsc %1, %3, #0" \
  244. : "=r" (lo), "=r" (hi) \
  245. : "0" (lo), "1" (hi) \
  246. : "cc")
  247. # define mad_f_scale64(hi, lo) \
  248. ({ mad_fixed_t __result; \
  249. asm ("movs %0, %1, lsr %3\n\t" \
  250. "adc %0, %0, %2, lsl %4" \
  251. : "=&r" (__result) \
  252. : "r" (lo), "r" (hi), \
  253. "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
  254. : "cc"); \
  255. __result; \
  256. })
  257. # define MAD_F_SCALEBITS MAD_F_FRACBITS
  258. /* --- MIPS ---------------------------------------------------------------- */
  259. # elif defined(FPM_MIPS)
  260. /*
  261. * This MIPS version is fast and accurate; the disposition of the least
  262. * significant bit depends on OPT_ACCURACY via mad_f_scale64().
  263. */
  264. # define MAD_F_MLX(hi, lo, x, y) \
  265. asm ("mult %2,%3" \
  266. : "=l" (lo), "=h" (hi) \
  267. : "%r" (x), "r" (y))
  268. # if defined(HAVE_MADD_ASM)
  269. # define MAD_F_MLA(hi, lo, x, y) \
  270. asm ("madd %2,%3" \
  271. : "+l" (lo), "+h" (hi) \
  272. : "%r" (x), "r" (y))
  273. # elif defined(HAVE_MADD16_ASM)
  274. /*
  275. * This loses significant accuracy due to the 16-bit integer limit in the
  276. * multiply/accumulate instruction.
  277. */
  278. # define MAD_F_ML0(hi, lo, x, y) \
  279. asm ("mult %2,%3" \
  280. : "=l" (lo), "=h" (hi) \
  281. : "%r" ((x) >> 12), "r" ((y) >> 16))
  282. # define MAD_F_MLA(hi, lo, x, y) \
  283. asm ("madd16 %2,%3" \
  284. : "+l" (lo), "+h" (hi) \
  285. : "%r" ((x) >> 12), "r" ((y) >> 16))
  286. # define MAD_F_MLZ(hi, lo) ((mad_fixed_t) (lo))
  287. # endif
  288. # if defined(OPT_SPEED)
  289. # define mad_f_scale64(hi, lo) \
  290. ((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS)))
  291. # define MAD_F_SCALEBITS MAD_F_FRACBITS
  292. # endif
  293. /* --- SPARC --------------------------------------------------------------- */
  294. # elif defined(FPM_SPARC)
  295. /*
  296. * This SPARC V8 version is fast and accurate; the disposition of the least
  297. * significant bit depends on OPT_ACCURACY via mad_f_scale64().
  298. */
  299. # define MAD_F_MLX(hi, lo, x, y) \
  300. asm ("smul %2, %3, %0\n\t" \
  301. "rd %%y, %1" \
  302. : "=r" (lo), "=r" (hi) \
  303. : "%r" (x), "rI" (y))
  304. /* --- PowerPC ------------------------------------------------------------- */
  305. # elif defined(FPM_PPC)
  306. /*
  307. * This PowerPC version is fast and accurate; the disposition of the least
  308. * significant bit depends on OPT_ACCURACY via mad_f_scale64().
  309. */
  310. # define MAD_F_MLX(hi, lo, x, y) \
  311. do { \
  312. asm ("mullw %0,%1,%2" \
  313. : "=r" (lo) \
  314. : "%r" (x), "r" (y)); \
  315. asm ("mulhw %0,%1,%2" \
  316. : "=r" (hi) \
  317. : "%r" (x), "r" (y)); \
  318. } \
  319. while (0)
  320. # if defined(OPT_ACCURACY)
  321. /*
  322. * This gives best accuracy but is not very fast.
  323. */
  324. # define MAD_F_MLA(hi, lo, x, y) \
  325. ({ mad_fixed64hi_t __hi; \
  326. mad_fixed64lo_t __lo; \
  327. MAD_F_MLX(__hi, __lo, (x), (y)); \
  328. asm ("addc %0,%2,%3\n\t" \
  329. "adde %1,%4,%5" \
  330. : "=r" (lo), "=r" (hi) \
  331. : "%r" (lo), "r" (__lo), \
  332. "%r" (hi), "r" (__hi) \
  333. : "xer"); \
  334. })
  335. # endif
  336. # if defined(OPT_ACCURACY)
  337. /*
  338. * This is slower than the truncating version below it.
  339. */
  340. # define mad_f_scale64(hi, lo) \
  341. ({ mad_fixed_t __result, __round; \
  342. asm ("rotrwi %0,%1,%2" \
  343. : "=r" (__result) \
  344. : "r" (lo), "i" (MAD_F_SCALEBITS)); \
  345. asm ("extrwi %0,%1,1,0" \
  346. : "=r" (__round) \
  347. : "r" (__result)); \
  348. asm ("insrwi %0,%1,%2,0" \
  349. : "+r" (__result) \
  350. : "r" (hi), "i" (MAD_F_SCALEBITS)); \
  351. asm ("add %0,%1,%2" \
  352. : "=r" (__result) \
  353. : "%r" (__result), "r" (__round)); \
  354. __result; \
  355. })
  356. # else
  357. # define mad_f_scale64(hi, lo) \
  358. ({ mad_fixed_t __result; \
  359. asm ("rotrwi %0,%1,%2" \
  360. : "=r" (__result) \
  361. : "r" (lo), "i" (MAD_F_SCALEBITS)); \
  362. asm ("insrwi %0,%1,%2,0" \
  363. : "+r" (__result) \
  364. : "r" (hi), "i" (MAD_F_SCALEBITS)); \
  365. __result; \
  366. })
  367. # endif
  368. # define MAD_F_SCALEBITS MAD_F_FRACBITS
  369. /* --- Default ------------------------------------------------------------- */
  370. # elif defined(FPM_DEFAULT)
  371. /*
  372. * This version is the most portable but it loses significant accuracy.
  373. * Furthermore, accuracy is biased against the second argument, so care
  374. * should be taken when ordering operands.
  375. *
  376. * The scale factors are constant as this is not used with SSO.
  377. *
  378. * Pre-rounding is required to stay within the limits of compliance.
  379. */
  380. # if defined(OPT_SPEED)
  381. # define mad_f_mul(x, y) (((x) >> 12) * ((y) >> 16))
  382. # else
  383. # define mad_f_mul(x, y) ((((x) + (1L << 11)) >> 12) * \
  384. (((y) + (1L << 15)) >> 16))
  385. # endif
  386. /* ------------------------------------------------------------------------- */
  387. # else
  388. # error "no FPM selected"
  389. # endif
  390. /* default implementations */
  391. # if !defined(mad_f_mul)
  392. # define mad_f_mul(x, y) \
  393. ({ register mad_fixed64hi_t __hi; \
  394. register mad_fixed64lo_t __lo; \
  395. MAD_F_MLX(__hi, __lo, (x), (y)); \
  396. mad_f_scale64(__hi, __lo); \
  397. })
  398. # endif
  399. # if !defined(MAD_F_MLA)
  400. # define MAD_F_ML0(hi, lo, x, y) ((lo) = mad_f_mul((x), (y)))
  401. # define MAD_F_MLA(hi, lo, x, y) ((lo) += mad_f_mul((x), (y)))
  402. # define MAD_F_MLN(hi, lo) ((lo) = -(lo))
  403. # define MAD_F_MLZ(hi, lo) ((void) (hi), (mad_fixed_t) (lo))
  404. # endif
  405. # if !defined(MAD_F_ML0)
  406. # define MAD_F_ML0(hi, lo, x, y) MAD_F_MLX((hi), (lo), (x), (y))
  407. # endif
  408. # if !defined(MAD_F_MLN)
  409. # define MAD_F_MLN(hi, lo) ((hi) = ((lo) = -(lo)) ? ~(hi) : -(hi))
  410. # endif
  411. # if !defined(MAD_F_MLZ)
  412. # define MAD_F_MLZ(hi, lo) mad_f_scale64((hi), (lo))
  413. # endif
  414. # if !defined(mad_f_scale64)
  415. # if defined(OPT_ACCURACY)
  416. # define mad_f_scale64(hi, lo) \
  417. ((((mad_fixed_t) \
  418. (((hi) << (32 - (MAD_F_SCALEBITS - 1))) | \
  419. ((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1)
  420. # else
  421. # define mad_f_scale64(hi, lo) \
  422. ((mad_fixed_t) \
  423. (((hi) << (32 - MAD_F_SCALEBITS)) | \
  424. ((lo) >> MAD_F_SCALEBITS)))
  425. # endif
  426. # define MAD_F_SCALEBITS MAD_F_FRACBITS
  427. # endif
  428. /* C routines */
  429. mad_fixed_t mad_f_abs(mad_fixed_t);
  430. mad_fixed_t mad_f_div(mad_fixed_t, mad_fixed_t);
  431. # endif