#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2013 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License #ifndef NUCLEX_GRAPHICS_HALF_H #define NUCLEX_GRAPHICS_HALF_H #include "Nuclex/Graphics/Config.h" #include namespace Nuclex { namespace Graphics { namespace Rasterization { // ------------------------------------------------------------------------------------------- // /// Half-precision (16 bit) floating point number class Half { /// The value 1.0 as a half-precision float public: NUCLEX_GRAPHICS_API static const Half One; /// The value 0.0 as a half-precision float public: NUCLEX_GRAPHICS_API static const Half Zero; /// Initializes a new half-precision floating point value /// /// To emulate the behavior of other C++ primitive types, the value remains /// uninitialized and it is up to the user to make sure a value is assigned before /// the variable is accessed. /// public: NUCLEX_GRAPHICS_API Half() {} /// Initializes a new half-precision floating point value /// Floating point value the half will be initialized with public: NUCLEX_GRAPHICS_API Half(float value) : value(FromFloat(value)) {} /// Initializes a new half-precision floating point value /// Bits that will be used for the half-precision float public: NUCLEX_GRAPHICS_API explicit Half(std::uint16_t value) : value(value) {} /// Converts the half precision floating point value into a float /// A float equivalent to the half precision floating point value public: NUCLEX_GRAPHICS_API operator float() const { return ToFloat(this->value); } /// Converts the half precision floating point value into a float /// A float equivalent to the half precision floating point value public: NUCLEX_GRAPHICS_API operator std::uint16_t() const { return this->value; } /// Converts a byte into a half precision float /// Byte that will be converted /// The half-precision float equivalent to the byte public: NUCLEX_GRAPHICS_API static Half FromNormalizedByte(std::uint8_t value) { float valueAsFloat = static_cast(value) / 255.0f; return Half(valueAsFloat); } /// Converts the half precision float into a normal float point value /// The equivalent float to the input half-precision float public: NUCLEX_GRAPHICS_API std::uint8_t ToNormalizedByte() const { float valueAsFloat = ToFloat(this->value); if(valueAsFloat <= 0.0f) { return 0; } else if(valueAsFloat >= 1.0f) { return 255; } else { return static_cast(valueAsFloat * 255.0f); } } /// /// Converts a floating point value into a half-precision floating point value /// /// Floating point value that will be converted /// /// The equivalent half-precision floating point value to the input value /// /// /// Based on a code snippet by Phermost /// http://stackoverflow.com/questions/1659440/32-bit-to-16-bit-floating-point-conversion /// public: NUCLEX_GRAPHICS_API static std::uint16_t FromFloat(float value); /// /// Converts a half-precision floating point value to a floating point value /// /// Half-precision floating point value that will be converted /// The equivalent floating point value to the input value /// /// Based on a code snippet by Phermost /// http://stackoverflow.com/questions/1659440/32-bit-to-16-bit-floating-point-conversion /// public: NUCLEX_GRAPHICS_API static float ToFloat(std::uint16_t value); /// Stores the bits of the half-precision floating point value private: std::uint16_t value; }; // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Graphics::Rasterization #endif // NUCLEX_GRAPHICS_HALF_H