Magnum::Math::Half class

Half-precision float literal.

The purpose of this class is just to make specifying and printing of half-float literals easier. By design no arithmetic operations are supported, as majority of CPUs has no dedicated instructions for half-precision floats and thus it is faster to just use regular single-precision Float. See Wikipedia for more information about half floats.

The class provides explicit conversion from and to Float, equality comparison with correct treatment of NaN values, promotion and negation operator, an operator""_h() literal and an operator<<(Debug&, Half) debug operator. Internally the class uses packHalf() and unpackHalf(). Example usage:

using namespace Math::Literals;

Half a = 3.14159_h;
Debug{} << a;                   // Prints 3.14159
Debug{} << Float(a);            // Prints 3.14159
Debug{} << UnsignedShort(a);    // Prints 25675

Note that it is also possible to use this type inside Vector classes, though, again, only for passing data around and converting them, without any arithmetic operations:

Math::Vector3<Half> a{3.14159_h, -1.4142_h, 1.618_h};
Vector3 b{a};                                // converts to 32-bit floats
Debug{} << a;                                // prints {3.14159, -1.4142, 1.618}
Debug{} << Math::Vector3<UnsignedShort>{a};  // prints {16968, 48552, 15993}

Constructors, destructors, conversion operators

Half(ZeroInitT = ZeroInit) constexpr noexcept
Default constructor.
Half(UnsignedShort data) explicit constexpr noexcept
Construct a half value from underlying 16-bit representation.
Half(Float value) explicit noexcept
Construct a half value from 32-bit float representation.
Half(NoInitT) explicit noexcept
Construct without initializing the contents.
operator UnsignedShort() const explicit constexpr
Conversion to underlying representation.
operator Float() const explicit
Conversion to 32-bit float representation.

Public functions

auto operator==(Half other) const -> bool constexpr
Equality comparison.
auto operator!=(Half other) const -> bool constexpr
Non-equality comparison.
auto operator+() const -> Half constexpr
Promotion.
auto operator-() const -> Half constexpr
Negation.
auto data() const -> UnsignedShort constexpr
Underlying representation.

Function documentation

Magnum::Math::Half::Half(ZeroInitT = ZeroInit) constexpr noexcept

Default constructor.

Creates a zero value.

Magnum::Math::Half::Half(Float value) explicit noexcept

Construct a half value from 32-bit float representation.

Magnum::Math::Half::operator UnsignedShort() const explicit constexpr

Conversion to underlying representation.

Magnum::Math::Half::operator Float() const explicit

Conversion to 32-bit float representation.

bool Magnum::Math::Half::operator==(Half other) const constexpr

Equality comparison.

Returns false if one of the values is half-float representation of NaN, otherwise does bitwise comparison.

bool Magnum::Math::Half::operator!=(Half other) const constexpr

Non-equality comparison.

Simply negates the result of operator==().

Half Magnum::Math::Half::operator+() const constexpr

Promotion.

Returns the value as-is.

UnsignedShort Magnum::Math::Half::data() const constexpr

Underlying representation.

Half operator""_h(long double value)

Half-float literal.

See Half for more information.

Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug& debug, Half value)

Debug output operator.