Welcome to mirror list, hosted at ThFree Co, Russian Federation.

upcast_test.cc « kernels « test - github.com/marian-nmt/intgemm/intgemm.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 92be1bd210844b4460e15d421b3c402135aa3d00 (plain)
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
// This test triggers an internal compiler error in gcc 5.
#if defined(__OPTIMIZE__) || defined(__clang__) || defined(__INTEL_COMPILER) || !defined(__GNUC__) || (__GNUC__ != 5)
#include "../test.h"
#include "../../intgemm/aligned.h"
#include "../../intgemm/kernels.h"

#include <cstdint>
#include <numeric>

namespace intgemm {

template <CPUType CPUType_>
void kernel_upcast8to16_test() {
  if (kCPU < CPUType_)
    return;

  using vi = vector_t<CPUType_, int>;
  constexpr int LENGTH = sizeof(vi) / sizeof(int8_t);

  AlignedVector<int8_t> input(LENGTH);
  AlignedVector<int16_t> output(LENGTH);

  std::iota(input.begin(), input.end(), static_cast<int8_t>(-LENGTH / 2));

  auto result = kernels::upcast8to16(*input.template as<vi>());
  output.template as<vi>()[0] = result.first;
  output.template as<vi>()[1] = result.second;

  for (std::size_t i = 0; i < output.size(); ++i)
    CHECK(output[i] == int16_t(input[i]));
}

template INTGEMM_SSE2 void kernel_upcast8to16_test<CPUType::SSE2>();
KERNEL_TEST_CASE("upcast8to16 SSE2") { return kernel_upcast8to16_test<CPUType::SSE2>(); }

template INTGEMM_AVX2 void kernel_upcast8to16_test<CPUType::AVX2>();
KERNEL_TEST_CASE("upcast8to16 AVX2") { return kernel_upcast8to16_test<CPUType::AVX2>(); }

#ifdef INTGEMM_COMPILER_SUPPORTS_AVX512BW
template INTGEMM_AVX512BW void kernel_upcast8to16_test<CPUType::AVX512BW>();
KERNEL_TEST_CASE("upcast8to16 AVX512BW") { return kernel_upcast8to16_test<CPUType::AVX512BW>(); }
#endif

template <CPUType CPUType_>
void kernel_upcast16to32_test() {
  if (kCPU < CPUType_)
    return;

  using vi = vector_t<CPUType_, int>;
  constexpr int LENGTH = sizeof(vi) / sizeof(int16_t);

  AlignedVector<int16_t> input(LENGTH);
  AlignedVector<int32_t> output(LENGTH);

  std::iota(input.begin(), input.end(), static_cast<int16_t>(-LENGTH / 2));

  auto result = kernels::upcast16to32(*input.template as<vi>());
  output.template as<vi>()[0] = result.first;
  output.template as<vi>()[1] = result.second;

  for (std::size_t i = 0; i < output.size(); ++i)
    CHECK(output[i] == int32_t(input[i]));
}

template INTGEMM_SSE2 void kernel_upcast16to32_test<CPUType::SSE2>();
KERNEL_TEST_CASE("upcast16to32 SSE2") { return kernel_upcast16to32_test<CPUType::SSE2>(); }

template INTGEMM_AVX2 void kernel_upcast16to32_test<CPUType::AVX2>();
KERNEL_TEST_CASE("upcast16to32 AVX2") { return kernel_upcast16to32_test<CPUType::AVX2>(); }

#ifdef INTGEMM_COMPILER_SUPPORTS_AVX512BW
template INTGEMM_AVX512BW void kernel_upcast16to32_test<CPUType::AVX512BW>();
KERNEL_TEST_CASE("upcast16to32 AVX512BW") { return kernel_upcast16to32_test<CPUType::AVX512BW>(); }
#endif


template <CPUType CPUType_>
void kernel_upcast8to32_test() {
  if (kCPU < CPUType_)
    return;

  using vi = vector_t<CPUType_, int>;
  constexpr int LENGTH = sizeof(vi) / sizeof(int8_t);

  AlignedVector<int8_t> input(LENGTH);
  AlignedVector<int32_t> output(LENGTH);

  std::iota(input.begin(), input.end(), static_cast<int8_t>(-LENGTH / 2));

  auto result = kernels::upcast8to32(*input.template as<vi>());
  output.template as<vi>()[0] = result.first;
  output.template as<vi>()[1] = result.second;
  output.template as<vi>()[2] = result.third;
  output.template as<vi>()[3] = result.fourth;

  for (std::size_t i = 0; i < output.size(); ++i)
    CHECK(output[i] == int32_t(input[i]));
}

template INTGEMM_SSE2 void kernel_upcast8to32_test<CPUType::SSE2>();
KERNEL_TEST_CASE("upcast8to32 SSE2") { return kernel_upcast8to32_test<CPUType::SSE2>(); }

template INTGEMM_AVX2 void kernel_upcast8to32_test<CPUType::AVX2>();
KERNEL_TEST_CASE("upcast8to32 AVX2") { return kernel_upcast8to32_test<CPUType::AVX2>(); }

#ifdef INTGEMM_COMPILER_SUPPORTS_AVX512BW
template INTGEMM_AVX512BW void kernel_upcast8to32_test<CPUType::AVX512BW>();
KERNEL_TEST_CASE("upcast8to32 AVX512BW") { return kernel_upcast8to32_test<CPUType::AVX512BW>(); }
#endif

}
#endif