Merge pull request #156 from p12nGH/noncontiguous_harts
[riscv-isa-sim.git] / softfloat / specialize.h
1
2 /*============================================================================
3
4 This C header 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 #ifndef specialize_h
38 #define specialize_h 1
39
40 #include <stdbool.h>
41 #include <stdint.h>
42 #include "primitiveTypes.h"
43 #include "softfloat.h"
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 /*----------------------------------------------------------------------------
50 | Default value for `softfloat_detectTininess'.
51 *----------------------------------------------------------------------------*/
52 #define init_detectTininess softfloat_tininess_afterRounding
53
54 /*----------------------------------------------------------------------------
55 | The values to return on conversions to 32-bit integer formats that raise an
56 | invalid exception.
57 *----------------------------------------------------------------------------*/
58 #define ui32_fromPosOverflow 0xFFFFFFFF
59 #define ui32_fromNegOverflow 0
60 #define ui32_fromNaN 0xFFFFFFFF
61 #define i32_fromPosOverflow 0x7FFFFFFF
62 #define i32_fromNegOverflow (-0x7FFFFFFF - 1)
63 #define i32_fromNaN 0x7FFFFFFF
64
65 /*----------------------------------------------------------------------------
66 | The values to return on conversions to 64-bit integer formats that raise an
67 | invalid exception.
68 *----------------------------------------------------------------------------*/
69 #define ui64_fromPosOverflow UINT64_C( 0xFFFFFFFFFFFFFFFF )
70 #define ui64_fromNegOverflow 0
71 #define ui64_fromNaN UINT64_C( 0xFFFFFFFFFFFFFFFF )
72 #define i64_fromPosOverflow UINT64_C( 0x7FFFFFFFFFFFFFFF )
73 #define i64_fromNegOverflow (-UINT64_C( 0x7FFFFFFFFFFFFFFF ) - 1)
74 #define i64_fromNaN UINT64_C( 0x7FFFFFFFFFFFFFFF )
75
76 /*----------------------------------------------------------------------------
77 | "Common NaN" structure, used to transfer NaN representations from one format
78 | to another.
79 *----------------------------------------------------------------------------*/
80 struct commonNaN { char _unused; };
81
82 /*----------------------------------------------------------------------------
83 | The bit pattern for a default generated 16-bit floating-point NaN.
84 *----------------------------------------------------------------------------*/
85 #define defaultNaNF16UI 0x7E00
86
87 /*----------------------------------------------------------------------------
88 | Returns true when 16-bit unsigned integer `uiA' has the bit pattern of a
89 | 16-bit floating-point signaling NaN.
90 | Note: This macro evaluates its argument more than once.
91 *----------------------------------------------------------------------------*/
92 #define softfloat_isSigNaNF16UI( uiA ) ((((uiA) & 0x7E00) == 0x7C00) && ((uiA) & 0x01FF))
93
94 /*----------------------------------------------------------------------------
95 | Assuming `uiA' has the bit pattern of a 16-bit floating-point NaN, converts
96 | this NaN to the common NaN form, and stores the resulting common NaN at the
97 | location pointed to by `zPtr'. If the NaN is a signaling NaN, the invalid
98 | exception is raised.
99 *----------------------------------------------------------------------------*/
100 #define softfloat_f16UIToCommonNaN( uiA, zPtr ) if ( ! ((uiA) & 0x0200) ) softfloat_raiseFlags( softfloat_flag_invalid )
101
102 /*----------------------------------------------------------------------------
103 | Converts the common NaN pointed to by `aPtr' into a 16-bit floating-point
104 | NaN, and returns the bit pattern of this value as an unsigned integer.
105 *----------------------------------------------------------------------------*/
106 #define softfloat_commonNaNToF16UI( aPtr ) ((uint_fast16_t) defaultNaNF16UI)
107
108 /*----------------------------------------------------------------------------
109 | Interpreting `uiA' and `uiB' as the bit patterns of two 16-bit floating-
110 | point values, at least one of which is a NaN, returns the bit pattern of
111 | the combined NaN result. If either `uiA' or `uiB' has the pattern of a
112 | signaling NaN, the invalid exception is raised.
113 *----------------------------------------------------------------------------*/
114 uint_fast16_t
115 softfloat_propagateNaNF16UI( uint_fast16_t uiA, uint_fast16_t uiB );
116
117 /*----------------------------------------------------------------------------
118 | The bit pattern for a default generated 32-bit floating-point NaN.
119 *----------------------------------------------------------------------------*/
120 #define defaultNaNF32UI 0x7FC00000
121
122 /*----------------------------------------------------------------------------
123 | Returns true when 32-bit unsigned integer `uiA' has the bit pattern of a
124 | 32-bit floating-point signaling NaN.
125 | Note: This macro evaluates its argument more than once.
126 *----------------------------------------------------------------------------*/
127 #define softfloat_isSigNaNF32UI( uiA ) ((((uiA) & 0x7FC00000) == 0x7F800000) && ((uiA) & 0x003FFFFF))
128
129 /*----------------------------------------------------------------------------
130 | Assuming `uiA' has the bit pattern of a 32-bit floating-point NaN, converts
131 | this NaN to the common NaN form, and stores the resulting common NaN at the
132 | location pointed to by `zPtr'. If the NaN is a signaling NaN, the invalid
133 | exception is raised.
134 *----------------------------------------------------------------------------*/
135 #define softfloat_f32UIToCommonNaN( uiA, zPtr ) if ( ! ((uiA) & 0x00400000) ) softfloat_raiseFlags( softfloat_flag_invalid )
136
137 /*----------------------------------------------------------------------------
138 | Converts the common NaN pointed to by `aPtr' into a 32-bit floating-point
139 | NaN, and returns the bit pattern of this value as an unsigned integer.
140 *----------------------------------------------------------------------------*/
141 #define softfloat_commonNaNToF32UI( aPtr ) ((uint_fast32_t) defaultNaNF32UI)
142
143 /*----------------------------------------------------------------------------
144 | Interpreting `uiA' and `uiB' as the bit patterns of two 32-bit floating-
145 | point values, at least one of which is a NaN, returns the bit pattern of
146 | the combined NaN result. If either `uiA' or `uiB' has the pattern of a
147 | signaling NaN, the invalid exception is raised.
148 *----------------------------------------------------------------------------*/
149 uint_fast32_t
150 softfloat_propagateNaNF32UI( uint_fast32_t uiA, uint_fast32_t uiB );
151
152 /*----------------------------------------------------------------------------
153 | The bit pattern for a default generated 64-bit floating-point NaN.
154 *----------------------------------------------------------------------------*/
155 #define defaultNaNF64UI UINT64_C( 0x7FF8000000000000 )
156
157 /*----------------------------------------------------------------------------
158 | Returns true when 64-bit unsigned integer `uiA' has the bit pattern of a
159 | 64-bit floating-point signaling NaN.
160 | Note: This macro evaluates its argument more than once.
161 *----------------------------------------------------------------------------*/
162 #define softfloat_isSigNaNF64UI( uiA ) ((((uiA) & UINT64_C( 0x7FF8000000000000 )) == UINT64_C( 0x7FF0000000000000 )) && ((uiA) & UINT64_C( 0x0007FFFFFFFFFFFF )))
163
164 /*----------------------------------------------------------------------------
165 | Assuming `uiA' has the bit pattern of a 64-bit floating-point NaN, converts
166 | this NaN to the common NaN form, and stores the resulting common NaN at the
167 | location pointed to by `zPtr'. If the NaN is a signaling NaN, the invalid
168 | exception is raised.
169 *----------------------------------------------------------------------------*/
170 #define softfloat_f64UIToCommonNaN( uiA, zPtr ) if ( ! ((uiA) & UINT64_C( 0x0008000000000000 )) ) softfloat_raiseFlags( softfloat_flag_invalid )
171
172 /*----------------------------------------------------------------------------
173 | Converts the common NaN pointed to by `aPtr' into a 64-bit floating-point
174 | NaN, and returns the bit pattern of this value as an unsigned integer.
175 *----------------------------------------------------------------------------*/
176 #define softfloat_commonNaNToF64UI( aPtr ) ((uint_fast64_t) defaultNaNF64UI)
177
178 /*----------------------------------------------------------------------------
179 | Interpreting `uiA' and `uiB' as the bit patterns of two 64-bit floating-
180 | point values, at least one of which is a NaN, returns the bit pattern of
181 | the combined NaN result. If either `uiA' or `uiB' has the pattern of a
182 | signaling NaN, the invalid exception is raised.
183 *----------------------------------------------------------------------------*/
184 uint_fast64_t
185 softfloat_propagateNaNF64UI( uint_fast64_t uiA, uint_fast64_t uiB );
186
187 /*----------------------------------------------------------------------------
188 | The bit pattern for a default generated 80-bit extended floating-point NaN.
189 *----------------------------------------------------------------------------*/
190 #define defaultNaNExtF80UI64 0x7FFF
191 #define defaultNaNExtF80UI0 UINT64_C( 0xC000000000000000 )
192
193 /*----------------------------------------------------------------------------
194 | Returns true when the 80-bit unsigned integer formed from concatenating
195 | 16-bit `uiA64' and 64-bit `uiA0' has the bit pattern of an 80-bit extended
196 | floating-point signaling NaN.
197 | Note: This macro evaluates its arguments more than once.
198 *----------------------------------------------------------------------------*/
199 #define softfloat_isSigNaNExtF80UI( uiA64, uiA0 ) ((((uiA64) & 0x7FFF) == 0x7FFF) && ! ((uiA0) & UINT64_C( 0x4000000000000000 )) && ((uiA0) & UINT64_C( 0x3FFFFFFFFFFFFFFF )))
200
201 #ifdef SOFTFLOAT_FAST_INT64
202
203 /*----------------------------------------------------------------------------
204 | The following functions are needed only when `SOFTFLOAT_FAST_INT64' is
205 | defined.
206 *----------------------------------------------------------------------------*/
207
208 /*----------------------------------------------------------------------------
209 | Assuming the unsigned integer formed from concatenating `uiA64' and `uiA0'
210 | has the bit pattern of an 80-bit extended floating-point NaN, converts
211 | this NaN to the common NaN form, and stores the resulting common NaN at the
212 | location pointed to by `zPtr'. If the NaN is a signaling NaN, the invalid
213 | exception is raised.
214 *----------------------------------------------------------------------------*/
215 #define softfloat_extF80UIToCommonNaN( uiA64, uiA0, zPtr ) if ( ! ((uiA0) & UINT64_C( 0x4000000000000000 )) ) softfloat_raiseFlags( softfloat_flag_invalid )
216
217 /*----------------------------------------------------------------------------
218 | Converts the common NaN pointed to by `aPtr' into an 80-bit extended
219 | floating-point NaN, and returns the bit pattern of this value as an unsigned
220 | integer.
221 *----------------------------------------------------------------------------*/
222 #if defined INLINE && ! defined softfloat_commonNaNToExtF80UI
223 INLINE
224 struct uint128 softfloat_commonNaNToExtF80UI( const struct commonNaN *aPtr )
225 {
226 struct uint128 uiZ;
227 uiZ.v64 = defaultNaNExtF80UI64;
228 uiZ.v0 = defaultNaNExtF80UI0;
229 return uiZ;
230 }
231 #else
232 struct uint128 softfloat_commonNaNToExtF80UI( const struct commonNaN *aPtr );
233 #endif
234
235 /*----------------------------------------------------------------------------
236 | Interpreting the unsigned integer formed from concatenating `uiA64' and
237 | `uiA0' as an 80-bit extended floating-point value, and likewise interpreting
238 | the unsigned integer formed from concatenating `uiB64' and `uiB0' as another
239 | 80-bit extended floating-point value, and assuming at least on of these
240 | floating-point values is a NaN, returns the bit pattern of the combined NaN
241 | result. If either original floating-point value is a signaling NaN, the
242 | invalid exception is raised.
243 *----------------------------------------------------------------------------*/
244 struct uint128
245 softfloat_propagateNaNExtF80UI(
246 uint_fast16_t uiA64,
247 uint_fast64_t uiA0,
248 uint_fast16_t uiB64,
249 uint_fast64_t uiB0
250 );
251
252 /*----------------------------------------------------------------------------
253 | The bit pattern for a default generated 128-bit floating-point NaN.
254 *----------------------------------------------------------------------------*/
255 #define defaultNaNF128UI64 UINT64_C( 0x7FFF800000000000 )
256 #define defaultNaNF128UI0 UINT64_C( 0 )
257
258 /*----------------------------------------------------------------------------
259 | Returns true when the 128-bit unsigned integer formed from concatenating
260 | 64-bit `uiA64' and 64-bit `uiA0' has the bit pattern of a 128-bit floating-
261 | point signaling NaN.
262 | Note: This macro evaluates its arguments more than once.
263 *----------------------------------------------------------------------------*/
264 #define softfloat_isSigNaNF128UI( uiA64, uiA0 ) ((((uiA64) & UINT64_C( 0x7FFF800000000000 )) == UINT64_C( 0x7FFF000000000000 )) && ((uiA0) || ((uiA64) & UINT64_C( 0x00007FFFFFFFFFFF ))))
265
266 /*----------------------------------------------------------------------------
267 | Assuming the unsigned integer formed from concatenating `uiA64' and `uiA0'
268 | has the bit pattern of a 128-bit floating-point NaN, converts this NaN to
269 | the common NaN form, and stores the resulting common NaN at the location
270 | pointed to by `zPtr'. If the NaN is a signaling NaN, the invalid exception
271 | is raised.
272 *----------------------------------------------------------------------------*/
273 #define softfloat_f128UIToCommonNaN( uiA64, uiA0, zPtr ) if ( ! ((uiA64) & UINT64_C( 0x0000800000000000 )) ) softfloat_raiseFlags( softfloat_flag_invalid )
274
275 /*----------------------------------------------------------------------------
276 | Converts the common NaN pointed to by `aPtr' into a 128-bit floating-point
277 | NaN, and returns the bit pattern of this value as an unsigned integer.
278 *----------------------------------------------------------------------------*/
279 #if defined INLINE && ! defined softfloat_commonNaNToF128UI
280 INLINE
281 struct uint128 softfloat_commonNaNToF128UI( const struct commonNaN *aPtr )
282 {
283 struct uint128 uiZ;
284 uiZ.v64 = defaultNaNF128UI64;
285 uiZ.v0 = defaultNaNF128UI0;
286 return uiZ;
287 }
288 #else
289 struct uint128 softfloat_commonNaNToF128UI( const struct commonNaN * );
290 #endif
291
292 /*----------------------------------------------------------------------------
293 | Interpreting the unsigned integer formed from concatenating `uiA64' and
294 | `uiA0' as a 128-bit floating-point value, and likewise interpreting the
295 | unsigned integer formed from concatenating `uiB64' and `uiB0' as another
296 | 128-bit floating-point value, and assuming at least on of these floating-
297 | point values is a NaN, returns the bit pattern of the combined NaN result.
298 | If either original floating-point value is a signaling NaN, the invalid
299 | exception is raised.
300 *----------------------------------------------------------------------------*/
301 struct uint128
302 softfloat_propagateNaNF128UI(
303 uint_fast64_t uiA64,
304 uint_fast64_t uiA0,
305 uint_fast64_t uiB64,
306 uint_fast64_t uiB0
307 );
308
309 #else
310
311 /*----------------------------------------------------------------------------
312 | The following functions are needed only when `SOFTFLOAT_FAST_INT64' is not
313 | defined.
314 *----------------------------------------------------------------------------*/
315
316 /*----------------------------------------------------------------------------
317 | Assuming the 80-bit extended floating-point value pointed to by `aSPtr' is
318 | a NaN, converts this NaN to the common NaN form, and stores the resulting
319 | common NaN at the location pointed to by `zPtr'. If the NaN is a signaling
320 | NaN, the invalid exception is raised.
321 *----------------------------------------------------------------------------*/
322 #define softfloat_extF80MToCommonNaN( aSPtr, zPtr ) if ( ! ((aSPtr)->signif & UINT64_C( 0x4000000000000000 )) ) softfloat_raiseFlags( softfloat_flag_invalid )
323
324 /*----------------------------------------------------------------------------
325 | Converts the common NaN pointed to by `aPtr' into an 80-bit extended
326 | floating-point NaN, and stores this NaN at the location pointed to by
327 | `zSPtr'.
328 *----------------------------------------------------------------------------*/
329 #if defined INLINE && ! defined softfloat_commonNaNToExtF80M
330 INLINE
331 void
332 softfloat_commonNaNToExtF80M(
333 const struct commonNaN *aPtr, struct extFloat80M *zSPtr )
334 {
335 zSPtr->signExp = defaultNaNExtF80UI64;
336 zSPtr->signif = defaultNaNExtF80UI0;
337 }
338 #else
339 void
340 softfloat_commonNaNToExtF80M(
341 const struct commonNaN *aPtr, struct extFloat80M *zSPtr );
342 #endif
343
344 /*----------------------------------------------------------------------------
345 | Assuming at least one of the two 80-bit extended floating-point values
346 | pointed to by `aSPtr' and `bSPtr' is a NaN, stores the combined NaN result
347 | at the location pointed to by `zSPtr'. If either original floating-point
348 | value is a signaling NaN, the invalid exception is raised.
349 *----------------------------------------------------------------------------*/
350 void
351 softfloat_propagateNaNExtF80M(
352 const struct extFloat80M *aSPtr,
353 const struct extFloat80M *bSPtr,
354 struct extFloat80M *zSPtr
355 );
356
357 /*----------------------------------------------------------------------------
358 | The bit pattern for a default generated 128-bit floating-point NaN.
359 *----------------------------------------------------------------------------*/
360 #define defaultNaNF128UI96 0x7FFF8000
361 #define defaultNaNF128UI64 0
362 #define defaultNaNF128UI32 0
363 #define defaultNaNF128UI0 0
364
365 /*----------------------------------------------------------------------------
366 | Assuming the 128-bit floating-point value pointed to by `aWPtr' is a NaN,
367 | converts this NaN to the common NaN form, and stores the resulting common
368 | NaN at the location pointed to by `zPtr'. If the NaN is a signaling NaN,
369 | the invalid exception is raised. Argument `aWPtr' points to an array of
370 | four 32-bit elements that concatenate in the platform's normal endian order
371 | to form a 128-bit floating-point value.
372 *----------------------------------------------------------------------------*/
373 #define softfloat_f128MToCommonNaN( aWPtr, zPtr ) if ( ! ((aWPtr)[indexWordHi( 4 )] & UINT64_C( 0x0000800000000000 )) ) softfloat_raiseFlags( softfloat_flag_invalid )
374
375 /*----------------------------------------------------------------------------
376 | Converts the common NaN pointed to by `aPtr' into a 128-bit floating-point
377 | NaN, and stores this NaN at the location pointed to by `zWPtr'. Argument
378 | `zWPtr' points to an array of four 32-bit elements that concatenate in the
379 | platform's normal endian order to form a 128-bit floating-point value.
380 *----------------------------------------------------------------------------*/
381 #if defined INLINE && ! defined softfloat_commonNaNToF128M
382 INLINE
383 void
384 softfloat_commonNaNToF128M( const struct commonNaN *aPtr, uint32_t *zWPtr )
385 {
386 zWPtr[indexWord( 4, 3 )] = defaultNaNF128UI96;
387 zWPtr[indexWord( 4, 2 )] = defaultNaNF128UI64;
388 zWPtr[indexWord( 4, 1 )] = defaultNaNF128UI32;
389 zWPtr[indexWord( 4, 0 )] = defaultNaNF128UI0;
390 }
391 #else
392 void
393 softfloat_commonNaNToF128M( const struct commonNaN *aPtr, uint32_t *zWPtr );
394 #endif
395
396 /*----------------------------------------------------------------------------
397 | Assuming at least one of the two 128-bit floating-point values pointed to by
398 | `aWPtr' and `bWPtr' is a NaN, stores the combined NaN result at the location
399 | pointed to by `zWPtr'. If either original floating-point value is a
400 | signaling NaN, the invalid exception is raised. Each of `aWPtr', `bWPtr',
401 | and `zWPtr' points to an array of four 32-bit elements that concatenate in
402 | the platform's normal endian order to form a 128-bit floating-point value.
403 *----------------------------------------------------------------------------*/
404 void
405 softfloat_propagateNaNF128M(
406 const uint32_t *aWPtr, const uint32_t *bWPtr, uint32_t *zWPtr );
407
408 #endif
409
410 #ifdef __cplusplus
411 }
412 #endif
413
414 #endif
415