1
0

fix: fix comment for new added files.

- translate zh-CN comment into en-US.
- change some comment into Doxygen style.
- add lost Doxygen comment.
- enrich the testbench for ceil_div.
- add lost metaprogramming functions for some files in macro namespace.
This commit is contained in:
2025-08-11 21:57:42 +08:00
parent 17540072d3
commit 20a9ef4166
8 changed files with 419 additions and 187 deletions

View File

@@ -3,32 +3,35 @@
#include <concepts>
/**
* @brief The namespace providing function relative robust numeric operations.
* @brief The namespace providing functions for robust numeric operations.
* @details
* 在使用Rust编写一些程序后深刻地认识到Rust中针对基本类型的运算符的丰富程度。
* 提供了诸如整数向上取整除法等便利操作。
* 因此我在这个命名空间里复刻Rust中的这些便利功能。
* After writing some programs in Rust, I deeply appreciated the richness of operators
* for primitive types in Rust, which provides convenient operations like ceiling integral division.
* Therefore, I replicate these convenient features from Rust in this namespace.
*
* 由于没有需求,目前暂未实现以下功能:
* \li 仅支持无符号整数向上取整除法。
* \li 显式指定运算溢出行为
* Currently unimplemented features due to lack of demand:
* \li Only supports unsigned integer ceiling division
*/
namespace yycc::num::op {
/**
* @brief 无符号整数的向上整除
* @brief Unsigned integer ceiling division
* @details
* 执行两个无符号整数之间的整除,并将结果向上取整。
* 该函数可能在很多加密函数中进行使用以分配合适空间。
* @tparam T 除法操作建立在的无符号整数类型
* @param[in] lhs 左操作数
* @param[in] rhs 右操作数
* @return 向上取整的整除结果
* Performs division between two unsigned integers and rounds up the result.
* @exception std::logic_error If the divisor is zero
* @tparam T The unsigned integer type for division operation
* @param[in] lhs Left operand
* @param[in] rhs Right operand
* @return Ceiling division result
*/
template<typename T>
requires std::unsigned_integral<T>
T div_ceil(T lhs, T rhs) {
// Check divisor first
if (rhs == 0) throw std::logic_error("div with 0");
// YYC MARK:
// We use this algorithm, instead of traditional `(lhs + rhs - 1) / rhs`,
// which may have unsafe overflow case.
return (lhs % rhs == 0) ? (lhs / rhs) : (lhs / rhs) + 1u;
}