Skip to content

Commit 1b81e40

Browse files
committed
softfloat: extend bf16 APIs
also add fxx_neg helper functions Signed-off-by: Chih-Min Chao <chihmin.chao@sifive.com>
1 parent d425703 commit 1b81e40

15 files changed

+614
-5
lines changed

softfloat/bf16_classify.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
#include <stdbool.h>
3+
#include <stdint.h>
4+
#include "platform.h"
5+
#include "internals.h"
6+
#include "specialize.h"
7+
#include "softfloat.h"
8+
9+
uint_fast16_t bf16_classify( bfloat16_t a )
10+
{
11+
union ui16_f16 uA;
12+
uint_fast16_t uiA;
13+
14+
uA.f = a;
15+
uiA = uA.ui;
16+
17+
uint_fast16_t infOrNaN = expBF16UI( uiA ) == 0xFF;
18+
uint_fast16_t subnormalOrZero = expBF16UI( uiA ) == 0;
19+
bool sign = signBF16UI( uiA );
20+
bool fracZero = fracBF16UI( uiA ) == 0;
21+
bool isNaN = isNaNBF16UI( uiA );
22+
bool isSNaN = softfloat_isSigNaNBF16UI( uiA );
23+
24+
return
25+
( sign && infOrNaN && fracZero ) << 0 |
26+
( sign && !infOrNaN && !subnormalOrZero ) << 1 |
27+
( sign && subnormalOrZero && !fracZero ) << 2 |
28+
( sign && subnormalOrZero && fracZero ) << 3 |
29+
( !sign && infOrNaN && fracZero ) << 7 |
30+
( !sign && !infOrNaN && !subnormalOrZero ) << 6 |
31+
( !sign && subnormalOrZero && !fracZero ) << 5 |
32+
( !sign && subnormalOrZero && fracZero ) << 4 |
33+
( isNaN && isSNaN ) << 8 |
34+
( isNaN && !isSNaN ) << 9;
35+
}
36+

softfloat/bf16_cmp.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 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+
bool bf16_eq( 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_eq ( f32A, f32B );
49+
}
50+
51+
bool bf16_le( bfloat16_t a, bfloat16_t b )
52+
{
53+
float32_t f32A = { (uint_fast32_t)a.v << 16 };
54+
float32_t f32B = { (uint_fast32_t)b.v << 16 };
55+
56+
return f32_le ( f32A, f32B );
57+
}
58+
59+
bool bf16_lt( bfloat16_t a, bfloat16_t b )
60+
{
61+
float32_t f32A = { (uint_fast32_t)a.v << 16 };
62+
float32_t f32B = { (uint_fast32_t)b.v << 16 };
63+
64+
return f32_lt ( f32A, f32B );
65+
}

softfloat/bf16_to_i32.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
int_fast32_t bf16_to_i32( bfloat16_t a, uint_fast8_t roundingMode, bool exact )
45+
{
46+
return f32_to_i32(bf16_to_f32(a), roundingMode, exact);
47+
}
48+

softfloat/bf16_to_i8.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 <stdint.h>
38+
#include "specialize.h"
39+
#include "softfloat.h"
40+
41+
int_fast8_t bf16_to_i8( bfloat16_t a, uint_fast8_t roundingMode, bool exact )
42+
{
43+
uint_fast8_t old_flags = softfloat_exceptionFlags;
44+
45+
int_fast32_t sig32 = bf16_to_i32(a, roundingMode, exact);
46+
47+
if (sig32 > INT8_MAX) {
48+
softfloat_exceptionFlags = old_flags | softfloat_flag_invalid;
49+
return i8_fromPosOverflow;
50+
} else if (sig32 < INT8_MIN) {
51+
softfloat_exceptionFlags = old_flags | softfloat_flag_invalid;
52+
return i8_fromNegOverflow;
53+
} else {
54+
return sig32;
55+
}
56+
}
57+

softfloat/bf16_to_ui32.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
uint_fast32_t bf16_to_ui32( float16_t a, uint_fast8_t roundingMode, bool exact )
45+
{
46+
return f32_to_ui32(bf16_to_f32(a), roundingMode, exact);
47+
}
48+

softfloat/bf16_to_ui8.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 <stdint.h>
38+
#include "specialize.h"
39+
#include "softfloat.h"
40+
41+
uint_fast8_t bf16_to_ui8( bfloat16_t a, uint_fast8_t roundingMode, bool exact )
42+
{
43+
uint_fast8_t old_flags = softfloat_exceptionFlags;
44+
45+
uint_fast32_t sig32 = bf16_to_ui32(a, roundingMode, exact);
46+
47+
if (sig32 > UINT8_MAX) {
48+
softfloat_exceptionFlags = old_flags | softfloat_flag_invalid;
49+
return ui8_fromPosOverflow;
50+
} else {
51+
return sig32;
52+
}
53+
}
54+

softfloat/fall_maxmin.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,19 @@ COMPARE_MAX(a, b, 64);
7979
COMPARE_MIN(a, b, 16);
8080
COMPARE_MIN(a, b, 32);
8181
COMPARE_MIN(a, b, 64);
82+
83+
bfloat16_t bf16_max( bfloat16_t a, bfloat16_t b )
84+
{
85+
float32_t f32A = { (uint32_t)a.v << 16 };
86+
float32_t f32B = { (uint32_t)b.v << 16 };
87+
88+
return f32_to_bf16 ( f32_max( f32A, f32B ) );
89+
}
90+
91+
bfloat16_t bf16_min( bfloat16_t a, bfloat16_t b )
92+
{
93+
float32_t f32A = { (uint32_t)a.v << 16 };
94+
float32_t f32B = { (uint32_t)b.v << 16 };
95+
96+
return f32_to_bf16 ( f32_min( f32A, f32B ) );
97+
}

0 commit comments

Comments
 (0)