summaryrefslogtreecommitdiffstats
path: root/eval
diff options
context:
space:
mode:
authorArne Juul <arnej@verizonmedia.com>2020-11-20 14:16:19 +0000
committerArne Juul <arnej@verizonmedia.com>2020-11-23 11:04:18 +0000
commite122e2fde2c837120427328fc5bee19faf461554 (patch)
treed6be5379d06962dbc88748319435e7e698bac533 /eval
parent359903c03748d520357067d03e7ac8eed7c5a19b (diff)
add CellTypeUtils class with various static constexpr methods
Diffstat (limited to 'eval')
-rw-r--r--eval/src/vespa/eval/eval/cell_type.cpp16
-rw-r--r--eval/src/vespa/eval/eval/cell_type.h58
2 files changed, 72 insertions, 2 deletions
diff --git a/eval/src/vespa/eval/eval/cell_type.cpp b/eval/src/vespa/eval/eval/cell_type.cpp
index e5729c547b0..365a3f59a56 100644
--- a/eval/src/vespa/eval/eval/cell_type.cpp
+++ b/eval/src/vespa/eval/eval/cell_type.cpp
@@ -1,3 +1,19 @@
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "cell_type.h"
+#include <stdio.h>
+#include <cstdlib>
+#include <vespa/vespalib/util/exceptions.h>
+#include <vespa/vespalib/util/stringfmt.h>
+
+using vespalib::make_string_short::fmt;
+
+namespace vespalib::eval {
+
+void
+CellTypeUtils::bad_argument(uint32_t id)
+{
+ throw IllegalArgumentException(fmt("Unknown CellType id=%u", id));
+}
+
+}
diff --git a/eval/src/vespa/eval/eval/cell_type.h b/eval/src/vespa/eval/eval/cell_type.h
index 0e878f26f47..b2b71930ff2 100644
--- a/eval/src/vespa/eval/eval/cell_type.h
+++ b/eval/src/vespa/eval/eval/cell_type.h
@@ -3,7 +3,7 @@
#pragma once
#include <vespa/vespalib/util/typify.h>
-#include <cstdlib>
+#include <cstdint>
namespace vespalib::eval {
@@ -25,6 +25,60 @@ template <typename CT> inline CellType get_cell_type();
template <> inline CellType get_cell_type<double>() { return CellType::DOUBLE; }
template <> inline CellType get_cell_type<float>() { return CellType::FLOAT; }
+struct CellTypeUtils {
+ template <typename CT> static constexpr bool check(CellType type);
+ template <typename CT> static constexpr CellType get_type();
+
+ static constexpr uint32_t DOUBLE_ID = 0;
+ static constexpr uint32_t FLOAT_ID = 1;
+
+ static constexpr uint32_t id_of(CellType cell_type) {
+ switch (cell_type) {
+ case CellType::DOUBLE: return DOUBLE_ID;
+ case CellType::FLOAT: return FLOAT_ID;
+ }
+ bad_argument((uint32_t)cell_type);
+ }
+
+ static constexpr CellType type_of(uint32_t id) {
+ switch (id) {
+ case DOUBLE_ID: return CellType::DOUBLE;
+ case FLOAT_ID: return CellType::FLOAT;
+ }
+ bad_argument(id);
+ }
+
+ static void bad_argument [[ noreturn ]] (uint32_t id);
+
+ static constexpr uint32_t alignment(CellType cell_type) {
+ switch (cell_type) {
+ case CellType::DOUBLE: return sizeof(double);
+ case CellType::FLOAT: return sizeof(float);
+ }
+ bad_argument((uint32_t)cell_type);
+ }
+
+ static constexpr size_t mem_size(CellType cell_type, size_t sz) {
+ switch (cell_type) {
+ case CellType::DOUBLE: return sz * sizeof(double);
+ case CellType::FLOAT: return sz * sizeof(float);
+ }
+ bad_argument((uint32_t)cell_type);
+ }
+};
+
+template <> constexpr bool
+CellTypeUtils::check<double>(CellType type) { return (type == CellType::DOUBLE); }
+
+template <> constexpr bool
+CellTypeUtils::check<float>(CellType type) { return (type == CellType::FLOAT); }
+
+template <> constexpr CellType
+CellTypeUtils::get_type<double>() { return CellType::DOUBLE; }
+
+template <> constexpr CellType
+CellTypeUtils::get_type<float>() { return CellType::FLOAT; }
+
struct TypifyCellType {
template <typename T> using Result = TypifyResultType<T>;
template <typename F> static decltype(auto) resolve(CellType value, F &&f) {
@@ -32,7 +86,7 @@ struct TypifyCellType {
case CellType::DOUBLE: return f(Result<double>());
case CellType::FLOAT: return f(Result<float>());
}
- abort();
+ CellTypeUtils::bad_argument((uint32_t)value);
}
};