summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--examples/hash_crc32_example/example.cc2
-rw-r--r--include/ocl/crc_hash.hpp36
-rw-r--r--include/ocl/detail/config.hpp19
-rw-r--r--include/ocl/io.hpp18
-rw-r--r--include/ocl/is_same.hpp41
5 files changed, 64 insertions, 52 deletions
diff --git a/examples/hash_crc32_example/example.cc b/examples/hash_crc32_example/example.cc
index fa72909..258aedc 100644
--- a/examples/hash_crc32_example/example.cc
+++ b/examples/hash_crc32_example/example.cc
@@ -9,7 +9,7 @@ int main(int argc, char** argv)
std::hash<ocl::crc_hash_trait> hash{};
ocl::io::enable_stdio_sync(false);
- ocl::io::print(hash(argv[1]));
+ ocl::io::print(hash.operator()<true, false>(argv[1]));
return 0;
}
diff --git a/include/ocl/crc_hash.hpp b/include/ocl/crc_hash.hpp
index f0b155c..c782cd3 100644
--- a/include/ocl/crc_hash.hpp
+++ b/include/ocl/crc_hash.hpp
@@ -16,11 +16,11 @@
#include <concepts>
#include <cstring>
-/// @brief Crc32 implementation in C++
-/// @author Amlal El Mahrouss (amlal@nekernel.org)
-
namespace ocl
{
+ /// @brief Crc32 implementation in C++
+ /// @author Amlal El Mahrouss (amlal@nekernel.org)
+
/// @brief implements a trait object for crc hashing.
struct crc_hash_trait
{
@@ -60,23 +60,29 @@ namespace ocl
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d};
- static std::size_t
- crc32(const char* in, std::size_t len) noexcept
+ template <typename algorithm>
+ static constexpr algorithm crc(const char* in, std::size_t len)
{
if (!len)
- return 0;
+ return 0UL;
- std::uint32_t crc = 0xffffffff;
+ algorithm crc = 0xffffffff;
std::size_t cnt = 0;
while ((len--) > 0)
{
- crc = (crc >> 8) ^ crc_array_[(crc ^ in[cnt]) & 0xFF];
+ crc = (crc >> 8) ^ ocl::crc_hash_trait::crc_array_[(crc ^ in[cnt]) & 0xFF];
++cnt;
}
return ~crc;
}
+
+ template <std::uint8_t algorithm>
+ constexpr std::uint8_t crc(const char* in, std::size_t len) = delete;
+
+ template <std::uint64_t algorithm>
+ constexpr std::uint64_t crc(const char* in, std::size_t len) = delete;
};
template <typename V>
@@ -86,6 +92,12 @@ namespace ocl
using hash_map = std::unordered_map<K, V, std::hash<ocl::crc_hash_trait>>;
} // namespace ocl
+inline void
+operator""_crc(const char* in, std::size_t len) noexcept
+{
+ ocl::crc_hash_trait::crc<std::uint32_t>(reinterpret_cast<const char*>(in), strlen(in));
+}
+
namespace std
{
template <>
@@ -97,17 +109,19 @@ namespace std
template <typename T>
inline std::size_t operator()(T* in_) const
{
- return ocl::crc_hash_trait::crc32(reinterpret_cast<const char*>(in_), sizeof(T));
+ return ocl::crc_hash_trait::crc<std::uint32_t>(reinterpret_cast<const char*>(in_), sizeof(T));
}
+ template <bool is_mem = true>
inline std::size_t operator()(const char* in_) const
{
- return ocl::crc_hash_trait::crc32(in_, strlen(in_));
+ return ocl::crc_hash_trait::crc<std::uint32_t>(in_, strlen(in_));
}
+ template <bool is_me = true, bool string_overload = true>
inline std::size_t operator()(const std::string& in_) const
{
- return ocl::crc_hash_trait::crc32(in_.c_str(), in_.size());
+ return ocl::crc_hash_trait::crc<std::uint32_t>(in_.c_str(), in_.size());
}
};
diff --git a/include/ocl/detail/config.hpp b/include/ocl/detail/config.hpp
index 116c001..2efc037 100644
--- a/include/ocl/detail/config.hpp
+++ b/include/ocl/detail/config.hpp
@@ -8,9 +8,8 @@
#ifndef __OCL_CORE_CONFIG
#define __OCL_CORE_CONFIG
-#include <boost/config.hpp>
-
#ifndef __OCL_FREESTANDING
+#include <boost/config.hpp>
#include <boost/core/addressof.hpp>
#include <boost/core/nvp.hpp>
#include <boost/core/demangle.hpp>
@@ -29,10 +28,18 @@
#ifdef __cplusplus
#define OCL_DECL extern "C"
/// DLL/Dylib/So specific macro.
-#define OCL_EXPORT_DECL extern "C" __attribute__((visibility("default")))
+#ifdef __GNUC__
+#define OCL_EXPORT_DECL OCL_DECL __attribute__((visibility("default")))
+#else
+#define OCL_EXPORT_DECL OCL_DECL declspec(dllexport)
+#endif
#else
#define OCL_DECL
+#ifdef __GNUC__
#define OCL_EXPORT_DECL
+#else
+#define OCL_EXPORT_DECL OCL_DECL declspec(dllimport)
+#endif
#endif
#ifdef _WIN32
@@ -40,12 +47,6 @@
#define OCL_WINDOWS 1
#endif
-#ifndef OCL_WINDOWS
-#ifndef __OCL_FREESTANDING
-#include <unistd.h>
-#endif
-#endif
-
#if OCL_WANTS_PRAGMA_ONCE
#define OCL_HAS_PRAGMA_ONCE 1
#endif
diff --git a/include/ocl/io.hpp b/include/ocl/io.hpp
index 135ddc1..834031b 100644
--- a/include/ocl/io.hpp
+++ b/include/ocl/io.hpp
@@ -16,24 +16,24 @@
#include <iostream>
#else
-#define console_io_out ::ocl::io::null_cout
-#define console_io_in ::ocl::io::null_cin
+#define console_io_out ::ocl::io::void_cout
+#define console_io_in ::ocl::io::void_cin
#warning The OCL doesn't define IO streams in a freestanding host.
namespace ocl::io
{
- class nullable_stream final
+ class void_stream final
{
- nullable_stream() = default;
- ~nullable_stream() = default;
+ void_stream() = default;
+ ~void_stream() = default;
- nullable_stream& operator<<(...) = delete;
- nullable_stream& operator>>(...) = delete;
+ void_stream& operator<<(...) = delete;
+ void_stream& operator>>(...) = delete;
}
- inline nullable_stream null_cout;
- inline nullable_stream null_cin;
+ inline void_stream void_cout;
+ inline void_stream void_cin;
} // namespace ocl::io
#endif
diff --git a/include/ocl/is_same.hpp b/include/ocl/is_same.hpp
index 2ad6db5..57d372b 100644
--- a/include/ocl/is_same.hpp
+++ b/include/ocl/is_same.hpp
@@ -59,62 +59,59 @@ namespace ocl
template <typename T>
struct equiv_is_int8 final
{
- private:
- T left_ = 127, right_ = 127;
-
- public:
using result_type = bool;
constexpr result_type hash()
{
- return (left_ + right_) < 1;
+ return sizeof(T) == 1;
}
};
template <typename T>
struct equiv_not_int8 final
{
- private:
- // these shall overflow if not int8.
- T left_ = 127, right_ = 127;
-
- public:
using result_type = bool;
constexpr result_type hash()
{
- return (left_ + right_) > 0;
+ return sizeof(T) > 1;
}
};
template <typename T>
struct equiv_is_real final
{
- private:
- T left_ = 5, right_ = 3;
-
- public:
using result_type = bool;
constexpr result_type hash()
{
- return (left_ / right_) == 1;
+ return sizeof(T) >= 4;
}
};
- template <typename T>
+ template <typename L, typename R>
struct equiv_is final
{
- private:
- T left_{}, right_{};
+ using result_type = bool;
+ using left_type = L;
+ using right_type = R;
- public:
+ constexpr result_type hash()
+ {
+ return false;
+ }
+ };
+
+ template <typename L>
+ struct equiv_is<L, L> final
+ {
using result_type = bool;
- using type = T;
+ using left_type = L;
+ using right_type = L;
constexpr result_type hash()
{
- return (left_ == right_);
+ return true;
}
};
} // namespace ocl