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

Krishna Pandey via libc-commits libc-commits at lists.llvm.org
Wed Mar 26 13:23:20 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);
+  }
----------------
krishna2803 wrote:

can we do something like:
```c++
#if defined(LIBC_ADD_FIXED_POINT_CHECKS)
#define LIBC_CRASH_ON_ZERO_DENOM(den, zero)                                    \
  do {                                                                         \
    if (LIBC_UNLIKELY((den) == (zero)))                                        \
      __builtin_trap();                                                        \
  } while (0)
#endif
```

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


More information about the libc-commits mailing list