aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/boost/atomic/src/bit_operation_tools.hpp
blob: 2c114b92aa2b92c2d1caf48868cf787042edfe85 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE_1_0.txt or copy at
 * http://www.boost.org/LICENSE_1_0.txt)
 *
 * Copyright (c) 2020 Andrey Semashev
 */
/*!
 * \file   bit_operation_tools.hpp
 *
 * This file contains bit operation tools
 */

#ifndef BOOST_ATOMIC_BIT_OPERATION_TOOLS_HPP_INCLUDED_
#define BOOST_ATOMIC_BIT_OPERATION_TOOLS_HPP_INCLUDED_

#include <boost/predef/architecture/x86.h>

#if BOOST_ARCH_X86

#include <boost/atomic/detail/config.hpp>
#include <boost/atomic/detail/header.hpp>

#if defined(_MSC_VER)
extern "C" unsigned char _BitScanForward(unsigned long* index, unsigned long x);
#if defined(BOOST_MSVC)
#pragma intrinsic(_BitScanForward)
#endif
#endif

namespace boost {
namespace atomics {
namespace detail {

//! Counts trailing zero bits
BOOST_FORCEINLINE unsigned int count_trailing_zeros(unsigned int x)
{
#if defined(__GNUC__)
    return __builtin_ctz(x);
#elif defined(_MSC_VER)
    unsigned long index;
    _BitScanForward(&index, x);
    return static_cast< unsigned int >(index);
#else
    unsigned int index = 0u;
    if ((x & 0xFFFF) == 0u)
    {
        x >>= 16;
        index += 16u;
    }
    if ((x & 0xFF) == 0u)
    {
        x >>= 8;
        index += 8u;
    }
    if ((x & 0xF) == 0u)
    {
        x >>= 4;
        index += 4u;
    }
    if ((x & 0x3) == 0u)
    {
        x >>= 2;
        index += 2u;
    }
    if ((x & 0x1) == 0u)
    {
        index += 1u;
    }
    return index;
#endif
}

} // namespace detail
} // namespace atomics
} // namespace boost

#include <boost/atomic/detail/footer.hpp>

#endif // BOOST_ARCH_X86

#endif // BOOST_ATOMIC_BIT_OPERATION_TOOLS_HPP_INCLUDED_