openGPMP
Open Source Mathematics Package
mtx_sse2_arr_i8.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file. As this is an Open Source effort, all implementations
25  * must be of the same methodology.
26  *
27  *
28  *
29  * This software is distributed on an AS IS basis, WITHOUT
30  * WARRANTY OF ANY KIND, either express or implied.
31  *
32  ************************************************************************/
33 #include <cassert>
34 #include <cstddef>
35 #include <cstdint>
36 #include <iostream>
37 #include <openGPMP/linalg/mtx.hpp>
38 #include <vector>
39 
40 #if defined(__x86_64__) || defined(__amd64__) || defined(__amd64)
41 
42 /************************************************************************
43  *
44  * Matrix Operations for SSE ISA
45  *
46  ************************************************************************/
47 #elif defined(__SSE2__)
48 // SSE2
49 #include <emmintrin.h>
50 #include <smmintrin.h>
51 /************************************************************************
52  *
53  * Matrix Operations on Arrays
54  *
55  ************************************************************************/
56 // matrix addition for 8-bit integers using SSE and 128-bit SIMD registers
57 void gpmp::linalg::Mtx::mtx_add(const int8_t *A,
58  const int8_t *B,
59  int8_t *C,
60  int rows,
61  int cols) {
62  for (int i = 0; i < rows; ++i) {
63  int j = 0;
64  for (; j < cols - 15; j += 16) {
65  __m128i a = _mm_loadu_si128(
66  reinterpret_cast<const __m128i *>(&A[i * cols + j]));
67  __m128i b = _mm_loadu_si128(
68  reinterpret_cast<const __m128i *>(&B[i * cols + j]));
69  __m128i c = _mm_loadu_si128(
70  reinterpret_cast<const __m128i *>(&C[i * cols + j]));
71 
72  // Perform vectorized addition and accumulate the result
73  c = _mm_add_epi8(c, _mm_add_epi8(a, b));
74 
75  // Store the result back to the C matrix
76  _mm_storeu_si128(reinterpret_cast<__m128i *>(&C[i * cols + j]), c);
77  }
78 
79  for (; j < cols; ++j) {
80  C[i * cols + j] = A[i * cols + j] + B[i * cols + j];
81  }
82  }
83 }
84 
85 #endif
86 
87 #endif
list C
Definition: linalg.py:24
list A
Definition: linalg.py:22
list B
Definition: linalg.py:23