assembly.h

Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK ***** 
00002  * Version: RCSL 1.0/RPSL 1.0 
00003  *  
00004  * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved. 
00005  *      
00006  * The contents of this file, and the files included with this file, are 
00007  * subject to the current version of the RealNetworks Public Source License 
00008  * Version 1.0 (the "RPSL") available at 
00009  * http://www.helixcommunity.org/content/rpsl unless you have licensed 
00010  * the file under the RealNetworks Community Source License Version 1.0 
00011  * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl, 
00012  * in which case the RCSL will apply. You may also obtain the license terms 
00013  * directly from RealNetworks.  You may not use this file except in 
00014  * compliance with the RPSL or, if you have a valid RCSL with RealNetworks 
00015  * applicable to this file, the RCSL.  Please see the applicable RPSL or 
00016  * RCSL for the rights, obligations and limitations governing use of the 
00017  * contents of the file.  
00018  *  
00019  * This file is part of the Helix DNA Technology. RealNetworks is the 
00020  * developer of the Original Code and owns the copyrights in the portions 
00021  * it created. 
00022  *  
00023  * This file, and the files included with this file, is distributed and made 
00024  * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
00025  * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
00026  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS 
00027  * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
00028  * 
00029  * Technology Compatibility Kit Test Suite(s) Location: 
00030  *    http://www.helixcommunity.org/content/tck 
00031  * 
00032  * Contributor(s): 
00033  *  
00034  * ***** END LICENSE BLOCK ***** */ 
00035 
00036 /**************************************************************************************
00037  * Fixed-point MP3 decoder
00038  * Jon Recker (jrecker@real.com), Ken Cooke (kenc@real.com)
00039  * June 2003
00040  *
00041  * assembly.h - assembly language functions and prototypes for supported platforms
00042  *
00043  * - inline rountines with access to 64-bit multiply results 
00044  * - x86 (_WIN32) and ARM (ARM_ADS, _WIN32_WCE) versions included
00045  * - some inline functions are mix of asm and C for speed
00046  * - some functions are in native asm files, so only the prototype is given here
00047  *
00048  * MULSHIFT32(x, y)    signed multiply of two 32-bit integers (x and y), returns top 32 bits of 64-bit result
00049  * FASTABS(x)          branchless absolute value of signed integer x
00050  * CLZ(x)              count leading zeros in x
00051  * MADD64(sum, x, y)   (Windows only) sum [64-bit] += x [32-bit] * y [32-bit]
00052  * SHL64(sum, x, y)    (Windows only) 64-bit left shift using __int64
00053  * SAR64(sum, x, y)    (Windows only) 64-bit right shift using __int64
00054  */
00055 
00056 #ifndef _ASSEMBLY_H
00057 #define _ASSEMBLY_H
00058 
00059 #if (defined _WIN32 && !defined _WIN32_WCE) || (defined __WINS__ && defined _SYMBIAN) || defined(_OPENWAVE_SIMULATOR) || defined(WINCE_EMULATOR)    /* Symbian emulator for Ix86 */
00060 
00061 #pragma warning( disable : 4035 )   /* complains about inline asm not returning a value */
00062 
00063 static __inline int MULSHIFT32(int x, int y)    
00064 {
00065     __asm {
00066         mov     eax, x
00067         imul    y
00068         mov     eax, edx
00069     }
00070 }
00071 
00072 static __inline int FASTABS(int x) 
00073 {
00074     int sign;
00075 
00076     sign = x >> (sizeof(int) * 8 - 1);
00077     x ^= sign;
00078     x -= sign;
00079 
00080     return x;
00081 }
00082 
00083 static __inline int CLZ(int x)
00084 {
00085     int numZeros;
00086 
00087     if (!x)
00088         return (sizeof(int) * 8);
00089 
00090     numZeros = 0;
00091     while (!(x & 0x80000000)) {
00092         numZeros++;
00093         x <<= 1;
00094     } 
00095 
00096     return numZeros;
00097 }
00098 
00099 /* MADD64, SHL64, SAR64:
00100  * write in assembly to avoid dependency on run-time lib for 64-bit shifts, muls
00101  *  (sometimes compiler thunks to function calls instead of code generating)
00102  * required for Symbian emulator
00103  */
00104 static __inline __int64 MADD64(__int64 sum, int x, int y)
00105 {
00106     unsigned int sumLo = ((unsigned int *)&sum)[0];
00107     int sumHi = ((int *)&sum)[1];
00108 
00109     __asm {
00110         mov     eax, x
00111         imul    y
00112         add     eax, sumLo
00113         adc     edx, sumHi
00114     }
00115 
00116     /* equivalent to return (sum + ((__int64)x * y)); */
00117 }
00118 
00119 static __inline __int64 SHL64(__int64 x, int n)
00120 {
00121     unsigned int xLo = ((unsigned int *)&x)[0];
00122     int xHi = ((int *)&x)[1];
00123     unsigned char nb = (unsigned char)n;
00124 
00125     if (n < 32) {
00126         __asm {
00127             mov     edx, xHi
00128             mov     eax, xLo
00129             mov     cl, nb
00130             shld    edx, eax, cl
00131             shl     eax, cl
00132         }
00133     } else if (n < 64) {
00134         /* shl masks cl to 0x1f */
00135         __asm {
00136             mov     edx, xLo
00137             mov     cl, nb
00138             xor     eax, eax
00139             shl     edx, cl
00140         }
00141     } else {
00142         __asm {
00143             xor     edx, edx
00144             xor     eax, eax
00145         }
00146     }
00147 }
00148 
00149 static __inline __int64 SAR64(__int64 x, int n)
00150 {
00151     unsigned int xLo = ((unsigned int *)&x)[0];
00152     int xHi = ((int *)&x)[1];
00153     unsigned char nb = (unsigned char)n;
00154 
00155     if (n < 32) {
00156         __asm {
00157             mov     edx, xHi
00158             mov     eax, xLo
00159             mov     cl, nb
00160             shrd    eax, edx, cl
00161             sar     edx, cl
00162         }
00163     } else if (n < 64) {
00164         /* sar masks cl to 0x1f */
00165         __asm {
00166             mov     edx, xHi
00167             mov     eax, xHi
00168             mov     cl, nb
00169             sar     edx, 31
00170             sar     eax, cl
00171         }
00172     } else {
00173         __asm {
00174             sar     xHi, 31
00175             mov     eax, xHi
00176             mov     edx, xHi
00177         }
00178     }
00179 }
00180 
00181 #elif (defined _WIN32) && (defined _WIN32_WCE)
00182 
00183 /* use asm function for now (EVC++ 3.0 does horrible job compiling __int64 version) */
00184 #define MULSHIFT32  xmp3_MULSHIFT32
00185 int MULSHIFT32(int x, int y);
00186 
00187 static __inline int FASTABS(int x) 
00188 {
00189     int sign;
00190 
00191     sign = x >> (sizeof(int) * 8 - 1);
00192     x ^= sign;
00193     x -= sign;
00194 
00195     return x;
00196 }
00197 
00198 static __inline int CLZ(int x)
00199 {
00200     int numZeros;
00201 
00202     if (!x)
00203         return (sizeof(int) * 8);
00204 
00205     numZeros = 0;
00206     while (!(x & 0x80000000)) {
00207         numZeros++;
00208         x <<= 1;
00209     } 
00210 
00211     return numZeros;
00212 }
00213 
00214 #elif defined ARM_ADS
00215 
00216 static __inline int MULSHIFT32(int x, int y)
00217 {
00218     /* important rules for smull RdLo, RdHi, Rm, Rs:
00219      *     RdHi and Rm can't be the same register
00220      *     RdLo and Rm can't be the same register
00221      *     RdHi and RdLo can't be the same register
00222      * Note: Rs determines early termination (leading sign bits) so if you want to specify
00223      *   which operand is Rs, put it in the SECOND argument (y)
00224      * For inline assembly, x and y are not assumed to be R0, R1 so it shouldn't matter
00225      *   which one is returned. (If this were a function call, returning y (R1) would 
00226      *   require an extra "mov r0, r1")
00227      */
00228     int zlow;
00229     __asm {
00230         smull zlow,y,x,y
00231     }
00232 
00233     return y;
00234 }
00235 
00236 static __inline int FASTABS(int x) 
00237 {
00238     int t;
00239 
00240     __asm {
00241         eor t, x, x, asr #31
00242         sub t, t, x, asr #31
00243     }
00244 
00245     return t;
00246 }
00247 
00248 static __inline int CLZ(int x)
00249 {
00250     int numZeros;
00251 
00252     if (!x)
00253         return (sizeof(int) * 8);
00254 
00255     numZeros = 0;
00256     while (!(x & 0x80000000)) {
00257         numZeros++;
00258         x <<= 1;
00259     } 
00260 
00261     return numZeros;
00262 }
00263 
00264 #elif defined(__GNUC__) && defined(__arm__)
00265 
00266 static __inline int MULSHIFT32(int x, int y)
00267 {
00268     /* important rules for smull RdLo, RdHi, Rm, Rs:
00269      *     RdHi and Rm can't be the same register
00270      *     RdLo and Rm can't be the same register
00271      *     RdHi and RdLo can't be the same register
00272      * Note: Rs determines early termination (leading sign bits) so if you want to specify
00273      *   which operand is Rs, put it in the SECOND argument (y)
00274      * For inline assembly, x and y are not assumed to be R0, R1 so it shouldn't matter
00275      *   which one is returned. (If this were a function call, returning y (R1) would 
00276      *   require an extra "mov r0, r1")
00277      */
00278     int zlow;
00279     __asm__ volatile ("smull %0,%1,%2,%3" : "=&r" (zlow), "=r" (y) : "r" (x), "1" (y)) ;
00280 
00281     return y;
00282 }
00283 
00284 static __inline int FASTABS(int x) 
00285 {
00286     int t = 0;
00287 
00288     __asm__ volatile (
00289         "eor %0,%2,%2, asr #31;"
00290         "sub %0,%1,%2, asr #31;"
00291         : "=&r" (t) 
00292         : "0" (t), "r" (x)
00293      );
00294 
00295     return t;
00296 }
00297 
00298 static __inline int CLZ(int x)
00299 {
00300     int numZeros;
00301 
00302     if (!x)
00303         return (sizeof(int) * 8);
00304 
00305     numZeros = 0;
00306     while (!(x & 0x80000000)) {
00307         numZeros++;
00308         x <<= 1;
00309     } 
00310 
00311     return numZeros;
00312 }
00313 
00314 #else
00315 
00316 #error Unsupported platform in assembly.h
00317 
00318 #endif  /* platforms */
00319 
00320 #endif /* _ASSEMBLY_H */

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/