diff options
Diffstat (limited to 'lib/dhcp/utils.h')
-rw-r--r-- | lib/dhcp/utils.h | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/dhcp/utils.h b/lib/dhcp/utils.h new file mode 100644 index 0000000..9016007 --- /dev/null +++ b/lib/dhcp/utils.h @@ -0,0 +1,31 @@ +// Copyright (c) 2022 Johannes Stoelp + +#ifndef UTILS_H +#define UTILS_H + +#include <type_traits> + +// Convert from an underlying enum type into an enum variant. +template<typename E> +constexpr E from_raw(std::underlying_type_t<E> u) { + static_assert(std::is_enum_v<E>); + return static_cast<E>(u); +} + +// Convert from an enum variant into an underlying enum type. +template<typename E> +constexpr std::underlying_type_t<E> into_raw(E e) { + static_assert(std::is_enum_v<E>); + return static_cast<std::underlying_type_t<E>>(e); +} + +// Simple cyclic rotation hash function. +constexpr u32 hash(const u8* data, usize len) { + u32 hash = 0xa5a55a5a /* seed */; + for (usize i = 0; i < len; ++i) { + hash += ((hash << 25) | (hash >> 7) /* rrot(7) */) ^ data[i]; + } + return hash; +} + +#endif |