From 7d414fcfa5641b727e048180367d1de267cfbbe8 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Wed, 22 Jul 2020 21:16:29 +0200 Subject: [PATCH] - binary angle class draft. --- source/core/binaryangle.h | 103 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 source/core/binaryangle.h diff --git a/source/core/binaryangle.h b/source/core/binaryangle.h new file mode 100644 index 000000000..20d476a04 --- /dev/null +++ b/source/core/binaryangle.h @@ -0,0 +1,103 @@ +#pragma once + +#include +#include "m_fixed.h" +#include "xs_Float.h" // needed for reliably overflowing float->int conversions. +#include "build.h" + +class binangle +{ + unsigned int value; + + inline constexpr double pi() { return 3.14159265358979323846; } + + constexpr binangle(unsigned int v) : value(v) {} + + friend binangle bamang(unsigned int v); + friend binangle q16ang(unsigned int v); + friend binangle buildang(unsigned int v); + friend binangle radang(double v); + friend binangle degang(double v); + +public: + // This class intentionally makes no allowances for implicit type conversions because those would render it ineffective. + constexpr short asbuild() const { return value >> 21; } + constexpr fixed_t asq16() const { return value >> 5; } + constexpr double asrad() const { return value * (pi::pi() / 0x80000000u); } + constexpr double asdeg() const { return value * (90. / 0x40000000); } + constexpr unsigned asbam() const { return value; } + + double fsin() const { return sin(asrad()); } + double fcos() const { return cos(asrad()); } + double ftan() const { return tan(asrad()); } + int bsin() const { return sintable(asbuild()); } + int bcos() const { return sintable((asbuild() + 512) & 2047); } + +#if 0 // This makes no sense + bool operator< (binangle other) const + { + return value < other.value; + } + + bool operator> (binangle other) const + { + return value > other.value; + } + + bool operator<= (binangle other) const + { + return value <= other.value; + } + + bool operator>= (binangle other) const + { + return value >= other.value; + } +#endif + constexpr bool operator== (binangle other) const + { + return value == other.value; + } + + constexpr bool operator!= (binangle other) const + { + return value != other.value; + } + + constexpr binangle &operator+= (binangle other) const + { + value += other.value; + return *this; + } + + constexpr binangle &operator-= (binangle other) const + { + value -+= other.value; + return *this; + } + + constexpr binangle operator- () const + { + return binangle(-value); + } + + constexpr binangle operator+ (binangle other) const + { + return binangle(value + other.value); + } + + constexpr binangle operator- (binangle other) const + { + return binangle(value - other.value); + } + + +}; + + +inline constexpr binangle bamang(unsigned int v) { return binangle(v); } +inline constexpr binangle q16ang(unsigned int v) { return binangle(v << 5); } +inline constexpr binangle buildang(unsigned int v) { return binangle(v << 21); } +inline binangle radang(double v) { return binangle(xs_CRoundToUInt(v * (0x80000000u / pi())); } +inline binangle degang(double v) { return binangle(xs_CRoundToUInt(v * (0x40000000 / 90.)); } +