File: math_errhandling.c

package info (click to toggle)
picolibc 1.8.11-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 50,064 kB
  • sloc: ansic: 404,031; asm: 24,984; sh: 2,585; python: 2,289; perl: 680; pascal: 329; exp: 287; makefile: 222; cpp: 71; xml: 40
file content (374 lines) | stat: -rw-r--r-- 9,424 bytes parent folder | download
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
/*
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Copyright © 2020 Keith Packard
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#define _GNU_SOURCE
#include <fenv.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <stdbool.h>

#if __has_attribute(__always_inline__)
#define ALWAYS_INLINE __inline __attribute__((__always_inline__))
#else
#define ALWAYS_INLINE __inline
#endif

#ifdef __sh__
#if !(defined(__SH4__) || defined(__SH4_SINGLE__) || defined(__SH4_SINGLE_ONLY__))
#define GDB_SIMULATOR
#endif
#endif

#ifdef _RX_PID
#define TEST_CONST
#else
#define TEST_CONST const
#endif

static ALWAYS_INLINE float
opt_barrier_float(float x)
{
    volatile float y = x;
    return y;
}

static ALWAYS_INLINE double
opt_barrier_double(double x)
{
    volatile double y = x;
    return y;
}

#if __SIZEOF_LONG_DOUBLE__ > 0
static ALWAYS_INLINE long double
opt_barrier_long_double(long double x)
{
    volatile long double y = x;
    return y;
}
#endif

#ifdef __clang__
#define clang_barrier_long_double(x) opt_barrier_long_double(x)
#define clang_barrier_double(x)      opt_barrier_double(x)
#define clang_barrier_float(x)       opt_barrier_float(x)
#else
#define clang_barrier_long_double(x) (x)
#define clang_barrier_double(x)      (x)
#define clang_barrier_float(x)       (x)
#endif

static const char *
e_to_str(int e)
{
    if (e == 0)
        return "NONE";

#ifdef FE_DIVBYZERO
    if (e == FE_DIVBYZERO)
        return "FE_DIVBYZERO";
#endif
#ifdef FE_OVERFLOW
    if (e == FE_OVERFLOW)
        return "FE_OVERFLOW";
#endif
#ifdef FE_UNDERFLOW
    if (e == FE_UNDERFLOW)
        return "FE_UNDERFLOW";
#endif
#ifdef FE_INEXACT
    if (e == FE_INEXACT)
        return "FE_INEXACT";
#endif
#ifdef FE_INVALID
    if (e == FE_INVALID)
        return "FE_INVALID";
#endif
#if defined(FE_OVERFLOW) && defined(FE_INEXACT)
    if (e == (FE_OVERFLOW | FE_INEXACT))
        return "FE_OVERFLOW|FE_INEXACT";
#endif
#if defined(FE_UNDERFLOW) && defined(FE_INEXACT)
    if (e == (FE_UNDERFLOW | FE_INEXACT))
        return "FE_UNDERFLOW|FE_INEXACT";
#endif
    static char buf[3][128];
    static int  i = 0;
    buf[i][0] = '\0';
    while (e) {
        char *v = NULL;
        char  tmp[24];
#ifdef FE_DIVBYZERO
        if (e & FE_DIVBYZERO) {
            v = "FE_DIVBYZERO";
            e &= ~FE_DIVBYZERO;
        } else
#endif
#ifdef FE_OVERFLOW
            if (e & FE_OVERFLOW) {
            v = "FE_OVERFLOW";
            e &= ~FE_OVERFLOW;
        } else
#endif
#ifdef FE_UNDERFLOW
            if (e & FE_UNDERFLOW) {
            v = "FE_UNDERFLOW";
            e &= ~FE_UNDERFLOW;
        } else
#endif
#ifdef FE_INEXACT
            if (e & FE_INEXACT) {
            v = "FE_INEXACT";
            e &= ~FE_INEXACT;
        } else
#endif
#ifdef FE_INVALID
            if (e & FE_INVALID) {
            v = "FE_INVALID";
            e &= ~FE_INVALID;
        } else
#endif
        {
            snprintf(tmp, sizeof(tmp), "?? 0x%x", e);
            v = tmp;
            e = 0;
        }
#define check_add(s)                                               \
    do {                                                           \
        if (strlen(buf[i]) + strlen(s) + 1 > sizeof(buf[i]))       \
            printf("exception buf overflow %s + %s\n", buf[i], s); \
        else                                                       \
            strcat(buf[i], s);                                     \
    } while (0)
        if (buf[i][0])
            check_add(" | ");
        check_add(v);
    }
    char *ret = buf[i];
    i = (i + 1) % 3;
    return ret;
}

#define scat(a, b) a##b

#ifdef __mcffpu__
#define SKIP_SNAN_CHECKS
#endif

#ifdef __HAVE_68881__
#define SKIP_SNAN_CHECKS
#endif

#if defined(__m68k__) && !defined(__mcoldfire__) && !defined(__HAVE_M68881__)
#undef _TEST_LONG_DOUBLE
#define NO_NEXTTOWARD
#endif

/* Tests with long doubles */
#ifdef _TEST_LONG_DOUBLE

#if defined(__PICOLIBC__) && !defined(__HAVE_LONG_DOUBLE_MATH)
#define SIMPLE_MATH_ONLY
#define NO_NEXTTOWARD
#endif

