Skip to content

Commit

Permalink
softfloat: Define misc operations for bfloat16
Browse files Browse the repository at this point in the history
Signed-off-by: LIU Zhiwei <[email protected]>
Reviewed-by: Richard Henderson <[email protected]>
Message-Id: <[email protected]>
[rth: Fix merge conflict with NO_SIGNALING_NANS; use bool for predicates.]
Signed-off-by: Richard Henderson <[email protected]>
  • Loading branch information
LIU Zhiwei authored and rth7680 committed Aug 28, 2020
1 parent 34f0c0a commit 5ebf5f4
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
38 changes: 38 additions & 0 deletions fpu/softfloat-specialize.c.inc
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,25 @@ bool float16_is_quiet_nan(float16 a_, float_status *status)
}
}

/*----------------------------------------------------------------------------
| Returns 1 if the bfloat16 value `a' is a quiet
| NaN; otherwise returns 0.
*----------------------------------------------------------------------------*/

bool bfloat16_is_quiet_nan(bfloat16 a_, float_status *status)
{
if (no_signaling_nans(status)) {
return bfloat16_is_any_nan(a_);
} else {
uint16_t a = a_;
if (snan_bit_is_one(status)) {
return (((a >> 6) & 0x1FF) == 0x1FE) && (a & 0x3F);
} else {
return ((a >> 6) & 0x1FF) == 0x1FF;
}
}
}

/*----------------------------------------------------------------------------
| Returns 1 if the half-precision floating-point value `a' is a signaling
| NaN; otherwise returns 0.
Expand All @@ -284,6 +303,25 @@ bool float16_is_signaling_nan(float16 a_, float_status *status)
}
}

/*----------------------------------------------------------------------------
| Returns 1 if the bfloat16 value `a' is a signaling
| NaN; otherwise returns 0.
*----------------------------------------------------------------------------*/

bool bfloat16_is_signaling_nan(bfloat16 a_, float_status *status)
{
if (no_signaling_nans(status)) {
return 0;
} else {
uint16_t a = a_;
if (snan_bit_is_one(status)) {
return ((a >> 6) & 0x1FF) == 0x1FF;
} else {
return (((a >> 6) & 0x1FF) == 0x1FE) && (a & 0x3F);
}
}
}

/*----------------------------------------------------------------------------
| Returns 1 if the single-precision floating-point value `a' is a quiet
| NaN; otherwise returns 0.
Expand Down
48 changes: 48 additions & 0 deletions include/fpu/softfloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,57 @@ bfloat16 bfloat16_sqrt(bfloat16, float_status *status);
FloatRelation bfloat16_compare(bfloat16, bfloat16, float_status *status);
FloatRelation bfloat16_compare_quiet(bfloat16, bfloat16, float_status *status);

bool bfloat16_is_quiet_nan(bfloat16, float_status *status);
bool bfloat16_is_signaling_nan(bfloat16, float_status *status);
bfloat16 bfloat16_silence_nan(bfloat16, float_status *status);
bfloat16 bfloat16_default_nan(float_status *status);

static inline bool bfloat16_is_any_nan(bfloat16 a)
{
return ((a & ~0x8000) > 0x7F80);
}

static inline bool bfloat16_is_neg(bfloat16 a)
{
return a >> 15;
}

static inline bool bfloat16_is_infinity(bfloat16 a)
{
return (a & 0x7fff) == 0x7F80;
}

static inline bool bfloat16_is_zero(bfloat16 a)
{
return (a & 0x7fff) == 0;
}

static inline bool bfloat16_is_zero_or_denormal(bfloat16 a)
{
return (a & 0x7F80) == 0;
}

static inline bool bfloat16_is_normal(bfloat16 a)
{
return (((a >> 7) + 1) & 0xff) >= 2;
}

static inline bfloat16 bfloat16_abs(bfloat16 a)
{
/* Note that abs does *not* handle NaN specially, nor does
* it flush denormal inputs to zero.
*/
return a & 0x7fff;
}

static inline bfloat16 bfloat16_chs(bfloat16 a)
{
/* Note that chs does *not* handle NaN specially, nor does
* it flush denormal inputs to zero.
*/
return a ^ 0x8000;
}

static inline bfloat16 bfloat16_set_sign(bfloat16 a, int sign)
{
return (a & 0x7fff) | (sign << 15);
Expand Down

0 comments on commit 5ebf5f4

Please sign in to comment.