Skip to content

Commit

Permalink
[GCC] Build fix for Ubuntu 22.04 after 281895@main
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=277672

Reviewed by NOBODY (OOPS!).

GCC <12 do not feature type '_Float16'.

Check compiler flag '__FLT16_MANT_DIG__' is defined before using '_Float16'.

* Source/WTF/wtf/Float16.h:
(WTF::convertFloat16ToFloat32):
(WTF::convertFloat32ToFloat16):
(WTF::convertFloat64ToFloat16):
  • Loading branch information
dpino authored and dylan-conway committed Aug 7, 2024
1 parent 20e56b9 commit 14ebbc3
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions Source/WTF/wtf/Float16.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#pragma once

#include <bit>
#include <cfloat>
#include <wtf/StdLibExtras.h>

namespace WTF {
Expand All @@ -55,9 +56,10 @@ namespace WTF {
*/
constexpr float convertFloat16ToFloat32(uint16_t h)
{
#if CPU(ARM64) || CPU(X86_64)
#if (CPU(ARM64) || CPU(X86_64)) && defined(__FLT16_MANT_DIG__)
return static_cast<float>(std::bit_cast<_Float16>(h));
#else

/*
* Extend the half-precision floating-point number to 32 bits and shift to the upper part of the 32-bit word:
* +---+-----+------------+-------------------+
Expand Down Expand Up @@ -176,7 +178,7 @@ constexpr double convertFloat16ToFloat64(uint16_t h)
*/
constexpr uint16_t convertFloat32ToFloat16(float f)
{
#if CPU(ARM64) || CPU(X86_64)
#if (CPU(ARM64) || CPU(X86_64)) && defined(__FLT16_MANT_DIG__)
return std::bit_cast<uint16_t>(static_cast<_Float16>(f));
#else
const float scale_to_inf = 0x1.0p+112f; // 0x77800000
Expand Down Expand Up @@ -204,7 +206,7 @@ constexpr uint16_t convertFloat32ToFloat16(float f)
// Adopted from V8's DoubleToFloat16, which adopted from https://gist.github.com/rygorous/2156668.
constexpr uint16_t convertFloat64ToFloat16(double value)
{
#if CPU(ARM64) || CPU(X86_64)
#if (CPU(ARM64) || CPU(X86_64)) && defined(__FLT16_MANT_DIG__)
return std::bit_cast<uint16_t>(static_cast<_Float16>(value));
#else
// uint64_t constants prefixed with kFP64 are bit patterns of doubles.
Expand Down Expand Up @@ -284,7 +286,7 @@ constexpr uint16_t convertFloat64ToFloat16(double value)
#endif
}

#if CPU(ARM64) || CPU(X86_64)
#if (CPU(ARM64) || CPU(X86_64)) && defined(__FLT16_MANT_DIG__)
class Float16 {
public:
constexpr Float16() = default;
Expand Down

0 comments on commit 14ebbc3

Please sign in to comment.