summaryrefslogtreecommitdiff
path: root/libgcc/fp-bit.h
blob: 29661be8a317615d709968551ca266533878f57c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/* Header file for fp-bit.c.  */
/* Copyright (C) 2000-2015 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.

You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
<http://www.gnu.org/licenses/>.  */

#ifndef GCC_FP_BIT_H
#define GCC_FP_BIT_H

/* Defining FINE_GRAINED_LIBRARIES allows one to select which routines
   from this file are compiled via additional -D options.

   This avoids the need to pull in the entire fp emulation library
   when only a small number of functions are needed.

   If FINE_GRAINED_LIBRARIES is not defined, then compile every
   suitable routine.  */
#ifndef FINE_GRAINED_LIBRARIES
#define L_pack_df
#define L_unpack_df
#define L_pack_sf
#define L_unpack_sf
#define L_addsub_sf
#define L_addsub_df
#define L_mul_sf
#define L_mul_df
#define L_div_sf
#define L_div_df
#define L_fpcmp_parts_sf
#define L_fpcmp_parts_df
#define L_compare_sf
#define L_compare_df
#define L_eq_sf
#define L_eq_df
#define L_ne_sf
#define L_ne_df
#define L_gt_sf
#define L_gt_df
#define L_ge_sf
#define L_ge_df
#define L_lt_sf
#define L_lt_df
#define L_le_sf
#define L_le_df
#define L_unord_sf
#define L_unord_df
#define L_usi_to_sf
#define L_usi_to_df
#define L_si_to_sf
#define L_si_to_df
#define L_sf_to_si
#define L_df_to_si
#define L_f_to_usi
#define L_df_to_usi
#define L_negate_sf
#define L_negate_df
#define L_make_sf
#define L_make_df
#define L_sf_to_df
#define L_df_to_sf
#ifdef FLOAT
#define L_thenan_sf
#else
#define L_thenan_df
#endif
#endif /* ! FINE_GRAINED_LIBRARIES */

#if __LDBL_MANT_DIG__ == 113 || __LDBL_MANT_DIG__ == 106
# if defined(TFLOAT) || defined(L_sf_to_tf) || defined(L_df_to_tf)
#  define TMODES
# endif
#endif

typedef float SFtype __attribute__ ((mode (SF)));
typedef float DFtype __attribute__ ((mode (DF)));
#ifdef TMODES
typedef float TFtype __attribute__ ((mode (TF)));
#endif

typedef int HItype __attribute__ ((mode (HI)));
typedef int SItype __attribute__ ((mode (SI)));
typedef int DItype __attribute__ ((mode (DI)));
#ifdef TMODES
typedef int TItype __attribute__ ((mode (TI)));
#endif

/* The type of the result of a floating point comparison.  This must
   match `__libgcc_cmp_return__' in GCC for the target.  */
#ifndef CMPtype
typedef int CMPtype __attribute__ ((mode (__libgcc_cmp_return__)));
#endif

typedef unsigned int UHItype __attribute__ ((mode (HI)));
typedef unsigned int USItype __attribute__ ((mode (SI)));
typedef unsigned int UDItype __attribute__ ((mode (DI)));
#ifdef TMODES
typedef unsigned int UTItype __attribute__ ((mode (TI)));
#endif

#define MAX_USI_INT  (~(USItype)0)
#define MAX_SI_INT   ((SItype) (MAX_USI_INT >> 1))
#define BITS_PER_SI  (4 * __CHAR_BIT__)
#ifdef TMODES
#define MAX_UDI_INT  (~(UDItype)0)
#define MAX_DI_INT   ((DItype) (MAX_UDI_INT >> 1))
#define BITS_PER_DI  (8 * __CHAR_BIT__)
#endif

#ifdef FLOAT_ONLY
#define NO_DI_MODE
#endif

#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define FLOAT_BIT_ORDER_MISMATCH
#endif

#if __BYTE_ORDER__ != __FLOAT_WORD_ORDER__
#define FLOAT_WORD_ORDER_MISMATCH
#endif

#ifdef TFLOAT
# ifndef TMODES
#  error "TFLOAT requires long double to have 113 bits of mantissa"
# endif

