Utilities and Helpers

namespace aarith

Enums

enum IEEEClass

Enumeration of the different types of floating-points.

These are defined in Section 5.7.2. of the 2019 standard

Values:

enumerator signalingNaN
enumerator quietNaN
enumerator negativeInfinity
enumerator negativeNormal
enumerator negativeSubnormal
enumerator negativeZero
enumerator positiveZero
enumerator positiveSubnormal
enumerator positiveNormal
enumerator positiveInfinity
enumerator UNCLASSIFIED
enum Radix

The possible radices used to store floating-point numbers.

Aarith uses base two only.

Values:

enumerator Two
enumerator Ten

Functions

template<typename F>
bool constexpr isSignMinus(const F &f)

Tests whether a floating-point number is negative.

isSignMinus(x) is true if and only if x has negative sign. isSignMinus applies to zeros and NaNs as well.

Template Parameters

F – The floating-point type

Parameters

f – The floating-point number to test

Returns

True iff the number is negative

template<typename F>
bool constexpr isNormal(const F &f)

Tests whether a floating-point number is normal.

isNormal(f) is true if and only if f is normal (not zero, subnormal, infinite, or NaN)

Template Parameters

F – The floating-point type

Parameters

f – The floating-point number to test

Returns

True iff the number is normal

template<typename F>
bool constexpr isFinite(const F &f)

Tests whether a floating-point number is finite.

isFinite(f) is true if and only if f is zero, subnormal or normal (not infinite or NaN).

Template Parameters

F – The floating-point type

Parameters

f – The floating-point number to test

Returns

True iff the number is finite

template<typename F>
bool constexpr isZero(const F &f)

Tests whether a floating-point number is zero (ignoring the sign).

Template Parameters

F – The floating-point type

Parameters

f – The floating-point number to test

Returns

True iff the number is +/- zero

template<typename F>
bool constexpr isSubnormal(const F &f)

Tests whether a floating-point number is zero (ignoring the sign).

Note

Zero is not considered subnormal!

Template Parameters

F – The floating-point type

Parameters

f – The floating-point number to test

Returns

True iff the number is +/- zero

template<typename F>
bool constexpr isInfinite(const F &f)

Tests whether a floating-point number is infinite (ignoring the sign)

Template Parameters

F – The floating-point type

Parameters

f – The floating-point number to test

Returns

True iff the number is infinite

template<typename F>
bool constexpr isNaN(const F &f)

Tests whether a floating-point number is NaN.

Note

This method does not distinguish between signalling and quiet NaNs

Template Parameters

F – The floating-point type

Parameters

f – The floating-point number to test

Returns

True iff the number is NaN

template<typename F>
bool constexpr isSignaling(const F &f)

Tests whether a floating-point number is a signaling NaN.

Template Parameters

F – The floating-point type

Parameters

f – The floating-point number to test

Returns

True iff the number is a signaling NaN

template<typename F>
bool constexpr isQuiet(const F &f)

Tests whether a floating-point number is a quiet NaN.

Note

This method is not required by the IEEE 754 standard

Template Parameters

F – The floating-point type

Parameters

f – The floating-point number to test

Returns

True iff the number is a quiet NaN

template<typename F>
constexpr IEEEClass fp_class(const F &f)

Determines the class (e.g. NaN, positive subnormal) of a floating-point number.

This method corresponds to the “class” method described in Section 5.7.2 of the 2019 standard. The function name had to be changed as C++ does not allow to name functions “class”.

Template Parameters

F – The floating-point type

Parameters

f – The floating-point number to test

Returns

The class of the floating-point number

template<typename F>
Radix constexpr radix([[maybe_unused]] const F &f)

Returns the radix of the floating-point number.

Aarith only supports base two, hence Radix::Two is the only return value.

Template Parameters

F – The floating-point type

Parameters

f – The floating-point number to test

Returns

template<typename F>
bool constexpr isCanonical([[maybe_unused]] const F &f)

Tests whether a floating-point number is canonical.

Aarith does not support non-canonical numbers hence this method always returns true.

Template Parameters

F – The floating-point type

Parameters

f – The floating-point number to test

Returns

True

template<typename F, typename = std::enable_if_t<std::is_floating_point<F>::value>>
constexpr size_t get_mantissa_width()
template<>
constexpr size_t get_mantissa_width<float>()
template<>
constexpr size_t get_mantissa_width<double>()
template<typename F, typename = std::enable_if_t<std::is_floating_point<F>::value>>
constexpr size_t get_exponent_width()
template<>
constexpr size_t get_exponent_width<float>()
template<>
constexpr size_t get_exponent_width<double>()
template<typename F, typename = std::enable_if_t<std::is_floating_point<F>::value>>
inline auto disassemble_float(F num) -> float_disassembly
template<typename F, typename WordType, typename = std::enable_if_t<std::is_floating_point<F>::value>>
inline constexpr auto extract_exponent(F num)
template<typename F, typename Wordtype, typename = std::enable_if_t<std::is_floating_point<F>::value>>
inline constexpr auto extract_mantissa(F num)
struct float_disassembly

Public Members

uint64_t exponent
uint64_t mantissa
bool is_neg
namespace float_extraction_helper

Namespace to prevent people from accidentally using this trait.

template<typename F>
struct bit_cast_to_type_trait
template<>
struct bit_cast_to_type_trait<double>

Public Types

using type = uint64_t

Public Static Attributes

static constexpr size_t width = 64
template<>
struct bit_cast_to_type_trait<float>

Public Types

using type = uint32_t

Public Static Attributes

static constexpr size_t width = 32

NaN Payloads

Header aarith/float/nan_payload.hpp

Giving access to the payloads encoded in NaN values is mandatory as per [IEEE754].

namespace aarith

Functions

template<size_t E, size_t M, typename WordType>
floating_point<E, M, WordType> constexpr getPayload(const floating_point<E, M, WordType> &x)

Extracts the payload from an NaN.

Template Parameters
  • E – Exponent width

  • M – Mantissa width

  • WordType – The data type the underlying data is stored in

Parameters

x – Floating-point to extract the payload from

Returns

The payload or -1, when x is not NaN

template<size_t E, size_t M, typename WordType>
floating_point<E, M, WordType> constexpr setPayload(const floating_point<E, M, WordType> &x)

Creates quiet NaN with a specified payload.

For some reason, negative parameters result in +0 to be returned. Don’t ask me, ask the standard.

Template Parameters
  • E – Exponent width

  • M – Mantissa width

  • WordType – The data type the underlying data is stored in

Parameters

x – Floating-point number to take the payload to be stored from

Returns

A quiet NaN with the specified payload, +0 in case of error

template<size_t E, size_t M, typename WordType>
floating_point<E, M, WordType> constexpr setPayloadSignaling(const floating_point<E, M, WordType> &x)

Creates signaling NaN with a specified payload.

For some reason, negative parameters result in +0 to be returned. Don’t ask me, ask the standard.

Template Parameters
  • E – Exponent width

  • M – Mantissa width

  • WordType – The data type the underlying data is stored in

Parameters

x – Floating-point number to take the payload to be stored from

Returns

A signaling NaN with the specified payload, +0 in case of error