diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2018-10-25 11:24:16 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-10-25 11:24:16 -0400 |
| commit | 4f0415e338862ffec50c2d47eddea958255b504e (patch) | |
| tree | 084c8e25552e328ed14eafcf0431493f58f973e8 /source/core/slang-math.h | |
| parent | 2700a89f8c80620f1d523563cc80ec0da39e9761 (diff) | |
Feature/premake linux (#689)
* Premake work in progress for linux.
* Added dump function.
* Remove examples on linux
Small warning fix.
* * Don't build render-test on linux
* Removed work around virtual destructor warning, and just used virtual dtor for simplicity
* Git ignore obj directories
* Fix premake working on windows.
* * Fix sprintf_s functions
* Make generates arg parsing more robust
* Added FloatIntUnion to avoid type punning/strong aliasing issues, and repeated union definitions.
* Work around problems building on linux with getClass claiming a strict aliasing issue.
* Fix for targetBlock appearing potentiall used unintialized to gcc.
* Linux slang link options -fPIC to make dll.
* Add -fPIC to build options on linux.
* Add -ldl for linux on slang.
* Fixes to try and get premake working with .so on linux.
* Make core compile with -fPIC
* Try to fix linux linking with --no-as-needed before -ldl
* Add rpath back.
* Remove render-gl from linux build.
* Re-add location for linux.
* Don't include <malloc.h> except on windows.
* Remove unused line to fix warning on osx.
* Remove ambiguity on OSX for operator <<.
* Fixing ambiguity with operator overloading and Int types for OSX.
* Fix ambiguity around UInt and operator
* Fix ambiguity of UInt conversion for OSX.
* Added UnambiguousInt and UnambiguousUInt to make it easier to work around OSX integer coercion for UInt/Int types.
Diffstat (limited to 'source/core/slang-math.h')
| -rw-r--r-- | source/core/slang-math.h | 50 |
1 files changed, 20 insertions, 30 deletions
diff --git a/source/core/slang-math.h b/source/core/slang-math.h index 6d6b3e7a1..a245e2d2c 100644 --- a/source/core/slang-math.h +++ b/source/core/slang-math.h @@ -8,6 +8,17 @@ namespace Slang class Math { public: + // Use to fix type punning issues with strict aliasing + union FloatIntUnion + { + float fvalue; + int ivalue; + + inline static FloatIntUnion makeFromInt(int i) { FloatIntUnion cast; cast.ivalue = i; return cast; } + inline static FloatIntUnion makeFromFloat(float f) { FloatIntUnion cast; cast.fvalue = f; return cast; } + }; + + static const float Pi; template<typename T> static T Min(const T& v1, const T&v2) @@ -110,30 +121,19 @@ namespace Slang } */ }; - inline int FloatAsInt(float val) + inline int FloatAsInt(float val) { - union InterCast - { - float fvalue; - int ivalue; - } cast; - cast.fvalue = val; - return cast.ivalue; + return Math::FloatIntUnion::makeFromFloat(val).ivalue; } - inline float IntAsFloat(int val) + inline float IntAsFloat(int val) { - union InterCast - { - float fvalue; - int ivalue; - } cast; - cast.ivalue = val; - return cast.fvalue; + return Math::FloatIntUnion::makeFromInt(val).fvalue; } inline unsigned short FloatToHalf(float val) { - int x = *(int*)&val; + const auto x = FloatAsInt(val); + unsigned short bits = (x >> 16) & 0x8000; unsigned short m = (x >> 12) & 0x07ff; unsigned int e = (x >> 23) & 0xff; @@ -158,19 +158,9 @@ namespace Slang inline float HalfToFloat(unsigned short input) { - union InterCast - { - float fvalue; - int ivalue; - InterCast() = default; - InterCast(int ival) - { - ivalue = ival; - } - }; - static const InterCast magic = InterCast((127 + (127 - 15)) << 23); - static const InterCast was_infnan = InterCast((127 + 16) << 23); - InterCast o; + static const auto magic = Math::FloatIntUnion::makeFromInt((127 + (127 - 15)) << 23); + static const auto was_infnan = Math::FloatIntUnion::makeFromInt((127 + 16) << 23); + Math::FloatIntUnion o; o.ivalue = (input & 0x7fff) << 13; // exponent/mantissa bits o.fvalue *= magic.fvalue; // exponent adjust if (o.fvalue >= was_infnan.fvalue) // make sure Inf/NaN survive |
