[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;
- [lmi-commits] [lmi] master updated (60df9bef -> ae3e7881), Greg Chicares, 2022/06/10
- [lmi-commits] [lmi] master c77dfa03 01/13: Routinely make 'system_test' with UBSan, Greg Chicares, 2022/06/10
- [lmi-commits] [lmi] master 2ca30a6e 09/13: Specify $ASAN_OPTIONS in anticipation of using ASan, Greg Chicares, 2022/06/10
- [lmi-commits] [lmi] master 8fba89d4 11/13: Harden ELF linking, just because we can, Greg Chicares, 2022/06/10
- [lmi-commits] [lmi] master 8340ac54 04/13: Fix defect introduced 20050114T1947Z: use before initialization, Greg Chicares, 2022/06/10
- [lmi-commits] [lmi] master 70d45fa9 12/13: Add many sanitizer options, Greg Chicares, 2022/06/10
- [lmi-commits] [lmi] master 49fbebcb 02/13: Record speed measurements, Greg Chicares, 2022/06/10
- [lmi-commits] [lmi] master ba44bdb9 07/13: Assume IEEE 754 division by zero where desirable,
Greg Chicares <=
- [lmi-commits] [lmi] master 21263558 10/13: Ask gcc to comply with IEEE 754 more completely, Greg Chicares, 2022/06/10
- [lmi-commits] [lmi] master 7d702ce2 06/13: Replace std::vector with std::array, Greg Chicares, 2022/06/10
- [lmi-commits] [lmi] master ade6b59e 03/13: Fix defect introduced 20170221T0207Z: potential UB, Greg Chicares, 2022/06/10
- [lmi-commits] [lmi] master 4f2f4bf7 05/13: Avoid gratuitous UB, Greg Chicares, 2022/06/10
- [lmi-commits] [lmi] master eabe76c5 08/13: Specify $UBSAN_OPTIONS OAOO in script, Greg Chicares, 2022/06/10
- [lmi-commits] [lmi] master ae3e7881 13/13: Strengthen warnings, Greg Chicares, 2022/06/10