[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master d9bdcd4d 5/5: Remove UB
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master d9bdcd4d 5/5: Remove UB |
Date: |
Thu, 2 Jun 2022 20:13:04 -0400 (EDT) |
branch: master
commit d9bdcd4d7f4821bf51619c2b948e5083757e2f4b
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Remove UB
Comparing the original (with UB) to this commit (using unsigned int)
and also to a less elegant alternative using an int64_t instead of
unsigned int, unit-test timings are:
// original unsigned int64_t
// GNU/Linux 13300 15350 16400 [default flags]
// GNU/Linux 13450 12300 13400 -O3 -fno-omit-frame-pointer
// msw 13400 12400 12400 [default flags]
// msw 13400 12400 12400 -O3 -fno-omit-frame-pointer
For the gcc-10 msw builds, either non-UB alternative is faster. For the
gcc-11 GNU/Linux builds, this commit is discernibly slower with '-O2'
(lmi's usual optimization level), but faster with '-O3'.
Oddly, the usual lmi speed test shows a small but opposite effect:
GNU/Linux became about half a percent faster, while msw became two or
three percent slower.
---
Speed_gcc_x86_64-pc-linux-gnu | 12 ++++++------
Speed_gcc_x86_64-w64-mingw32 | 12 ++++++------
bin_exp.hpp | 8 +++++---
3 files changed, 17 insertions(+), 15 deletions(-)
diff --git a/Speed_gcc_x86_64-pc-linux-gnu b/Speed_gcc_x86_64-pc-linux-gnu
index 8f5e8c83..d25f5d37 100644
--- a/Speed_gcc_x86_64-pc-linux-gnu
+++ b/Speed_gcc_x86_64-pc-linux-gnu
@@ -1,7 +1,7 @@
Test speed:
- naic, no solve : 1.301e-02 s mean; 12880 us least of 77 runs
- naic, specamt solve : 2.722e-02 s mean; 26775 us least of 37 runs
- naic, ee prem solve : 2.395e-02 s mean; 23725 us least of 42 runs
- finra, no solve : 4.972e-03 s mean; 4781 us least of 100 runs
- finra, specamt solve: 1.807e-02 s mean; 17653 us least of 56 runs
- finra, ee prem solve: 1.603e-02 s mean; 15718 us least of 63 runs
+ naic, no solve : 1.285e-02 s mean; 12692 us least of 78 runs
+ naic, specamt solve : 2.694e-02 s mean; 26677 us least of 38 runs
+ naic, ee prem solve : 2.364e-02 s mean; 23478 us least of 43 runs
+ finra, no solve : 4.963e-03 s mean; 4773 us least of 100 runs
+ finra, specamt solve: 1.782e-02 s mean; 17609 us least of 57 runs
+ finra, ee prem solve: 1.585e-02 s mean; 15495 us least of 64 runs
diff --git a/Speed_gcc_x86_64-w64-mingw32 b/Speed_gcc_x86_64-w64-mingw32
index b6a47327..c0af4c79 100644
--- a/Speed_gcc_x86_64-w64-mingw32
+++ b/Speed_gcc_x86_64-w64-mingw32
@@ -1,7 +1,7 @@
Test speed:
- naic, no solve : 2.752e-02 s mean; 26034 us least of 37 runs
- naic, specamt solve : 4.784e-02 s mean; 46855 us least of 21 runs
- naic, ee prem solve : 4.305e-02 s mean; 42312 us least of 24 runs
- finra, no solve : 1.590e-02 s mean; 15481 us least of 63 runs
- finra, specamt solve: 3.408e-02 s mean; 33505 us least of 30 runs
- finra, ee prem solve: 3.130e-02 s mean; 30774 us least of 32 runs
+ naic, no solve : 2.833e-02 s mean; 27762 us least of 36 runs
+ naic, specamt solve : 4.861e-02 s mean; 47763 us least of 21 runs
+ naic, ee prem solve : 4.412e-02 s mean; 43033 us least of 23 runs
+ finra, no solve : 1.642e-02 s mean; 14582 us least of 61 runs
+ finra, specamt solve: 3.501e-02 s mean; 34512 us least of 29 runs
+ finra, ee prem solve: 3.233e-02 s mean; 31589 us least of 31 runs
diff --git a/bin_exp.hpp b/bin_exp.hpp
index ba337a61..8188d608 100644
--- a/bin_exp.hpp
+++ b/bin_exp.hpp
@@ -24,6 +24,8 @@
#include "config.hpp"
+#include "math_functions.hpp" // u_abs()
+
#include <type_traits> // is_floating_point_v
/// Binary method for exponentiation.
@@ -59,11 +61,11 @@
/// unsigned values anyway.
template<typename T>
-constexpr T bin_exp(T x, int n)
+constexpr T bin_exp(T x, int exponent)
{
static_assert(std::is_floating_point_v<T>);
- bool negative_exponent {n < 0};
- if(negative_exponent) n = -n;
+ bool negative_exponent {exponent < 0};
+ unsigned int n = u_abs(exponent);
T y = 1;
for(;;)
{