[libc-commits] [libc] [libc][stdfix] Implement `idivfx` functions in LLVM libc (PR #133005)

via libc-commits libc-commits at lists.llvm.org
Thu Mar 27 08:17:50 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:

`LIBC_CRASH_ON_VALUE(var, value)` sounds a bit more generic.

https://github.com/llvm/llvm-project/pull/133005


More information about the libc-commits mailing list