assembly.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Source last modified: $Id: assembly.h,v 1.7 2005/11/10 00:04:40 margotm Exp $
  3. *
  4. * Portions Copyright (c) 1995-2005 RealNetworks, Inc. All Rights Reserved.
  5. *
  6. * The contents of this file, and the files included with this file,
  7. * are subject to the current version of the RealNetworks Public
  8. * Source License (the "RPSL") available at
  9. * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10. * the file under the current version of the RealNetworks Community
  11. * Source License (the "RCSL") available at
  12. * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
  13. * will apply. You may also obtain the license terms directly from
  14. * RealNetworks. You may not use this file except in compliance with
  15. * the RPSL or, if you have a valid RCSL with RealNetworks applicable
  16. * to this file, the RCSL. Please see the applicable RPSL or RCSL for
  17. * the rights, obligations and limitations governing use of the
  18. * contents of the file.
  19. *
  20. * This file is part of the Helix DNA Technology. RealNetworks is the
  21. * developer of the Original Code and owns the copyrights in the
  22. * portions it created.
  23. *
  24. * This file, and the files included with this file, is distributed
  25. * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
  26. * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
  27. * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
  28. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
  29. * ENJOYMENT OR NON-INFRINGEMENT.
  30. *
  31. * Technology Compatibility Kit Test Suite(s) Location:
  32. * http://www.helixcommunity.org/content/tck
  33. *
  34. * Contributor(s):
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. /**************************************************************************************
  38. * Fixed-point HE-AAC decoder
  39. * Jon Recker (jrecker@real.com)
  40. * February 2005
  41. *
  42. * assembly.h - inline assembly language functions and prototypes
  43. *
  44. * MULSHIFT32(x, y) signed multiply of two 32-bit integers (x and y),
  45. * returns top 32-bits of 64-bit result
  46. * CLIPTOSHORT(x) convert 32-bit integer to 16-bit short,
  47. * clipping to [-32768, 32767]
  48. * FASTABS(x) branchless absolute value of signed integer x
  49. * CLZ(x) count leading zeros on signed integer x
  50. * MADD64(sum64, x, y) 64-bit multiply accumulate: sum64 += (x*y)
  51. **************************************************************************************/
  52. #ifndef _ASSEMBLY_H
  53. #define _ASSEMBLY_H
  54. /* toolchain: MSFT Visual C++
  55. * target architecture: x86
  56. */
  57. #if (defined (_WIN32) && !defined (_WIN32_WCE)) || (defined (__WINS__) && defined (_SYMBIAN)) || (defined (WINCE_EMULATOR)) || (defined (_OPENWAVE_SIMULATOR))
  58. #pragma warning( disable : 4035 ) /* complains about inline asm not returning a value */
  59. static __inline int MULSHIFT32(int x, int y)
  60. {
  61. __asm {
  62. mov eax, x
  63. imul y
  64. mov eax, edx
  65. }
  66. }
  67. static __inline short CLIPTOSHORT(int x)
  68. {
  69. int sign;
  70. /* clip to [-32768, 32767] */
  71. sign = x >> 31;
  72. if (sign != (x >> 15))
  73. x = sign ^ ((1 << 15) - 1);
  74. return (short)x;
  75. }
  76. static __inline int FASTABS(int x)
  77. {
  78. int sign;
  79. sign = x >> (sizeof(int) * 8 - 1);
  80. x ^= sign;
  81. x -= sign;
  82. return x;
  83. }
  84. static __inline int CLZ(int x)
  85. {
  86. int numZeros;
  87. if (!x)
  88. return 32;
  89. /* count leading zeros with binary search */
  90. numZeros = 1;
  91. if (!((unsigned int)x >> 16)) { numZeros += 16; x <<= 16; }
  92. if (!((unsigned int)x >> 24)) { numZeros += 8; x <<= 8; }
  93. if (!((unsigned int)x >> 28)) { numZeros += 4; x <<= 4; }
  94. if (!((unsigned int)x >> 30)) { numZeros += 2; x <<= 2; }
  95. numZeros -= ((unsigned int)x >> 31);
  96. return numZeros;
  97. }
  98. #ifdef __CW32__
  99. typedef long long Word64;
  100. #else
  101. typedef __int64 Word64;
  102. #endif
  103. typedef union _U64 {
  104. Word64 w64;
  105. struct {
  106. /* x86 = little endian */
  107. unsigned int lo32;
  108. signed int hi32;
  109. } r;
  110. } U64;
  111. /* returns 64-bit value in [edx:eax] */
  112. static __inline Word64 MADD64(Word64 sum64, int x, int y)
  113. {
  114. #if (defined (_SYMBIAN_61_) || defined (_SYMBIAN_70_)) && defined (__WINS__) && !defined (__CW32__)
  115. /* Workaround for the Symbian emulator because of non existing longlong.lib and
  116. * hence __allmul not defined. */
  117. __asm {
  118. mov eax, x
  119. imul y
  120. add dword ptr sum64, eax
  121. adc dword ptr sum64 + 4, edx
  122. }
  123. #else
  124. sum64 += (Word64)x * (Word64)y;
  125. #endif
  126. return sum64;
  127. }
  128. /* toolchain: MSFT Embedded Visual C++
  129. * target architecture: ARM v.4 and above (require 'M' type processor for 32x32->64 multiplier)
  130. */
  131. #elif defined (_WIN32) && defined (_WIN32_WCE) && defined (ARM)
  132. static __inline short CLIPTOSHORT(int x)
  133. {
  134. int sign;
  135. /* clip to [-32768, 32767] */
  136. sign = x >> 31;
  137. if (sign != (x >> 15))
  138. x = sign ^ ((1 << 15) - 1);
  139. return (short)x;
  140. }
  141. static __inline int FASTABS(int x)
  142. {
  143. int sign;
  144. sign = x >> (sizeof(int) * 8 - 1);
  145. x ^= sign;
  146. x -= sign;
  147. return x;
  148. }
  149. static __inline int CLZ(int x)
  150. {
  151. int numZeros;
  152. if (!x)
  153. return 32;
  154. /* count leading zeros with binary search (function should be 17 ARM instructions total) */
  155. numZeros = 1;
  156. if (!((unsigned int)x >> 16)) { numZeros += 16; x <<= 16; }
  157. if (!((unsigned int)x >> 24)) { numZeros += 8; x <<= 8; }
  158. if (!((unsigned int)x >> 28)) { numZeros += 4; x <<= 4; }
  159. if (!((unsigned int)x >> 30)) { numZeros += 2; x <<= 2; }
  160. numZeros -= ((unsigned int)x >> 31);
  161. return numZeros;
  162. }
  163. /* implemented in asmfunc.s */
  164. #ifdef __cplusplus
  165. extern "C" {
  166. #endif
  167. typedef __int64 Word64;
  168. typedef union _U64 {
  169. Word64 w64;
  170. struct {
  171. /* ARM WinCE = little endian */
  172. unsigned int lo32;
  173. signed int hi32;
  174. } r;
  175. } U64;
  176. /* manual name mangling for just this platform (must match labels in .s file) */
  177. #define MULSHIFT32 raac_MULSHIFT32
  178. #define MADD64 raac_MADD64
  179. int MULSHIFT32(int x, int y);
  180. Word64 MADD64(Word64 sum64, int x, int y);
  181. #ifdef __cplusplus
  182. }
  183. #endif
  184. /* toolchain: ARM ADS or RealView
  185. * target architecture: ARM v.4 and above (requires 'M' type processor for 32x32->64 multiplier)
  186. */
  187. #elif defined (XXX__arm) && defined (__ARMCC_VERSION)
  188. static __inline int MULSHIFT32(int x, int y)
  189. {
  190. /* rules for smull RdLo, RdHi, Rm, Rs:
  191. * RdHi != Rm
  192. * RdLo != Rm
  193. * RdHi != RdLo
  194. */
  195. int zlow;
  196. __asm {
  197. smull zlow,y,x,y
  198. }
  199. return y;
  200. }
  201. static __inline short CLIPTOSHORT(int x)
  202. {
  203. int sign;
  204. /* clip to [-32768, 32767] */
  205. sign = x >> 31;
  206. if (sign != (x >> 15))
  207. x = sign ^ ((1 << 15) - 1);
  208. return (short)x;
  209. }
  210. static __inline int FASTABS(int x)
  211. {
  212. int sign;
  213. sign = x >> (sizeof(int) * 8 - 1);
  214. x ^= sign;
  215. x -= sign;
  216. return x;
  217. }
  218. static __inline int CLZ(int x)
  219. {
  220. int numZeros;
  221. if (!x)
  222. return 32;
  223. /* count leading zeros with binary search (function should be 17 ARM instructions total) */
  224. numZeros = 1;
  225. if (!((unsigned int)x >> 16)) { numZeros += 16; x <<= 16; }
  226. if (!((unsigned int)x >> 24)) { numZeros += 8; x <<= 8; }
  227. if (!((unsigned int)x >> 28)) { numZeros += 4; x <<= 4; }
  228. if (!((unsigned int)x >> 30)) { numZeros += 2; x <<= 2; }
  229. numZeros -= ((unsigned int)x >> 31);
  230. return numZeros;
  231. /* ARM code would look like this, but do NOT use inline asm in ADS for this,
  232. because you can't safely use the status register flags intermixed with C code
  233. __asm {
  234. mov numZeros, #1
  235. tst x, 0xffff0000
  236. addeq numZeros, numZeros, #16
  237. moveq x, x, lsl #16
  238. tst x, 0xff000000
  239. addeq numZeros, numZeros, #8
  240. moveq x, x, lsl #8
  241. tst x, 0xf0000000
  242. addeq numZeros, numZeros, #4
  243. moveq x, x, lsl #4
  244. tst x, 0xc0000000
  245. addeq numZeros, numZeros, #2
  246. moveq x, x, lsl #2
  247. sub numZeros, numZeros, x, lsr #31
  248. }
  249. */
  250. /* reference:
  251. numZeros = 0;
  252. while (!(x & 0x80000000)) {
  253. numZeros++;
  254. x <<= 1;
  255. }
  256. */
  257. }
  258. typedef __int64 Word64;
  259. typedef union _U64 {
  260. Word64 w64;
  261. struct {
  262. /* ARM ADS = little endian */
  263. unsigned int lo32;
  264. signed int hi32;
  265. } r;
  266. } U64;
  267. static __inline Word64 MADD64(Word64 sum64, int x, int y)
  268. {
  269. U64 u;
  270. u.w64 = sum64;
  271. __asm {
  272. smlal u.r.lo32, u.r.hi32, x, y
  273. }
  274. return u.w64;
  275. }
  276. /* toolchain: ARM gcc
  277. * target architecture: ARM v.4 and above (requires 'M' type processor for 32x32->64 multiplier)
  278. */
  279. #elif defined(__GNUC__) && defined(XXXX__arm__)
  280. static inline int MULSHIFT32(int x, int y)
  281. {
  282. int zlow;
  283. asm ("smull %0,%1,%2,%3" : "=&r" (zlow), "=r" (y) : "r" (x), "1" (y) : "cc");
  284. return y;
  285. }
  286. /*
  287. static inline short CLIPTOSHORT(int x)
  288. {
  289. int sign;
  290. // clip to [-32768, 32767] //
  291. sign = x >> 31;
  292. if (sign != (x >> 15))
  293. x = sign ^ ((1 << 15) - 1);
  294. return (short)x;
  295. }
  296. */
  297. static inline short CLIPTOSHORT(int x)
  298. {
  299. asm ("ssat %0, #16, %1" : "=r" (x) : "r" (x));
  300. return x;
  301. }
  302. /* From coder.h, ORIGINAL:
  303. clip to [-2^n, 2^n-1], valid range of n = [1, 30]
  304. //TODO (FB) Is there a better way ?
  305. */
  306. #define CLIP_2N(y, n) { \
  307. int sign = (y) >> 31; \
  308. if (sign != (y) >> (n)) { \
  309. (y) = sign ^ ((1 << (n)) - 1); \
  310. } \
  311. }
  312. /* From coder.h, ORIGINAL:
  313. do y <<= n, clipping to range [-2^30, 2^30 - 1] (i.e. output has one guard bit)
  314. */
  315. //TODO (FB) Is there a better way ?
  316. #define CLIP_2N_SHIFT(y, n) { \
  317. int sign = (y) >> 31; \
  318. if (sign != (y) >> (30 - (n))) { \
  319. (y) = sign ^ (0x3fffffff); \
  320. } else { \
  321. (y) = (y) << (n); \
  322. } \
  323. }
  324. #define FASTABS(x) abs(x) //FB
  325. #define CLZ(x) __builtin_clz(x) //FB
  326. //Reverse byte order (16 bit) //FB
  327. static inline unsigned int REV16( unsigned int value)
  328. {
  329. asm ("rev16 %0, %1" : "=r" (value) : "r" (value) );
  330. return(value);
  331. }
  332. //Reverse byte order (32 bit) //FB
  333. static inline unsigned int REV32( unsigned int value)
  334. {
  335. asm ("rev %0, %1" : "=r" (value) : "r" (value) );
  336. return(value);
  337. }
  338. typedef long long Word64;
  339. typedef union _U64 {
  340. Word64 w64;
  341. struct {
  342. /* little endian */
  343. unsigned int lo32;
  344. signed int hi32;
  345. } r;
  346. } U64;
  347. static inline Word64 MADD64(Word64 sum64, int x, int y)
  348. {
  349. U64 u;
  350. u.w64 = sum64;
  351. asm ("smlal %0,%1,%2,%3" : "+&r" (u.r.lo32), "+&r" (u.r.hi32) : "r" (x), "r" (y) : "cc");
  352. return u.w64;
  353. }
  354. /* toolchain: x86 gcc
  355. * target architecture: x86
  356. */
  357. #elif defined(__APPLE__) || defined(__GNUC__) && (defined(__i386__) || defined(__amd64__)) || (defined (_SOLARIS) && !defined (__GNUC__) && defined(_SOLARISX86))
  358. typedef long long Word64;
  359. static __inline__ int MULSHIFT32(int x, int y)
  360. {
  361. int z;
  362. z = (Word64)x * (Word64)y >> 32;
  363. return z;
  364. }
  365. static __inline short CLIPTOSHORT(int x)
  366. {
  367. int sign;
  368. /* clip to [-32768, 32767] */
  369. sign = x >> 31;
  370. if (sign != (x >> 15))
  371. x = sign ^ ((1 << 15) - 1);
  372. return (short)x;
  373. }
  374. static __inline int FASTABS(int x)
  375. {
  376. int sign;
  377. sign = x >> (sizeof(int) * 8 - 1);
  378. x ^= sign;
  379. x -= sign;
  380. return x;
  381. }
  382. static __inline int CLZ(int x)
  383. {
  384. int numZeros;
  385. if (!x)
  386. return 32;
  387. /* count leading zeros with binary search (function should be 17 ARM instructions total) */
  388. numZeros = 1;
  389. if (!((unsigned int)x >> 16)) { numZeros += 16; x <<= 16; }
  390. if (!((unsigned int)x >> 24)) { numZeros += 8; x <<= 8; }
  391. if (!((unsigned int)x >> 28)) { numZeros += 4; x <<= 4; }
  392. if (!((unsigned int)x >> 30)) { numZeros += 2; x <<= 2; }
  393. numZeros -= ((unsigned int)x >> 31);
  394. return numZeros;
  395. }
  396. typedef union _U64 {
  397. Word64 w64;
  398. struct {
  399. /* x86 = little endian */
  400. unsigned int lo32;
  401. signed int hi32;
  402. } r;
  403. } U64;
  404. static __inline Word64 MADD64(Word64 sum64, int x, int y)
  405. {
  406. sum64 += (Word64)x * (Word64)y;
  407. return sum64;
  408. }
  409. #elif defined(ESP_PLATFORM) || defined(__GNUC__) && (defined(__powerpc__) || defined(__POWERPC__)) || (defined (_SOLARIS) && !defined (__GNUC__) && !defined (_SOLARISX86))
  410. typedef long long Word64;
  411. static __inline__ int MULSHIFT32(int x, int y)
  412. {
  413. int z;
  414. z = (Word64)x * (Word64)y >> 32;
  415. return z;
  416. }
  417. static __inline short CLIPTOSHORT(int x)
  418. {
  419. int sign;
  420. /* clip to [-32768, 32767] */
  421. sign = x >> 31;
  422. if (sign != (x >> 15))
  423. x = sign ^ ((1 << 15) - 1);
  424. return (short)x;
  425. }
  426. static __inline int FASTABS(int x)
  427. {
  428. int sign;
  429. sign = x >> (sizeof(int) * 8 - 1);
  430. x ^= sign;
  431. x -= sign;
  432. return x;
  433. }
  434. static __inline int CLZ(int x)
  435. {
  436. int numZeros;
  437. if (!x)
  438. return 32;
  439. /* count leading zeros with binary search (function should be 17 ARM instructions total) */
  440. numZeros = 1;
  441. if (!((unsigned int)x >> 16)) { numZeros += 16; x <<= 16; }
  442. if (!((unsigned int)x >> 24)) { numZeros += 8; x <<= 8; }
  443. if (!((unsigned int)x >> 28)) { numZeros += 4; x <<= 4; }
  444. if (!((unsigned int)x >> 30)) { numZeros += 2; x <<= 2; }
  445. numZeros -= ((unsigned int)x >> 31);
  446. return numZeros;
  447. }
  448. typedef union _U64 {
  449. Word64 w64;
  450. struct {
  451. #ifdef __XTENSA__
  452. unsigned int lo32;
  453. signed int hi32;
  454. #else
  455. /* PowerPC = big endian */
  456. signed int hi32;
  457. unsigned int lo32;
  458. #endif
  459. } r;
  460. } U64;
  461. static __inline Word64 MADD64(Word64 sum64, int x, int y)
  462. {
  463. sum64 += (Word64)x * (Word64)y;
  464. return sum64;
  465. }
  466. /* From coder.h, ORIGINAL:
  467. clip to [-2^n, 2^n-1], valid range of n = [1, 30]
  468. //TODO (FB) Is there a better way ?
  469. */
  470. #define CLIP_2N(y, n) { \
  471. int sign = (y) >> 31; \
  472. if (sign != (y) >> (n)) { \
  473. (y) = sign ^ ((1 << (n)) - 1); \
  474. } \
  475. }
  476. /* From coder.h, ORIGINAL:
  477. do y <<= n, clipping to range [-2^30, 2^30 - 1] (i.e. output has one guard bit)
  478. */
  479. //TODO (FB) Is there a better way ?
  480. #define CLIP_2N_SHIFT(y, n) { \
  481. int sign = (y) >> 31; \
  482. if (sign != (y) >> (30 - (n))) { \
  483. (y) = sign ^ (0x3fffffff); \
  484. } else { \
  485. (y) = (y) << (n); \
  486. } \
  487. }
  488. //#define FASTABS(x) abs(x) //FB
  489. //#define CLZ(x) __builtin_clz(x) //FB
  490. #else
  491. #error Unsupported platform in assembly.h
  492. #endif /* platforms */
  493. #ifndef CLIP_2N
  494. #define CLIP_2N(y, n) { \
  495. int sign = (y) >> 31; \
  496. if (sign != (y) >> (n)) { \
  497. (y) = sign ^ ((1 << (n)) - 1); \
  498. } \
  499. }
  500. #endif
  501. #ifndef CLIP_2N_SHIFT
  502. /* From coder.h, ORIGINAL:
  503. do y <<= n, clipping to range [-2^30, 2^30 - 1] (i.e. output has one guard bit)
  504. */
  505. //TODO (FB) Is there a better way ?
  506. #define CLIP_2N_SHIFT(y, n) { \
  507. int sign = (y) >> 31; \
  508. if (sign != (y) >> (30 - (n))) { \
  509. (y) = sign ^ (0x3fffffff); \
  510. } else { \
  511. (y) = (y) << (n); \
  512. } \
  513. }
  514. #endif
  515. #endif /* _ASSEMBLY_H */