#ifdef __LONG_DOUBLE_NOEXCEPT
#define EXCEPTION_TEST 0
#else
#define EXCEPTION_TEST MATH_ERREXCEPT
#endif
#define LONG_DOUBLE_EXCEPTION_TEST EXCEPTION_TEST
#ifdef _M_PI_L
#define PI_VAL _M_PI_L
#else
#define PI_VAL 3.141592653589793238462643383279502884L
#endif

#ifdef __PICOLIBC__
#define NO_BESSEL_TESTS
#endif

#define _PASTE_LDBL(exp) 0x.fp##exp##L
#define PASTE_LDBL(exp)  _PASTE_LDBL(exp)

#define BIG              PASTE_LDBL(__LDBL_MAX_EXP__)
#if __LDBL_MANT_DIG__ >= 64
#define BIGODD  0x1.123456789abcdef2p+63l
#define BIGEVEN 0x1.123456789abcdef0p+63l
#else
#define BIGODD  0x1.123456789abcdp+52l
#define BIGEVEN 0x1.123456789abccp+52l
#endif
#define SMALL         __LDBL_DENORM_MIN__
#define FLOAT_T       long double
#define MIN_VAL       __LDBL_DENORM_MIN__
#define MAX_VAL       __LDBL_MAX__
#define MANT_DIG      __LDBL_MANT_DIG__
#define EPSILON_VAL   __LDBL_EPSILON__
#define sNAN          __builtin_nansl("")
#define force_eval(x) clang_barrier_long_double(x)

#define TEST_LONG_DOUBLE

#define makemathname(s)   scat(s, l)
#define makemathname_r(s) scat(s, l_r)

#include "math_errhandling_tests.c"

#undef BIG
#undef BIGODD
#undef BIGEVEN
#undef SMALL
#undef MIN_VAL
#undef MAX_VAL
#undef MANT_DIG
#undef EPSILON_VAL
#undef force_eval
#undef sNAN
#undef makemathname
#undef makemathname_r
#undef FLOAT_T
#undef EXCEPTION_TEST
#undef TEST_LONG_DOUBLE
#undef NO_BESSEL_TESTS
#undef PI_VAL
#undef SIMPLE_MATH_ONLY
#endif

/* Tests with doubles */
#ifdef __DOUBLE_NOEXCEPT
#define EXCEPTION_TEST 0
#else
#define EXCEPTION_TEST MATH_ERREXCEPT
#endif
#define DOUBLE_EXCEPTION_TEST EXCEPTION_TEST

#if __SIZEOF_DOUBLE__ == 4
#define BIG     3e38
#define BIGODD  0x1.123456p+23
#define BIGEVEN 0x1.123454p+23
#define SMALL   1e-45
#else
#define BIG     1.7e308
#define BIGODD  0x1.123456789abcdp+52
#define BIGEVEN 0x1.123456789abccp+52
#define SMALL   5e-324
#endif
#define FLOAT_T       double
#define MIN_VAL       __DBL_DENORM_MIN__
#define MAX_VAL       __DBL_MAX__
#define MANT_DIG      __DBL_MANT_DIG__
#define EPSILON_VAL   __DBL_EPSILON__
#define sNAN          __builtin_nans("")
#define force_eval(x) clang_barrier_double(x)
#define PI_VAL        M_PI

#define TEST_DOUBLE

#define makemathname(s)   s
#define makemathname_r(s) scat(s, _r)

#include "math_errhandling_tests.c"

#undef BIG
#undef BIGODD
#undef BIGEVEN
#undef SMALL
#undef MIN_VAL
#undef MAX_VAL
#undef MANT_DIG
#undef EPSILON_VAL
#undef force_eval
#undef sNAN
#undef makemathname
#undef makemathname_r
#undef FLOAT_T
#undef EXCEPTION_TEST
#undef TEST_DOUBLE
#undef PI_VAL

/* Tests with floats */
#define EXCEPTION_TEST MATH_ERREXCEPT
#define BIG            3e38
#define BIGODD         0x1.123456p+23
#define BIGEVEN        0x1.123454p+23
#define SMALL          1e-45
#define MIN_VAL        0x8p-152f
#define MAX_VAL        0xf.fffffp+124f
#define MANT_DIG       __FLT_MANT_DIG__
#define EPSILON_VAL    0x1p-23f
#define sNAN           __builtin_nansf("")
#define force_eval(x)  clang_barrier_float(x)
#define FLOAT_T        float
#define TEST_FLOAT
#define makemathname(s)   scat(s, f)
#define makemathname_r(s) scat(s, f_r)
#define PI_VAL            ((float)M_PI)

#include "math_errhandling_tests.c"

int
main(void)
{
    int result = 0;

    printf("Double tests:\n");
    result += run_tests();
#ifdef _TEST_LONG_DOUBLE
    printf("Long double tests:\n");
#ifdef __m68k__
    volatile long double zero = 0.0L;
    volatile long double one = 1.0L;
    volatile long double check = nextafterl(zero, one);
    if (check + check == zero) {
        printf("m68k emulating long double with double, skipping\n");
    } else
#endif
        result += run_testsl();
#endif
    printf("Float tests:\n");
    result += run_testsf();
#ifdef __RX__
    if (result) {
        printf("Expected failure on RX target, ignoring\n");
        result = 77;
    }
#endif
    return result;
}