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 specialize_h
39 #define specialize_h 1
40 
41 #include <stdbool.h>
42 #include <stdint.h>
43 #include "softfloat_types.h"
44 
45 /*----------------------------------------------------------------------------
46 | Default value for `softfloat_detectTininess'.
47 *----------------------------------------------------------------------------*/
48 #define init_detectTininess softfloat_tininess_afterRounding;
49 
50 /*----------------------------------------------------------------------------
51 | "Common NaN" structure, used to transfer NaN representations from one format
52 | to another.
53 *----------------------------------------------------------------------------*/
54 struct commonNaN {
55     bool sign;
56 #ifdef LITTLEENDIAN
57     uint64_t v0, v64;
58 #else
59     uint64_t v64, v0;
60 #endif
61 };
62 
63 /*----------------------------------------------------------------------------
64 | The bit pattern for a default generated 32-bit floating-point NaN.
65 *----------------------------------------------------------------------------*/
66 #define defaultNaNF32UI 0xFFC00000
67 
68 /*----------------------------------------------------------------------------
69 | Returns true when 32-bit unsigned integer `uiA' has the bit pattern of a
70 | 32-bit floating-point signaling NaN.
71 | Note:  This macro evaluates its argument more than once.
72 *----------------------------------------------------------------------------*/
73 #define softfloat_isSigNaNF32UI( uiA ) ((((uiA) & 0x7FC00000) == 0x7F800000) && ((uiA) & 0x003FFFFF))
74 
75 /*----------------------------------------------------------------------------
76 | Assuming `uiA' has the bit pattern of a 32-bit floating-point NaN, converts
77 | this NaN to the common NaN form, and stores the resulting common NaN at the
78 | location pointed to by `zPtr'.  If the NaN is a signaling NaN, the invalid
79 | exception is raised.
80 *----------------------------------------------------------------------------*/
81 void softfloat_f32UIToCommonNaN( uint_fast32_t uiA, struct commonNaN *zPtr );
82 
83 /*----------------------------------------------------------------------------
84 | Converts the common NaN pointed to by `aPtr' into a 32-bit floating-point
85 | NaN, and returns the bit pattern of this value as an unsigned integer.
86 *----------------------------------------------------------------------------*/
87 uint_fast32_t softfloat_commonNaNToF32UI( const struct commonNaN *aPtr );
88 
89 /*----------------------------------------------------------------------------
90 | Interpreting `uiA' and `uiB' as the bit patterns of two 32-bit floating-
91 | point values, at least one of which is a NaN, returns the bit pattern of
92 | the combined NaN result.  If either `uiA' or `uiB' has the pattern of a
93 | signaling NaN, the invalid exception is raised.
94 *----------------------------------------------------------------------------*/
95 uint_fast32_t
96  softfloat_propagateNaNF32UI( uint_fast32_t uiA, uint_fast32_t uiB );
97 
98 /*----------------------------------------------------------------------------
99 | The bit pattern for a default generated 64-bit floating-point NaN.
100 *----------------------------------------------------------------------------*/
101 #define defaultNaNF64UI UINT64_C( 0xFFF8000000000000 )
102 
103 /*----------------------------------------------------------------------------
104 | Returns true when 64-bit unsigned integer `uiA' has the bit pattern of a
105 | 64-bit floating-point signaling NaN.
106 | Note:  This macro evaluates its argument more than once.
107 *----------------------------------------------------------------------------*/
108 #define softfloat_isSigNaNF64UI( uiA ) ((((uiA) & UINT64_C( 0x7FF8000000000000 )) == UINT64_C( 0x7FF0000000000000 )) && ((uiA) & UINT64_C( 0x0007FFFFFFFFFFFF )))
109 
110 /*----------------------------------------------------------------------------
111 | Assuming `uiA' has the bit pattern of a 64-bit floating-point NaN, converts
112 | this NaN to the common NaN form, and stores the resulting common NaN at the
113 | location pointed to by `zPtr'.  If the NaN is a signaling NaN, the invalid
114 | exception is raised.
115 *----------------------------------------------------------------------------*/
116 void softfloat_f64UIToCommonNaN( uint_fast64_t uiA, struct commonNaN *zPtr );
117 
118 /*----------------------------------------------------------------------------
119 | Converts the common NaN pointed to by `aPtr' into a 64-bit floating-point
120 | NaN, and returns the bit pattern of this value as an unsigned integer.
121 *----------------------------------------------------------------------------*/
122 uint_fast64_t softfloat_commonNaNToF64UI( const struct commonNaN *aPtr );
123 
124 /*----------------------------------------------------------------------------
125 | Interpreting `uiA' and `uiB' as the bit patterns of two 64-bit floating-
126 | point values, at least one of which is a NaN, returns the bit pattern of
127 | the combined NaN result.  If either `uiA' or `uiB' has the pattern of a
128 | signaling NaN, the invalid exception is raised.
129 *----------------------------------------------------------------------------*/
130 uint_fast64_t
131  softfloat_propagateNaNF64UI( uint_fast64_t uiA, uint_fast64_t uiB );
132 
133 /*----------------------------------------------------------------------------
134 | The bit pattern for a default generated 80-bit extended floating-point NaN.
135 *----------------------------------------------------------------------------*/
136 #define defaultNaNExtF80UI64 0xFFFF
137 #define defaultNaNExtF80UI0  UINT64_C( 0xC000000000000000 )
138 
139 /*----------------------------------------------------------------------------
140 | Returns true when the 80-bit unsigned integer formed from concatenating
141 | 16-bit `uiA64' and 64-bit `uiA0' has the bit pattern of an 80-bit extended
142 | floating-point signaling NaN.
143 | Note:  This macro evaluates its arguments more than once.
144 *----------------------------------------------------------------------------*/
145 #define softfloat_isSigNaNExtF80UI( uiA64, uiA0 ) ((((uiA64) & 0x7FFF) == 0x7FFF) && ! ((uiA0) & UINT64_C( 0x4000000000000000 )) && ((uiA0) & UINT64_C( 0x3FFFFFFFFFFFFFFF )))
146 
147 #ifdef SOFTFLOAT_FAST_INT64
148 
149 /*----------------------------------------------------------------------------
150 | The following functions are needed only when `SOFTFLOAT_FAST_INT64' is
151 | defined.
152 *----------------------------------------------------------------------------*/
153 
154 /*----------------------------------------------------------------------------
155 | Assuming the unsigned integer formed from concatenating `uiA64' and `uiA0'
156 | has the bit pattern of an 80-bit extended floating-point NaN, converts
157 | this NaN to the common NaN form, and stores the resulting common NaN at the
158 | location pointed to by `zPtr'.  If the NaN is a signaling NaN, the invalid
159 | exception is raised.
160 *----------------------------------------------------------------------------*/
161 void
162  softfloat_extF80UIToCommonNaN(
163      uint_fast16_t uiA64, uint_fast64_t uiA0, struct commonNaN *zPtr );
164 
165 /*----------------------------------------------------------------------------
166 | Converts the common NaN pointed to by `aPtr' into an 80-bit extended
167 | floating-point NaN, and returns the bit pattern of this value as an unsigned
168 | integer.
169 *----------------------------------------------------------------------------*/
170 struct uint128 softfloat_commonNaNToExtF80UI( const struct commonNaN *aPtr );
171 
172 /*----------------------------------------------------------------------------
173 | Interpreting the unsigned integer formed from concatenating `uiA64' and
174 | `uiA0' as an 80-bit extended floating-point value, and likewise interpreting
175 | the unsigned integer formed from concatenating `uiB64' and `uiB0' as another
176 | 80-bit extended floating-point value, and assuming at least on of these
177 | floating-point values is a NaN, returns the bit pattern of the combined NaN
178 | result.  If either original floating-point value is a signaling NaN, the
179 | invalid exception is raised.
180 *----------------------------------------------------------------------------*/
181 struct uint128
182  softfloat_propagateNaNExtF80UI(
183      uint_fast16_t uiA64,
184      uint_fast64_t uiA0,
185      uint_fast16_t uiB64,
186      uint_fast64_t uiB0
187  );
188 
189 /*----------------------------------------------------------------------------
190 | The bit pattern for a default generated 128-bit floating-point NaN.
191 *----------------------------------------------------------------------------*/
192 #define defaultNaNF128UI64 UINT64_C( 0xFFFF800000000000 )
193 #define defaultNaNF128UI0  UINT64_C( 0 )
194 
195 /*----------------------------------------------------------------------------
196 | Returns true when the 128-bit unsigned integer formed from concatenating
197 | 64-bit `uiA64' and 64-bit `uiA0' has the bit pattern of a 128-bit floating-
198 | point signaling NaN.
199 | Note:  This macro evaluates its arguments more than once.
200 *----------------------------------------------------------------------------*/
201 #define softfloat_isSigNaNF128UI( uiA64, uiA0 ) ((((uiA64) & UINT64_C( 0x7FFF800000000000 )) == UINT64_C( 0x7FFF000000000000 )) && ((uiA0) || ((uiA64) & UINT64_C( 0x00007FFFFFFFFFFF ))))
202 
203 /*----------------------------------------------------------------------------
204 | Assuming the unsigned integer formed from concatenating `uiA64' and `uiA0'
205 | has the bit pattern of a 128-bit floating-point NaN, converts this NaN to
206 | the common NaN form, and stores the resulting common NaN at the location
207 | pointed to by `zPtr'.  If the NaN is a signaling NaN, the invalid exception
208 | is raised.
209 *----------------------------------------------------------------------------*/
210 void
211  softfloat_f128UIToCommonNaN(
212      uint_fast64_t uiA64, uint_fast64_t uiA0, struct commonNaN *zPtr );
213 
214 /*----------------------------------------------------------------------------
215 | Converts the common NaN pointed to by `aPtr' into a 128-bit floating-point
216 | NaN, and returns the bit pattern of this value as an unsigned integer.
217 *----------------------------------------------------------------------------*/
218 struct uint128 softfloat_commonNaNToF128UI( const struct commonNaN * );
219 
220 /*----------------------------------------------------------------------------
221 | Interpreting the unsigned integer formed from concatenating `uiA64' and
222 | `uiA0' as a 128-bit floating-point value, and likewise interpreting the
223 | unsigned integer formed from concatenating `uiB64' and `uiB0' as another
224 | 128-bit floating-point value, and assuming at least on of these floating-
225 | point values is a NaN, returns the bit pattern of the combined NaN result.
226 | If either original floating-point value is a signaling NaN, the invalid
227 | exception is raised.
228 *----------------------------------------------------------------------------*/
229 struct uint128
230  softfloat_propagateNaNF128UI(
231      uint_fast64_t uiA64,
232      uint_fast64_t uiA0,
233      uint_fast64_t uiB64,
234      uint_fast64_t uiB0
235  );
236 
237 #else
238 
239 /*----------------------------------------------------------------------------
240 | The following functions are needed only when `SOFTFLOAT_FAST_INT64' is not
241 | defined.
242 *----------------------------------------------------------------------------*/
243 
244 /*----------------------------------------------------------------------------
245 | Assuming the 80-bit extended floating-point value pointed to by `aSPtr' is
246 | a NaN, converts this NaN to the common NaN form, and stores the resulting
247 | common NaN at the location pointed to by `zPtr'.  If the NaN is a signaling
248 | NaN, the invalid exception is raised.
249 *----------------------------------------------------------------------------*/
250 void
251  softfloat_extF80MToCommonNaN(
252      const struct extFloat80M *aSPtr, struct commonNaN *zPtr );
253 
254 /*----------------------------------------------------------------------------
255 | Converts the common NaN pointed to by `aPtr' into an 80-bit extended
256 | floating-point NaN, and stores this NaN at the location pointed to by
257 | `zSPtr'.
258 *----------------------------------------------------------------------------*/
259 void
260  softfloat_commonNaNToExtF80M(
261      const struct commonNaN *aPtr, struct extFloat80M *zSPtr );
262 
263 /*----------------------------------------------------------------------------
264 | Assuming at least one of the two 80-bit extended floating-point values
265 | pointed to by `aSPtr' and `bSPtr' is a NaN, stores the combined NaN result
266 | at the location pointed to by `zSPtr'.  If either original floating-point
267 | value is a signaling NaN, the invalid exception is raised.
268 *----------------------------------------------------------------------------*/
269 void
270  softfloat_propagateNaNExtF80M(
271      const struct extFloat80M *aSPtr,
272      const struct extFloat80M *bSPtr,
273      struct extFloat80M *zSPtr
274  );
275 
276 /*----------------------------------------------------------------------------
277 | The bit pattern for a default generated 128-bit floating-point NaN.
278 *----------------------------------------------------------------------------*/
279 #define defaultNaNF128UI96 0xFFFF8000
280 #define defaultNaNF128UI64 0
281 #define defaultNaNF128UI32 0
282 #define defaultNaNF128UI0  0
283 
284 /*----------------------------------------------------------------------------
285 | Assuming the 128-bit floating-point value pointed to by `aWPtr' is a NaN,
286 | converts this NaN to the common NaN form, and stores the resulting common
287 | NaN at the location pointed to by `zPtr'.  If the NaN is a signaling NaN,
288 | the invalid exception is raised.  Argument `aWPtr' points to an array of
289 | four 32-bit elements that concatenate in the platform's normal endian order
290 | to form a 128-bit floating-point value.
291 *----------------------------------------------------------------------------*/
292 void
293  softfloat_f128MToCommonNaN( const uint32_t *aWPtr, struct commonNaN *zPtr );
294 
295 /*----------------------------------------------------------------------------
296 | Converts the common NaN pointed to by `aPtr' into a 128-bit floating-point
297 | NaN, and stores this NaN at the location pointed to by `zWPtr'.  Argument
298 | `zWPtr' points to an array of four 32-bit elements that concatenate in the
299 | platform's normal endian order to form a 128-bit floating-point value.
300 *----------------------------------------------------------------------------*/
301 void
302  softfloat_commonNaNToF128M( const struct commonNaN *aPtr, uint32_t *zWPtr );
303 
304 /*----------------------------------------------------------------------------
305 | Assuming at least one of the two 128-bit floating-point values pointed to by
306 | `aWPtr' and `bWPtr' is a NaN, stores the combined NaN result at the location
307 | pointed to by `zWPtr'.  If either original floating-point value is a
308 | signaling NaN, the invalid exception is raised.  Each of `aWPtr', `bWPtr',
309 | and `zWPtr' points to an array of four 32-bit elements that concatenate in
310 | the platform's normal endian order to form a 128-bit floating-point value.
311 *----------------------------------------------------------------------------*/
312 void
313  softfloat_propagateNaNF128M(
314      const uint32_t *aWPtr, const uint32_t *bWPtr, uint32_t *zWPtr );
315 
316 #endif
317 
318 #endif
319 
320