Skip to content

Commit

Permalink
adding a playground skeleton for lazy evaluation experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravenwater committed Sep 10, 2024
1 parent 1f8a08c commit 7367903
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 2 deletions.
1 change: 1 addition & 0 deletions playground/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
4 changes: 2 additions & 2 deletions playground/float_to_decimal_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
114 changes: 114 additions & 0 deletions playground/lazy_evaluation.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <type_traits>

#include <universal/number/cfloat/cfloat.hpp>

namespace test1 {
template<typename T>
struct Expression {
T value;

Expression(T v) : value(v) {}

template<typename U>
Expression<decltype(value + U::value)> operator+(const Expression<U>& other) const {
return Expression<decltype(value + U::value)>(value + other.value);
}

template<typename U>
Expression<decltype(value* U::value)> operator*(const Expression<U>& other) const {
return Expression<decltype(value * U::value)>(value * other.value);
}

operator T() const { return value; }

};
}

namespace test2 {

template <typename T>
class Expression {
private:
T value;

public:
Expression(T value) : value(value) {}

template <typename U>
auto operator+(const Expression<U>& other) const -> decltype(value + other.value) {
return Expression<decltype(value + other.value)>(value + other.value);
}

template <typename U>
auto operator*(const Expression<U>& other) const -> decltype(value * other.value) {
return Expression<decltype(value * other.value)>(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<Real> a(2.0), b(3.0), c(4.0);

test1::Expression<Real> result = a * (b + c);

std::cout << result << '\n';
}

{
using Real = cfloat<24,8, uint32_t, true, false, false>;

test2::Expression<Real> a(2.5), b(3.0), c(4.0);

// test2::Expression<Real> result = a * (b + c);
// required: binary operator that supports: Expression * cfloat

test2::Expression<Real> tmpSum = (b + c);

test2::Expression<Real> 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;
}

0 comments on commit 7367903

Please sign in to comment.