From 3d0376df7b25ea90ac62b2f89374e7d98ff84f5f Mon Sep 17 00:00:00 2001 From: Nicolas Brunie Date: Sat, 12 Aug 2023 04:13:37 +0200 Subject: adding initial bf16 support --- source/RISCV/specialize.h | 7 +++++ source/bf16_isSignalingNaN.c | 51 ++++++++++++++++++++++++++++++++++++ source/bf16_to_f32.c | 56 ++++++++++++++++++++++++++++++++++++++++ source/include/internals.h | 1 + source/include/softfloat.h | 7 +++++ source/include/softfloat_types.h | 1 + 6 files changed, 123 insertions(+) create mode 100644 source/bf16_isSignalingNaN.c create mode 100644 source/bf16_to_f32.c (limited to 'source') diff --git a/source/RISCV/specialize.h b/source/RISCV/specialize.h index c638264..70fb808 100644 --- a/source/RISCV/specialize.h +++ b/source/RISCV/specialize.h @@ -88,6 +88,13 @@ struct commonNaN { char _unused; }; #define softfloat_isSigNaNF16UI( uiA ) ((((uiA) & 0x7E00) == 0x7C00) && ((uiA) & 0x01FF)) /*---------------------------------------------------------------------------- +| Returns true when 16-bit unsigned integer 'uiA' has the bit pattern of a +| 16-bit brain floating-point (BF16) signaling NaN. +| Note: This macro evaluates its argument more than once. +*----------------------------------------------------------------------------*/ +#define softfloat_isSigNaNBF16UI( uiA ) ((((uiA) & 0x7FC0) == 0x7F80) && ((uiA) & 0x003F)) + +/*---------------------------------------------------------------------------- | Assuming 'uiA' has the bit pattern of a 16-bit floating-point NaN, converts | this NaN to the common NaN form, and stores the resulting common NaN at the | location pointed to by 'zPtr'. If the NaN is a signaling NaN, the invalid diff --git a/source/bf16_isSignalingNaN.c b/source/bf16_isSignalingNaN.c new file mode 100644 index 0000000..79ca87f --- /dev/null +++ b/source/bf16_isSignalingNaN.c @@ -0,0 +1,51 @@ + +/*============================================================================ + +This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic +Package, Release 3e, by John R. Hauser. + +Copyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of +California. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions, and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions, and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + 3. Neither the name of the University nor the names of its contributors may + be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE +DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +=============================================================================*/ + +#include +#include "platform.h" +#include "internals.h" +#include "specialize.h" +#include "softfloat.h" + +bool bf16_isSignalingNaN( bfloat16_t a ) +{ + union ui16_bf16 uA; + + uA.f = a; + return softfloat_isSigNaNBF16UI( uA.ui ); + +} + diff --git a/source/bf16_to_f32.c b/source/bf16_to_f32.c new file mode 100644 index 0000000..175d0d4 --- /dev/null +++ b/source/bf16_to_f32.c @@ -0,0 +1,56 @@ + +/*============================================================================ + +This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic +Package, Release 3e, by John R. Hauser. + +Copyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of +California. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions, and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions, and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + 3. Neither the name of the University nor the names of its contributors may + be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE +DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +=============================================================================*/ + +#include +#include +#include "platform.h" +#include "internals.h" +#include "specialize.h" +#include "softfloat.h" + +float32_t bf16_to_f32( bfloat16_t a ) +{ + union ui16_bf16 uA; + union ui32_f32 uZ; + + /*------------------------------------------------------------------------ + *------------------------------------------------------------------------*/ + uA.f = a; + uZ.ui = (uint32_t) uA.ui << 16; + return uZ.f; + +} + diff --git a/source/include/internals.h b/source/include/internals.h index f8eac05..ccd7747 100644 --- a/source/include/internals.h +++ b/source/include/internals.h @@ -43,6 +43,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "softfloat_types.h" union ui16_f16 { uint16_t ui; float16_t f; }; +union ui16_bf16 { uint16_t ui; bfloat16_t f; }; union ui32_f32 { uint32_t ui; float32_t f; }; union ui64_f64 { uint64_t ui; float64_t f; }; diff --git a/source/include/softfloat.h b/source/include/softfloat.h index 9ed17c1..c1757b8 100644 --- a/source/include/softfloat.h +++ b/source/include/softfloat.h @@ -170,6 +170,13 @@ bool f16_lt_quiet( float16_t, float16_t ); bool f16_isSignalingNaN( float16_t ); /*---------------------------------------------------------------------------- +| 16-bit (brain float 16) floating-point operations. +*----------------------------------------------------------------------------*/ +float32_t bf16_to_f32( bfloat16_t ); +bfloat16_t f32_to_bf16( float32_t ); +bool bf16_isSignalingNaN( bfloat16_t ); + +/*---------------------------------------------------------------------------- | 32-bit (single-precision) floating-point operations. *----------------------------------------------------------------------------*/ uint_fast32_t f32_to_ui32( float32_t, uint_fast8_t, bool ); diff --git a/source/include/softfloat_types.h b/source/include/softfloat_types.h index b92d246..25039b5 100644 --- a/source/include/softfloat_types.h +++ b/source/include/softfloat_types.h @@ -48,6 +48,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | (typically 'float' and 'double', and possibly 'long double'). *----------------------------------------------------------------------------*/ typedef struct { uint16_t v; } float16_t; +typedef struct { uint16_t v; } bfloat16_t; typedef struct { uint32_t v; } float32_t; typedef struct { uint64_t v; } float64_t; typedef struct { uint64_t v[2]; } float128_t; -- cgit v1.1