fixed.h 13 KB

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