tesseract  4.1.0
intsimdmatrixsse.cpp
Go to the documentation of this file.
1 // File: intsindmatrixsse.cpp
3 // Description: SSE implementation of 8-bit int SIMD matrix multiply.
4 // Author: Ray Smith
5 //
6 // (C) Copyright 2017, Google Inc.
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 
18 #if !defined(__SSE4_1__)
19 #error Implementation only for SSE 4.1 capable architectures
20 #endif
21 
22 #include "intsimdmatrix.h"
23 
24 #include <cstdint>
25 #include <emmintrin.h>
26 #include <smmintrin.h>
27 #include "dotproductsse.h"
28 
29 namespace tesseract {
30 
31 // Computes and returns the dot product of the n-vectors u and v.
32 // Uses Intel SSE intrinsics to access the SIMD instruction set.
33 static int32_t IntDotProductSSE(const int8_t* u, const int8_t* v, int n) {
34  int max_offset = n - 8;
35  int offset = 0;
36  // Accumulate a set of 4 32-bit sums in sum, by loading 8 pairs of 8-bit
37  // values, extending to 16 bit, multiplying to make 32 bit results.
38  int32_t result = 0;
39  if (offset <= max_offset) {
40  offset = 8;
41  __m128i packed1 = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(u));
42  __m128i packed2 = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(v));
43  __m128i sum = _mm_cvtepi8_epi16(packed1);
44  packed2 = _mm_cvtepi8_epi16(packed2);
45  // The magic _mm_add_epi16 is perfect here. It multiplies 8 pairs of 16 bit
46  // ints to make 32 bit results, which are then horizontally added in pairs
47  // to make 4 32 bit results that still fit in a 128 bit register.
48  sum = _mm_madd_epi16(sum, packed2);
49  while (offset <= max_offset) {
50  packed1 = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(u + offset));
51  packed2 = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(v + offset));
52  offset += 8;
53  packed1 = _mm_cvtepi8_epi16(packed1);
54  packed2 = _mm_cvtepi8_epi16(packed2);
55  packed1 = _mm_madd_epi16(packed1, packed2);
56  sum = _mm_add_epi32(sum, packed1);
57  }
58  // Sum the 4 packed 32 bit sums and extract the low result.
59  sum = _mm_hadd_epi32(sum, sum);
60  sum = _mm_hadd_epi32(sum, sum);
61  result = _mm_cvtsi128_si32(sum);
62  }
63  while (offset < n) {
64  result += u[offset] * v[offset];
65  ++offset;
66  }
67  return result;
68 }
69 
70 // Computes part of matrix.vector v = Wu. Computes 1 result.
71 static void PartialMatrixDotVector1(const int8_t* wi, const double* scales,
72  const int8_t* u, int num_in,
73  double* v) {
74  double total = IntDotProductSSE(u, wi, num_in);
75  // Add in the bias and correct for integer values.
76  *v = (total / INT8_MAX + wi[num_in]) * *scales;
77 }
78 
79 static void matrixDotVector(int dim1, int dim2, const int8_t* wi,
80  const double* scales, const int8_t* u, double* v) {
81  const int num_out = dim1;
82  const int num_in = dim2 - 1;
83  int output = 0;
84 
85  for (; output < num_out; output++) {
86  PartialMatrixDotVector1(wi, scales, u, num_in, v);
87  wi += dim2;
88  scales++;
89  v++;
90  }
91 }
92 
93 const IntSimdMatrix IntSimdMatrix::intSimdMatrixSSE = {
94  matrixDotVector,
95  // Number of 32 bit outputs held in each register.
96  1,
97  // Maximum number of registers that we will use to hold outputs.
98  1,
99  // Number of 8 bit inputs in the inputs register.
100  1,
101  // Number of inputs in each weight group.
102  1
103 };
104 
105 } // namespace tesseract.
static const IntSimdMatrix intSimdMatrixSSE