Skip to content

Commit 5e7928a

Browse files
authored
Merge pull request #1695 from riscv-software-src/bf16-ops
Add several BF16 ops to SoftFloat
2 parents a826a4e + fe47d09 commit 5e7928a

File tree

10 files changed

+443
-4
lines changed

10 files changed

+443
-4
lines changed

softfloat/bf16_add.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
/*============================================================================
3+
4+
This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
5+
Package, Release 3d, by John R. Hauser.
6+
7+
Copyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of
8+
California. All rights reserved.
9+
10+
Redistribution and use in source and binary forms, with or without
11+
modification, are permitted provided that the following conditions are met:
12+
13+
1. Redistributions of source code must retain the above copyright notice,
14+
this list of conditions, and the following disclaimer.
15+
16+
2. Redistributions in binary form must reproduce the above copyright notice,
17+
this list of conditions, and the following disclaimer in the documentation
18+
and/or other materials provided with the distribution.
19+
20+
3. Neither the name of the University nor the names of its contributors may
21+
be used to endorse or promote products derived from this software without
22+
specific prior written permission.
23+
24+
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
25+
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
27+
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
28+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34+
35+
=============================================================================*/
36+
37+
#include <stdbool.h>
38+
#include <stdint.h>
39+
#include "platform.h"
40+
#include "internals.h"
41+
#include "softfloat.h"
42+
43+
bfloat16_t bf16_add( bfloat16_t a, bfloat16_t b )
44+
{
45+
float32_t f32A = { (uint_fast32_t)a.v << 16 };
46+
float32_t f32B = { (uint_fast32_t)b.v << 16 };
47+
48+
return f32_to_bf16 ( f32_add ( f32A, f32B ) );
49+
}
50+

softfloat/bf16_div.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
/*============================================================================
3+
4+
This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
5+
Package, Release 3d, by John R. Hauser.
6+
7+
Copyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of
8+
California. All rights reserved.
9+
10+
Redistribution and use in source and binary forms, with or without
11+
modification, are permitted provided that the following conditions are met:
12+
13+
1. Redistributions of source code must retain the above copyright notice,
14+
this list of conditions, and the following disclaimer.
15+
16+
2. Redistributions in binary form must reproduce the above copyright notice,
17+
this list of conditions, and the following disclaimer in the documentation
18+
and/or other materials provided with the distribution.
19+
20+
3. Neither the name of the University nor the names of its contributors may
21+
be used to endorse or promote products derived from this software without
22+
specific prior written permission.
23+
24+
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
25+
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
27+
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
28+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34+
35+
=============================================================================*/
36+
37+
#include <stdbool.h>
38+
#include <stdint.h>
39+
#include "platform.h"
40+
#include "internals.h"
41+
#include "softfloat.h"
42+
43+
bfloat16_t bf16_div( bfloat16_t a, bfloat16_t b )
44+
{
45+
float32_t f32A = { (uint_fast32_t)a.v << 16 };
46+
float32_t f32B = { (uint_fast32_t)b.v << 16 };
47+
48+
return f32_to_bf16 ( f32_div ( f32A, f32B ) );
49+
}
50+

softfloat/bf16_mul.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
/*============================================================================
3+
4+
This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
5+
Package, Release 3d, by John R. Hauser.
6+
7+
Copyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of
8+
California. All rights reserved.
9+
10+
Redistribution and use in source and binary forms, with or without
11+
modification, are permitted provided that the following conditions are met:
12+
13+
1. Redistributions of source code must retain the above copyright notice,
14+
this list of conditions, and the following disclaimer.
15+
16+
2. Redistributions in binary form must reproduce the above copyright notice,
17+
this list of conditions, and the following disclaimer in the documentation
18+
and/or other materials provided with the distribution.
19+
20+
3. Neither the name of the University nor the names of its contributors may
21+
be used to endorse or promote products derived from this software without
22+
specific prior written permission.
23+
24+
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
25+
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
27+
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
28+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34+
35+
=============================================================================*/
36+
37+
#include <stdbool.h>
38+
#include <stdint.h>
39+
#include "platform.h"
40+
#include "internals.h"
41+
#include "softfloat.h"
42+
43+
bfloat16_t bf16_mul( bfloat16_t a, bfloat16_t b )
44+
{
45+
float32_t f32A = { (uint_fast32_t)a.v << 16 };
46+
float32_t f32B = { (uint_fast32_t)b.v << 16 };
47+
48+
return f32_to_bf16 ( f32_mul ( f32A, f32B ) );
49+
}
50+

