From 7367903fd4470a1e1b0724ddccf4258218f3bb95 Mon Sep 17 00:00:00 2001 From: Ravenwater Date: Tue, 10 Sep 2024 11:16:23 -0400 Subject: [PATCH] adding a playground skeleton for lazy evaluation experiments --- playground/CMakeLists.txt | 1 + playground/float_to_decimal_string.cpp | 4 +- playground/lazy_evaluation.cpp | 114 +++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 playground/lazy_evaluation.cpp diff --git a/playground/CMakeLists.txt b/playground/CMakeLists.txt index c69dd4f8c..ad1f526c1 100644 --- a/playground/CMakeLists.txt +++ b/playground/CMakeLists.txt @@ -9,6 +9,7 @@ set(REAL_SRCS efunc_posits.cpp skeleton.cpp type_test.cpp float_to_decimal_string.cpp + lazy_evaluation.cpp ) compile_all("true" "playground" "Playground" "${REAL_SRCS}") diff --git a/playground/float_to_decimal_string.cpp b/playground/float_to_decimal_string.cpp index 0f9465f99..ad3d23ab0 100644 --- a/playground/float_to_decimal_string.cpp +++ b/playground/float_to_decimal_string.cpp @@ -15,7 +15,7 @@ int main(int argc, char** argv) try { - using namespace sw::universal; + using namespace sw::universal; float f = 3.14156; float x = f * 1e6; @@ -50,7 +50,7 @@ try { std::cout << "Custom conversion: " << S << std::endl; - return EXIT_SUCCESS; + return EXIT_SUCCESS; } catch (char const* msg) { std::cerr << "Caught exception: " << msg << std::endl; diff --git a/playground/lazy_evaluation.cpp b/playground/lazy_evaluation.cpp new file mode 100644 index 000000000..a0c525a6f --- /dev/null +++ b/playground/lazy_evaluation.cpp @@ -0,0 +1,114 @@ +// lazy_evaluation.cpp: experiments in lazy evaluation and state management +// +// Copyright (C) 2017 Stillwater Supercomputing, Inc. +// SPDX-License-Identifier: MIT +// +// This file is part of the universal numbers project, which is released under an MIT Open Source license. +#include +#include + +#include + +namespace test1 { + template + struct Expression { + T value; + + Expression(T v) : value(v) {} + + template + Expression operator+(const Expression& other) const { + return Expression(value + other.value); + } + + template + Expression operator*(const Expression& other) const { + return Expression(value * other.value); + } + + operator T() const { return value; } + + }; +} + +namespace test2 { + + template + class Expression { + private: + T value; + + public: + Expression(T value) : value(value) {} + + template + auto operator+(const Expression& other) const -> decltype(value + other.value) { + return Expression(value + other.value); + } + + template + auto operator*(const Expression& other) const -> decltype(value * other.value) { + return Expression(value * other.value); + } + + operator T() const { return T(value); } + + }; +} + +int main(int argc, char** argv) +try { + using namespace sw::universal; + + // Expression as defined above only works on native types as some implicit conversions make the class definition simpler + // precondition is definition of operator+(), operator*(), and conversion operator + + + { + using Real = float; + test1::Expression a(2.0), b(3.0), c(4.0); + + test1::Expression result = a * (b + c); + + std::cout << result << '\n'; + } + + { + using Real = cfloat<24,8, uint32_t, true, false, false>; + + test2::Expression a(2.5), b(3.0), c(4.0); + + // test2::Expression result = a * (b + c); + // required: binary operator that supports: Expression * cfloat + + test2::Expression tmpSum = (b + c); + + test2::Expression result = a * tmpSum; + + Real value = Real(result); + + std::cout << value << '\n'; + } + + return EXIT_SUCCESS; +} +catch (char const* msg) { + std::cerr << "Caught exception: " << msg << std::endl; + return EXIT_FAILURE; +} +catch (const sw::universal::universal_arithmetic_exception& err) { + std::cerr << "Uncaught universal arithmetic exception: " << err.what() << std::endl; + return EXIT_FAILURE; +} +catch (const sw::universal::universal_internal_exception& err) { + std::cerr << "Uncaught universal internal exception: " << err.what() << std::endl; + return EXIT_FAILURE; +} +catch (const std::runtime_error& err) { + std::cerr << "Uncaught runtime exception: " << err.what() << std::endl; + return EXIT_FAILURE; +} +catch (...) { + std::cerr << "Caught unknown exception" << std::endl; + return EXIT_FAILURE; +}