对于32bit平台进行64bit的除法操作时,不能直接计算。需要使用专门的除法操作相关函数,下面的参考链接有64bit除法的相关算法,是基于div64.h中的do_div(n, base)实现的。
一.以下是错误示例:
uint64 a,b; uint32 c; a = a/c; a = a/b; int64 ia,ib; int32 ic; a = ia/ic; a = ia/ib;二.以下是linux lib/math/div64.c的函数相关注释,可供参考。[1][2]
//一 @note 无符号64位被除数➗无符号32位除数=(无符号32位商+无符号32位余数) @parameter uint64_t *n 传入无符号被除数,函数运行存放无符号商。 @parameter uint32_t base 无符号除数。 @return uint32_t 无符号余数。 uint32_t __attribute__((weak)) __div64_32(uint64_t *n, uint32_t base) //二 @note 有符号64位被除数➗有符号64位除数=(有符号64位商+有符号64位余数) @parameter s64 dividend 有符号被除数。 @parameter s64 divisor 有符号除数。 @parameter s64 *remainder 有符号余数的指针。 @return s64,有符号商。 s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder) //三 @note 无符号64位被除数➗无符号64位除数=(无符号64位商+无符号64位余数) @parameter u64 dividend 无符号被除数。 @parameter u64 divisor 无符号除数。 @parameter u64 *remainder 无符号余数的指针。 @return u64,无符号商。 u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder) //四 @note 无符号64位被除数➗无符号64位除数=(无符号64位商+无符号64位余数) @parameter u64 dividend 无符号被除数。 @parameter u64 divisor 无符号除数。 @return u64,无符号商。 u64 div64_u64(u64 dividend, u64 divisor) //五 @note 有符号64位被除数➗有符号64位除数=(有符号64位商+有符号64位余数) @parameter s64 dividend 有符号被除数。 @parameter s64 divisor 有符号除数。 @return s64,有符号商。 s64 div64_s64(s64 dividend, s64 divisor)reference:
[1]
div64.c - lib/math/div64.c - Linux source code (v6.7.9) - Bootlin
Elixir Cross Referencer - Explore source code in your browser - Particularly useful for the Linux kernel and other low-level projects in C/C++ (bootloaders, C libraries...)
https://elixir.bootlin.com/linux/latest/source/lib/math/div64.c#L102
[2]
math64.h - include/linux/math64.h - Linux source code (v6.7.9) - Bootlin
Elixir Cross Referencer - Explore source code in your browser - Particularly useful for the Linux kernel and other low-level projects in C/C++ (bootloaders, C libraries...)
https://elixir.bootlin.com/linux/latest/source/include/linux/math64.h
Disclaimer / 免责声明:
本文仅代表作者在撰写和修改时的个人观点,不代表当前或未来立场。文中观点和内容未经学术机构或标准组织验证,作者不对其准确性、完整性或可靠性作任何保证。请读者仅供参考,并自行核实相关信息。本文旨在探索与经验分享,因篇幅及作者水平所限,难免存在疏漏,欢迎批评指正。如有问题或交流建议,请联系:flourishinggarden@outlook.com。
copyright / 版权声明:
本文为作者原创。引用或转载需注明“转自或引用自 flourishinggarden@outlook.com”。 未经注明来源的擅自使用、抄袭或传播行为均被禁止。