diff --git a/applications/accuracy/mathematics/rump_equation.cpp b/applications/accuracy/mathematics/rump_equation.cpp index 0bbec5268..e3f164db1 100644 --- a/applications/accuracy/mathematics/rump_equation.cpp +++ b/applications/accuracy/mathematics/rump_equation.cpp @@ -251,6 +251,14 @@ There are a couple fpbench versions of it: https://fpbench.org/benchmarks.html# TraceRump1(a, b); // TraceRump1>(a, b); + blas::vector> v{}; + for (int i = 5; i >= -5; --i) { + v.push_back(posit<8,1>(i)); + } + std::cout << "original vector : " << v << '\n'; + auto w = compress,posit<5,1>>(v); + std::cout << "compressed vector : " << w << '\n'; + return EXIT_SUCCESS; } catch (char const* msg) { diff --git a/include/universal/blas/scaling.hpp b/include/universal/blas/scaling.hpp index 7e796112a..215faf4c5 100644 --- a/include/universal/blas/scaling.hpp +++ b/include/universal/blas/scaling.hpp @@ -10,7 +10,7 @@ namespace sw { namespace universal { namespace blas { -// range returns the minimum and maximum value of the vector +// range == regular range: returns the minimum and maximum value of the vector template std::pair range(const Vector& v, unsigned incx = 1) { using Scalar = typename Vector::value_type; @@ -25,9 +25,10 @@ std::pair range(const return std::pair(running_min, running_max); } -// arange returns the absolute minimum and maximum value of the vector +// arange == absolute range: returns the absolute minimum and maximum value of the vector template std::pair arange(const Vector& v, unsigned incx = 1) { + using std::abs; using Scalar = typename Vector::value_type; if (v.size() == 0) return std::pair(Scalar(0), Scalar(0)); Scalar e = abs(v[0]); @@ -74,21 +75,22 @@ blas::vector minmaxscaler(const blas::vector& v, Scalar lb = 0, return t; } -template -blas::vector compress(const blas::vector& v) { - auto maxpos = double(std::numeric_limits::max()); +template +blas::vector compress(const blas::vector& v) { + using std::sqrt, std::abs; auto vminmax = arange(v); -// auto minValue = vminmax.first; - auto maxValue = vminmax.second; - - sw::universal::blas::vector t(v.size()); - auto sqrtMaxpos = sqrt(maxpos); + SrcType maxValue = vminmax.second; + SrcType maxpos = double(std::numeric_limits::max()); // this is not entirely correct + // but we don't have a general conversion between SrcType and TgtType, so we are using a double conversion as intermediate + SrcType sqrtMaxpos = sqrt(maxpos); //std::cout << "maxValue : " << maxValue << " sqrt(maxpos) : " << sqrtMaxpos << '\n'; - double maxScale = 1.0; + SrcType maxScale{ 1.0 }; if (abs(maxValue) >= sqrtMaxpos) maxScale = sqrtMaxpos / maxValue; //std::cout << "scale factor : " << maxScale << '\n'; - t = maxScale * v; + // now create the compressed vector storage + sw::universal::blas::vector t(v.size()); + t = maxScale * v; // and compress //std::cout << "compressed vector : " << t << '\n'; return t;