[libc-commits] [libc] [libc][stdfix] Implement `idivfx` functions in LLVM libc (PR #133005)
via libc-commits
libc-commits at lists.llvm.org
Wed Mar 26 11:28:35 PDT 2025
================
@@ -201,6 +201,30 @@ bitsfx(T f) {
return cpp::bit_cast<XType, T>(f);
}
+// divide the two fixed-point types and return an integer result
+template <typename T, typename XType>
+LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_fixed_point_v<T>, XType>
+idiv(T x, T y) {
+ using FXBits = FXBits<T>;
+ using FXRep = FXRep<T>;
+ using CompType = typename FXRep::CompType;
+
+ if (y == FXRep::ZERO()) {
+ // If the value of the second operand of the / operator is zero, the
+ // behavior is undefined. Ref: ISO/IEC TR 18037:2008(E) p.g. 16
+ return static_cast<XType>(0);
+ }
----------------
lntue wrote:
Probably we can have some optional hardening macro/option for division by 0 similar to https://github.com/llvm/llvm-project/blob/main/libc/src/__support/macros/null_check.h?
https://github.com/llvm/llvm-project/pull/133005
More information about the libc-commits
mailing list