CpuId.cpp 2.1 KB
Newer Older
L
liaogang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "paddle/utils/CpuId.h"
#include "paddle/utils/Util.h"

#ifdef _WIN32

17 18
#include <intrin.h>

L
liaogang 已提交
19
/// for MSVC
Y
Yu Yang 已提交
20
#define CPUID(info, x) __cpuidex(info, x, 0)
L
liaogang 已提交
21

H
hedaoyuan 已提交
22
#else
L
liaogang 已提交
23

24
#if !defined(__arm__) && !defined(__aarch64__)
L
liaogang 已提交
25 26
#include <cpuid.h>
/// for GCC/Clang
Y
Yu Yang 已提交
27
#define CPUID(info, x) __cpuid_count(x, 0, info[0], info[1], info[2], info[3])
H
hedaoyuan 已提交
28
#endif
L
liaogang 已提交
29 30 31 32 33 34

#endif

namespace paddle {

SIMDFlags::SIMDFlags() {
35
#if defined(__arm__) || defined(__aarch64__)
H
hedaoyuan 已提交
36 37
  simd_flags_ = SIMD_NEON;
#else
Y
Yu Yang 已提交
38 39
  unsigned int cpuInfo[4];
  // CPUID: https://en.wikipedia.org/wiki/CPUID
40
  // clang-format off
Y
Yu Yang 已提交
41
  CPUID(cpuInfo, 0x00000001);
42 43 44 45
  simd_flags_ |= cpuInfo[3] & (1 << 25) ? SIMD_SSE   : SIMD_NONE;
  simd_flags_ |= cpuInfo[3] & (1 << 26) ? SIMD_SSE2  : SIMD_NONE;
  simd_flags_ |= cpuInfo[2] & (1 <<  0) ? SIMD_SSE3  : SIMD_NONE;
  simd_flags_ |= cpuInfo[2] & (1 <<  9) ? SIMD_SSSE3 : SIMD_NONE;
Y
Yu Yang 已提交
46 47
  simd_flags_ |= cpuInfo[2] & (1 << 19) ? SIMD_SSE41 : SIMD_NONE;
  simd_flags_ |= cpuInfo[2] & (1 << 20) ? SIMD_SSE42 : SIMD_NONE;
48 49
  simd_flags_ |= cpuInfo[2] & (1 << 12) ? SIMD_FMA3  : SIMD_NONE;
  simd_flags_ |= cpuInfo[2] & (1 << 28) ? SIMD_AVX   : SIMD_NONE;
Y
Yu Yang 已提交
50 51

  CPUID(cpuInfo, 0x00000007);
52 53
  simd_flags_ |= cpuInfo[1] & (1 <<  5) ? SIMD_AVX2  : SIMD_NONE;
  simd_flags_ |= cpuInfo[1] & (1 << 16) ? SIMD_AVX512: SIMD_NONE;
Y
Yu Yang 已提交
54 55

  CPUID(cpuInfo, 0x80000001);
56 57
  simd_flags_ |= cpuInfo[2] & (1 << 16) ? SIMD_FMA4  : SIMD_NONE;
  // clang-fotmat on
L
Liu Yiqun 已提交
58
#endif
59 60 61
}

SIMDFlags const* SIMDFlags::instance() {
Y
Yu Yang 已提交
62 63
  static SIMDFlags instance;
  return &instance;
L
liaogang 已提交
64 65
}

Y
Yu Yang 已提交
66
}  // namespace paddle