1 /* SPDX-License-Identifier: BSD-3-Clause */
2 
3 /*============================================================================
4 
5 This C header file is part of the SoftFloat IEEE Floating-Point Arithmetic
6 Package, Release 3a, by John R. Hauser.
7 
8 Copyright 2011, 2012, 2013, 2014 The Regents of the University of California.
9 All rights reserved.
10 
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are met:
13 
14  1. Redistributions of source code must retain the above copyright notice,
15     this list of conditions, and the following disclaimer.
16 
17  2. Redistributions in binary form must reproduce the above copyright notice,
18     this list of conditions, and the following disclaimer in the documentation
19     and/or other materials provided with the distribution.
20 
21  3. Neither the name of the University nor the names of its contributors may
22     be used to endorse or promote products derived from this software without
23     specific prior written permission.
24 
25 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
26 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
28 DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
29 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 
36 =============================================================================*/
37 
38 #ifndef internals_h
39 #define internals_h 1
40 
41 #include <stdbool.h>
42 #include <stdint.h>
43 #include "primitives.h"
44 #include "softfloat_types.h"
45 
46 union ui32_f32 { uint32_t ui; float32_t f; };
47 union ui64_f64 { uint64_t ui; float64_t f; };
48 
49 #ifdef SOFTFLOAT_FAST_INT64
50 union extF80M_extF80 { struct extFloat80M fM; extFloat80_t f; };
51 union ui128_f128 { struct uint128 ui; float128_t f; };
52 #endif
53 
54 enum {
55     softfloat_mulAdd_subC    = 1,
56     softfloat_mulAdd_subProd = 2
57 };
58 
59 /*----------------------------------------------------------------------------
60 *----------------------------------------------------------------------------*/
61 uint_fast32_t
62  softfloat_roundPackToUI32( bool, uint_fast64_t, uint_fast8_t, bool );
63 
64 #ifdef SOFTFLOAT_FAST_INT64
65 uint_fast64_t
66  softfloat_roundPackToUI64(
67      bool, uint_fast64_t, uint_fast64_t, uint_fast8_t, bool );
68 #else
69 uint_fast64_t
70  softfloat_roundPackMToUI64( bool, uint32_t *, uint_fast8_t, bool );
71 #endif
72 
73 int_fast32_t
74  softfloat_roundPackToI32( bool, uint_fast64_t, uint_fast8_t, bool );
75 
76 #ifdef SOFTFLOAT_FAST_INT64
77 int_fast64_t
78  softfloat_roundPackToI64(
79      bool, uint_fast64_t, uint_fast64_t, uint_fast8_t, bool );
80 #else
81 int_fast64_t softfloat_roundPackMToI64( bool, uint32_t *, uint_fast8_t, bool );
82 #endif
83 
84 /*----------------------------------------------------------------------------
85 *----------------------------------------------------------------------------*/
86 #define signF32UI( a ) ((bool) ((uint32_t) (a)>>31))
87 #define expF32UI( a ) ((int_fast16_t) ((a)>>23) & 0xFF)
88 #define fracF32UI( a ) ((a) & 0x007FFFFF)
89 #define packToF32UI( sign, exp, sig ) (((uint32_t) (sign)<<31) + ((uint32_t) (exp)<<23) + (sig))
90 
91 #define isNaNF32UI( a ) ((((a) & 0x7F800000) == 0x7F800000) && ((a) & 0x007FFFFF))
92 
93 struct exp16_sig32 { int_fast16_t exp; uint_fast32_t sig; };
94 struct exp16_sig32 softfloat_normSubnormalF32Sig( uint_fast32_t );
95 
96 float32_t softfloat_roundPackToF32( bool, int_fast16_t, uint_fast32_t );
97 float32_t softfloat_normRoundPackToF32( bool, int_fast16_t, uint_fast32_t );
98 
99 float32_t softfloat_addMagsF32( uint_fast32_t, uint_fast32_t, bool );
100 float32_t softfloat_subMagsF32( uint_fast32_t, uint_fast32_t, bool );
101 float32_t
102  softfloat_mulAddF32(
103      uint_fast32_t, uint_fast32_t, uint_fast32_t, uint_fast8_t );
104 
105 /*----------------------------------------------------------------------------
106 *----------------------------------------------------------------------------*/
107 #define signF64UI( a ) ((bool) ((uint64_t) (a)>>63))
108 #define expF64UI( a ) ((int_fast16_t) ((a)>>52) & 0x7FF)
109 #define fracF64UI( a ) ((a) & UINT64_C( 0x000FFFFFFFFFFFFF ))
110 #define packToF64UI( sign, exp, sig ) ((uint64_t) (((uint_fast64_t) (sign)<<63) + ((uint_fast64_t) (exp)<<52) + (sig)))
111 
112 #define isNaNF64UI( a ) ((((a) & UINT64_C( 0x7FF0000000000000 )) == UINT64_C( 0x7FF0000000000000 )) && ((a) & UINT64_C( 0x000FFFFFFFFFFFFF )))
113 
114 struct exp16_sig64 { int_fast16_t exp; uint_fast64_t sig; };
115 struct exp16_sig64 softfloat_normSubnormalF64Sig( uint_fast64_t );
116 
117 float64_t softfloat_roundPackToF64( bool, int_fast16_t, uint_fast64_t );
118 float64_t softfloat_normRoundPackToF64( bool, int_fast16_t, uint_fast64_t );
119 
120 float64_t softfloat_addMagsF64( uint_fast64_t, uint_fast64_t, bool );
121 float64_t softfloat_subMagsF64( uint_fast64_t, uint_fast64_t, bool );
122 float64_t
123  softfloat_mulAddF64(
124      uint_fast64_t, uint_fast64_t, uint_fast64_t, uint_fast8_t );
125 
126 /*----------------------------------------------------------------------------
127 *----------------------------------------------------------------------------*/
128 #define signExtF80UI64( a64 ) ((bool) ((uint16_t) (a64)>>15))
129 #define expExtF80UI64( a64 ) ((a64) & 0x7FFF)
130 #define packToExtF80UI64( sign, exp ) ((uint_fast16_t) (sign)<<15 | (exp))
131 
132 #define isNaNExtF80UI( a64, a0 ) ((((a64) & 0x7FFF) == 0x7FFF) && ((a0) & UINT64_C( 0x7FFFFFFFFFFFFFFF )))
133 
134 #ifdef SOFTFLOAT_FAST_INT64
135 
136 /*----------------------------------------------------------------------------
137 *----------------------------------------------------------------------------*/
138 
139 struct exp32_sig64 { int_fast32_t exp; uint64_t sig; };
140 struct exp32_sig64 softfloat_normSubnormalExtF80Sig( uint_fast64_t );
141 
142 extFloat80_t
143  softfloat_roundPackToExtF80(
144      bool, int_fast32_t, uint_fast64_t, uint_fast64_t, uint_fast8_t );
145 extFloat80_t
146  softfloat_normRoundPackToExtF80(
147      bool, int_fast32_t, uint_fast64_t, uint_fast64_t, uint_fast8_t );
148 
149 extFloat80_t
150  softfloat_addMagsExtF80(
151      uint_fast16_t, uint_fast64_t, uint_fast16_t, uint_fast64_t, bool );
152 extFloat80_t
153  softfloat_subMagsExtF80(
154      uint_fast16_t, uint_fast64_t, uint_fast16_t, uint_fast64_t, bool );
155 
156 /*----------------------------------------------------------------------------
157 *----------------------------------------------------------------------------*/
158 #define signF128UI64( a64 ) ((bool) ((uint64_t) (a64)>>63))
159 #define expF128UI64( a64 ) ((int_fast32_t) ((a64)>>48) & 0x7FFF)
160 #define fracF128UI64( a64 ) ((a64) & UINT64_C( 0x0000FFFFFFFFFFFF ))
161 #define packToF128UI64( sign, exp, sig64 ) (((uint_fast64_t) (sign)<<63) + ((uint_fast64_t) (exp)<<48) + (sig64))
162 
163 #define isNaNF128UI( a64, a0 ) ((((a64) & UINT64_C( 0x7FFF000000000000 )) == UINT64_C( 0x7FFF000000000000 )) && (a0 || ((a64) & UINT64_C( 0x0000FFFFFFFFFFFF ))))
164 
165 struct exp32_sig128 { int_fast32_t exp; struct uint128 sig; };
166 struct exp32_sig128
167  softfloat_normSubnormalF128Sig( uint_fast64_t, uint_fast64_t );
168 
169 float128_t
170  softfloat_roundPackToF128(
171      bool, int_fast32_t, uint_fast64_t, uint_fast64_t, uint_fast64_t );
172 float128_t
173  softfloat_normRoundPackToF128(
174      bool, int_fast32_t, uint_fast64_t, uint_fast64_t );
175 
176 float128_t
177  softfloat_addMagsF128(
178      uint_fast64_t, uint_fast64_t, uint_fast64_t, uint_fast64_t, bool );
179 float128_t
180  softfloat_subMagsF128(
181      uint_fast64_t, uint_fast64_t, uint_fast64_t, uint_fast64_t, bool );
182 float128_t
183  softfloat_mulAddF128(
184      uint_fast64_t,
185      uint_fast64_t,
186      uint_fast64_t,
187      uint_fast64_t,
188      uint_fast64_t,
189      uint_fast64_t,
190      uint_fast8_t
191  );
192 
193 #else
194 
195 /*----------------------------------------------------------------------------
196 *----------------------------------------------------------------------------*/
197 
198 bool
199  softfloat_tryPropagateNaNExtF80M(
200      const struct extFloat80M *,
201      const struct extFloat80M *,
202      struct extFloat80M *
203  );
204 void softfloat_invalidExtF80M( struct extFloat80M * );
205 
206 int softfloat_normExtF80SigM( uint64_t * );
207 
208 void
209  softfloat_roundPackMToExtF80M(
210      bool, int32_t, uint32_t *, uint_fast8_t, struct extFloat80M * );
211 void
212  softfloat_normRoundPackMToExtF80M(
213      bool, int32_t, uint32_t *, uint_fast8_t, struct extFloat80M * );
214 
215 void
216  softfloat_addExtF80M(
217      const struct extFloat80M *,
218      const struct extFloat80M *,
219      struct extFloat80M *,
220      bool
221  );
222 
223 int
224  softfloat_compareNonnormExtF80M(
225      const struct extFloat80M *, const struct extFloat80M * );
226 
227 /*----------------------------------------------------------------------------
228 *----------------------------------------------------------------------------*/
229 #define signF128UI96( a96 ) ((bool) ((uint32_t) (a96)>>31))
230 #define expF128UI96( a96 ) ((int32_t) ((a96)>>16) & 0x7FFF)
231 #define fracF128UI96( a96 ) ((a96) & 0x0000FFFF)
232 #define packToF128UI96( sign, exp, sig96 ) (((uint32_t) (sign)<<31) + ((uint32_t) (exp)<<16) + (sig96))
233 
234 bool softfloat_isNaNF128M( const uint32_t * );
235 
236 bool
237  softfloat_tryPropagateNaNF128M(
238      const uint32_t *, const uint32_t *, uint32_t * );
239 void softfloat_invalidF128M( uint32_t * );
240 
241 int softfloat_shiftNormSigF128M( const uint32_t *, uint_fast8_t, uint32_t * );
242 
243 void softfloat_roundPackMToF128M( bool, int32_t, uint32_t *, uint32_t * );
244 void softfloat_normRoundPackMToF128M( bool, int32_t, uint32_t *, uint32_t * );
245 
246 void
247  softfloat_addF128M( const uint32_t *, const uint32_t *, uint32_t *, bool );
248 void
249  softfloat_mulAddF128M(
250      const uint32_t *,
251      const uint32_t *,
252      const uint32_t *,
253      uint32_t *,
254      uint_fast8_t
255  );
256 
257 #endif
258 
259 #endif
260 
261