summaryrefslogtreecommitdiff
path: root/libgfortran/ieee/issignaling_fallback.h
blob: e824cf8c59bde4b2cf58ac3834a3cf5e82f751c1 (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
/* Fallback implementation of issignaling macro.
   Copyright (C) 2022 Free Software Foundation, Inc.
   Contributed by Francois-Xavier Coudert <fxcoudert@gcc.gnu.org>

This file is part of the GNU Fortran runtime library (libgfortran).

Libgfortran 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 of the License, or (at your option) any later version.

Libgfortran 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/>.  */

#include "libgfortran.h"

/* This header provides an implementation of the type-generic issignaling macro.
   Some points of note:

     - This header is only included if the issignaling macro is not defined.
     - All targets for which Fortran IEEE modules are supported currently have
       the high-order bit of the NaN mantissa clear for signaling (and set
       for quiet), as recommended by IEEE.
     - We use the __*_IS_IEC_60559__ macros to make sure we only deal with formats
       we know. For other floating-point formats, we consider all NaNs as quiet.

 */

typedef union
{
  float value;
  uint32_t word;
} ieee_float_shape_type;

static inline int
__issignalingf (float x)
{
#if __FLT_IS_IEC_60559__
  uint32_t xi;
  ieee_float_shape_type u;

  u.value = x;
  xi = u.word;

  xi ^= 0x00400000;
  return (xi & 0x7fffffff) > 0x7fc00000;
#else
  return 0;
#endif
}


typedef union
{
  double value;
  uint64_t word;
} ieee_double_shape_type;

static inline int
__issignaling (double x)
{
#if __DBL_IS_IEC_60559__
  ieee_double_shape_type u;
  uint64_t xi;

  u.value = x;
  xi = u.word;

  xi ^= UINT64_C (0x0008000000000000);
  return (xi & UINT64_C (0x7fffffffffffffff)) > UINT64_C (0x7ff8000000000000);
#else
  return 0;
#endif
}


#if __LDBL_DIG__ == __DBL_DIG__

/* Long double is the same as double.  */
static inline int
__issignalingl (long double x)
{
  return __issignaling (x);
}

#elif (__LDBL_DIG__ == 18) && __LDBL_IS_IEC_60559__

/* Long double is x86 extended type.  */

typedef union
{
  long double value;
  struct
  {
#if __FLOAT_WORD_ORDER == __BIG_ENDIAN
    int sign_exponent:16;
    unsigned int empty:16;
    uint32_t msw;
    uint32_t lsw;
#elif __FLOAT_WORD_ORDER == __LITTLE_ENDIAN
    uint32_t lsw;
    uint32_t msw;
    int sign_exponent:16;
    unsigned int empty:16;
#endif
  } parts;
} ieee_long_double_shape_type;

static inline int
__issignalingl (long double x)
{
  int ret;
  uint32_t exi, hxi, lxi;
  ieee_long_double_shape_type u;

  u.value = x;
  exi = u.parts.sign_exponent;
  hxi = u.parts.msw;
  lxi = u.parts.lsw;

  /* Pseudo numbers on x86 are always signaling.  */
  ret = (exi & 0x7fff) && ((hxi & 0x80000000) == 0);

  hxi ^= 0x40000000;
  hxi |= (lxi | -lxi) >> 31;
  return ret || (((exi & 0x7fff) == 0x7fff) && (hxi > 0xc0000000));
}

#elif (__LDBL_DIG__ = 33) && __LDBL_IS_IEC_60559__

/* Long double is 128-bit type.  */

typedef union
{
  long double value;
  struct
  {
#if __FLOAT_WORD_ORDER == __BIG_ENDIAN
    uint64_t msw;
    uint64_t lsw;
#elif __FLOAT_WORD_ORDER == __LITTLE_ENDIAN
    uint64_t lsw;
    uint64_t msw;
#endif
  } parts64;
} ieee854_long_double_shape_type;

static inline int
__issignalingl (long double x)
{
  uint64_t hxi, lxi;
  ieee854_long_double_shape_type u;

  u.value = x;
  hxi = u.parts64.msw;
  lxi = u.parts64.lsw;

  hxi ^= UINT64_C (0x0000800000000000);
  hxi |= (lxi | -lxi) >> 63;
  return (hxi & UINT64_C (0x7fffffffffffffff)) > UINT64_C (0x7fff800000000000);
}

#else

static inline int
__issignalingl (long double x)
{
  return 0;
}

#endif


#if __FLT128_IS_IEC_60559__

/* We have a _Float128 type.  */

typedef union
{
  __float128 value;
  struct
  {
#if __FLOAT_WORD_ORDER == __BIG_ENDIAN
    uint64_t msw;
    uint64_t lsw;
#elif __FLOAT_WORD_ORDER == __LITTLE_ENDIAN
    uint64_t lsw;
    uint64_t msw;
#endif
  } parts64;
} ieee854_float128_shape_type;

static inline int
__issignalingf128 (__float128 x)
{
  uint64_t hxi, lxi;
  ieee854_float128_shape_type u;

  u.value = x;
  hxi = u.parts64.msw;
  lxi = u.parts64.lsw;

  hxi ^= UINT64_C (0x0000800000000000);
  hxi |= (lxi | -lxi) >> 63;
  return (hxi & UINT64_C (0x7fffffffffffffff)) > UINT64_C (0x7fff800000000000);
}

#endif


/* Define the type-generic macro based on the functions above.  */

#if __FLT128_IS_IEC_60559__
# define issignaling(X) \
  _Generic ((X), \
	    __float128: __issignalingf128, \
	    float: __issignalingf, \
	    double: __issignaling, \
	    long double: __issignalingl)(X)
#else
# define issignaling(X) \
  _Generic ((X), \
	    float: __issignalingf, \
	    double: __issignaling, \
	    long double: __issignalingl)(X)
#endif