Add more BF16 conversions (#51) Signed-off-by: Andrew Waterman <andrew@sifive.com>
diff --git a/build/Linux-RISCV64-GCC/Makefile b/build/Linux-RISCV64-GCC/Makefile index 807e987..ed46849 100644 --- a/build/Linux-RISCV64-GCC/Makefile +++ b/build/Linux-RISCV64-GCC/Makefile
@@ -152,6 +152,7 @@ s_mulAddF128$(OBJ) \ softfloat_state$(OBJ) \ ui32_to_f16$(OBJ) \ + ui32_to_bf16$(OBJ) \ ui32_to_f32$(OBJ) \ ui32_to_f64$(OBJ) \ ui32_to_extF80$(OBJ) \ @@ -166,6 +167,7 @@ ui64_to_f128$(OBJ) \ ui64_to_f128M$(OBJ) \ i32_to_f16$(OBJ) \ + i32_to_bf16$(OBJ) \ i32_to_f32$(OBJ) \ i32_to_f64$(OBJ) \ i32_to_extF80$(OBJ) \
diff --git a/build/Linux-x86_64-GCC/Makefile b/build/Linux-x86_64-GCC/Makefile index 97683a7..8fe5d7e 100644 --- a/build/Linux-x86_64-GCC/Makefile +++ b/build/Linux-x86_64-GCC/Makefile
@@ -149,6 +149,7 @@ s_mulAddF128$(OBJ) \ softfloat_state$(OBJ) \ ui32_to_f16$(OBJ) \ + ui32_to_bf16$(OBJ) \ ui32_to_f32$(OBJ) \ ui32_to_f64$(OBJ) \ ui32_to_extF80$(OBJ) \ @@ -163,6 +164,7 @@ ui64_to_f128$(OBJ) \ ui64_to_f128M$(OBJ) \ i32_to_f16$(OBJ) \ + i32_to_bf16$(OBJ) \ i32_to_f32$(OBJ) \ i32_to_f64$(OBJ) \ i32_to_extF80$(OBJ) \
diff --git a/build/template-FAST_INT64/Makefile b/build/template-FAST_INT64/Makefile index 44d720a..2ee090c 100644 --- a/build/template-FAST_INT64/Makefile +++ b/build/template-FAST_INT64/Makefile
@@ -148,6 +148,7 @@ s_mulAddF128$(OBJ) \ softfloat_state$(OBJ) \ ui32_to_f16$(OBJ) \ + ui32_to_bf16$(OBJ) \ ui32_to_f32$(OBJ) \ ui32_to_f64$(OBJ) \ ui32_to_extF80$(OBJ) \ @@ -162,6 +163,7 @@ ui64_to_f128$(OBJ) \ ui64_to_f128M$(OBJ) \ i32_to_f16$(OBJ) \ + i32_to_bf16$(OBJ) \ i32_to_f32$(OBJ) \ i32_to_f64$(OBJ) \ i32_to_extF80$(OBJ) \
diff --git a/source/i32_to_bf16.c b/source/i32_to_bf16.c new file mode 100644 index 0000000..a0e3b34 --- /dev/null +++ b/source/i32_to_bf16.c
@@ -0,0 +1,53 @@ + +/*============================================================================ + +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, 2016 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 <stdint.h> +#include "platform.h" +#include "internals.h" +#include "softfloat.h" + +bfloat16_t i32_to_bf16( int32_t a ) +{ + uint_fast8_t roundingMode = softfloat_roundingMode; + softfloat_roundingMode = softfloat_round_odd; + + float32_t f32A = i32_to_f32( a ); + + softfloat_roundingMode = roundingMode; + + return f32_to_bf16( f32A ); +} +
diff --git a/source/include/softfloat.h b/source/include/softfloat.h index ee73502..ba1b03e 100644 --- a/source/include/softfloat.h +++ b/source/include/softfloat.h
@@ -98,6 +98,7 @@ | Integer-to-floating-point conversion routines. *----------------------------------------------------------------------------*/ float16_t ui32_to_f16( uint32_t ); +bfloat16_t ui32_to_bf16( uint32_t ); float32_t ui32_to_f32( uint32_t ); float64_t ui32_to_f64( uint32_t ); #ifdef SOFTFLOAT_FAST_INT64 @@ -116,6 +117,7 @@ void ui64_to_extF80M( uint64_t, extFloat80_t * ); void ui64_to_f128M( uint64_t, float128_t * ); float16_t i32_to_f16( int32_t ); +bfloat16_t i32_to_bf16( int32_t ); float32_t i32_to_f32( int32_t ); float64_t i32_to_f64( int32_t ); #ifdef SOFTFLOAT_FAST_INT64
diff --git a/source/ui32_to_bf16.c b/source/ui32_to_bf16.c new file mode 100644 index 0000000..0fe17d3 --- /dev/null +++ b/source/ui32_to_bf16.c
@@ -0,0 +1,53 @@ + +/*============================================================================ + +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, 2016 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 <stdint.h> +#include "platform.h" +#include "internals.h" +#include "softfloat.h" + +bfloat16_t ui32_to_bf16( uint32_t a ) +{ + uint_fast8_t roundingMode = softfloat_roundingMode; + softfloat_roundingMode = softfloat_round_odd; + + float32_t f32A = ui32_to_f32( a ); + + softfloat_roundingMode = roundingMode; + + return f32_to_bf16( f32A ); +} +