File: SimdBaseCpu.cpp

package info (click to toggle)
visp 3.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 119,296 kB
  • sloc: cpp: 500,914; ansic: 52,904; xml: 22,642; python: 7,365; java: 4,247; sh: 482; makefile: 237; objc: 145
file content (243 lines) | stat: -rwxr-xr-x 7,881 bytes parent folder | download
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Simd Library (http://ermig1979.github.io/Simd).
*
* Copyright (c) 2011-2022 Yermalayeu Ihar,
*               2022-2022 Souriya Trinh,
*               2022-2022 Fabien Spindler.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "Simd/SimdCpu.h"

#include <stdio.h>
#include <vector>
#ifdef SIMD_CPP_2011_ENABLE
#include <thread>
#endif
#include <sstream>
#include <iostream>

#if defined(_MSC_VER)

#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <intrin.h>

#elif defined(__GNUC__)
#include <unistd.h>
#include <stdbool.h>
#include <stdlib.h>

#if defined(SIMD_X86_ENABLE) || defined(SIMD_X64_ENABLE)
#include <cpuid.h>
#endif

#if defined(SIMD_PPC_ENABLE) || defined(SIMD_PPC64_ENABLE) || defined(SIMD_ARM_ENABLE) || defined(SIMD_ARM64_ENABLE)
#include <fcntl.h>
#if !defined(__APPLE__)
#include <sys/auxv.h>
#if (defined(SIMD_ARM_ENABLE) || defined(SIMD_ARM64_ENABLE)) && !defined(__FreeBSD__)
#include <asm/hwcap.h>
#endif
#endif
#endif

#ifndef PATH_MAX
#ifdef _MAX_PATH
#define PATH_MAX _MAX_PATH
#else
#define PATH_MAX 1024
#endif
#endif

#else
# error Do not know how to detect CPU info
#endif

namespace Simd {
  namespace Base {
#if defined(SIMD_X86_ENABLE) || defined(SIMD_X64_ENABLE)
    bool CheckBit(int eax, int ecx, Cpuid::Register index, Cpuid::Bit bit) {
      unsigned int registers[4] = { 0, 0, 0, 0 };
#if defined(_MSC_VER)
      __cpuidex((int*)registers, eax, ecx);
#elif (defined __GNUC__)
      if (__get_cpuid_max(0, NULL) < eax)
        return false;
      __cpuid_count(eax, ecx,
                    registers[Cpuid::Eax],
                    registers[Cpuid::Ebx],
                    registers[Cpuid::Ecx],
                    registers[Cpuid::Edx]);
#else
#error Do not know how to detect CPU info!
#endif
      return (registers[index] & bit) == bit;
      }
#endif//defined(SIMD_X86_ENABLE) || defined(SIMD_X64_ENABLE)

#if defined(__GNUC__) && (defined(SIMD_PPC_ENABLE) || defined(SIMD_PPC64_ENABLE) || defined(SIMD_ARM_ENABLE) || defined(SIMD_ARM64_ENABLE)) && !defined(__APPLE__)
    bool CheckBit(int at, int bit) {
      bool result = false;
      int file = ::open("/proc/self/auxv", O_RDONLY);
      if (file < 0)
        return false;
      const ssize_t size = 64;
      unsigned long buffer[size];
      for (ssize_t count = size; count == size;) {
        count = ::read(file, buffer, sizeof(buffer)) / sizeof(unsigned long);
        for (int i = 0; i < count; i += 2) {
          if (buffer[i] == (unsigned)at) {
            result = !!(buffer[i + 1] & bit);
            count = 0;
            }
          if (buffer[i] == AT_NULL)
            count = 0;
          }
        }
      ::close(file);
      return result;
      }
#endif//defined(__GNUC__) && (defined(SIMD_PPC_ENABLE) || defined(SIMD_PPC64_ENABLE) || defined(SIMD_ARM_ENABLE) || defined(SIMD_ARM64_ENABLE)) && !defined(__APPLE__)

#ifdef SIMD_CPP_2011_ENABLE
    size_t CpuThreadNumber() {
      return std::thread::hardware_concurrency();
      }
#endif

#if defined(_MSC_VER)
    typedef SYSTEM_LOGICAL_PROCESSOR_INFORMATION Info;

    void GetLogicalProcessorInformation(std::vector<Info>& info) {
      DWORD size = 0;
      ::GetLogicalProcessorInformation(0, &size);
      info.resize(size / sizeof(Info));
      ::GetLogicalProcessorInformation(info.data(), &size);
      }

    size_t CpuSocketNumber() {
      std::vector<Info> info;
      GetLogicalProcessorInformation(info);
      size_t number = 0;
      for (size_t i = 0; i < info.size(); ++i)
        if (info[i].Relationship == ::RelationNumaNode)
          number++;
      return number;
      }

    size_t CpuCoreNumber() {
      std::vector<Info> info;
      GetLogicalProcessorInformation(info);
      size_t number = 0;
      for (size_t i = 0; i < info.size(); ++i)
        if (info[i].Relationship == ::RelationProcessorCore)
          number++;
      return number;
      }

    size_t CpuCacheSize(size_t level) {
      std::vector<Info> info;
      GetLogicalProcessorInformation(info);
      for (size_t i = 0; i < info.size(); ++i)
        if (info[i].Relationship == ::RelationCache && info[i].Cache.Level == level && (info[i].Cache.Type == ::CacheData || info[i].Cache.Type == CacheUnified))
          return info[i].Cache.Size;
      return 0;
      }
#elif defined(__GNUC__)
    size_t CpuSocketNumber() {
      uint32_t number = 0;
      ::FILE* p = ::popen("lscpu -b -p=Socket 2>/dev/null | grep -v '^#' | sort -u 2>/dev/null | wc -l 2>/dev/null", "r");
      if (p) {
        char buffer[PATH_MAX];
        while (::fgets(buffer, PATH_MAX, p));
        number = ::atoi(buffer);
        ::pclose(p);
        }
      return number;
      }

    size_t CpuCoreNumber() {
      uint32_t number = 0;
      ::FILE* p = ::popen("lscpu -b -p=Core 2>/dev/null | grep -v '^#' | sort -u 2>/dev/null | wc -l 2>/dev/null", "r");
      if (p) {
        char buffer[PATH_MAX];
        while (::fgets(buffer, PATH_MAX, p));
        number = ::atoi(buffer);
        ::pclose(p);
        }
      return number;
      }

    SIMD_INLINE size_t CorrectIfZero(size_t value, size_t otherwise) {
      return value ? value : otherwise;
      }

#if defined(_SC_LEVEL1_DCACHE_SIZE) && defined(_SC_LEVEL2_CACHE_SIZE) && defined(_SC_LEVEL3_CACHE_SIZE)
    size_t CpuCacheSize(size_t level) {
      switch (level) {
        case 1:
        {
        const size_t sz = ::sysconf(_SC_LEVEL1_DCACHE_SIZE) < 0 ? 0 : ::sysconf(_SC_LEVEL1_DCACHE_SIZE);
        return CorrectIfZero(sz, 32 * 1024);
        }
        case 2:
        {
        const size_t sz = ::sysconf(_SC_LEVEL2_CACHE_SIZE) < 0 ? 0 : ::sysconf(_SC_LEVEL2_CACHE_SIZE);
        return CorrectIfZero(sz, 256 * 1024);
        }
        case 3:
        {
        const size_t sz = ::sysconf(_SC_LEVEL3_CACHE_SIZE) < 0 ? 0 : ::sysconf(_SC_LEVEL3_CACHE_SIZE);
        return CorrectIfZero(sz, 2048 * 1024);
        }
        default:
          return 0;
        }
      }
#else
    size_t CpuCacheSize(size_t level) {
      switch (level) {
        case 1: return 32 * 1024;
        case 2: return 256 * 1024;
        case 3: return 2048 * 1024;
        default:
          return 0;
        }
      }
#endif

#else
#error This platform is unsupported!
#endif
    }

  namespace Cpu {
    const size_t SOCKET_NUMBER = Base::CpuSocketNumber();
    const size_t CORE_NUMBER = Base::CpuCoreNumber();
#ifdef SIMD_CPP_2011_ENABLE
    const size_t THREAD_NUMBER = Base::CpuThreadNumber();
#endif
    const size_t L1_CACHE_SIZE = Base::CpuCacheSize(1);
    const size_t L2_CACHE_SIZE = Base::CpuCacheSize(2);
    const size_t L3_CACHE_SIZE = Base::CpuCacheSize(3);
    }
  }