summaryrefslogtreecommitdiff
path: root/src/utilities
diff options
context:
space:
mode:
Diffstat (limited to 'src/utilities')
-rw-r--r--src/utilities/utilities.cpp72
-rw-r--r--src/utilities/utilities.hpp8
2 files changed, 70 insertions, 10 deletions
diff --git a/src/utilities/utilities.cpp b/src/utilities/utilities.cpp
index 5e445bb9..fd198c0d 100644
--- a/src/utilities/utilities.cpp
+++ b/src/utilities/utilities.cpp
@@ -46,6 +46,30 @@ double2 GetScalar() {
return {2.0, 0.5};
}
+// Returns a scalar of value 0
+template <typename T>
+T ConstantZero() {
+ return static_cast<T>(0.0);
+}
+template float ConstantZero<float>();
+template double ConstantZero<double>();
+
+// Specialized version of the above for half-precision
+template <>
+half ConstantZero() {
+ return FloatToHalf(0.0f);
+}
+
+// Specialized versions of the above for complex data-types
+template <>
+float2 ConstantZero() {
+ return {0.0f, 0.0f};
+}
+template <>
+double2 ConstantZero() {
+ return {0.0, 0.0};
+}
+
// Returns a scalar of value 1
template <typename T>
T ConstantOne() {
@@ -70,6 +94,30 @@ double2 ConstantOne() {
return {1.0, 0.0};
}
+// Returns a scalar of value -1
+template <typename T>
+T ConstantNegOne() {
+ return static_cast<T>(-1.0);
+}
+template float ConstantNegOne<float>();
+template double ConstantNegOne<double>();
+
+// Specialized version of the above for half-precision
+template <>
+half ConstantNegOne() {
+ return FloatToHalf(-1.0f);
+}
+
+// Specialized versions of the above for complex data-types
+template <>
+float2 ConstantNegOne() {
+ return {-1.0f, 0.0f};
+}
+template <>
+double2 ConstantNegOne() {
+ return {-1.0, 0.0};
+}
+
// =================================================================================================
// Implements the string conversion using std::to_string if possible
@@ -79,23 +127,27 @@ std::string ToString(T value) {
}
template std::string ToString<int>(int value);
template std::string ToString<size_t>(size_t value);
-template std::string ToString<float>(float value);
-template std::string ToString<double>(double value);
+template <>
+std::string ToString(float value) {
+ std::ostringstream result;
+ result << std::fixed << std::setprecision(2) << value;
+ return result.str();
+}
+template <>
+std::string ToString(double value) {
+ std::ostringstream result;
+ result << std::fixed << std::setprecision(2) << value;
+ return result.str();
+}
// If not possible directly: special cases for complex data-types
template <>
std::string ToString(float2 value) {
- std::ostringstream real, imag;
- real << std::setprecision(2) << value.real();
- imag << std::setprecision(2) << value.imag();
- return real.str()+"+"+imag.str()+"i";
+ return ToString(value.real())+"+"+ToString(value.imag())+"i";
}
template <>
std::string ToString(double2 value) {
- std::ostringstream real, imag;
- real << std::setprecision(2) << value.real();
- imag << std::setprecision(2) << value.imag();
- return real.str()+"+"+imag.str()+"i";
+ return ToString(value.real())+"+"+ToString(value.imag())+"i";
}
// If not possible directly: special case for half-precision
diff --git a/src/utilities/utilities.hpp b/src/utilities/utilities.hpp
index a1d4e2db..757f1b5e 100644
--- a/src/utilities/utilities.hpp
+++ b/src/utilities/utilities.hpp
@@ -102,10 +102,18 @@ constexpr auto kArgNoAbbreviations = "no_abbrv";
template <typename T>
T GetScalar();
+// Returns a scalar of value 0
+template <typename T>
+T ConstantZero();
+
// Returns a scalar of value 1
template <typename T>
T ConstantOne();
+// Returns a scalar of value -1
+template <typename T>
+T ConstantNegOne();
+
// =================================================================================================
// Structure containing all possible arguments for test clients, including their default values