From 5fc298964b76d6770f54b74a8456869c929a57f0 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Tue, 20 Sep 2011 12:03:55 +0000 Subject: [PATCH] core: start working on a "real" class for arbitrarily sized floats. --- src/Makefile.am | 1 + src/core.h | 1 + src/real.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/real.h | 42 ++++++++++++++++++++++++++++++++++++++++++ test/Makefile.am | 2 +- 5 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 src/real.cpp create mode 100644 src/real.h diff --git a/src/Makefile.am b/src/Makefile.am index eb1b157a..886558d2 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -11,6 +11,7 @@ liblol_a_SOURCES = \ text.cpp text.h emitter.cpp emitter.h numeric.h hash.cpp hash.h \ worldentity.cpp worldentity.h gradient.cpp gradient.h half.cpp half.h \ platform.cpp platform.h sprite.cpp sprite.h trig.cpp trig.h \ + real.cpp real.h \ \ lol/unit.h \ \ diff --git a/src/core.h b/src/core.h index 55a18d37..feefb6fc 100644 --- a/src/core.h +++ b/src/core.h @@ -64,6 +64,7 @@ static inline int isnan(float f) // Base types #include "trig.h" #include "half.h" +#include "real.h" #include "matrix.h" #include "numeric.h" #include "timer.h" diff --git a/src/real.cpp b/src/real.cpp new file mode 100644 index 00000000..dfe0bd52 --- /dev/null +++ b/src/real.cpp @@ -0,0 +1,48 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2011 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://sam.zoy.org/projects/COPYING.WTFPL for more details. +// + +#if defined HAVE_CONFIG_H +# include "config.h" +#endif + +#include + +#include "core.h" + +using namespace std; + +namespace lol +{ + +template<> real4k::Real(float f) +{ + union { float f; uint32_t x; } u = { f }; + + uint32_t sign = u.x & 0x80000000u; + int e = ((u.x >> 23) & 0xff) + (1 << 30) - (1 << 10); + + m_signexp = sign | e; + m_mantissa[0] = u.x << 17; + memset(m_mantissa + 1, 0, sizeof(m_mantissa) - sizeof(m_mantissa[0])); +} + +template<> real4k::operator float() const +{ + union { float f; uint32_t x; } u; + + u.x = m_mantissa[0] >> 17; + u.x |= ((m_signexp & 0x7fffffffu) - (1 << 30) + (1 << 10)) << 23; + u.x |= m_signexp & 0x80000000u; + + return u.f; +} + +} /* namespace lol */ + diff --git a/src/real.h b/src/real.h new file mode 100644 index 00000000..d31238c3 --- /dev/null +++ b/src/real.h @@ -0,0 +1,42 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2011 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://sam.zoy.org/projects/COPYING.WTFPL for more details. +// + +// +// The Real class +// -------------- +// + +#if !defined __LOL_REAL_H__ +#define __LOL_REAL_H__ + +#include + +namespace lol +{ + +template class Real +{ +public: + inline Real() {} + Real(float f); + + operator float() const; + +private: + uint32_t m_signexp; + uint32_t m_mantissa[(NBITS + 31) / 32]; +}; + +typedef Real<4096> real4k; + +} /* namespace lol */ + +#endif // __LOL_REAL_H__ + diff --git a/test/Makefile.am b/test/Makefile.am index 23e654fa..0ffe132e 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -19,7 +19,7 @@ noinst_PROGRAMS = quad sandbox benchsuite testsuite TESTS = testsuite testsuite_SOURCES = testsuite.cpp \ - unit/matrix.cpp unit/half.cpp unit/trig.cpp unit/build.cpp + unit/matrix.cpp unit/half.cpp unit/trig.cpp unit/build.cpp unit/real.cpp testsuite_CPPFLAGS = @LOL_CFLAGS@ @PIPI_CFLAGS@ testsuite_LDFLAGS = $(top_builddir)/src/liblol.a @LOL_LIBS@ @PIPI_LIBS@ testsuite_DEPENDENCIES = $(top_builddir)/src/liblol.a