#	define PREFIXFPDP tp
#	define PREFIXSFDF tf
#	define NGARDS 10L /* Is this right? */
#	define GARDROUND 0x1ff
#	define GARDMASK  0x3ff
#	define GARDMSB   0x200
#	define FRAC_NBITS 128

# if __LDBL_MANT_DIG__ == 113 /* IEEE quad */
#	define EXPBITS 15
#	define EXPBIAS 16383
#	define EXPMAX (0x7fff)
#	define QUIET_NAN ((TItype)0x8 << 108)
#	define FRACHIGH  ((TItype)0x8 << 124)
#	define FRACHIGH2 ((TItype)0xc << 124)
#	define FRACBITS 112
# endif

# if __LDBL_MANT_DIG__ == 106 /* IBM extended (double+double) */
#	define EXPBITS 11
#	define EXPBIAS 1023
#	define EXPMAX (0x7ff)
#	define QUIET_NAN ((TItype)0x8 << (48 + 64))
#	define FRACHIGH  ((TItype)0x8 << 124)
#	define FRACHIGH2 ((TItype)0xc << 124)
#	define FRACBITS 105
#	define HALFFRACBITS 52
#	define HALFSHIFT 64
# endif

#	define pack_d __pack_t
#	define unpack_d __unpack_t
#	define __fpcmp_parts __fpcmp_parts_t
	typedef UTItype fractype;
	typedef UDItype halffractype;
	typedef USItype qrtrfractype;
#define qrtrfractype qrtrfractype
	typedef TFtype FLO_type;
	typedef TItype intfrac;
#elif defined FLOAT
#	define NGARDS    7L
#	define GARDROUND 0x3f
#	define GARDMASK  0x7f
#	define GARDMSB   0x40
#	define EXPBITS 8
#	define EXPBIAS 127
#	define FRACBITS 23
#	define EXPMAX (0xff)
#	define QUIET_NAN 0x400000L
#	define FRAC_NBITS 32
#	define FRACHIGH  0x80000000L
#	define FRACHIGH2 0xc0000000L
#	define pack_d __pack_f
#	define unpack_d __unpack_f
#	define __fpcmp_parts __fpcmp_parts_f
	typedef USItype fractype;
	typedef UHItype halffractype;
	typedef SFtype FLO_type;
	typedef SItype intfrac;

#else
#	define PREFIXFPDP dp
#	define PREFIXSFDF df
#	define NGARDS 8L
#	define GARDROUND 0x7f
#	define GARDMASK  0xff
#	define GARDMSB   0x80
#	define EXPBITS 11
#	define EXPBIAS 1023
#	define FRACBITS 52
#	define EXPMAX (0x7ff)
#	define QUIET_NAN 0x8000000000000LL
#	define FRAC_NBITS 64
#	define FRACHIGH  0x8000000000000000LL
#	define FRACHIGH2 0xc000000000000000LL
#	define pack_d __pack_d
#	define unpack_d __unpack_d
#	define __fpcmp_parts __fpcmp_parts_d
	typedef UDItype fractype;
	typedef USItype halffractype;
	typedef DFtype FLO_type;
	typedef DItype intfrac;
#endif /* FLOAT */

