1 /* Copyright 2019 Google LLC. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #ifndef RUY_RUY_SIDE_PAIR_H_
17 #define RUY_RUY_SIDE_PAIR_H_
18
19 #include "ruy/check_macros.h"
20
21 namespace ruy {
22
23 // Enumeration of the sides, i.e. the operands 'slots', in a matrix
24 // multiplication. The numerical values of these enumeration constants matter
25 // because these will be used as indices into the array underlying a SidePair.
26 enum class Side {
27 // Left-hand side
28 kLhs = 0,
29 // Right-hand side
30 kRhs = 1
31 };
32
OtherSide(Side side)33 inline Side OtherSide(Side side) {
34 return side == Side::kLhs ? Side::kRhs : Side::kLhs;
35 }
36
37 // SidePair is a pair container where the two elements are indexed by a Side
38 // enum.
39 template <typename T>
40 class SidePair final {
41 public:
SidePair()42 SidePair() {}
SidePair(const T & a,const T & b)43 SidePair(const T& a, const T& b) : elem_{a, b} {}
44 const T& operator[](Side side) const {
45 const int index = static_cast<int>(side);
46 // Technically this check is vacuous, since other values would be
47 // out-of-range for enum Side.
48 RUY_DCHECK(index == 0 || index == 1);
49 return elem_[index];
50 }
51
52 T& operator[](Side side) {
53 const int index = static_cast<int>(side);
54 // Technically this check is vacuous, since other values would be
55 // out-of-range for enum Side.
56 RUY_DCHECK(index == 0 || index == 1);
57 return elem_[index];
58 }
59
60 private:
61 static_assert(static_cast<int>(Side::kLhs) == 0, "");
62 static_assert(static_cast<int>(Side::kRhs) == 1, "");
63 T elem_[2];
64 };
65
66 } // namespace ruy
67
68 #endif // RUY_RUY_SIDE_PAIR_H_
69