Coverage Report

Created: 2026-03-25 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libtorrent/src/cpuid.cpp
Line
Count
Source
1
/*
2
3
Copyright (c) 2015-2017, 2019-2020, Arvid Norberg
4
Copyright (c) 2016-2018, Alden Torres
5
Copyright (c) 2016, Andrei Kurushin
6
Copyright (c) 2018, Alexandre Janniaux
7
All rights reserved.
8
9
Redistribution and use in source and binary forms, with or without
10
modification, are permitted provided that the following conditions
11
are met:
12
13
    * Redistributions of source code must retain the above copyright
14
      notice, this list of conditions and the following disclaimer.
15
    * Redistributions in binary form must reproduce the above copyright
16
      notice, this list of conditions and the following disclaimer in
17
      the documentation and/or other materials provided with the distribution.
18
    * Neither the name of the author nor the names of its
19
      contributors may be used to endorse or promote products derived
20
      from this software without specific prior written permission.
21
22
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
POSSIBILITY OF SUCH DAMAGE.
33
34
*/
35
36
#include "libtorrent/config.hpp"
37
#include "libtorrent/aux_/cpuid.hpp"
38
39
#include <cstdint>
40
41
#if defined _MSC_VER && TORRENT_HAS_SSE
42
#include <intrin.h>
43
#include <nmmintrin.h>
44
#endif
45
46
#if TORRENT_HAS_SSE && defined __GNUC__
47
#include <cpuid.h>
48
#else
49
#include <cstring> // for std::memset
50
#endif
51
52
#if defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16))
53
#define TORRENT_HAS_AUXV 1
54
#elif defined TORRENT_ANDROID
55
#define TORRENT_HAS_AUXV 1
56
#else
57
#define TORRENT_HAS_AUXV 0
58
#endif
59
60
#if TORRENT_HAS_ARM && TORRENT_HAS_AUXV
61
#if defined TORRENT_ANDROID
62
#include <dlfcn.h>
63
namespace {
64
unsigned long int helper_getauxval(unsigned long int type)
65
{
66
    using getauxval_t = unsigned long int(*)(unsigned long int);
67
    getauxval_t pf_getauxval = reinterpret_cast<getauxval_t>(dlsym(RTLD_DEFAULT, "getauxval"));
68
    if (pf_getauxval == nullptr)
69
        return 0;
70
    return pf_getauxval(type);
71
}
72
}
73
#else // TORRENT_ANDROID
74
#include <sys/auxv.h>
75
#define helper_getauxval getauxval
76
#endif
77
#endif // TORRENT_HAS_ARM && TORRENT_HAS_AUXV
78
79
namespace libtorrent {
80
namespace aux {
81
namespace {
82
83
#if TORRENT_HAS_SSE
84
  // internal
85
  void cpuid(std::uint32_t* info, int type) noexcept
86
2
  {
87
#if defined _MSC_VER
88
    __cpuid(reinterpret_cast<int*>(info), type);
89
90
#elif defined __GNUC__
91
    __get_cpuid(std::uint32_t(type), &info[0], &info[1], &info[2], &info[3]);
92
#else
93
    TORRENT_UNUSED(type);
94
    // for non-x86 and non-amd64, just return zeroes
95
    std::memset(&info[0], 0, sizeof(std::uint32_t) * 4);
96
#endif
97
2
  }
98
#endif
99
100
  bool supports_sse42() noexcept
101
1
  {
102
1
#if TORRENT_HAS_SSE
103
1
    std::uint32_t cpui[4] = {0};
104
1
    cpuid(cpui, 1);
105
1
    return (cpui[2] & (1 << 20)) != 0;
106
#else
107
    return false;
108
#endif
109
1
  }
110
111
  bool supports_mmx() noexcept
112
1
  {
113
1
#if TORRENT_HAS_SSE
114
1
    std::uint32_t cpui[4] = {0};
115
1
    cpuid(cpui, 1);
116
1
    return (cpui[2] & (1 << 23)) != 0;
117
#else
118
    return false;
119
#endif
120
1
  }
121
122
  bool supports_arm_neon() noexcept
123
1
  {
124
#if TORRENT_HAS_ARM_NEON && TORRENT_HAS_AUXV
125
#if defined __arm__
126
    //return (getauxval(AT_HWCAP) & HWCAP_NEON);
127
    return (helper_getauxval(16) & (1 << 12));
128
#elif defined __aarch64__
129
    //return (getauxval(AT_HWCAP) & HWCAP_ASIMD);
130
    //return (getauxval(16) & (1 << 1));
131
    // TODO: enable when aarch64 is really tested
132
    return false;
133
#endif
134
#else
135
1
    return false;
136
1
#endif
137
1
  }
138
139
  bool supports_arm_crc32c() noexcept
140
1
  {
141
#if TORRENT_HAS_ARM_CRC32 && TORRENT_HAS_AUXV
142
#if defined TORRENT_FORCE_ARM_CRC32
143
    return true;
144
#elif defined __arm__
145
    //return (getauxval(AT_HWCAP2) & HWCAP2_CRC32);
146
    return (helper_getauxval(26) & (1 << 4));
147
#elif defined __aarch64__
148
    //return (getauxval(AT_HWCAP) & HWCAP_CRC32);
149
    return (helper_getauxval(16) & (1 << 7));
150
#endif
151
#else
152
1
    return false;
153
1
#endif
154
1
  }
155
156
} // anonymous namespace
157
158
  bool const sse42_support = supports_sse42();
159
  bool const mmx_support = supports_mmx();
160
  bool const arm_neon_support = supports_arm_neon();
161
  bool const arm_crc32c_support = supports_arm_crc32c();
162
} }