/home/runner/work/LabNeura/LabNeura/src/labneura/backends/cpu/generic/generic_ops_int8.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "labneura/backends/cpu/generic.h" |
2 | | #include <algorithm> |
3 | | #include <stdexcept> |
4 | | |
5 | | namespace labneura { |
6 | | |
7 | 11 | void GenericBackend::operation_int8(const TensorBackend& other, OperationType op_type) { |
8 | 11 | const int8_t* other_data = other.data_int8(); |
9 | 11 | int8_t* this_data = data_int8(); |
10 | | |
11 | 11 | if (op_type == OperationType::ADD) { |
12 | 13 | for (std::size_t i = 0; i < size_; ++i) { |
13 | 10 | int16_t sum = static_cast<int16_t>(this_data[i]) + static_cast<int16_t>(other_data[i]); |
14 | 10 | this_data[i] = static_cast<int8_t>(std::max(-128, std::min(127, static_cast<int>(sum)))); |
15 | 10 | } |
16 | 3 | return; |
17 | 3 | } |
18 | 8 | if (op_type == OperationType::MUL) { |
19 | 10.0k | for (std::size_t i = 0; i < size_; ++i) { |
20 | 10.0k | int16_t prod = static_cast<int16_t>(this_data[i]) * static_cast<int16_t>(other_data[i]); |
21 | 10.0k | this_data[i] = static_cast<int8_t>(std::max(-128, std::min(127, static_cast<int>(prod)))); |
22 | 10.0k | } |
23 | 5 | return; |
24 | 5 | } |
25 | 3 | if (op_type == OperationType::DIV) { |
26 | 0 | for (std::size_t i = 0; i < size_; ++i) { |
27 | 0 | if (other_data[i] == 0) { |
28 | 0 | this_data[i] = 0; // Division by zero yields 0 |
29 | 0 | } else { |
30 | 0 | int16_t quot = static_cast<int16_t>(this_data[i]) / static_cast<int16_t>(other_data[i]); |
31 | 0 | this_data[i] = static_cast<int8_t>(std::max(-128, std::min(127, static_cast<int>(quot)))); |
32 | 0 | } |
33 | 0 | } |
34 | 0 | return; |
35 | 0 | } |
36 | 13 | for (std::size_t i = 0; i < size_; ++i) { |
37 | 10 | int16_t diff = static_cast<int16_t>(this_data[i]) - static_cast<int16_t>(other_data[i]); |
38 | 10 | this_data[i] = static_cast<int8_t>(std::max(-128, std::min(127, static_cast<int>(diff)))); |
39 | 10 | } |
40 | 3 | } |
41 | | |
42 | | }// namespace labneura |
43 | | |
44 | | |