#ifdef TFLOAT
#	define add 		__addtf3
#	define sub 		__subtf3
#	define multiply 	__multf3
#	define divide 		__divtf3
#	define compare 		__cmptf2
#	define _eq_f2 		__eqtf2
#	define _ne_f2 		__netf2
#	define _gt_f2 		__gttf2
#	define _ge_f2 		__getf2
#	define _lt_f2 		__lttf2
#	define _le_f2 		__letf2
#	define _unord_f2	__unordtf2
#	define usi_to_float 	__floatunsitf
#	define si_to_float 	__floatsitf
#	define float_to_si 	__fixtfsi
#	define float_to_usi 	__fixunstfsi
#	define negate 		__negtf2
#	define tf_to_sf		__trunctfsf2
#	define tf_to_df		__trunctfdf2
#elif defined FLOAT
#	define add 		__addsf3
#	define sub 		__subsf3
#	define multiply 	__mulsf3
#	define divide 		__divsf3
#	define compare 		__cmpsf2
#	define _eq_f2 		__eqsf2
#	define _ne_f2 		__nesf2
#	define _gt_f2 		__gtsf2
#	define _ge_f2 		__gesf2
#	define _lt_f2 		__ltsf2
#	define _le_f2 		__lesf2
#	define _unord_f2	__unordsf2
#	define usi_to_float 	__floatunsisf
#	define si_to_float 	__floatsisf
#	define float_to_si 	__fixsfsi
#	define float_to_usi 	__fixunssfsi
#	define negate 		__negsf2
#	define sf_to_df		__extendsfdf2
#	define sf_to_tf		__extendsftf2
#else
#	define add 		__adddf3
#	define sub 		__subdf3
#	define multiply 	__muldf3
#	define divide 		__divdf3
#	define compare 		__cmpdf2
#	define _eq_f2 		__eqdf2
#	define _ne_f2 		__nedf2
#	define _gt_f2 		__gtdf2
#	define _ge_f2 		__gedf2
#	define _lt_f2 		__ltdf2
#	define _le_f2 		__ledf2
#	define _unord_f2	__unorddf2
#	define usi_to_float 	__floatunsidf
#	define si_to_float 	__floatsidf
#	define float_to_si 	__fixdfsi
#	define float_to_usi 	__fixunsdfsi
#	define negate 		__negdf2
#	define df_to_sf		__truncdfsf2
#	define df_to_tf		__extenddftf2
#endif /* FLOAT */

#ifndef INLINE
#define INLINE __inline__
#endif

/* Preserve the sticky-bit when shifting fractions to the right.  */
#define LSHIFT(a, s) { a = (a >> s) | !!(a & (((fractype) 1 << s) - 1)); }

/* numeric parameters */
/* F_D_BITOFF is the number of bits offset between the MSB of the mantissa
   of a float and of a double. Assumes there are only two float types.
   (double::FRAC_BITS+double::NGARDS-(float::FRAC_BITS+float::NGARDS))
 */
#define F_D_BITOFF (52+8-(23+7))

#ifdef TMODES
# define F_T_BITOFF (__LDBL_MANT_DIG__-1+10-(23+7))
# define D_T_BITOFF (__LDBL_MANT_DIG__-1+10-(52+8))
#endif


#define NORMAL_EXPMIN (-(EXPBIAS)+1)
#define IMPLICIT_1 ((fractype)1<<(FRACBITS+NGARDS))
#define IMPLICIT_2 ((fractype)1<<(FRACBITS+1+NGARDS))

/* common types */

typedef enum
{
  CLASS_SNAN,
  CLASS_QNAN,
  CLASS_ZERO,
  CLASS_NUMBER,
  CLASS_INFINITY
} fp_class_type;

typedef struct
{
#ifdef SMALL_MACHINE
  char class;
  unsigned char sign;
  short normal_exp;
#else
  fp_class_type class;
  unsigned int sign;
  int normal_exp;
#endif

  union
    {
      fractype ll;
      halffractype l[2];
    } fraction;
} fp_number_type;

typedef union
{
  FLO_type value;
  fractype value_raw;

#ifndef FLOAT
# ifdef qrtrfractype
  qrtrfractype qwords[4];
# else
  halffractype words[2];
# endif
#endif

#ifdef FLOAT_BIT_ORDER_MISMATCH
  struct
    {
      fractype fraction:FRACBITS __attribute__ ((packed));
      unsigned int exp:EXPBITS __attribute__ ((packed));
      unsigned int sign:1 __attribute__ ((packed));
    }
  bits;
#endif

#ifdef _DEBUG_BITFLOAT
  struct
    {
      unsigned int sign:1 __attribute__ ((packed));
      unsigned int exp:EXPBITS __attribute__ ((packed));
      fractype fraction:FRACBITS __attribute__ ((packed));
    }
  bits_big_endian;

  struct
    {
      fractype fraction:FRACBITS __attribute__ ((packed));
      unsigned int exp:EXPBITS __attribute__ ((packed));
      unsigned int sign:1 __attribute__ ((packed));
    }
  bits_little_endian;
#endif
}
FLO_union_type;

/* Prototypes.  */

#if defined(L_pack_df) || defined(L_pack_sf) || defined(L_pack_tf)
extern FLO_type pack_d (const fp_number_type *);
#endif

extern void unpack_d (FLO_union_type *, fp_number_type *);

