cannam@154: /* Copyright (c) 2003-2008 Timothy B. Terriberry cannam@154: Copyright (c) 2008 Xiph.Org Foundation */ cannam@154: /* cannam@154: Redistribution and use in source and binary forms, with or without cannam@154: modification, are permitted provided that the following conditions cannam@154: are met: cannam@154: cannam@154: - Redistributions of source code must retain the above copyright cannam@154: notice, this list of conditions and the following disclaimer. cannam@154: cannam@154: - Redistributions in binary form must reproduce the above copyright cannam@154: notice, this list of conditions and the following disclaimer in the cannam@154: documentation and/or other materials provided with the distribution. cannam@154: cannam@154: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS cannam@154: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT cannam@154: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR cannam@154: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER cannam@154: OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, cannam@154: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, cannam@154: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR cannam@154: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF cannam@154: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING cannam@154: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS cannam@154: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. cannam@154: */ cannam@154: cannam@154: /*Some common macros for potential platform-specific optimization.*/ cannam@154: #include "opus_types.h" cannam@154: #include cannam@154: #include cannam@154: #include "arch.h" cannam@154: #if !defined(_ecintrin_H) cannam@154: # define _ecintrin_H (1) cannam@154: cannam@154: /*Some specific platforms may have optimized intrinsic or OPUS_INLINE assembly cannam@154: versions of these functions which can substantially improve performance. cannam@154: We define macros for them to allow easy incorporation of these non-ANSI cannam@154: features.*/ cannam@154: cannam@154: /*Modern gcc (4.x) can compile the naive versions of min and max with cmov if cannam@154: given an appropriate architecture, but the branchless bit-twiddling versions cannam@154: are just as fast, and do not require any special target architecture. cannam@154: Earlier gcc versions (3.x) compiled both code to the same assembly cannam@154: instructions, because of the way they represented ((_b)>(_a)) internally.*/ cannam@154: # define EC_MINI(_a,_b) ((_a)+(((_b)-(_a))&-((_b)<(_a)))) cannam@154: cannam@154: /*Count leading zeros. cannam@154: This macro should only be used for implementing ec_ilog(), if it is defined. cannam@154: All other code should use EC_ILOG() instead.*/ cannam@154: #if defined(_MSC_VER) && (_MSC_VER >= 1400) cannam@154: # include cannam@154: /*In _DEBUG mode this is not an intrinsic by default.*/ cannam@154: # pragma intrinsic(_BitScanReverse) cannam@154: cannam@154: static __inline int ec_bsr(unsigned long _x){ cannam@154: unsigned long ret; cannam@154: _BitScanReverse(&ret,_x); cannam@154: return (int)ret; cannam@154: } cannam@154: # define EC_CLZ0 (1) cannam@154: # define EC_CLZ(_x) (-ec_bsr(_x)) cannam@154: #elif defined(ENABLE_TI_DSPLIB) cannam@154: # include "dsplib.h" cannam@154: # define EC_CLZ0 (31) cannam@154: # define EC_CLZ(_x) (_lnorm(_x)) cannam@154: #elif __GNUC_PREREQ(3,4) cannam@154: # if INT_MAX>=2147483647 cannam@154: # define EC_CLZ0 ((int)sizeof(unsigned)*CHAR_BIT) cannam@154: # define EC_CLZ(_x) (__builtin_clz(_x)) cannam@154: # elif LONG_MAX>=2147483647L cannam@154: # define EC_CLZ0 ((int)sizeof(unsigned long)*CHAR_BIT) cannam@154: # define EC_CLZ(_x) (__builtin_clzl(_x)) cannam@154: # endif cannam@154: #endif cannam@154: cannam@154: #if defined(EC_CLZ) cannam@154: /*Note that __builtin_clz is not defined when _x==0, according to the gcc cannam@154: documentation (and that of the BSR instruction that implements it on x86). cannam@154: The majority of the time we can never pass it zero. cannam@154: When we need to, it can be special cased.*/ cannam@154: # define EC_ILOG(_x) (EC_CLZ0-EC_CLZ(_x)) cannam@154: #else cannam@154: int ec_ilog(opus_uint32 _v); cannam@154: # define EC_ILOG(_x) (ec_ilog(_x)) cannam@154: #endif cannam@154: #endif