summaryrefslogtreecommitdiff
path: root/nn/common/include/nnapi/TypeUtils.h
diff options
context:
space:
mode:
Diffstat (limited to 'nn/common/include/nnapi/TypeUtils.h')
-rw-r--r--nn/common/include/nnapi/TypeUtils.h92
1 files changed, 92 insertions, 0 deletions
diff --git a/nn/common/include/nnapi/TypeUtils.h b/nn/common/include/nnapi/TypeUtils.h
index 77761c40b..9dc67cf46 100644
--- a/nn/common/include/nnapi/TypeUtils.h
+++ b/nn/common/include/nnapi/TypeUtils.h
@@ -17,6 +17,9 @@
#ifndef ANDROID_FRAMEWORKS_ML_NN_COMMON_NNAPI_TYPE_UTILS_H
#define ANDROID_FRAMEWORKS_ML_NN_COMMON_NNAPI_TYPE_UTILS_H
+#include <android-base/logging.h>
+#include <android-base/macros.h>
+
#include <ostream>
#include <utility>
#include <vector>
@@ -28,6 +31,15 @@
namespace android::nn {
+enum class HalVersion : int32_t {
+ UNKNOWN,
+ V1_0,
+ V1_1,
+ V1_2,
+ V1_3,
+ LATEST = V1_3,
+};
+
bool isExtension(OperandType type);
bool isExtension(OperationType type);
@@ -93,6 +105,7 @@ std::ostream& operator<<(std::ostream& os, const OptionalTimePoint& optionalTime
std::ostream& operator<<(std::ostream& os, const TimeoutDuration& timeoutDuration);
std::ostream& operator<<(std::ostream& os, const OptionalTimeoutDuration& optionalTimeoutDuration);
std::ostream& operator<<(std::ostream& os, const Version& version);
+std::ostream& operator<<(std::ostream& os, const HalVersion& halVersion);
bool operator==(const Timing& a, const Timing& b);
bool operator!=(const Timing& a, const Timing& b);
@@ -112,6 +125,85 @@ bool operator!=(const Operand& a, const Operand& b);
bool operator==(const Operation& a, const Operation& b);
bool operator!=(const Operation& a, const Operation& b);
+// The NN_RET_CHECK family of macros defined below is similar to the CHECK family defined in
+// system/libbase/include/android-base/logging.h
+//
+// The difference is that NN_RET_CHECK macros use LOG(ERROR) instead of LOG(FATAL)
+// and return false instead of aborting.
+
+// Logs an error and returns false. Append context using << after. For example:
+//
+// NN_RET_CHECK_FAIL() << "Something went wrong";
+//
+// The containing function must return a bool.
+#define NN_RET_CHECK_FAIL() \
+ return ::android::nn::FalseyErrorStream() \
+ << "NN_RET_CHECK failed (" << __FILE__ << ":" << __LINE__ << "): "
+
+// Logs an error and returns false if condition is false. Extra logging can be appended using <<
+// after. For example:
+//
+// NN_RET_CHECK(false) << "Something went wrong";
+//
+// The containing function must return a bool.
+#define NN_RET_CHECK(condition) \
+ while (UNLIKELY(!(condition))) NN_RET_CHECK_FAIL() << #condition << " "
+
+// Helper for NN_CHECK_xx(x, y) macros.
+#define NN_RET_CHECK_OP(LHS, RHS, OP) \
+ for (auto _values = ::android::base::MakeEagerEvaluator(LHS, RHS); \
+ UNLIKELY(!(_values.lhs.v OP _values.rhs.v)); \
+ /* empty */) \
+ NN_RET_CHECK_FAIL() \
+ << #LHS << " " << #OP << " " << #RHS << " (" << #LHS << " = " \
+ << ::android::base::LogNullGuard<decltype(_values.lhs.v)>::Guard(_values.lhs.v) \
+ << ", " << #RHS << " = " \
+ << ::android::base::LogNullGuard<decltype(_values.rhs.v)>::Guard(_values.rhs.v) \
+ << ") "
+
+// Logs an error and returns false if a condition between x and y does not hold. Extra logging can
+// be appended using << after. For example:
+//
+// NN_RET_CHECK_EQ(a, b) << "Something went wrong";
+//
+// The values must implement the appropriate comparison operator as well as
+// `operator<<(std::ostream&, ...)`.
+// The containing function must return a bool.
+#define NN_RET_CHECK_EQ(x, y) NN_RET_CHECK_OP(x, y, ==)
+#define NN_RET_CHECK_NE(x, y) NN_RET_CHECK_OP(x, y, !=)
+#define NN_RET_CHECK_LE(x, y) NN_RET_CHECK_OP(x, y, <=)
+#define NN_RET_CHECK_LT(x, y) NN_RET_CHECK_OP(x, y, <)
+#define NN_RET_CHECK_GE(x, y) NN_RET_CHECK_OP(x, y, >=)
+#define NN_RET_CHECK_GT(x, y) NN_RET_CHECK_OP(x, y, >)
+
+// Ensure that every user of FalseyErrorStream is linked to the
+// correct instance, using the correct LOG_TAG
+namespace {
+
+// A wrapper around LOG(ERROR) that can be implicitly converted to bool (always evaluates to false).
+// Used to implement stream logging in NN_RET_CHECK.
+class FalseyErrorStream {
+ DISALLOW_COPY_AND_ASSIGN(FalseyErrorStream);
+
+ public:
+ FalseyErrorStream() {}
+
+ template <typename T>
+ FalseyErrorStream& operator<<(const T& value) {
+ mBuffer << value;
+ return *this;
+ }
+
+ ~FalseyErrorStream() { LOG(ERROR) << mBuffer.str(); }
+
+ operator bool() const { return false; }
+
+ private:
+ std::ostringstream mBuffer;
+};
+
+} // namespace
+
} // namespace android::nn
#endif // ANDROID_FRAMEWORKS_ML_NN_COMMON_NNAPI_TYPE_UTILS_H