1 /* This file is part of the Vc library.
3 Copyright (C) 2011-2012 Matthias Kretz <kretz@kde.org>
5 Vc is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation, either version 3 of
8 the License, or (at your option) any later version.
10 Vc is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with Vc. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef VC_COMMON_BITSCANINTRINSICS_H
21 #define VC_COMMON_BITSCANINTRINSICS_H
23 #if defined(VC_GCC) || defined(VC_CLANG)
24 # if VC_GCC >= 0x40500
25 // GCC 4.5.0 introduced _bit_scan_forward / _bit_scan_reverse
26 # include <x86intrin.h>
28 // GCC <= 4.4 and clang have x86intrin.h, but not the required functions
29 # define _bit_scan_forward(x) __builtin_ctz(x)
30 static inline int _Vc_bit_scan_reverse_asm(unsigned int x) {
32 __asm__("bsr %1,%0" : "=r"(r) : "X"(x));
35 # define _bit_scan_reverse(x) _Vc_bit_scan_reverse_asm(x)
38 // for all I know ICC supports the _bit_scan_* intrinsics
39 #elif defined(VC_OPEN64)
41 #elif defined(VC_MSVC)
42 #include "windows_fix_intrin.h"
43 #pragma intrinsic(_BitScanForward)
44 #pragma intrinsic(_BitScanReverse)
45 static inline __forceinline unsigned long _bit_scan_forward(unsigned long x) {
47 _BitScanForward(&index, x);
50 static inline __forceinline unsigned long _bit_scan_reverse(unsigned long x) {
52 _BitScanReverse(&index, x);
56 // just assume the compiler can do it
60 #endif // VC_COMMON_BITSCANINTRINSICS_H