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