Debian Codegraph @

runtime/softfloat/primitives.h

raw
1
2 /*============================================================================
3
4 This C header file is part of the SoftFloat IEEE Floating-Point Arithmetic
5
6 Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the
7 University of California. 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 #ifndef primitives_h
37 #define primitives_h 1
38 #define primitives_h 1
39
40 // #include <stdbool.h>
41 // #include <stdint.h>
42 #include "softfloat/primitiveTypes.h"
43
44 #ifndef softfloat_shortShiftRightJam64
45 /*----------------------------------------------------------------------------
46 | Shifts 'a' right by the number of bits given in 'dist', which must be in
47 | the range 1 to 63. If any nonzero bits are shifted off, they are "jammed"
48 | into the least-significant bit of the shifted value by setting the least-
49 | significant bit to 1. This shifted-and-jammed value is returned.
50 *----------------------------------------------------------------------------*/
51 #if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)
52 INLINE
53 uint64_t softfloat_shortShiftRightJam64( uint64_t a, uint_fast8_t dist )
54 { return a>>dist | ((a & (((uint_fast64_t) 1<<dist) - 1)) != 0); }
55 #else
56 uint64_t softfloat_shortShiftRightJam64( uint64_t a, uint_fast8_t dist );
57 #endif
58 #endif
59
60 #ifndef softfloat_shiftRightJam32
61 /*----------------------------------------------------------------------------
62 | Shifts 'a' right by the number of bits given in 'dist', which must not
63 | be zero. If any nonzero bits are shifted off, they are "jammed" into the
64 | least-significant bit of the shifted value by setting the least-significant
65 | bit to 1. This shifted-and-jammed value is returned.
66 | The value of 'dist' can be arbitrarily large. In particular, if 'dist' is
67 | greater than 32, the result will be either 0 or 1, depending on whether 'a'
68 | is zero or nonzero.
69 *----------------------------------------------------------------------------*/
70 #if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)
71 INLINE uint32_t softfloat_shiftRightJam32( uint32_t a, uint_fast16_t dist )
72 {
73 return
74 (dist < 31) ? a>>dist | ((uint32_t) (a<<(-dist & 31)) != 0) : (a != 0);
75 }
76 #else
77 uint32_t softfloat_shiftRightJam32( uint32_t a, uint_fast16_t dist );
78 #endif
79 #endif
80
81 #ifndef softfloat_shiftRightJam64
82 /*----------------------------------------------------------------------------
83 | Shifts 'a' right by the number of bits given in 'dist', which must not
84 | be zero. If any nonzero bits are shifted off, they are "jammed" into the
85 | least-significant bit of the shifted value by setting the least-significant
86 | bit to 1. This shifted-and-jammed value is returned.
87 | The value of 'dist' can be arbitrarily large. In particular, if 'dist' is
88 | greater than 64, the result will be either 0 or 1, depending on whether 'a'
89 | is zero or nonzero.
90 *----------------------------------------------------------------------------*/
91 #if defined INLINE_LEVEL && (3 <= INLINE_LEVEL)
92 INLINE uint64_t softfloat_shiftRightJam64( uint64_t a, uint_fast32_t dist )
93 {
94 return
95 (dist < 63) ? a>>dist | ((uint64_t) (a<<(-dist & 63)) != 0) : (a != 0);
96 }
97 #else
98 uint64_t softfloat_shiftRightJam64( uint64_t a, uint_fast32_t dist );
99 #endif
100 #endif
101
102 #ifndef softfloat_shortShiftRightM
103 /*----------------------------------------------------------------------------
104 | Shifts the N-bit unsigned integer pointed to by 'aPtr' right by the number
105 | of bits given in 'dist', where N = 'size_words' * 32. The value of 'dist'
106 | must be in the range 1 to 31. Any nonzero bits shifted off are lost. The
107 | shifted N-bit result is stored at the location pointed to by 'zPtr'. Each
108 | of 'aPtr' and 'zPtr' points to a 'size_words'-long array of 32-bit elements
109 | that concatenate in the platform's normal endian order to form an N-bit
110 | integer.
111 *----------------------------------------------------------------------------*/
112 void
113 softfloat_shortShiftRightM(
114 uint_fast8_t size_words,
115 const uint32_t *aPtr,
116 uint_fast8_t dist,
117 uint32_t *zPtr
118 );
119 #endif
120
121 #ifndef softfloat_shortShiftRight128M
122 /*----------------------------------------------------------------------------
123 | This function or macro is the same as 'softfloat_shortShiftRightM' with
124 | 'size_words' = 4 (N = 128).
125 *----------------------------------------------------------------------------*/
126 #define softfloat_shortShiftRight128M( aPtr, dist, zPtr ) softfloat_shortShiftRightM( 4, aPtr, dist, zPtr )
127 #endif
128
129 #ifndef softfloat_shortShiftRightJamM
130 /*----------------------------------------------------------------------------
131 | Shifts the N-bit unsigned integer pointed to by 'aPtr' right by the number
132 | of bits given in 'dist', where N = 'size_words' * 32. The value of 'dist'
133 | must be in the range 1 to 31. If any nonzero bits are shifted off, they are
134 | "jammed" into the least-significant bit of the shifted value by setting the
135 | least-significant bit to 1. This shifted-and-jammed N-bit result is stored
136 | at the location pointed to by 'zPtr'. Each of 'aPtr' and 'zPtr' points
137 | to a 'size_words'-long array of 32-bit elements that concatenate in the
138 | platform's normal endian order to form an N-bit integer.
139 *----------------------------------------------------------------------------*/
140 void
141 softfloat_shortShiftRightJamM(
142 uint_fast8_t, const uint32_t *, uint_fast8_t, uint32_t * );
143 #endif
144
145 #ifndef softfloat_shiftRightJamM
146 /*----------------------------------------------------------------------------
147 | Shifts the N-bit unsigned integer pointed to by 'aPtr' right by the number
148 | of bits given in 'dist', where N = 'size_words' * 32. The value of 'dist'
149 | must not be zero. If any nonzero bits are shifted off, they are "jammed"
150 | into the least-significant bit of the shifted value by setting the least-
151 | significant bit to 1. This shifted-and-jammed N-bit result is stored
152 | at the location pointed to by 'zPtr'. Each of 'aPtr' and 'zPtr' points
153 | to a 'size_words'-long array of 32-bit elements that concatenate in the
154 | platform's normal endian order to form an N-bit integer.
155 | The value of 'dist' can be arbitrarily large. In particular, if 'dist'
156 | is greater than N, the stored result will be either 0 or 1, depending on
157 | whether the original N bits are all zeros.
158 *----------------------------------------------------------------------------*/
159 void
160 softfloat_shiftRightJamM(
161 uint_fast8_t size_words,
162 const uint32_t *aPtr,
163 uint32_t dist,
164 uint32_t *zPtr
165 );
166 #endif
167
168 #ifndef softfloat_shiftRightJam96M
169 /*----------------------------------------------------------------------------
170 | This function or macro is the same as 'softfloat_shiftRightJamM' with
171 | 'size_words' = 3 (N = 96).
172 *----------------------------------------------------------------------------*/
173 #define softfloat_shiftRightJam96M( aPtr, dist, zPtr ) softfloat_shiftRightJamM( 3, aPtr, dist, zPtr )
174 #endif
175
176 #ifndef softfloat_shiftRightJam128M
177 /*----------------------------------------------------------------------------
178 | This function or macro is the same as 'softfloat_shiftRightJamM' with
179 | 'size_words' = 4 (N = 128).
180 *----------------------------------------------------------------------------*/
181 #define softfloat_shiftRightJam128M( aPtr, dist, zPtr ) softfloat_shiftRightJamM( 4, aPtr, dist, zPtr )
182 #endif
183
184 #ifndef softfloat_shortShiftLeftM
185 /*----------------------------------------------------------------------------
186 | Shifts the N-bit unsigned integer pointed to by 'aPtr' left by the number
187 | of bits given in 'dist', where N = 'size_words' * 32. The value of 'dist'
188 | must be in the range 1 to 31. Any nonzero bits shifted off are lost. The
189 | shifted N-bit result is stored at the location pointed to by 'zPtr'. Each
190 | of 'aPtr' and 'zPtr' points to a 'size_words'-long array of 32-bit elements
191 | that concatenate in the platform's normal endian order to form an N-bit
192 | integer.
193 *----------------------------------------------------------------------------*/
194 void
195 softfloat_shortShiftLeftM(
196 uint_fast8_t size_words,
197 const uint32_t *aPtr,
198 uint_fast8_t dist,
199 uint32_t *zPtr
200 );
201 #endif
202
203 #ifndef softfloat_shiftLeftM
204 /*----------------------------------------------------------------------------
205 | Shifts the N-bit unsigned integer pointed to by 'aPtr' left by the number
206 | of bits given in 'dist', where N = 'size_words' * 32. The value of 'dist'
207 | must not be zero. Any nonzero bits shifted off are lost. The shifted
208 | N-bit result is stored at the location pointed to by 'zPtr'. Each of 'aPtr'
209 | and 'zPtr' points to a 'size_words'-long array of 32-bit elements that
210 | concatenate in the platform's normal endian order to form an N-bit integer.
211 | The value of 'dist' can be arbitrarily large. In particular, if 'dist' is
212 | greater than N, the stored result will be 0.
213 *----------------------------------------------------------------------------*/
214 void
215 softfloat_shiftLeftM(
216 uint_fast8_t size_words,
217 const uint32_t *aPtr,
218 uint32_t dist,
219 uint32_t *zPtr
220 );
221 #endif
222
223 #ifndef softfloat_shiftLeft128M
224 /*----------------------------------------------------------------------------
225 | This function or macro is the same as 'softfloat_shiftLeftM' with
226 | 'size_words' = 4 (N = 128).
227 *----------------------------------------------------------------------------*/
228 #define softfloat_shiftLeft128M( aPtr, dist, zPtr ) softfloat_shiftLeftM( 4, aPtr, dist, zPtr )
229 #endif
230
231 #ifndef softfloat_countLeadingZeros64
232 /*----------------------------------------------------------------------------
233 | Returns the number of leading 0 bits before the most-significant 1 bit of
234 | 'a'. If 'a' is zero, 64 is returned.
235 *----------------------------------------------------------------------------*/
236 uint_fast8_t softfloat_countLeadingZeros64( uint64_t a );
237 #endif
238
239 extern const uint16_t softfloat_approxRecip_1k0s[16];
240 extern const uint16_t softfloat_approxRecip_1k1s[16];
241
242 #ifndef softfloat_approxRecip32_1
243 /*----------------------------------------------------------------------------
244 | Returns an approximation to the reciprocal of the number represented by 'a',
245 | where 'a' is interpreted as an unsigned fixed-point number with one integer
246 | bit and 31 fraction bits. The 'a' input must be "normalized", meaning that
247 | its most-significant bit (bit 31) must be 1. Thus, if A is the value of
248 | the fixed-point interpretation of 'a', then 1 <= A < 2. The returned value
249 | is interpreted as a pure unsigned fraction, having no integer bits and 32
250 | fraction bits. The approximation returned is never greater than the true
251 | reciprocal 1/A, and it differs from the true reciprocal by at most 2.006 ulp
252 | (units in the last place).
253 *----------------------------------------------------------------------------*/
254 #ifdef SOFTFLOAT_FAST_DIV64TO32
255 #define softfloat_approxRecip32_1( a ) ((uint32_t) (UINT64_C( 0x7FFFFFFFFFFFFFFF ) / (uint32_t) (a)))
256 #else
257 uint32_t softfloat_approxRecip32_1( uint32_t a );
258 #endif
259 #endif
260
261 extern const uint16_t softfloat_approxRecipSqrt_1k0s[16];
262 extern const uint16_t softfloat_approxRecipSqrt_1k1s[16];
263
264 #ifndef softfloat_approxRecipSqrt32_1
265 /*----------------------------------------------------------------------------
266 | Returns an approximation to the reciprocal of the square root of the number
267 | represented by 'a', where 'a' is interpreted as an unsigned fixed-point
268 | number either with one integer bit and 31 fraction bits or with two integer
269 | bits and 30 fraction bits. The format of 'a' is determined by 'oddExpA',
270 | which must be either 0 or 1. If 'oddExpA' is 1, 'a' is interpreted as
271 | having one integer bit, and if 'oddExpA' is 0, 'a' is interpreted as having
272 | two integer bits. The 'a' input must be "normalized", meaning that its
273 | most-significant bit (bit 31) must be 1. Thus, if A is the value of the
274 | fixed-point interpretation of 'a', it follows that 1 <= A < 2 when 'oddExpA'
275 | is 1, and 2 <= A < 4 when 'oddExpA' is 0.
276 | The returned value is interpreted as a pure unsigned fraction, having
277 | no integer bits and 32 fraction bits. The approximation returned is never
278 | greater than the true reciprocal 1/sqrt(A), and it differs from the true
279 | reciprocal by at most 2.06 ulp (units in the last place). The approximation
280 | returned is also always within the range 0.5 to 1; thus, the most-
281 | significant bit of the result is always set.
282 *----------------------------------------------------------------------------*/
283 uint32_t softfloat_approxRecipSqrt32_1( unsigned int oddExpA, uint32_t a );
284 #endif
285
286 #ifndef softfloat_mul64To128M
287 /*----------------------------------------------------------------------------
288 | Multiplies 'a' and 'b' and stores the 128-bit product at the location
289 | pointed to by 'zPtr'. Argument 'zPtr' points to an array of four 32-bit
290 | elements that concatenate in the platform's normal endian order to form a
291 | 128-bit integer.
292 *----------------------------------------------------------------------------*/
293 void softfloat_mul64To128M( uint64_t a, uint64_t b, uint32_t *zPtr );
294 #endif
295
296 #ifndef softfloat_negXM
297 /*----------------------------------------------------------------------------
298 | Replaces the N-bit unsigned integer pointed to by 'zPtr' by the
299 | 2s-complement of itself, where N = 'size_words' * 32. Argument 'zPtr'
300 | points to a 'size_words'-long array of 32-bit elements that concatenate in
301 | the platform's normal endian order to form an N-bit integer.
302 *----------------------------------------------------------------------------*/
303 void softfloat_negXM( uint_fast8_t size_words, uint32_t *zPtr );
304 #endif
305
306 #ifndef softfloat_negX128M
307 /*----------------------------------------------------------------------------
308 | This function or macro is the same as 'softfloat_negXM' with 'size_words'
309 | = 4 (N = 128).
310 *----------------------------------------------------------------------------*/
311 #define softfloat_negX128M( zPtr ) softfloat_negXM( 4, zPtr )
312 #endif
313
314 #ifndef softfloat_addM
315 /*----------------------------------------------------------------------------
316 | Adds the two N-bit integers pointed to by 'aPtr' and 'bPtr', where N =
317 | 'size_words' * 32. The addition is modulo 2^N, so any carry out is lost.
318 | The N-bit sum is stored at the location pointed to by 'zPtr'. Each of
319 | 'aPtr', 'bPtr', and 'zPtr' points to a 'size_words'-long array of 32-bit
320 | elements that concatenate in the platform's normal endian order to form an
321 | N-bit integer.
322 *----------------------------------------------------------------------------*/
323 void
324 softfloat_addM(
325 uint_fast8_t size_words,
326 const uint32_t *aPtr,
327 const uint32_t *bPtr,
328 uint32_t *zPtr
329 );
330 #endif
331
332 #ifndef softfloat_add128M
333 /*----------------------------------------------------------------------------
334 | This function or macro is the same as 'softfloat_addM' with 'size_words'
335 | = 4 (N = 128).
336 *----------------------------------------------------------------------------*/
337 #define softfloat_add128M( aPtr, bPtr, zPtr ) softfloat_addM( 4, aPtr, bPtr, zPtr )
338 #endif
339
340 #ifndef softfloat_subM
341 /*----------------------------------------------------------------------------
342 | Subtracts the two N-bit integers pointed to by 'aPtr' and 'bPtr', where N =
343 | 'size_words' * 32. The subtraction is modulo 2^N, so any borrow out (carry
344 | out) is lost. The N-bit difference is stored at the location pointed to by
345 | 'zPtr'. Each of 'aPtr', 'bPtr', and 'zPtr' points to a 'size_words'-long
346 | array of 32-bit elements that concatenate in the platform's normal endian
347 | order to form an N-bit integer.
348 *----------------------------------------------------------------------------*/
349 void
350 softfloat_subM(
351 uint_fast8_t size_words,
352 const uint32_t *aPtr,
353 const uint32_t *bPtr,
354 uint32_t *zPtr
355 );
356 #endif
357
358 #ifndef softfloat_sub128M
359 /*----------------------------------------------------------------------------
360 | This function or macro is the same as 'softfloat_subM' with 'size_words'
361 | = 4 (N = 128).
362 *----------------------------------------------------------------------------*/
363 #define softfloat_sub128M( aPtr, bPtr, zPtr ) softfloat_subM( 4, aPtr, bPtr, zPtr )
364 #endif
365
366 #endif