diff --git a/Makefile b/Makefile index f34f88a..bd18f16 100755 --- a/Makefile +++ b/Makefile @@ -28,6 +28,11 @@ sheet04: chmod +x build/sheet04 build/sheet04 +cint: + ${BUILD_CMD} src/cint.cpp -o build/cint + chmod +x build/cint + build/cint + pizza: ${BUILD_CMD} src/pizza.cpp -o build/pizza chmod +x build/pizza diff --git a/src/cint.cpp b/src/cint.cpp new file mode 100644 index 0000000..349a3e3 --- /dev/null +++ b/src/cint.cpp @@ -0,0 +1,135 @@ +//===----------------------------------------------------------------------===// +// +// Philipp Schubert +// +// Copyright (c) 2017 - 2021 +// Secure Software Engineering Group +// Heinz Nixdorf Institute +// Paderborn University +// philipp.schubert@upb.de +// +//===----------------------------------------------------------------------===// + +#include +#include +#include +#include + +const int MIN_MAX_DIFF = std::abs(std::abs(std::numeric_limits::min())-std::abs(std::numeric_limits::max())); +class sint { + private: + int value; + + public: + sint() : value(0) {} + sint(int i) : value(i) {} + ~sint() = default; + sint(const sint &s) = default; + sint &operator=(const sint &s) = default; + sint(sint &&s) = default; + sint &operator=(sint &&s) = default; + int getUnderlyingValue() const noexcept { return value; } + friend sint operator+(sint lhs, sint rhs) + { + int result; + if(__builtin_sadd_overflow(lhs.getUnderlyingValue(), rhs.getUnderlyingValue(), &result)) + { + throw std::overflow_error("addition overflow"); + } + return result; + } + friend sint operator-(sint lhs, sint rhs) + { + int result; + if(__builtin_ssub_overflow(lhs.getUnderlyingValue(), rhs.getUnderlyingValue(), &result)) + { + throw std::overflow_error("subtraction overflow"); + } + return result; + } + + friend sint operator*(sint lhs, sint rhs) + { + int result; + if(__builtin_smul_overflow(lhs.getUnderlyingValue(), rhs.getUnderlyingValue(), &result)) + { + throw std::overflow_error("multiplication overflow"); + } + return result; + } + + // friend sint operator/(sint lhs, sint rhs) + // { + // int result; + // if(__builtin_sdiv_overflow(lhs.getUnderlyingValue(), rhs.getUnderlyingValue(), &result)) + // { + // throw std::overflow_error("division overflow"); + // } + // return result; + // } + sint &operator++() + { + *this = *this + 1; + return *this; + } + sint operator++(int) + { + sint out(*this); + ++(*this); + return out; + } + + sint &operator--() + { + *this = *this - 1; + return *this; + } + sint operator--(int) + { + sint out(*this); + --(*this); + return out; + } + friend std::ostream &operator<<(std::ostream &os, const sint &s) + { + return os << s.getUnderlyingValue(); + } +}; + +int main() { + // You are of course free to extend these tests! + + // // just a small test + sint a = 1; + sint b(41); + sint c = a + b; + std::cout << c << '\n'; + + // // setup some values + sint max = std::numeric_limits::max(); + sint min = std::numeric_limits::min(); + sint result; + + // // test operator+ + // result = max + sint(10); + // std::cout << result << '\n'; + // result = min + sint(-10); + // std::cout << result << '\n'; + + // // test operator- + // result = min - sint(10); + // std::cout << result << '\n'; + // result = max - sint(-10); + // std::cout << result << '\n'; + + // // test operator* + // result = max * sint(2); + // std::cout << result << '\n'; + // result = min * sint(2); + // std::cout << result << '\n'; + + // // test operator/ + // result = sint(42) / sint(0); + // cout << result << '\n'; + return 0; +} \ No newline at end of file diff --git a/src/sheet05.cpp b/src/sheet05.cpp new file mode 100644 index 0000000..e69de29