mirror of
https://github.com/sendyne/cppreg.git
synced 2025-05-09 23:24:05 +00:00
* Revise and simplify most enable_if statements in the project. * Update code style in all files. * Update copyright year. * Add a dedicated Memory.h header with the memory device implementation. * Update documentation. * CMake cleanup.
55 lines
1.4 KiB
C++
55 lines
1.4 KiB
C++
//! Bit mask implementation.
|
|
/**
|
|
* @file Mask.h
|
|
* @author Nicolas Clauvelin (nclauvelin@sendyne.com)
|
|
* @copyright Copyright 2010-2019 Sendyne Corp. All rights reserved.
|
|
*
|
|
* The implementation is designed to compute masks prior to runtime by
|
|
* relying on constexpr function. This will work as intended if the function
|
|
* argument is known at compile time, otherwise it will be evaluated at runtime.
|
|
*/
|
|
|
|
|
|
#ifndef CPPREG_MASK_H
|
|
#define CPPREG_MASK_H
|
|
|
|
|
|
#include "cppreg_Defines.h"
|
|
|
|
|
|
namespace cppreg {
|
|
|
|
|
|
//! Mask constexpr function.
|
|
/**
|
|
* @tparam Mask Mask data type (will be derived from register).
|
|
* @param width Mask width.
|
|
* @return The mask value.
|
|
*/
|
|
template <typename Mask>
|
|
constexpr Mask make_mask(const FieldWidth width) noexcept {
|
|
return width == 0 ? static_cast<Mask>(0u)
|
|
: static_cast<Mask>(
|
|
(make_mask<Mask>(FieldWidth(width - 1)) << 1) | 1);
|
|
}
|
|
|
|
|
|
//! Shifted mask constexpr function.
|
|
/**
|
|
* @tparam Mask Mask data type (will be derived from register).
|
|
* @param width Mask width.
|
|
* @param offset Mask offset.
|
|
* @return The mask value.
|
|
*/
|
|
template <typename Mask>
|
|
constexpr Mask make_shifted_mask(const FieldWidth width,
|
|
const FieldOffset offset) noexcept {
|
|
return static_cast<Mask>(make_mask<Mask>(width) << offset);
|
|
}
|
|
|
|
|
|
} // namespace cppreg
|
|
|
|
|
|
#endif // CPPREG_MASK_H
|