PageRenderTime 59ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/dpf-30-linux/libmad-0.15.0b/fixed.h

https://github.com/liuzhwei/digital-frame-for-pictures
C Header | 484 lines | 310 code | 68 blank | 106 comment | 26 complexity | ab1a229588737d346ae62e503eb88dec MD5 | raw file
  1. /*
  2. * libmad - MPEG audio decoder library
  3. * Copyright (C) 2000-2003 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.36 2003/05/28 04:36:00 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. # else
  182. # define mad_f_scale64(hi, lo) \
  183. ({ mad_fixed_t __result; \
  184. asm ("shrdl %3,%2,%1" \
  185. : "=rm" (__result) \
  186. : "0" (lo), "r" (hi), "I" (MAD_F_SCALEBITS) \
  187. : "cc"); \
  188. __result; \
  189. })
  190. # endif /* OPT_ACCURACY */
  191. # define MAD_F_SCALEBITS MAD_F_FRACBITS
  192. # endif
  193. /* --- ARM ----------------------------------------------------------------- */
  194. # elif defined(FPM_ARM)
  195. /*
  196. * This ARM V4 version is as accurate as FPM_64BIT but much faster. The
  197. * least significant bit is properly rounded at no CPU cycle cost!
  198. */
  199. # if 1
  200. /*
  201. * This is faster than the default implementation via MAD_F_MLX() and
  202. * mad_f_scale64().
  203. */
  204. # define mad_f_mul(x, y) \
  205. ({ mad_fixed64hi_t __hi; \
  206. mad_fixed64lo_t __lo; \
  207. mad_fixed_t __result; \
  208. asm ("smull %0, %1, %3, %4\n\t" \
  209. "movs %0, %0, lsr %5\n\t" \
  210. "adc %2, %0, %1, lsl %6" \
  211. : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
  212. : "%r" (x), "r" (y), \
  213. "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
  214. : "cc"); \
  215. __result; \
  216. })
  217. # endif
  218. # define MAD_F_MLX(hi, lo, x, y) \
  219. asm ("smull %0, %1, %2, %3" \
  220. : "=&r" (lo), "=&r" (hi) \
  221. : "%r" (x), "r" (y))
  222. # define MAD_F_MLA(hi, lo, x, y) \
  223. asm ("smlal %0, %1, %2, %3" \
  224. : "+r" (lo), "+r" (hi) \
  225. : "%r" (x), "r" (y))
  226. # define MAD_F_MLN(hi, lo) \
  227. asm ("rsbs %0, %2, #0\n\t" \
  228. "rsc %1, %3, #0" \
  229. : "=r" (lo), "=r" (hi) \
  230. : "0" (lo), "1" (hi) \
  231. : "cc")
  232. # define mad_f_scale64(hi, lo) \
  233. ({ mad_fixed_t __result; \
  234. asm ("movs %0, %1, lsr %3\n\t" \
  235. "adc %0, %0, %2, lsl %4" \
  236. : "=&r" (__result) \
  237. : "r" (lo), "r" (hi), \
  238. "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
  239. : "cc"); \
  240. __result; \
  241. })
  242. # define MAD_F_SCALEBITS MAD_F_FRACBITS
  243. /* --- MIPS ---------------------------------------------------------------- */
  244. # elif defined(FPM_MIPS)
  245. /*
  246. * This MIPS version is fast and accurate; the disposition of the least
  247. * significant bit depends on OPT_ACCURACY via mad_f_scale64().
  248. */
  249. # define MAD_F_MLX(hi, lo, x, y) \
  250. asm ("mult %2,%3" \
  251. : "=l" (lo), "=h" (hi) \
  252. : "%r" (x), "r" (y))
  253. # if defined(HAVE_MADD_ASM)
  254. # define MAD_F_MLA(hi, lo, x, y) \
  255. asm ("madd %2,%3" \
  256. : "+l" (lo), "+h" (hi) \
  257. : "%r" (x), "r" (y))
  258. # elif defined(HAVE_MADD16_ASM)
  259. /*
  260. * This loses significant accuracy due to the 16-bit integer limit in the
  261. * multiply/accumulate instruction.
  262. */
  263. # define MAD_F_ML0(hi, lo, x, y) \
  264. asm ("mult %2,%3" \
  265. : "=l" (lo), "=h" (hi) \
  266. : "%r" ((x) >> 12), "r" ((y) >> 16))
  267. # define MAD_F_MLA(hi, lo, x, y) \
  268. asm ("madd16 %2,%3" \
  269. : "+l" (lo), "+h" (hi) \
  270. : "%r" ((x) >> 12), "r" ((y) >> 16))
  271. # define MAD_F_MLZ(hi, lo) ((mad_fixed_t) (lo))
  272. # endif
  273. # if defined(OPT_SPEED)
  274. # define mad_f_scale64(hi, lo) \
  275. ((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS)))
  276. # define MAD_F_SCALEBITS MAD_F_FRACBITS
  277. # endif
  278. /* --- SPARC --------------------------------------------------------------- */
  279. # elif defined(FPM_SPARC)
  280. /*
  281. * This SPARC V8 version is fast and accurate; the disposition of the least
  282. * significant bit depends on OPT_ACCURACY via mad_f_scale64().
  283. */
  284. # define MAD_F_MLX(hi, lo, x, y) \
  285. asm ("smul %2, %3, %0\n\t" \
  286. "rd %%y, %1" \
  287. : "=r" (lo), "=r" (hi) \
  288. : "%r" (x), "rI" (y))
  289. /* --- PowerPC ------------------------------------------------------------- */
  290. # elif defined(FPM_PPC)
  291. /*
  292. * This PowerPC version is fast and accurate; the disposition of the least
  293. * significant bit depends on OPT_ACCURACY via mad_f_scale64().
  294. */
  295. # define MAD_F_MLX(hi, lo, x, y) \
  296. do { \
  297. asm ("mullw %0,%1,%2" \
  298. : "=r" (lo) \
  299. : "%r" (x), "r" (y)); \
  300. asm ("mulhw %0,%1,%2" \
  301. : "=r" (hi) \
  302. : "%r" (x), "r" (y)); \
  303. } \
  304. while (0)
  305. # if defined(OPT_ACCURACY)
  306. /*
  307. * This gives best accuracy but is not very fast.
  308. */
  309. # define MAD_F_MLA(hi, lo, x, y) \
  310. ({ mad_fixed64hi_t __hi; \
  311. mad_fixed64lo_t __lo; \
  312. MAD_F_MLX(__hi, __lo, (x), (y)); \
  313. asm ("addc %0,%2,%3\n\t" \
  314. "adde %1,%4,%5" \
  315. : "=r" (lo), "=r" (hi) \
  316. : "%r" (lo), "r" (__lo), \
  317. "%r" (hi), "r" (__hi) \
  318. : "xer"); \
  319. })
  320. # endif
  321. # if defined(OPT_ACCURACY)
  322. /*
  323. * This is slower than the truncating version below it.
  324. */
  325. # define mad_f_scale64(hi, lo) \
  326. ({ mad_fixed_t __result, __round; \
  327. asm ("rotrwi %0,%1,%2" \
  328. : "=r" (__result) \
  329. : "r" (lo), "i" (MAD_F_SCALEBITS)); \
  330. asm ("extrwi %0,%1,1,0" \
  331. : "=r" (__round) \
  332. : "r" (__result)); \
  333. asm ("insrwi %0,%1,%2,0" \
  334. : "+r" (__result) \
  335. : "r" (hi), "i" (MAD_F_SCALEBITS)); \
  336. asm ("add %0,%1,%2" \
  337. : "=r" (__result) \
  338. : "%r" (__result), "r" (__round)); \
  339. __result; \
  340. })
  341. # else
  342. # define mad_f_scale64(hi, lo) \
  343. ({ mad_fixed_t __result; \
  344. asm ("rotrwi %0,%1,%2" \
  345. : "=r" (__result) \
  346. : "r" (lo), "i" (MAD_F_SCALEBITS)); \
  347. asm ("insrwi %0,%1,%2,0" \
  348. : "+r" (__result) \
  349. : "r" (hi), "i" (MAD_F_SCALEBITS)); \
  350. __result; \
  351. })
  352. # endif
  353. # define MAD_F_SCALEBITS MAD_F_FRACBITS
  354. /* --- Default ------------------------------------------------------------- */
  355. # elif defined(FPM_DEFAULT)
  356. /*
  357. * This version is the most portable but it loses significant accuracy.
  358. * Furthermore, accuracy is biased against the second argument, so care
  359. * should be taken when ordering operands.
  360. *
  361. * The scale factors are constant as this is not used with SSO.
  362. *
  363. * Pre-rounding is required to stay within the limits of compliance.
  364. */
  365. # if defined(OPT_SPEED)
  366. # define mad_f_mul(x, y) (((x) >> 12) * ((y) >> 16))
  367. # else
  368. # define mad_f_mul(x, y) ((((x) + (1L << 11)) >> 12) * \
  369. (((y) + (1L << 15)) >> 16))
  370. # endif
  371. /* ------------------------------------------------------------------------- */
  372. # else
  373. # error "no FPM selected"
  374. # endif
  375. /* default implementations */
  376. # if !defined(mad_f_mul)
  377. # define mad_f_mul(x, y) \
  378. ({ register mad_fixed64hi_t __hi; \
  379. register mad_fixed64lo_t __lo; \
  380. MAD_F_MLX(__hi, __lo, (x), (y)); \
  381. mad_f_scale64(__hi, __lo); \
  382. })
  383. # endif
  384. # if !defined(MAD_F_MLA)
  385. # define MAD_F_ML0(hi, lo, x, y) ((lo) = mad_f_mul((x), (y)))
  386. # define MAD_F_MLA(hi, lo, x, y) ((lo) += mad_f_mul((x), (y)))
  387. # define MAD_F_MLN(hi, lo) ((lo) = -(lo))
  388. # define MAD_F_MLZ(hi, lo) ((void) (hi), (mad_fixed_t) (lo))
  389. # endif
  390. # if !defined(MAD_F_ML0)
  391. # define MAD_F_ML0(hi, lo, x, y) MAD_F_MLX((hi), (lo), (x), (y))
  392. # endif
  393. # if !defined(MAD_F_MLN)
  394. # define MAD_F_MLN(hi, lo) ((hi) = ((lo) = -(lo)) ? ~(hi) : -(hi))
  395. # endif
  396. # if !defined(MAD_F_MLZ)
  397. # define MAD_F_MLZ(hi, lo) mad_f_scale64((hi), (lo))
  398. # endif
  399. # if !defined(mad_f_scale64)
  400. # if defined(OPT_ACCURACY)
  401. # define mad_f_scale64(hi, lo) \
  402. ((((mad_fixed_t) \
  403. (((hi) << (32 - (MAD_F_SCALEBITS - 1))) | \
  404. ((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1)
  405. # else
  406. # define mad_f_scale64(hi, lo) \
  407. ((mad_fixed_t) \
  408. (((hi) << (32 - MAD_F_SCALEBITS)) | \
  409. ((lo) >> MAD_F_SCALEBITS)))
  410. # endif
  411. # define MAD_F_SCALEBITS MAD_F_FRACBITS
  412. # endif
  413. /* C routines */
  414. mad_fixed_t mad_f_abs(mad_fixed_t);
  415. mad_fixed_t mad_f_div(mad_fixed_t, mad_fixed_t);
  416. # endif