diff options
author | BaiXiaochun <bai.xiaochun.mofan@protonmail.com> | 2021-06-27 18:38:10 +0200 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2021-06-29 23:18:47 +0200 |
commit | 9c15dea0b2192d231b65175291a7655122c2e24c (patch) | |
tree | 960f8a4b94331056a4ca8400f8088a1bc5496d05 /sal | |
parent | a40334ef4deb03c207b7f959a0a9c93ef3e5c459 (diff) |
Simplify expresion
Reduce operation count by space / speed tradeoff.
This expression is widely used in LO.
Then needs to be fast.
Change-Id: Ic88cf15d451ec95a8ad6da88cd9f601cf2876871
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/117954
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Tested-by: Jenkins
Diffstat (limited to 'sal')
-rw-r--r-- | sal/rtl/math.cxx | 29 |
1 files changed, 8 insertions, 21 deletions
diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx index 7db362f35cfe..88354763b2b4 100644 --- a/sal/rtl/math.cxx +++ b/sal/rtl/math.cxx @@ -44,33 +44,20 @@ #include <dtoa.h> -int const n10Count = 16; -double const n10s[2][n10Count] = { - { 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, - 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16 }, - { 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7, 1e-8, - 1e-9, 1e-10, 1e-11, 1e-12, 1e-13, 1e-14, 1e-15, 1e-16 } +constexpr int n10Count = 16; +constexpr double n10s[n10Count*2+1] = { + 1e-16, 1e-15, 1e-14, 1e-13, 1e-12, 1e-11, 1e-10, 1e-9, + 1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1e0, + 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, + 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16 , }; // return pow(10.0,nExp) optimized for exponents in the interval [-16,16] static double getN10Exp(int nExp) { - if (nExp < 0) - { - // && -nExp > 0 necessary for std::numeric_limits<int>::min() - // because -nExp = nExp - if (-nExp <= n10Count && -nExp > 0) - return n10s[1][-nExp-1]; + if (nExp < -n10Count || nExp > n10Count) return pow(10.0, static_cast<double>(nExp)); - } - if (nExp > 0) - { - if (nExp <= n10Count) - return n10s[0][nExp-1]; - - return pow(10.0, static_cast<double>(nExp)); - } - return 1.0; + return n10s[nExp + n10Count]; } namespace { |