1 /* Header file for fp-bit.c.  */
2 /* Copyright (C) 2000-2022 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10 
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15 
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
19 
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 <http://www.gnu.org/licenses/>.  */
24 
25 #ifndef GCC_FP_BIT_H
26 #define GCC_FP_BIT_H
27 
28 /* Defining FINE_GRAINED_LIBRARIES allows one to select which routines
29    from this file are compiled via additional -D options.
30 
31    This avoids the need to pull in the entire fp emulation library
32    when only a small number of functions are needed.
33 
34    If FINE_GRAINED_LIBRARIES is not defined, then compile every
35    suitable routine.  */
36 #ifndef FINE_GRAINED_LIBRARIES
37 #define L_pack_df
38 #define L_unpack_df
39 #define L_pack_sf
40 #define L_unpack_sf
41 #define L_addsub_sf
42 #define L_addsub_df
43 #define L_mul_sf
44 #define L_mul_df
45 #define L_div_sf
46 #define L_div_df
47 #define L_fpcmp_parts_sf
48 #define L_fpcmp_parts_df
49 #define L_compare_sf
50 #define L_compare_df
51 #define L_eq_sf
52 #define L_eq_df
53 #define L_ne_sf
54 #define L_ne_df
55 #define L_gt_sf
56 #define L_gt_df
57 #define L_ge_sf
58 #define L_ge_df
59 #define L_lt_sf
60 #define L_lt_df
61 #define L_le_sf
62 #define L_le_df
63 #define L_unord_sf
64 #define L_unord_df
65 #define L_usi_to_sf
66 #define L_usi_to_df
67 #define L_si_to_sf
68 #define L_si_to_df
69 #define L_sf_to_si
70 #define L_df_to_si
71 #define L_f_to_usi
72 #define L_df_to_usi
73 #define L_negate_sf
74 #define L_negate_df
75 #define L_make_sf
76 #define L_make_df
77 #define L_sf_to_df
78 #define L_df_to_sf
79 #ifdef FLOAT
80 #define L_thenan_sf
81 #else
82 #define L_thenan_df
83 #endif
84 #endif /* ! FINE_GRAINED_LIBRARIES */
85 
86 #if __LDBL_MANT_DIG__ == 113 || __LDBL_MANT_DIG__ == 106
87 # if defined(TFLOAT) || defined(L_sf_to_tf) || defined(L_df_to_tf)
88 #  define TMODES
89 # endif
90 #endif
91 
92 typedef float SFtype __attribute__ ((mode (SF)));
93 typedef float DFtype __attribute__ ((mode (DF)));
94 #ifdef TMODES
95 typedef float TFtype __attribute__ ((mode (TF)));
96 #endif
97 
98 typedef int HItype __attribute__ ((mode (HI)));
99 typedef int SItype __attribute__ ((mode (SI)));
100 typedef int DItype __attribute__ ((mode (DI)));
101 #ifdef TMODES
102 typedef int TItype __attribute__ ((mode (TI)));
103 #endif
104 
105 /* The type of the result of a floating point comparison.  This must
106    match `__libgcc_cmp_return__' in GCC for the target.  */
107 #ifndef CMPtype
108 typedef int CMPtype __attribute__ ((mode (__libgcc_cmp_return__)));
109 #endif
110 
111 typedef unsigned int UHItype __attribute__ ((mode (HI)));
112 typedef unsigned int USItype __attribute__ ((mode (SI)));
113 typedef unsigned int UDItype __attribute__ ((mode (DI)));
114 #ifdef TMODES
115 typedef unsigned int UTItype __attribute__ ((mode (TI)));
116 #endif
117 
118 #define MAX_USI_INT  (~(USItype)0)
119 #define MAX_SI_INT   ((SItype) (MAX_USI_INT >> 1))
120 #define BITS_PER_SI  (4 * __CHAR_BIT__)
121 #ifdef TMODES
122 #define MAX_UDI_INT  (~(UDItype)0)
123 #define MAX_DI_INT   ((DItype) (MAX_UDI_INT >> 1))
124 #define BITS_PER_DI  (8 * __CHAR_BIT__)
125 #endif
126 
127 #ifdef FLOAT_ONLY
128 #define NO_DI_MODE
129 #endif
130 
131 #if __BYTE_ORDER__ != __FLOAT_WORD_ORDER__
132 #define FLOAT_WORD_ORDER_MISMATCH
133 #endif
134 
135 #ifdef TFLOAT
136 # ifndef TMODES
137 #  error "TFLOAT requires long double to have 113 bits of mantissa"
138 # endif
139 
140 #         define PREFIXFPDP tp
141 #         define PREFIXSFDF tf
142 #         define NGARDS 10L /* Is this right? */
143 #         define GARDROUND 0x1ff
144 #         define GARDMASK  0x3ff
145 #         define GARDMSB   0x200
146 #         define FRAC_NBITS 128
147 
148 # if __LDBL_MANT_DIG__ == 113 /* IEEE quad */
149 #         define EXPBITS 15
150 #         define EXPBIAS 16383
151 #         define EXPMAX (0x7fff)
152 #         define QUIET_NAN ((TItype)0x8 << 108)
153 #         define FRACHIGH  ((TItype)0x8 << 124)
154 #         define FRACHIGH2 ((TItype)0xc << 124)
155 #         define FRACBITS 112
156 # endif
157 
158 # if __LDBL_MANT_DIG__ == 106 /* IBM extended (double+double) */
159 #         define EXPBITS 11
160 #         define EXPBIAS 1023
161 #         define EXPMAX (0x7ff)
162 #         define QUIET_NAN ((TItype)0x8 << (48 + 64))
163 #         define FRACHIGH  ((TItype)0x8 << 124)
164 #         define FRACHIGH2 ((TItype)0xc << 124)
165 #         define FRACBITS 105
166 #         define HALFFRACBITS 52
167 #         define HALFSHIFT 64
168 # endif
169 
170 #         define pack_d __pack_t
171 #         define unpack_d __unpack_t
172 #         define __fpcmp_parts __fpcmp_parts_t
173           typedef UTItype fractype;
174           typedef UDItype halffractype;
175           typedef USItype qrtrfractype;
176 #define qrtrfractype qrtrfractype
177           typedef TFtype FLO_type;
178           typedef TItype intfrac;
179 #elif defined FLOAT
180 #         define NGARDS    7L
181 #         define GARDROUND 0x3f
182 #         define GARDMASK  0x7f
183 #         define GARDMSB   0x40
184 #         define EXPBITS 8
185 #         define EXPBIAS 127
186 #         define FRACBITS 23
187 #         define EXPMAX (0xff)
188 #         define QUIET_NAN 0x400000L
189 #         define FRAC_NBITS 32
190 #         define FRACHIGH  0x80000000L
191 #         define FRACHIGH2 0xc0000000L
192 #         define pack_d __pack_f
193 #         define unpack_d __unpack_f
194 #         define __fpcmp_parts __fpcmp_parts_f
195           typedef USItype fractype;
196           typedef UHItype halffractype;
197           typedef SFtype FLO_type;
198           typedef SItype intfrac;
199 
200 #else
201 #         define PREFIXFPDP dp
202 #         define PREFIXSFDF df
203 #         define NGARDS 8L
204 #         define GARDROUND 0x7f
205 #         define GARDMASK  0xff
206 #         define GARDMSB   0x80
207 #         define EXPBITS 11
208 #         define EXPBIAS 1023
209 #         define FRACBITS 52
210 #         define EXPMAX (0x7ff)
211 #         define QUIET_NAN 0x8000000000000LL
212 #         define FRAC_NBITS 64
213 #         define FRACHIGH  0x8000000000000000LL
214 #         define FRACHIGH2 0xc000000000000000LL
215 #         define pack_d __pack_d
216 #         define unpack_d __unpack_d
217 #         define __fpcmp_parts __fpcmp_parts_d
218           typedef UDItype fractype;
219           typedef USItype halffractype;
220           typedef DFtype FLO_type;
221           typedef DItype intfrac;
222 #endif /* FLOAT */
223 
224 #ifdef TFLOAT
225 #         define add                    __addtf3
226 #         define sub                    __subtf3
227 #         define multiply     __multf3
228 #         define divide                 __divtf3
229 #         define compare                __cmptf2
230 #         define _eq_f2                 __eqtf2
231 #         define _ne_f2                 __netf2
232 #         define _gt_f2                 __gttf2
233 #         define _ge_f2                 __getf2
234 #         define _lt_f2                 __lttf2
235 #         define _le_f2                 __letf2
236 #         define _unord_f2    __unordtf2
237 #         define usi_to_float           __floatunsitf
238 #         define si_to_float  __floatsitf
239 #         define float_to_si  __fixtfsi
240 #         define float_to_usi           __fixunstfsi
241 #         define negate                 __negtf2
242 #         define tf_to_sf               __trunctfsf2
243 #         define tf_to_df               __trunctfdf2
244 #elif defined FLOAT
245 #         define add                    __addsf3
246 #         define sub                    __subsf3
247 #         define multiply     __mulsf3
248 #         define divide                 __divsf3
249 #         define compare                __cmpsf2
250 #         define _eq_f2                 __eqsf2
251 #         define _ne_f2                 __nesf2
252 #         define _gt_f2                 __gtsf2
253 #         define _ge_f2                 __gesf2
254 #         define _lt_f2                 __ltsf2
255 #         define _le_f2                 __lesf2
256 #         define _unord_f2    __unordsf2
257 #         define usi_to_float           __floatunsisf
258 #         define si_to_float  __floatsisf
259 #         define float_to_si  __fixsfsi
260 #         define float_to_usi           __fixunssfsi
261 #         define negate                 __negsf2
262 #         define sf_to_df               __extendsfdf2
263 #         define sf_to_tf               __extendsftf2
264 #else
265 #         define add                    __adddf3
266 #         define sub                    __subdf3
267 #         define multiply     __muldf3
268 #         define divide                 __divdf3
269 #         define compare                __cmpdf2
270 #         define _eq_f2                 __eqdf2
271 #         define _ne_f2                 __nedf2
272 #         define _gt_f2                 __gtdf2
273 #         define _ge_f2                 __gedf2
274 #         define _lt_f2                 __ltdf2
275 #         define _le_f2                 __ledf2
276 #         define _unord_f2    __unorddf2
277 #         define usi_to_float           __floatunsidf
278 #         define si_to_float  __floatsidf
279 #         define float_to_si  __fixdfsi
280 #         define float_to_usi           __fixunsdfsi
281 #         define negate                 __negdf2
282 #         define df_to_sf               __truncdfsf2
283 #         define df_to_tf               __extenddftf2
284 #endif /* FLOAT */
285 
286 #ifndef INLINE
287 #define INLINE __inline__
288 #endif
289 
290 /* Preserve the sticky-bit when shifting fractions to the right.  */
291 #define LSHIFT(a, s) { a = (a >> s) | !!(a & (((fractype) 1 << s) - 1)); }
292 
293 /* numeric parameters */
294 /* F_D_BITOFF is the number of bits offset between the MSB of the mantissa
295    of a float and of a double. Assumes there are only two float types.
296    (double::FRAC_BITS+double::NGARDS-(float::FRAC_BITS+float::NGARDS))
297  */
298 #define F_D_BITOFF (52+8-(23+7))
299 
300 #ifdef TMODES
301 # define F_T_BITOFF (__LDBL_MANT_DIG__-1+10-(23+7))
302 # define D_T_BITOFF (__LDBL_MANT_DIG__-1+10-(52+8))
303 #endif
304 
305 
306 #define NORMAL_EXPMIN (-(EXPBIAS)+1)
307 #define IMPLICIT_1 ((fractype)1<<(FRACBITS+NGARDS))
308 #define IMPLICIT_2 ((fractype)1<<(FRACBITS+1+NGARDS))
309 
310 /* common types */
311 
312 typedef enum
313 {
314   CLASS_SNAN,
315   CLASS_QNAN,
316   CLASS_ZERO,
317   CLASS_NUMBER,
318   CLASS_INFINITY
319 } fp_class_type;
320 
321 typedef struct
322 {
323 #ifdef SMALL_MACHINE
324   char class;
325   unsigned char sign;
326   short normal_exp;
327 #else
328   fp_class_type class;
329   unsigned int sign;
330   int normal_exp;
331 #endif
332 
333   union
334     {
335       fractype ll;
336       halffractype l[2];
337     } fraction;
338 } fp_number_type;
339 
340 typedef union
341 {
342   FLO_type value;
343   fractype value_raw;
344 
345 #ifndef FLOAT
346 # ifdef qrtrfractype
347   qrtrfractype qwords[4];
348 # else
349   halffractype words[2];
350 # endif
351 #endif
352 
353 #ifdef _DEBUG_BITFLOAT
354   struct
355     {
356       unsigned int sign:1 __attribute__ ((packed));
357       unsigned int exp:EXPBITS __attribute__ ((packed));
358       fractype fraction:FRACBITS __attribute__ ((packed));
359     }
360   bits_big_endian;
361 
362   struct
363     {
364       fractype fraction:FRACBITS __attribute__ ((packed));
365       unsigned int exp:EXPBITS __attribute__ ((packed));
366       unsigned int sign:1 __attribute__ ((packed));
367     }
368   bits_little_endian;
369 #endif
370 }
371 FLO_union_type;
372 
373 /* Prototypes.  */
374 
375 #if defined(L_pack_df) || defined(L_pack_sf) || defined(L_pack_tf)
376 extern FLO_type pack_d (const fp_number_type *);
377 #endif
378 
379 extern void unpack_d (FLO_union_type *, fp_number_type *);
380 
381 #if defined(L_addsub_sf) || defined(L_addsub_df) || defined(L_addsub_tf)
382 extern FLO_type add (FLO_type, FLO_type);
383 extern FLO_type sub (FLO_type, FLO_type);
384 #endif
385 
386 #if defined(L_mul_sf) || defined(L_mul_df) || defined(L_mul_tf)
387 extern FLO_type multiply (FLO_type, FLO_type);
388 #endif
389 
390 #if defined(L_div_sf) || defined(L_div_df) || defined(L_div_tf)
391 extern FLO_type divide (FLO_type, FLO_type);
392 #endif
393 
394 extern int __fpcmp_parts (fp_number_type *, fp_number_type *);
395 
396 #if defined(L_compare_sf) || defined(L_compare_df) || defined(L_compare_tf)
397 extern CMPtype compare (FLO_type, FLO_type);
398 #endif
399 
400 #if defined(L_eq_sf) || defined(L_eq_df) || defined(L_eq_tf)
401 extern CMPtype _eq_f2 (FLO_type, FLO_type);
402 #endif
403 
404 #if defined(L_ne_sf) || defined(L_ne_df) || defined(L_ne_tf)
405 extern CMPtype _ne_f2 (FLO_type, FLO_type);
406 #endif
407 
408 #if defined(L_gt_sf) || defined(L_gt_df) || defined(L_gt_tf)
409 extern CMPtype _gt_f2 (FLO_type, FLO_type);
410 #endif
411 
412 #if defined(L_ge_sf) || defined(L_ge_df) || defined(L_ge_tf)
413 extern CMPtype _ge_f2 (FLO_type, FLO_type);
414 #endif
415 
416 #if defined(L_lt_sf) || defined(L_lt_df) || defined(L_lt_tf)
417 extern CMPtype _lt_f2 (FLO_type, FLO_type);
418 #endif
419 
420 #if defined(L_le_sf) || defined(L_le_df) || defined(L_le_tf)
421 extern CMPtype _le_f2 (FLO_type, FLO_type);
422 #endif
423 
424 #if defined(L_unord_sf) || defined(L_unord_df) || defined(L_unord_tf)
425 extern CMPtype _unord_f2 (FLO_type, FLO_type);
426 #endif
427 
428 #if defined(L_si_to_sf) || defined(L_si_to_df) || defined(L_si_to_tf)
429 extern FLO_type si_to_float (SItype);
430 #endif
431 
432 #if defined(L_sf_to_si) || defined(L_df_to_si) || defined(L_tf_to_si)
433 extern SItype float_to_si (FLO_type);
434 #endif
435 
436 #if defined(L_tf_to_usi)
437 extern USItype float_to_usi (FLO_type);
438 #endif
439 
440 #if defined(L_usi_to_sf) || defined(L_usi_to_df) || defined(L_usi_to_tf)
441 extern FLO_type usi_to_float (USItype);
442 #endif
443 
444 #if defined(L_negate_sf) || defined(L_negate_df) || defined(L_negate_tf)
445 extern FLO_type negate (FLO_type);
446 #endif
447 
448 #ifdef FLOAT
449 #if defined(L_make_sf)
450 extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
451 #endif
452 #ifndef FLOAT_ONLY
453 extern DFtype __make_dp (fp_class_type, unsigned int, int, UDItype);
454 #if defined(L_sf_to_df)
455 extern DFtype sf_to_df (SFtype);
456 #endif
457 #if defined(L_sf_to_tf) && defined(TMODES)
458 extern TFtype sf_to_tf (SFtype);
459 #endif
460 #endif /* ! FLOAT_ONLY */
461 #endif /* FLOAT */
462 
463 #ifndef FLOAT
464 extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
465 #if defined(L_make_df)
466 extern DFtype __make_dp (fp_class_type, unsigned int, int, UDItype);
467 #endif
468 #if defined(L_df_to_sf)
469 extern SFtype df_to_sf (DFtype);
470 #endif
471 #if defined(L_df_to_tf) && defined(TMODES)
472 extern TFtype df_to_tf (DFtype);
473 #endif
474 #endif /* ! FLOAT */
475 
476 #ifdef TMODES
477 extern DFtype __make_dp (fp_class_type, unsigned int, int, UDItype);
478 extern TFtype __make_tp (fp_class_type, unsigned int, int, UTItype);
479 #ifdef TFLOAT
480 #if defined(L_tf_to_sf)
481 extern SFtype tf_to_sf (TFtype);
482 #endif
483 #if defined(L_tf_to_df)
484 extern DFtype tf_to_df (TFtype);
485 #endif
486 #if defined(L_di_to_tf)
487 extern TFtype di_to_df (DItype);
488 #endif
489 #endif /* TFLOAT */
490 #endif /* TMODES */
491 
492 #endif /* ! GCC_FP_BIT_H */
493