nCine::Primitives::Half class

Half-precision float literal.

Represents a floating-point value in the binary16 format.

The sole purpose of this type is to make creation, conversion and visualization of half-float values easier. By design it doesn't support any arithmetic operations as not all CPU architecture have native support for half-floats and thus the operations would be done faster in a regular single-precision float.

The class provides explicit conversion from and to float, equality comparison with correct treatment of NaN values, promotion and negation operator, a Literals::operator""_h() literal.

Constructors, destructors, conversion operators

Half() constexpr noexcept
Default constructor.
Half(std::uint16_t data) explicit constexpr noexcept
Construct a half value from underlying 16-bit representation.
Half(float value) explicit noexcept
Construct a half value from a 32-bit float representation.
Half(double value) explicit noexcept
Construct a half value from a 64-bit float representation.
Half(NoInitT) explicit noexcept
Construct without initializing the contents.
operator std::uint16_t() 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 -> std::uint16_t constexpr
Underlying representation.

Function documentation

nCine::Primitives::Half::Half(double value) explicit noexcept

Construct a half value from a 64-bit float representation.

Present only to aid generic code, so e.g. T(1.0) works without being ambigous.

bool nCine::Primitives::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. Note that, unlike with other floating-point math types, due to the limited precision of half floats it's not a fuzzy compare.

bool nCine::Primitives::Half::operator!=(Half other) const constexpr

Non-equality comparison.

Simply negates the result of operator==().

Half nCine::Primitives::Half::operator+() const constexpr

Promotion.

Returns the value as-is.