1 /* Copyright (c) 2003-2008 Timothy B. Terriberry
2 Copyright (c) 2008 Xiph.Org Foundation */
3 /*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /*Some common macros for potential platform-specific optimization.*/
29 #include "opus_types.h"
30 #include <math.h>
31 #include <limits.h>
32 #include "arch.h"
33 #if !defined(_ecintrin_H)
34 # define _ecintrin_H (1)
35
36 /*Some specific platforms may have optimized intrinsic or OPUS_INLINE assembly
37 versions of these functions which can substantially improve performance.
38 We define macros for them to allow easy incorporation of these non-ANSI
39 features.*/
40
41 /*Modern gcc (4.x) can compile the naive versions of min and max with cmov if
42 given an appropriate architecture, but the branchless bit-twiddling versions
43 are just as fast, and do not require any special target architecture.
44 Earlier gcc versions (3.x) compiled both code to the same assembly
45 instructions, because of the way they represented ((_b)>(_a)) internally.*/
46 # define EC_MINI(_a,_b) ((_a)+(((_b)-(_a))&-((_b)<(_a))))
47
48 /*Count leading zeros.
49 This macro should only be used for implementing ec_ilog(), if it is defined.
50 All other code should use EC_ILOG() instead.*/
51 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
52 #if defined(_MSC_VER) && (_MSC_VER >= 1910)
53 # include <intrin0.h> /* Improve compiler throughput. */
54 #else
55 # include <intrin.h>
56 #endif
57 /*In _DEBUG mode this is not an intrinsic by default.*/
58 # pragma intrinsic(_BitScanReverse)
59
ec_bsr(unsigned long _x)60 static __inline int ec_bsr(unsigned long _x){
61 unsigned long ret;
62 _BitScanReverse(&ret,_x);
63 return (int)ret;
64 }
65 # define EC_CLZ0 (1)
66 # define EC_CLZ(_x) (-ec_bsr(_x))
67 #elif defined(ENABLE_TI_DSPLIB)
68 # include "dsplib.h"
69 # define EC_CLZ0 (31)
70 # define EC_CLZ(_x) (_lnorm(_x))
71 #elif __GNUC_PREREQ(3,4)
72 # if INT_MAX>=2147483647
73 # define EC_CLZ0 ((int)sizeof(unsigned)*CHAR_BIT)
74 # define EC_CLZ(_x) (__builtin_clz(_x))
75 # elif LONG_MAX>=2147483647L
76 # define EC_CLZ0 ((int)sizeof(unsigned long)*CHAR_BIT)
77 # define EC_CLZ(_x) (__builtin_clzl(_x))
78 # endif
79 #endif
80
81 #if defined(EC_CLZ)
82 /*Note that __builtin_clz is not defined when _x==0, according to the gcc
83 documentation (and that of the BSR instruction that implements it on x86).
84 The majority of the time we can never pass it zero.
85 When we need to, it can be special cased.*/
86 # define EC_ILOG(_x) (EC_CLZ0-EC_CLZ(_x))
87 #else
88 int ec_ilog(opus_uint32 _v);
89 # define EC_ILOG(_x) (ec_ilog(_x))
90 #endif
91 #endif
92