Skip to content

Commit

Permalink
AK: Add 16-bit float type
Browse files Browse the repository at this point in the history
  • Loading branch information
rmg-x committed Nov 10, 2024
1 parent db47cc4 commit 9cc4c6c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
7 changes: 7 additions & 0 deletions AK/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,13 @@ ErrorOr<void> Formatter<long double>::format(FormatBuilder& builder, long double
return builder.put_f80(value, base, upper_case, m_use_separator, m_align, m_width.value(), m_precision.value(), m_fill, m_sign_mode, real_number_display_mode);
}

ErrorOr<void> Formatter<f16>::format(FormatBuilder& builder, f16 value)
{
// FIXME: Create a proper put_f16() implementation
Formatter<double> formatter { *this };
return TRY(formatter.format(builder, static_cast<double>(value)));
}

ErrorOr<void> Formatter<double>::format(FormatBuilder& builder, double value)
{
u8 base;
Expand Down
11 changes: 11 additions & 0 deletions AK/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,17 @@ struct Formatter<long double> : StandardFormatter {
ErrorOr<void> format(FormatBuilder&, long double value);
};

template<>
struct Formatter<f16> : StandardFormatter {
Formatter() = default;
explicit Formatter(StandardFormatter formatter)
: StandardFormatter(formatter)
{
}

ErrorOr<void> format(FormatBuilder&, f16 value);
};

template<>
struct Formatter<nullptr_t> : Formatter<FlatPtr> {
ErrorOr<void> format(FormatBuilder& builder, nullptr_t)
Expand Down
11 changes: 11 additions & 0 deletions AK/NumericLimits.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ struct NumericLimits<long double> {
static constexpr size_t digits() { return __LDBL_MANT_DIG__; }
};

template<>
struct NumericLimits<f16> {
static constexpr f16 lowest() { return -__FLT16_MAX__; }
static constexpr f16 min_normal() { return __FLT16_MIN__; }
static constexpr f16 min_denormal() { return __FLT16_DENORM_MIN__; }
static constexpr f16 max() { return __FLT16_MAX__; }
static constexpr f16 epsilon() { return __FLT16_EPSILON__; }
static constexpr bool is_signed() { return true; }
static constexpr size_t digits() { return __FLT16_MANT_DIG__; }
};

}

#if USING_AK_GLOBALLY
Expand Down
2 changes: 2 additions & 0 deletions AK/StdLibExtraDetails.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ template<>
inline constexpr bool __IsFloatingPoint<double> = true;
template<>
inline constexpr bool __IsFloatingPoint<long double> = true;
template<>
inline constexpr bool __IsFloatingPoint<f16> = true;

template<typename T>
inline constexpr bool IsFloatingPoint = __IsFloatingPoint<RemoveCV<T>>;
Expand Down
3 changes: 3 additions & 0 deletions AK/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ using i32 = __INT32_TYPE__;
using i16 = __INT16_TYPE__;
using i8 = __INT8_TYPE__;

using f16 = _Float16;
static_assert(__FLT16_MANT_DIG__ == 11 && __FLT16_MAX_EXP__ == 16);

using f32 = float;
static_assert(__FLT_MANT_DIG__ == 24 && __FLT_MAX_EXP__ == 128);

Expand Down

0 comments on commit 9cc4c6c

Please sign in to comment.