lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master ba44bdb9 07/13: Assume IEEE 754 division by z


From: Greg Chicares
Subject: [lmi-commits] [lmi] master ba44bdb9 07/13: Assume IEEE 754 division by zero where desirable
Date: Fri, 10 Jun 2022 21:09:38 -0400 (EDT)

branch: master
commit ba44bdb96d62e6895dbb409a0f1b5973392d383e
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Assume IEEE 754 division by zero where desirable
    
    In this case, division by zero arises naturally, and IEEE 754 DTRT.
    Suppress a UBSan complaint because IEEE 754 is virtually universal.
---
 bin_exp.hpp | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/bin_exp.hpp b/bin_exp.hpp
index 157b994a..05106a1e 100644
--- a/bin_exp.hpp
+++ b/bin_exp.hpp
@@ -26,6 +26,7 @@
 
 #include "math_functions.hpp"           // u_abs()
 
+#include <limits>
 #include <type_traits>                  // is_floating_point_v
 
 /// Binary method for exponentiation.
@@ -59,11 +60,22 @@
 ///   (-1 >>1) = -1;
 /// and twenty-first-century optimizers generate the same code for
 /// unsigned values anyway.
+///
+/// The last line conditionally forms the reciprocal of 'y', which
+/// C++20 says is UB if ±0.0 == y. However, it's well defined by
+/// IEEE 754, and lmi is built with gcc's '-frounding-math' to choose
+/// IEEE 754 behavior, so it would be unreasonable for a compiler to
+/// perform any optimization that assumes otherwise. See:
+///   https://bugs.llvm.org/show_bug.cgi?id=19535#c1
 
 template<typename T>
+#if defined LMI_GCC || defined LMI_CLANG
+__attribute__((no_sanitize("float-divide-by-zero")))
+#endif // defined LMI_GCC || defined LMI_CLANG
 constexpr T bin_exp(T x, int exponent)
 {
     static_assert(std::is_floating_point_v<T>);
+    static_assert(std::numeric_limits<T>::is_iec559);
     bool is_exponent_negative {exponent < 0};
     unsigned int n = u_abs(exponent);
     T y = 1;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]