/src/tesseract/src/arch/dotproductavx512.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /////////////////////////////////////////////////////////////////////// |
2 | | // File: dotproductavx512.cpp |
3 | | // Description: Architecture-specific dot-product function. |
4 | | // Author: Stefan Weil |
5 | | // |
6 | | // (C) Copyright 2022 |
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. |
16 | | /////////////////////////////////////////////////////////////////////// |
17 | | |
18 | | #if !defined(__AVX__) |
19 | | # if defined(__i686__) || defined(__x86_64__) |
20 | | # error Implementation only for AVX capable architectures |
21 | | # endif |
22 | | #else |
23 | | |
24 | | # include <immintrin.h> |
25 | | # include <cstdint> |
26 | | # include "dotproduct.h" |
27 | | |
28 | | namespace tesseract { |
29 | | |
30 | | // Computes and returns the dot product of the n-vectors u and v. |
31 | | // Uses Intel AVX intrinsics to access the SIMD instruction set. |
32 | | # if defined(FAST_FLOAT) |
33 | 0 | float DotProductAVX512F(const float *u, const float *v, int n) { |
34 | 0 | const unsigned quot = n / 16; |
35 | 0 | const unsigned rem = n % 16; |
36 | 0 | __m512 t0 = _mm512_setzero_ps(); |
37 | 0 | for (unsigned k = 0; k < quot; k++) { |
38 | 0 | __m512 f0 = _mm512_loadu_ps(u); |
39 | 0 | __m512 f1 = _mm512_loadu_ps(v); |
40 | 0 | t0 = _mm512_fmadd_ps(f0, f1, t0); |
41 | 0 | u += 16; |
42 | 0 | v += 16; |
43 | 0 | } |
44 | 0 | float result = _mm512_reduce_add_ps(t0); |
45 | 0 | for (unsigned k = 0; k < rem; k++) { |
46 | 0 | result += *u++ * *v++; |
47 | 0 | } |
48 | 0 | return result; |
49 | 0 | } |
50 | | # else |
51 | | double DotProductAVX512F(const double *u, const double *v, int n) { |
52 | | const unsigned quot = n / 8; |
53 | | const unsigned rem = n % 8; |
54 | | __m512d t0 = _mm512_setzero_pd(); |
55 | | for (unsigned k = 0; k < quot; k++) { |
56 | | t0 = _mm512_fmadd_pd(_mm512_loadu_pd(u), _mm512_loadu_pd(v), t0); |
57 | | u += 8; |
58 | | v += 8; |
59 | | } |
60 | | double result = _mm512_reduce_add_pd(t0); |
61 | | for (unsigned k = 0; k < rem; k++) { |
62 | | result += *u++ * *v++; |
63 | | } |
64 | | return result; |
65 | | } |
66 | | # endif |
67 | | |
68 | | } // namespace tesseract. |
69 | | |
70 | | #endif |