idk
This commit is contained in:
parent
d436c6d90f
commit
82435afb4f
5
Makefile
5
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
|
||||
|
135
src/cint.cpp
Normal file
135
src/cint.cpp
Normal file
@ -0,0 +1,135 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Philipp Schubert
|
||||
//
|
||||
// Copyright (c) 2017 - 2021
|
||||
// Secure Software Engineering Group
|
||||
// Heinz Nixdorf Institute
|
||||
// Paderborn University
|
||||
// philipp.schubert@upb.de
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include <algorithm>
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
|
||||
const int MIN_MAX_DIFF = std::abs(std::abs(std::numeric_limits<int>::min())-std::abs(std::numeric_limits<int>::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<int>::max();
|
||||
sint min = std::numeric_limits<int>::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;
|
||||
}
|
0
src/sheet05.cpp
Normal file
0
src/sheet05.cpp
Normal file
Loading…
x
Reference in New Issue
Block a user