浏览代码

Added a test for comparison of 9floats

master
Ludovic 'Archivist' Lagouardette 6 年前
父节点
当前提交
cd223628ac
共有 2 个文件被更改,包括 164 次插入2 次删除
  1. +156
    -0
      9float.hpp
  2. +8
    -2
      primary_operations_test.cpp

+ 156
- 0
9float.hpp 查看文件

@ -0,0 +1,156 @@
#pragma once
#include <type_traits>
#include <iostream>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wnarrowing"
namespace ninefloat{
struct private_t{};
private_t priv;
template<class integer_type, int fractionals>
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<integer_type,fractionals>& value)
: backend{value.backend}
{}
template<class srcT>
constexpr Q (const srcT value)
: backend{(integer_type)(value*(1<<degen))}
{}
constexpr Q ()
: backend{0}
{}
constexpr bool is_negative()
{
return backend&(1<<(sizeof(backend)-1));
}
constexpr integer_type data()
{
return backend;
}
constexpr bool is_positive()
{
return !is_negative();
}
constexpr Q<integer_type, fractionals>& operator+=(const Q& oth)
{
backend+=oth.backend;
return *this;
}
constexpr Q<integer_type, fractionals>& operator-=(const Q& oth)
{
backend-=oth.backend;
return *this;
}
constexpr Q<integer_type, fractionals>& operator/=(const Q& oth)
{
backend*=1<<Q<integer_type, fractionals>::degen;
backend/=oth.backend;
return *this;
}
constexpr Q<integer_type, fractionals>& operator*=(const Q& oth)
{
if constexpr(Q<integer_type, fractionals>::degen%2==0)
{
backend=(backend>>(Q<integer_type, fractionals>::degen/2))*(oth.backend>>(Q<integer_type, fractionals>::degen/2));
}else{
backend=(backend>>(Q<integer_type, fractionals>::degen>>1))*(oth.backend>>((Q<integer_type, fractionals>::degen>>1)+1));
}
return *this;
}
constexpr Q<integer_type, fractionals> operator+(const Q& oth)
{
return Q{backend+oth.backend, priv};
}
constexpr Q<integer_type, fractionals> operator-(const Q& oth)
{
return Q{backend-oth.backend, priv};
}
constexpr Q<integer_type, fractionals> operator/(const Q& oth)
{
return Q{(1<<Q<integer_type, fractionals>::degen)*backend/oth.backend, priv};
}
constexpr Q<integer_type, fractionals> operator*(const Q& oth)
{
if constexpr(Q<integer_type, fractionals>::degen%2==0)
{
return Q{(backend>>(Q<integer_type, fractionals>::degen/2))*(oth.backend>>(Q<integer_type, fractionals>::degen/2)),priv};
}else{
return Q{(backend>>(Q<integer_type, fractionals>::degen>>1))*(oth.backend>>((Q<integer_type, fractionals>::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<<Q<integer_type, fractionals>::degen);
return n;
}
constexpr operator double()
{
double n=backend;
n/=(1<<Q<integer_type, fractionals>::degen);
return n;
}
};
#pragma GCC diagnostic pop
}

+ 8
- 2
primary_operations_test.cpp 查看文件

@ -2,14 +2,15 @@
#include <iostream>
#include <string>
#include <functional>
#include "9float.hpp"
using namespace std::chrono_literals;
template<class T>
void time_type(const size_t loop,T init,T incr ,const std::function<T(T,T)> lambda, std::string name)
void time_type(const T loop,T init,T incr ,const std::function<T(T,T)> lambda, std::string name)
{
T p;
auto begin = std::chrono::high_resolution_clock::now();
for(T i=0;i<loop;o">++i)
for(T i=0;i<loop;n">i+=1)
{
p=lambda(p,incr);
p=lambda(p,incr);
@ -47,4 +48,9 @@ int main()
time_type<double>(10000,20000,1.14,[](double a,double b)->double {return a-b;}, "double -");
time_type<double>(10000,1,1.25,[](double a,double b)->double {return a*b;}, "double *");
time_type<double>(10000,99999999,1.25,[](double a,double b)->double {return a/b;}, "double /");
time_type<ninefloat::Q<int,16>>(10000,1,0.5,[](ninefloat::Q<int,16> a,ninefloat::Q<int,16> b)->ninefloat::Q<int,16> {return a+b;}, "ninefloat::Q<int,16> +");
time_type<ninefloat::Q<int,16>>(10000,20000,1.14,[](ninefloat::Q<int,16> a,ninefloat::Q<int,16> b)->ninefloat::Q<int,16> {return a-b;}, "ninefloat::Q<int,16> -");
time_type<ninefloat::Q<int,16>>(10000,1,1.25,[](ninefloat::Q<int,16> a,ninefloat::Q<int,16> b)->ninefloat::Q<int,16> {return a*b;}, "ninefloat::Q<int,16> *");
time_type<ninefloat::Q<int,16>>(10000,99999999,1.25,[](ninefloat::Q<int,16> a,ninefloat::Q<int,16> b)->ninefloat::Q<int,16> {return a/b;}, "ninefloat::Q<int,16> /");
}

正在加载...
取消
保存