1 /* Copyright 2019 The TensorFlow Authors. 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 #include "tensorflow/compiler/xla/comparison_util.h"
17 #include "absl/container/flat_hash_map.h"
18 #include "tensorflow/compiler/xla/util.h"
19
20 namespace xla {
21
ComparisonDirectionToString(ComparisonDirection direction)22 std::string ComparisonDirectionToString(ComparisonDirection direction) {
23 switch (direction) {
24 case ComparisonDirection::kEq:
25 return "EQ";
26 case ComparisonDirection::kNe:
27 return "NE";
28 case ComparisonDirection::kGe:
29 return "GE";
30 case ComparisonDirection::kGt:
31 return "GT";
32 case ComparisonDirection::kLe:
33 return "LE";
34 case ComparisonDirection::kLt:
35 return "LT";
36 }
37 }
38
StringToComparisonDirection(absl::string_view direction_name)39 StatusOr<ComparisonDirection> StringToComparisonDirection(
40 absl::string_view direction_name) {
41 static auto* direction_map =
42 new absl::flat_hash_map<string, ComparisonDirection>({
43 {"EQ", ComparisonDirection::kEq},
44 {"NE", ComparisonDirection::kNe},
45 {"GE", ComparisonDirection::kGe},
46 {"GT", ComparisonDirection::kGt},
47 {"LE", ComparisonDirection::kLe},
48 {"LT", ComparisonDirection::kLt},
49 });
50 auto it = direction_map->find(direction_name);
51 if (it == direction_map->end()) {
52 return InvalidArgument("Unknown comparison direction: %s", direction_name);
53 }
54 return it->second;
55 }
56
57 } // namespace xla
58