Skip to content

Commit c838464

Browse files
committed
Implement (u)i32_to_f8 in softfloat
1 parent 6df0a18 commit c838464

File tree

5 files changed

+163
-0
lines changed

5 files changed

+163
-0
lines changed

softfloat/i32_to_f8.c

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

softfloat/sign_ui32_to_f8.c

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

softfloat/softfloat.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ void softfloat_raiseFlags( uint_fast8_t );
103103
/*----------------------------------------------------------------------------
104104
| Integer-to-floating-point conversion routines.
105105
*----------------------------------------------------------------------------*/
106+
float8_t sign_ui32_to_f8( uint32_t, bool );
107+
float8_t ui32_to_f8( uint32_t );
106108
float16_t ui32_to_f16( uint32_t );
107109
float32_t ui32_to_f32( uint32_t );
108110
float64_t ui32_to_f64( uint32_t );
@@ -121,6 +123,7 @@ float128_t ui64_to_f128( uint64_t );
121123
#endif
122124
void ui64_to_extF80M( uint64_t, extFloat80_t * );
123125
void ui64_to_f128M( uint64_t, float128_t * );
126+
float8_t i32_to_f8( int32_t );
124127
float16_t i32_to_f16( int32_t );
125128
float32_t i32_to_f32( int32_t );
126129
float64_t i32_to_f64( int32_t );

softfloat/softfloat.mk.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ softfloat_c_srcs = \
140140
f64_to_ui64_r_minMag.c \
141141
fall_maxmin.c \
142142
fall_reciprocal.c \
143+
i32_to_f8.c \
143144
i32_to_f128.c \
144145
i32_to_f16.c \
145146
i32_to_f32.c \
@@ -242,6 +243,8 @@ softfloat_c_srcs = \
242243
s_subMagsF32.c \
243244
s_subMagsF64.c \
244245
s_subM.c \
246+
sign_ui32_to_f8.c \
247+
ui32_to_f8.c \
245248
ui32_to_f128.c \
246249
ui32_to_f16.c \
247250
ui32_to_f32.c \

softfloat/ui32_to_f8.c

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

0 commit comments

Comments
 (0)