lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master d36e5145 03/11: Suppress undefined behavior;


From: Greg Chicares
Subject: [lmi-commits] [lmi] master d36e5145 03/11: Suppress undefined behavior; add a test with defined behavior
Date: Tue, 31 May 2022 17:52:05 -0400 (EDT)

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

    Suppress undefined behavior; add a test with defined behavior
    
    The 'a4' case exhibits UB for LLP64, though not for LP64.
---
 bin_exp_test.cpp | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/bin_exp_test.cpp b/bin_exp_test.cpp
index 8097ef62..9b7582e0 100644
--- a/bin_exp_test.cpp
+++ b/bin_exp_test.cpp
@@ -277,6 +277,7 @@ void test_quodlibet()
         (static_cast<double>(std::numeric_limits<double>::radix)
         ,                    std::numeric_limits<double>::digits
         );
+#if 0
     // This compiles, but its behavior is undefined unless an int
     // is at least 54 bits (53, + 1 for sign). Otherwise it cannot
     // return the hoped-for answer, and may return zero.
@@ -284,15 +285,26 @@ void test_quodlibet()
         (                    std::numeric_limits<double>::radix
         ,                    std::numeric_limits<double>::digits
         );
+    stifle_unused_warning(a3);
+    // This compiles, but its behavior is undefined unless a long int
+    // is at least 54 bits (53, + 1 for sign). Otherwise it cannot
+    // return the hoped-for answer, and may return zero.
     auto a4 = nonstd::power
         (static_cast<long int>(std::numeric_limits<double>::radix)
         ,static_cast<long int>(std::numeric_limits<double>::digits)
         );
+    stifle_unused_warning(a4);
+#endif // 0
+    // Type long long int is required to be at least 64 bits wide,
+    // so this cannot overflow.
+    auto a5 = nonstd::power
+        (static_cast<long long int>(std::numeric_limits<double>::radix)
+        ,static_cast<long long int>(std::numeric_limits<double>::digits)
+        );
     LMI_TEST_EQUAL(9007199254740992, a0);
     LMI_TEST_EQUAL(9007199254740992, a1);
     LMI_TEST_EQUAL(9007199254740992, a2);
-    stifle_unused_warning(a3);
-    stifle_unused_warning(a4);
+    LMI_TEST_EQUAL(9007199254740992, a5);
 }
 
 void mete0()



reply via email to

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