simd_helper.h 3.9 KB
Newer Older
1 2 3 4
/**
 * \file dnn/src/x86/simd_helper.h
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
5
 * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
6 7 8 9 10 11 12 13 14 15
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */
#pragma once
#include "src/x86/utils.h"
#include "megdnn/arch.h"

#include <immintrin.h>
16
#ifdef WIN32
17 18 19
#include <xmmintrin.h>
#include <avxintrin.h>
#include <fmaintrin.h>
20
#endif
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
#include <cmath>
#include <algorithm>

namespace megdnn {
namespace x86 {

template <SIMDType feature>
struct simd_traits {};

template <>
struct simd_traits<SIMDType::SSE> {
    using type = __m128;
    static MEGDNN_CONSTEXPR size_t width = 4;
    static type setzero() MEGDNN_ATTRIBUTE_TARGET("sse")
    {
        return _mm_setzero_ps();
    }
    static type set1(float a) MEGDNN_ATTRIBUTE_TARGET("sse")
    {
        return _mm_set1_ps(a);
    }
    static type loadu(const float *mem_addr) MEGDNN_ATTRIBUTE_TARGET("sse")
    {
        return _mm_loadu_ps(mem_addr);
    }
    static void storeu(float *mem_addr, type a) MEGDNN_ATTRIBUTE_TARGET("sse")
    {
        _mm_storeu_ps(mem_addr, a);
    }
    static type add(type a, type b) MEGDNN_ATTRIBUTE_TARGET("sse")
    {
        return _mm_add_ps(a, b);
    }
    static type sub(type a, type b) MEGDNN_ATTRIBUTE_TARGET("sse")
    {
        return _mm_sub_ps(a, b);
    }
    static type mul(type a, type b) MEGDNN_ATTRIBUTE_TARGET("sse")
    {
        return _mm_mul_ps(a, b);
    }
    static type fmadd(type a, type b, type c) MEGDNN_ATTRIBUTE_TARGET("sse")
    {
        return add(mul(a, b), c);
    }
    static type exp(type a) MEGDNN_ATTRIBUTE_TARGET("sse")
    {
        float b[4];
        _mm_store_ps(b, a);
        for (size_t i = 0; i < 4; ++i) b[i] = std::exp(b[i]);
        return _mm_load_ps(b);
    }
    static type log(type a) MEGDNN_ATTRIBUTE_TARGET("sse")
    {
        float b[4];
        _mm_store_ps(b, a);
        for (size_t i = 0; i < 4; ++i) b[i] = std::log(b[i]);
        return _mm_load_ps(b);
    }
};

struct simd_traits_avx_base {
    using type = __m256;
    static MEGDNN_CONSTEXPR size_t width = 8;
    static type set1(float a) MEGDNN_ATTRIBUTE_TARGET("avx")
    {
        return _mm256_set1_ps(a);
    }
    static type setzero() MEGDNN_ATTRIBUTE_TARGET("avx")
    {
        return _mm256_setzero_ps();
    }
    static type loadu(const float *mem_addr) MEGDNN_ATTRIBUTE_TARGET("avx")
    {
        return _mm256_loadu_ps(mem_addr);
    }
    static void storeu(float *mem_addr, type a) MEGDNN_ATTRIBUTE_TARGET("avx")
    {
        _mm256_storeu_ps(mem_addr, a);
    }
    static type add(type a, type b) MEGDNN_ATTRIBUTE_TARGET("avx")
    {
        return _mm256_add_ps(a, b);
    }
    static type sub(type a, type b) MEGDNN_ATTRIBUTE_TARGET("avx")
    {
        return _mm256_sub_ps(a, b);
    }
    static type mul(type a, type b) MEGDNN_ATTRIBUTE_TARGET("avx")
    {
        return _mm256_mul_ps(a, b);
    }
    static type exp(type a) MEGDNN_ATTRIBUTE_TARGET("avx")
    {
        float b[8];
        _mm256_storeu_ps(b, a);
        for (size_t i = 0; i < 8; ++i) b[i] = std::exp(b[i]);
        return _mm256_loadu_ps(b);
    }
    static type log(type a) MEGDNN_ATTRIBUTE_TARGET("avx")
    {
        float b[8];
        _mm256_storeu_ps(b, a);
        for (size_t i = 0; i < 8; ++i) b[i] = std::log(b[i]);
        return _mm256_loadu_ps(b);
    }
};

template <>
struct simd_traits<SIMDType::AVX>: simd_traits_avx_base {
    static type fmadd(type a, type b, type c) MEGDNN_ATTRIBUTE_TARGET("avx")
    {
        return add(mul(a, b), c);
    }
};

template <>
struct simd_traits<SIMDType::FMA>: simd_traits_avx_base {
    static type fmadd(type a, type b, type c) MEGDNN_ATTRIBUTE_TARGET("fma")
    {
        return _mm256_fmadd_ps(a, b, c);
    }
};

} // namespace x86
} // namespace megdnn

// vim: syntax=cpp.doxygen