softfloat/bf16_mulAdd.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
/*============================================================================
3+
4+
This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
5+
Package, Release 3d, by John R. Hauser.
6+
7+
Copyright 2011, 2012, 2013, 2014 The Regents of the University of California.
8+
All rights reserved.
9+
10+
Redistribution and use in source and binary forms, with or without
11+
modification, are permitted provided that the following conditions are met:
12+
13+
1. Redistributions of source code must retain the above copyright notice,
14+
this list of conditions, and the following disclaimer.
15+
16+
2. Redistributions in binary form must reproduce the above copyright notice,
17+
this list of conditions, and the following disclaimer in the documentation
18+
and/or other materials provided with the distribution.
19+
20+
3. Neither the name of the University nor the names of its contributors may
21+
be used to endorse or promote products derived from this software without
22+
specific prior written permission.
23+
24+
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
25+
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
27+
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
28+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34+
35+
=============================================================================*/
36+
37+
#include <stdint.h>
38+
#include "platform.h"
39+
#include "internals.h"
40+
#include "softfloat.h"
41+
42+
bfloat16_t bf16_mulAdd( bfloat16_t a, bfloat16_t b, bfloat16_t c )
43+
{
44+
uint_fast8_t roundingMode = softfloat_roundingMode;
45+
46+
float64_t f64A = bf16_to_f64( a );
47+
float64_t f64B = bf16_to_f64( b );
48+
float64_t f64C = bf16_to_f64( c );
49+
50+
float64_t prod = f64_mul( f64A, f64B );
51+
52+
softfloat_roundingMode = softfloat_round_odd;
53+
54+
float64_t sum = f64_add( prod, f64C );
55+
56+
softfloat_roundingMode = roundingMode;
57+
58+
/* When rounding down, return -0 instead of +0 when product's sign
59+
* differs from C's sign. */
60+
if ( softfloat_roundingMode == softfloat_round_min
61+
&& sum.v == 0
62+
&& ( ( prod.v ^ f64C.v ) & 0x8000000000000000U ) != 0 )
63+
return (bfloat16_t) { 0x8000 };
64+
65+
return f64_to_bf16( sum );
66+
}
67+

softfloat/bf16_sqrt.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
/*============================================================================
3+
4+
This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
5+
Package, Release 3d, by John R. Hauser.
6+
7+
Copyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of
8+
California. All rights reserved.
9+
10+
Redistribution and use in source and binary forms, with or without
11+
modification, are permitted provided that the following conditions are met:
12+
13+
1. Redistributions of source code must retain the above copyright notice,
14+
this list of conditions, and the following disclaimer.
15+
16+
2. Redistributions in binary form must reproduce the above copyright notice,
17+
this list of conditions, and the following disclaimer in the documentation
18+
and/or other materials provided with the distribution.
19+
20+
3. Neither the name of the University nor the names of its contributors may
21+
be used to endorse or promote products derived from this software without
22+
specific prior written permission.
23+
24+
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
25+
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
27+
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
28+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34+
35+
=============================================================================*/
36+
37+
#include <stdbool.h>
38+
#include <stdint.h>
39+
#include "platform.h"
40+
#include "internals.h"
41+
#include "softfloat.h"
42+
43+
bfloat16_t bf16_sqrt( bfloat16_t a )
44+
{
45+
float32_t f32A = { (uint_fast32_t)a.v << 16 };
46+
47+
return f32_to_bf16 ( f32_sqrt ( f32A ) );
48+
}
49+

softfloat/bf16_sub.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
/*============================================================================
3+
4+
This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
5+
Package, Release 3d, by John R. Hauser.
6+
7+
Copyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of
8+
California. All rights reserved.
9+
10+
Redistribution and use in source and binary forms, with or without
11+
modification, are permitted provided that the following conditions are met:
12+
13+
1. Redistributions of source code must retain the above copyright notice,
14+
this list of conditions, and the following disclaimer.
15+
16+
2. Redistributions in binary form must reproduce the above copyright notice,
17+
this list of conditions, and the following disclaimer in the documentation
18+
and/or other materials provided with the distribution.
19+
20+
3. Neither the name of the University nor the names of its contributors may
21+
be used to endorse or promote products derived from this software without
22+
specific prior written permission.
23+
24+
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
25+
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
27+
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
28+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34+
35+
=============================================================================*/
36+
37+
#include <stdbool.h>
38+
#include <stdint.h>
39+
#include "platform.h"
40+
#include "internals.h"
41+
#include "softfloat.h"
42+
43+
bfloat16_t bf16_sub( bfloat16_t a, bfloat16_t b )
44+
{
45+
float32_t f32A = { (uint_fast32_t)a.v << 16 };
46+
float32_t f32B = { (uint_fast32_t)b.v << 16 };
47+
48+
return f32_to_bf16 ( f32_sub ( f32A, f32B ) );
49+
}
50+

softfloat/bf16_to_f64.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
/*============================================================================
3+
4+
This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
5+
Package, Release 3d, by John R. Hauser.
6+
7+
Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the
8+
University of California. All rights reserved.
9+
10+
Redistribution and use in source and binary forms, with or without
11+
modification, are permitted provided that the following conditions are met:
12+
13+
1. Redistributions of source code must retain the above copyright notice,
14+
this list of conditions, and the following disclaimer.
15+
16+
2. Redistributions in binary form must reproduce the above copyright notice,
17+
this list of conditions, and the following disclaimer in the documentation
18+
and/or other materials provided with the distribution.
19+
20+
3. Neither the name of the University nor the names of its contributors may
21+
be used to endorse or promote products derived from this software without
22+
specific prior written permission.
23+
24+
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
25+
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
27+
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
28+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34+
35+
=============================================================================*/
36+
37+
#include <stdbool.h>
38+
#include <stdint.h>
39+
#include "platform.h"
40+
#include "internals.h"
41+
#include "specialize.h"
42+
#include "softfloat.h"
43+
44+
float64_t bf16_to_f64( bfloat16_t a )
45+
{
46+
float32_t f32A = (float32_t) { (uint_fast32_t)a.v << 16 };
47+
48+
return f32_to_f64( f32A );
49+
}

0 commit comments

Comments
 (0)