/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 | | void GenericBackend::operation_int16(const TensorBackend& other, |
43 | 1 | OperationType op_type) { |
44 | 1 | const int16_t* other_data = other.data_int16(); |
45 | 1 | int16_t* this_data = data_int16(); |
46 | | |
47 | 4 | for (std::size_t i = 0; i < size_; ++i) { |
48 | 3 | int32_t result = 0; |
49 | 3 | const int32_t lhs = this_data[i]; |
50 | 3 | const int32_t rhs = other_data[i]; |
51 | 3 | switch (op_type) { |
52 | 3 | case OperationType::ADD: |
53 | 3 | result = lhs + rhs; |
54 | 3 | break; |
55 | 0 | case OperationType::SUB: |
56 | 0 | result = lhs - rhs; |
57 | 0 | break; |
58 | 0 | case OperationType::MUL: |
59 | 0 | result = lhs * rhs; |
60 | 0 | break; |
61 | 0 | case OperationType::DIV: |
62 | 0 | if (rhs == 0) { |
63 | 0 | this_data[i] = 0; |
64 | 0 | continue; |
65 | 0 | } |
66 | 0 | result = lhs / rhs; |
67 | 0 | break; |
68 | 3 | } |
69 | 3 | this_data[i] = static_cast<int16_t>( |
70 | 3 | std::max<int32_t>(-32768, std::min<int32_t>(32767, result))); |
71 | 3 | } |
72 | 1 | } |
73 | | |
74 | | }// namespace labneura |
75 | | |