assembly.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: RCSL 1.0/RPSL 1.0
  3. *
  4. * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved.
  5. *
  6. * The contents of this file, and the files included with this file, are
  7. * subject to the current version of the RealNetworks Public Source License
  8. * Version 1.0 (the "RPSL") available at
  9. * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10. * the file under the RealNetworks Community Source License Version 1.0
  11. * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl,
  12. * in which case the RCSL will apply. You may also obtain the license terms
  13. * directly from RealNetworks. You may not use this file except in
  14. * compliance with the RPSL or, if you have a valid RCSL with RealNetworks
  15. * applicable to this file, the RCSL. Please see the applicable RPSL or
  16. * RCSL for the rights, obligations and limitations governing use of the
  17. * contents of the file.
  18. *
  19. * This file is part of the Helix DNA Technology. RealNetworks is the
  20. * developer of the Original Code and owns the copyrights in the portions
  21. * it created.
  22. *
  23. * This file, and the files included with this file, is distributed and made
  24. * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  25. * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  26. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
  27. * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  28. *
  29. * Technology Compatibility Kit Test Suite(s) Location:
  30. * http://www.helixcommunity.org/content/tck
  31. *
  32. * Contributor(s):
  33. *
  34. * ***** END LICENSE BLOCK ***** */
  35. /**************************************************************************************
  36. * Fixed-point MP3 decoder
  37. * Jon Recker (jrecker@real.com), Ken Cooke (kenc@real.com)
  38. * June 2003
  39. *
  40. * assembly.h - assembly language functions and prototypes for supported platforms
  41. *
  42. * - inline rountines with access to 64-bit multiply results
  43. * - x86 (_WIN32) and ARM (ARM_ADS, _WIN32_WCE) versions included
  44. * - some inline functions are mix of asm and C for speed
  45. * - some functions are in native asm files, so only the prototype is given here
  46. *
  47. * MULSHIFT32(x, y) signed multiply of two 32-bit integers (x and y), returns top 32 bits of 64-bit result
  48. * FASTABS(x) branchless absolute value of signed integer x
  49. * CLZ(x) count leading zeros in x
  50. * MADD64(sum, x, y) (Windows only) sum [64-bit] += x [32-bit] * y [32-bit]
  51. * SHL64(sum, x, y) (Windows only) 64-bit left shift using __int64
  52. * SAR64(sum, x, y) (Windows only) 64-bit right shift using __int64
  53. */
  54. #ifndef _ASSEMBLY_H
  55. #define _ASSEMBLY_H
  56. #if (defined _WIN32 && !defined _WIN32_WCE) || (defined __WINS__ && defined _SYMBIAN) || defined(_OPENWAVE_SIMULATOR) || defined(WINCE_EMULATOR) /* Symbian emulator for Ix86 */
  57. #pragma warning( disable : 4035 ) /* complains about inline asm not returning a value */
  58. static __inline int MULSHIFT32(int x, int y)
  59. {
  60. __asm {
  61. mov eax, x
  62. imul y
  63. mov eax, edx
  64. }
  65. }
  66. static __inline int FASTABS(int x)
  67. {
  68. int sign;
  69. sign = x >> (sizeof(int) * 8 - 1);
  70. x ^= sign;
  71. x -= sign;
  72. return x;
  73. }
  74. static __inline int CLZ(int x)
  75. {
  76. int numZeros;
  77. if (!x)
  78. return (sizeof(int) * 8);
  79. numZeros = 0;
  80. while (!(x & 0x80000000)) {
  81. numZeros++;
  82. x <<= 1;
  83. }
  84. return numZeros;
  85. }
  86. /* MADD64, SHL64, SAR64:
  87. * write in assembly to avoid dependency on run-time lib for 64-bit shifts, muls
  88. * (sometimes compiler thunks to function calls instead of code generating)
  89. * required for Symbian emulator
  90. */
  91. static __inline __int64 MADD64(__int64 sum, int x, int y)
  92. {
  93. unsigned int sumLo = ((unsigned int *)&sum)[0];
  94. int sumHi = ((int *)&sum)[1];
  95. __asm {
  96. mov eax, x
  97. imul y
  98. add eax, sumLo
  99. adc edx, sumHi
  100. }
  101. /* equivalent to return (sum + ((__int64)x * y)); */
  102. }
  103. static __inline __int64 SHL64(__int64 x, int n)
  104. {
  105. unsigned int xLo = ((unsigned int *)&x)[0];
  106. int xHi = ((int *)&x)[1];
  107. unsigned char nb = (unsigned char)n;
  108. if (n < 32) {
  109. __asm {
  110. mov edx, xHi
  111. mov eax, xLo
  112. mov cl, nb
  113. shld edx, eax, cl
  114. shl eax, cl
  115. }
  116. } else if (n < 64) {
  117. /* shl masks cl to 0x1f */
  118. __asm {
  119. mov edx, xLo
  120. mov cl, nb
  121. xor eax, eax
  122. shl edx, cl
  123. }
  124. } else {
  125. __asm {
  126. xor edx, edx
  127. xor eax, eax
  128. }
  129. }
  130. }
  131. static __inline __int64 SAR64(__int64 x, int n)
  132. {
  133. unsigned int xLo = ((unsigned int *)&x)[0];
  134. int xHi = ((int *)&x)[1];
  135. unsigned char nb = (unsigned char)n;
  136. if (n < 32) {
  137. __asm {
  138. mov edx, xHi
  139. mov eax, xLo
  140. mov cl, nb
  141. shrd eax, edx, cl
  142. sar edx, cl
  143. }
  144. } else if (n < 64) {
  145. /* sar masks cl to 0x1f */
  146. __asm {
  147. mov edx, xHi
  148. mov eax, xHi
  149. mov cl, nb
  150. sar edx, 31
  151. sar eax, cl
  152. }
  153. } else {
  154. __asm {
  155. sar xHi, 31
  156. mov eax, xHi
  157. mov edx, xHi
  158. }
  159. }
  160. }
  161. #elif (defined _WIN32) && (defined _WIN32_WCE)
  162. /* use asm function for now (EVC++ 3.0 does horrible job compiling __int64 version) */
  163. #define MULSHIFT32 xmp3_MULSHIFT32
  164. int MULSHIFT32(int x, int y);
  165. static __inline int FASTABS(int x)
  166. {
  167. int sign;
  168. sign = x >> (sizeof(int) * 8 - 1);
  169. x ^= sign;
  170. x -= sign;
  171. return x;
  172. }
  173. static __inline int CLZ(int x)
  174. {
  175. int numZeros;
  176. if (!x)
  177. return (sizeof(int) * 8);
  178. numZeros = 0;
  179. while (!(x & 0x80000000)) {
  180. numZeros++;
  181. x <<= 1;
  182. }
  183. return numZeros;
  184. }
  185. #elif defined ARM_ADS
  186. static __inline int MULSHIFT32(int x, int y)
  187. {
  188. /* important rules for smull RdLo, RdHi, Rm, Rs:
  189. * RdHi and Rm can't be the same register
  190. * RdLo and Rm can't be the same register
  191. * RdHi and RdLo can't be the same register
  192. * Note: Rs determines early termination (leading sign bits) so if you want to specify
  193. * which operand is Rs, put it in the SECOND argument (y)
  194. * For inline assembly, x and y are not assumed to be R0, R1 so it shouldn't matter
  195. * which one is returned. (If this were a function call, returning y (R1) would
  196. * require an extra "mov r0, r1")
  197. */
  198. int zlow;
  199. __asm {
  200. smull zlow,y,x,y
  201. }
  202. return y;
  203. }
  204. static __inline int FASTABS(int x)
  205. {
  206. int t;
  207. __asm {
  208. eor t, x, x, asr #31
  209. sub t, t, x, asr #31
  210. }
  211. return t;
  212. }
  213. static __inline int CLZ(int x)
  214. {
  215. int numZeros;
  216. if (!x)
  217. return (sizeof(int) * 8);
  218. numZeros = 0;
  219. while (!(x & 0x80000000)) {
  220. numZeros++;
  221. x <<= 1;
  222. }
  223. return numZeros;
  224. }
  225. #elif defined(__GNUC__) && defined(__arm__)
  226. static __inline int MULSHIFT32(int x, int y)
  227. {
  228. /* important rules for smull RdLo, RdHi, Rm, Rs:
  229. * RdHi and Rm can't be the same register
  230. * RdLo and Rm can't be the same register
  231. * RdHi and RdLo can't be the same register
  232. * Note: Rs determines early termination (leading sign bits) so if you want to specify
  233. * which operand is Rs, put it in the SECOND argument (y)
  234. * For inline assembly, x and y are not assumed to be R0, R1 so it shouldn't matter
  235. * which one is returned. (If this were a function call, returning y (R1) would
  236. * require an extra "mov r0, r1")
  237. */
  238. int zlow;
  239. __asm__ volatile ("smull %0,%1,%2,%3" : "=&r" (zlow), "=r" (y) : "r" (x), "1" (y)) ;
  240. return y;
  241. }
  242. static __inline int FASTABS(int x)
  243. {
  244. int t = 0;
  245. __asm__ volatile (
  246. "eor %0,%2,%2, asr #31;"
  247. "sub %0,%1,%2, asr #31;"
  248. : "=&r" (t)
  249. : "0" (t), "r" (x)
  250. );
  251. return t;
  252. }
  253. static __inline int CLZ(int x)
  254. {
  255. int numZeros;
  256. if (!x)
  257. return (sizeof(int) * 8);
  258. numZeros = 0;
  259. while (!(x & 0x80000000)) {
  260. numZeros++;
  261. x <<= 1;
  262. }
  263. return numZeros;
  264. }
  265. #else
  266. #error Unsupported platform in assembly.h
  267. #endif /* platforms */
  268. #endif /* _ASSEMBLY_H */