From cd223628acacaff30dbe22f573010f17a173646b Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Thu, 21 Jun 2018 11:40:29 +0200 Subject: [PATCH] Added a test for comparison of 9floats --- 9float.hpp | 156 ++++++++++++++++++++++++++++++++++++ primary_operations_test.cpp | 10 ++- 2 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 9float.hpp diff --git a/9float.hpp b/9float.hpp new file mode 100644 index 0000000..13b7fed --- /dev/null +++ b/9float.hpp @@ -0,0 +1,156 @@ +#pragma once +#include +#include + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wnarrowing" +namespace ninefloat{ + +struct private_t{}; + +private_t priv; + +template +class Q{ + integer_type backend; + static constexpr int degen=fractionals; + + + constexpr Q (const integer_type value, private_t) + : backend{value} + {} + +public: + constexpr Q(const Q& value) + : backend{value.backend} + {} + + template + constexpr Q (const srcT value) + : backend{(integer_type)(value*(1<& operator+=(const Q& oth) + { + backend+=oth.backend; + return *this; + } + + constexpr Q& operator-=(const Q& oth) + { + backend-=oth.backend; + return *this; + } + + constexpr Q& operator/=(const Q& oth) + { + backend*=1<::degen; + backend/=oth.backend; + return *this; + } + + constexpr Q& operator*=(const Q& oth) + { + if constexpr(Q::degen%2==0) + { + backend=(backend>>(Q::degen/2))*(oth.backend>>(Q::degen/2)); + }else{ + backend=(backend>>(Q::degen>>1))*(oth.backend>>((Q::degen>>1)+1)); + } + return *this; + } + + constexpr Q operator+(const Q& oth) + { + return Q{backend+oth.backend, priv}; + } + + constexpr Q operator-(const Q& oth) + { + return Q{backend-oth.backend, priv}; + } + + constexpr Q operator/(const Q& oth) + { + return Q{(1<::degen)*backend/oth.backend, priv}; + } + + constexpr Q operator*(const Q& oth) + { + if constexpr(Q::degen%2==0) + { + return Q{(backend>>(Q::degen/2))*(oth.backend>>(Q::degen/2)),priv}; + }else{ + return Q{(backend>>(Q::degen>>1))*(oth.backend>>((Q::degen>>1)+1)),priv}; + } + } + + constexpr bool operator==(const Q& oth) + { + return backend == oth.backend; + } + + constexpr bool operator!=(const Q& oth) + { + return backend != oth.backend; + } + + constexpr bool operator>=(const Q& oth) + { + return backend >= oth.backend; + } + + constexpr bool operator<=(const Q& oth) + { + return backend <= oth.backend; + } + + constexpr bool operator>(const Q& oth) + { + return backend > oth.backend; + } + + constexpr bool operator<(const Q& oth) + { + return backend < oth.backend; + } + + constexpr operator float() + { + float n=backend; + n/=(1<::degen); + return n; + } + + constexpr operator double() + { + double n=backend; + n/=(1<::degen); + return n; + } +}; +#pragma GCC diagnostic pop + +} \ No newline at end of file diff --git a/primary_operations_test.cpp b/primary_operations_test.cpp index 53a8e16..00742dc 100644 --- a/primary_operations_test.cpp +++ b/primary_operations_test.cpp @@ -2,14 +2,15 @@ #include #include #include +#include "9float.hpp" using namespace std::chrono_literals; template -void time_type(const size_t loop,T init,T incr ,const std::function lambda, std::string name) +void time_type(const T loop,T init,T incr ,const std::function lambda, std::string name) { T p; auto begin = std::chrono::high_resolution_clock::now(); - for(T i=0;i(10000,20000,1.14,[](double a,double b)->double {return a-b;}, "double -"); time_type(10000,1,1.25,[](double a,double b)->double {return a*b;}, "double *"); time_type(10000,99999999,1.25,[](double a,double b)->double {return a/b;}, "double /"); + + time_type>(10000,1,0.5,[](ninefloat::Q a,ninefloat::Q b)->ninefloat::Q {return a+b;}, "ninefloat::Q +"); + time_type>(10000,20000,1.14,[](ninefloat::Q a,ninefloat::Q b)->ninefloat::Q {return a-b;}, "ninefloat::Q -"); + time_type>(10000,1,1.25,[](ninefloat::Q a,ninefloat::Q b)->ninefloat::Q {return a*b;}, "ninefloat::Q *"); + time_type>(10000,99999999,1.25,[](ninefloat::Q a,ninefloat::Q b)->ninefloat::Q {return a/b;}, "ninefloat::Q /"); } \ No newline at end of file