refactor: use standard integer types; remove <algorithm> dependency
- Replaced unsigned char, int and long long with uint8_t int32_t and int64_t for platform independence - Removed <algorithm> dependency, implemented custom alternatives
This commit is contained in:
+48
-38
@@ -4,52 +4,62 @@
|
||||
|
||||
#ifndef CX9261ADRIVER_UTILS_H
|
||||
#define CX9261ADRIVER_UTILS_H
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <bitset>
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
namespace CX9261A {
|
||||
#if __cplusplus >= 201703L
|
||||
template <typename T>
|
||||
constexpr const T& clamp(const T& v, const T& lo, const T& hi) {
|
||||
return std::clamp(v, lo, hi);
|
||||
if (v < lo) return lo;
|
||||
if (v > hi) return hi;
|
||||
return v;
|
||||
}
|
||||
#else
|
||||
|
||||
template <typename Float>
|
||||
std::int32_t round32(Float f) {
|
||||
return static_cast<std::int32_t>(std::round(f));
|
||||
}
|
||||
|
||||
template <typename Float>
|
||||
std::int64_t round64(Float f) {
|
||||
return static_cast<std::int64_t>(std::round(f));
|
||||
}
|
||||
|
||||
template <typename Float>
|
||||
std::int32_t floor32(Float f) {
|
||||
return static_cast<std::int32_t>(std::floor(f));
|
||||
}
|
||||
|
||||
template <typename Float>
|
||||
std::int64_t floor64(Float f) {
|
||||
return static_cast<std::int64_t>(std::floor(f));
|
||||
}
|
||||
|
||||
template <typename Float>
|
||||
std::int32_t ceil32(Float f) {
|
||||
return static_cast<std::int32_t>(std::ceil(f));
|
||||
}
|
||||
|
||||
template <typename Float>
|
||||
std::int64_t ceil64(Float f) {
|
||||
return static_cast<std::int64_t>(std::ceil(f));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr const T& clamp(const T& v, const T& lo, const T& hi) {
|
||||
return std::max(std::min(hi, v), lo);
|
||||
}
|
||||
#endif
|
||||
|
||||
template <typename Float>
|
||||
int round(Float f) {
|
||||
return static_cast<int>(std::round(f));
|
||||
size_t search_first_ge(const std::vector<T>& arr, const T& value) {
|
||||
size_t ret = arr.size(); // 如果搜索失败默认返回数组长度
|
||||
for (size_t i = 0; i < arr.size(); ++i) {
|
||||
if (arr[i] >= value) {
|
||||
// 找到更小的元素时更新下标
|
||||
if (ret == arr.size() || arr[i] < arr[ret]) {
|
||||
ret = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename Float>
|
||||
long long round64(Float f) {
|
||||
return static_cast<long long>(std::round(f));
|
||||
}
|
||||
|
||||
template <typename Float>
|
||||
int floor(Float f) {
|
||||
return static_cast<int>(std::floor(f));
|
||||
}
|
||||
|
||||
template <typename Float>
|
||||
long long floor64(Float f) {
|
||||
return static_cast<long long>(std::floor(f));
|
||||
}
|
||||
|
||||
template <typename Float>
|
||||
int ceil(Float f) {
|
||||
return static_cast<int>(std::ceil(f));
|
||||
}
|
||||
|
||||
template <typename Float>
|
||||
long long ceil64(Float f) {
|
||||
return static_cast<long long>(std::ceil(f));
|
||||
}
|
||||
/**
|
||||
* @brief Register类,提供方便的位操作函数
|
||||
*/
|
||||
@@ -63,7 +73,7 @@ public:
|
||||
/**
|
||||
* 以十进制返回寄存器值
|
||||
*/
|
||||
unsigned char value() const { return static_cast<unsigned char>(m_value.to_ulong()); }
|
||||
std::uint8_t value() const { return static_cast<std::uint8_t>(m_value.to_ulong()); }
|
||||
/**
|
||||
* 对寄存器值进行部分值的替换操作
|
||||
* 例:replace(7, 4, 0b1111); 使寄存器的高四位为1
|
||||
@@ -71,7 +81,7 @@ public:
|
||||
* @param end 中止位
|
||||
* @param after 要替换的值
|
||||
*/
|
||||
void replace(const unsigned int start, const unsigned int end, unsigned char after) {
|
||||
void replace(const std::uint8_t start, const std::uint8_t end, std::uint8_t after) {
|
||||
for (auto i = end; i <= start; ++i) {
|
||||
m_value[i] = (after >> (i - end)) & 0b1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user