#if defined(L_addsub_sf) || defined(L_addsub_df) || defined(L_addsub_tf)
extern FLO_type add (FLO_type, FLO_type);
extern FLO_type sub (FLO_type, FLO_type);
#endif

#if defined(L_mul_sf) || defined(L_mul_df) || defined(L_mul_tf)
extern FLO_type multiply (FLO_type, FLO_type);
#endif

#if defined(L_div_sf) || defined(L_div_df) || defined(L_div_tf)
extern FLO_type divide (FLO_type, FLO_type);
#endif

extern int __fpcmp_parts (fp_number_type *, fp_number_type *);

#if defined(L_compare_sf) || defined(L_compare_df) || defined(L_compare_tf)
extern CMPtype compare (FLO_type, FLO_type);
#endif

#if defined(L_eq_sf) || defined(L_eq_df) || defined(L_eq_tf)
extern CMPtype _eq_f2 (FLO_type, FLO_type);
#endif

#if defined(L_ne_sf) || defined(L_ne_df) || defined(L_ne_tf)
extern CMPtype _ne_f2 (FLO_type, FLO_type);
#endif

#if defined(L_gt_sf) || defined(L_gt_df) || defined(L_gt_tf)
extern CMPtype _gt_f2 (FLO_type, FLO_type);
#endif

#if defined(L_ge_sf) || defined(L_ge_df) || defined(L_ge_tf)
extern CMPtype _ge_f2 (FLO_type, FLO_type);
#endif

#if defined(L_lt_sf) || defined(L_lt_df) || defined(L_lt_tf)
extern CMPtype _lt_f2 (FLO_type, FLO_type);
#endif

#if defined(L_le_sf) || defined(L_le_df) || defined(L_le_tf)
extern CMPtype _le_f2 (FLO_type, FLO_type);
#endif

#if defined(L_unord_sf) || defined(L_unord_df) || defined(L_unord_tf)
extern CMPtype _unord_f2 (FLO_type, FLO_type);
#endif

#if defined(L_si_to_sf) || defined(L_si_to_df) || defined(L_si_to_tf)
extern FLO_type si_to_float (SItype);
#endif

#if defined(L_sf_to_si) || defined(L_df_to_si) || defined(L_tf_to_si)
extern SItype float_to_si (FLO_type);
#endif

#if defined(L_tf_to_usi)
extern USItype float_to_usi (FLO_type);
#endif

#if defined(L_usi_to_sf) || defined(L_usi_to_df) || defined(L_usi_to_tf)
extern FLO_type usi_to_float (USItype);
#endif

#if defined(L_negate_sf) || defined(L_negate_df) || defined(L_negate_tf)
extern FLO_type negate (FLO_type);
#endif

#ifdef FLOAT
#if defined(L_make_sf)
extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
#endif
#ifndef FLOAT_ONLY
extern DFtype __make_dp (fp_class_type, unsigned int, int, UDItype);
#if defined(L_sf_to_df)
extern DFtype sf_to_df (SFtype);
#endif
#if defined(L_sf_to_tf) && defined(TMODES)
extern TFtype sf_to_tf (SFtype);
#endif
#endif /* ! FLOAT_ONLY */
#endif /* FLOAT */

#ifndef FLOAT
extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
#if defined(L_make_df)
extern DFtype __make_dp (fp_class_type, unsigned int, int, UDItype);
#endif
#if defined(L_df_to_sf)
extern SFtype df_to_sf (DFtype);
#endif
#if defined(L_df_to_tf) && defined(TMODES)
extern TFtype df_to_tf (DFtype);
#endif
#endif /* ! FLOAT */

#ifdef TMODES
extern DFtype __make_dp (fp_class_type, unsigned int, int, UDItype);
extern TFtype __make_tp (fp_class_type, unsigned int, int, UTItype);
#ifdef TFLOAT
#if defined(L_tf_to_sf)
extern SFtype tf_to_sf (TFtype);
#endif
#if defined(L_tf_to_df)
extern DFtype tf_to_df (TFtype);
#endif
#if defined(L_di_to_tf)
extern TFtype di_to_df (DItype);
#endif
#endif /* TFLOAT */
#endif /* TMODES */

#endif /* ! GCC_FP_BIT_H */