|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. |
| 4 | +You may obtain a copy of the License at |
| 5 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +Unless required by applicable law or agreed to in writing, software |
| 7 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +See the License for the specific language governing permissions and |
| 10 | +limitations under the License. */ |
| 11 | + |
| 12 | +#pragma once |
| 13 | + |
| 14 | +#include <iostream> |
| 15 | +#include "DisableCopy.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | + |
| 19 | +class SIMDFlags final { |
| 20 | +public: |
| 21 | + DISABLE_COPY(SIMDFlags); |
| 22 | + |
| 23 | + SIMDFlags(); |
| 24 | + |
| 25 | + static SIMDFlags* instance(); |
| 26 | + |
| 27 | + inline bool isSSE() const { return simd_flags_ & SIMD_SSE; } |
| 28 | + inline bool isSSE2() const { return simd_flags_ & SIMD_SSE2; } |
| 29 | + inline bool isSSE3() const { return simd_flags_ & SIMD_SSE3; } |
| 30 | + inline bool isSSSE3() const { return simd_flags_ & SIMD_SSSE3; } |
| 31 | + inline bool isSSE41() const { return simd_flags_ & SIMD_SSE41; } |
| 32 | + inline bool isSSE42() const { return simd_flags_ & SIMD_SSE42; } |
| 33 | + inline bool isFMA3() const { return simd_flags_ & SIMD_FMA3; } |
| 34 | + inline bool isFMA4() const { return simd_flags_ & SIMD_FMA4; } |
| 35 | + inline bool isAVX() const { return simd_flags_ & SIMD_AVX; } |
| 36 | + inline bool isAVX2() const { return simd_flags_ & SIMD_AVX2; } |
| 37 | + inline bool isAVX512()const { return simd_flags_ & SIMD_AVX512;} |
| 38 | + |
| 39 | +private: |
| 40 | + enum simd_t { |
| 41 | + SIMD_NONE = 0, ///< None |
| 42 | + SIMD_SSE = 1 << 0, ///< SSE |
| 43 | + SIMD_SSE2 = 1 << 1, ///< SSE 2 |
| 44 | + SIMD_SSE3 = 1 << 2, ///< SSE 3 |
| 45 | + SIMD_SSSE3 = 1 << 3, ///< SSSE 3 |
| 46 | + SIMD_SSE41 = 1 << 4, ///< SSE 4.1 |
| 47 | + SIMD_SSE42 = 1 << 5, ///< SSE 4.2 |
| 48 | + SIMD_FMA3 = 1 << 6, ///< FMA 3 |
| 49 | + SIMD_FMA4 = 1 << 7, ///< FMA 4 |
| 50 | + SIMD_AVX = 1 << 8, ///< AVX |
| 51 | + SIMD_AVX2 = 1 << 9, ///< AVX 2 |
| 52 | + SIMD_AVX512 = 1 << 10, ///< AVX 512 |
| 53 | + }; |
| 54 | + |
| 55 | + /// simd flags |
| 56 | + int simd_flags_ = SIMD_NONE; |
| 57 | +}; |
| 58 | + |
| 59 | +#define HAS_SSE SIMDFlags::instance()->isSSE() |
| 60 | +#define HAS_SSE2 SIMDFlags::instance()->isSSE2() |
| 61 | +#define HAS_SSE3 SIMDFlags::instance()->isSSE3() |
| 62 | +#define HAS_SSSE3 SIMDFlags::instance()->isSSSE3() |
| 63 | +#define HAS_SSE41 SIMDFlags::instance()->isSSE41() |
| 64 | +#define HAS_SSE42 SIMDFlags::instance()->isSSE42() |
| 65 | +#define HAS_FMA3 SIMDFlags::instance()->isFMA3() |
| 66 | +#define HAS_FMA4 SIMDFlags::instance()->isFMA4() |
| 67 | +#define HAS_AVX SIMDFlags::instance()->isAVX() |
| 68 | +#define HAS_AVX2 SIMDFlags::instance()->isAVX2() |
| 69 | +#define HAS_AVX512 SIMDFlags::instance()->isAVX512() |
| 70 | + |
| 71 | +} // namespace paddle |
0 commit comments