Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/lib/libc/tests/gen/fpsetmask_test.c
289379 views
1
/* $NetBSD: t_fpsetmask.c,v 1.16 2016/03/12 11:55:14 martin Exp $ */
2
3
/*-
4
* Copyright (c) 1995 The NetBSD Foundation, Inc.
5
* All rights reserved.
6
*
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
9
* are met:
10
* 1. Redistributions of source code must retain the above copyright
11
* notice, this list of conditions and the following disclaimer.
12
* 2. Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in the
14
* documentation and/or other materials provided with the distribution.
15
*
16
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
* POSSIBILITY OF SUCH DAMAGE.
27
*/
28
29
#include <sys/param.h>
30
31
#include <atf-c.h>
32
33
#include <stdio.h>
34
#include <signal.h>
35
#include <float.h>
36
#include <setjmp.h>
37
#include <stdlib.h>
38
#include <string.h>
39
40
#ifndef __i386__
41
42
ATF_TC(no_test);
43
ATF_TC_HEAD(no_test, tc)
44
{
45
46
atf_tc_set_md_var(tc, "descr", "Dummy test case");
47
}
48
49
ATF_TC_BODY(no_test, tc)
50
{
51
52
atf_tc_skip("Test not available on this architecture.");
53
}
54
55
#else /* defined(__i386__) */
56
57
#include <ieeefp.h>
58
#ifndef ___STRING
59
#define ___STRING(x) #x
60
#endif
61
#ifndef __arraycount
62
#define __arraycount(x) (sizeof(x) / sizeof((x)[0]))
63
#endif
64
65
66
#if __arm__ && !__SOFTFP__
67
/*
68
* Some NEON fpus do not implement IEEE exception handling,
69
* skip these tests if running on them and compiled for
70
* hard float.
71
*/
72
#define FPU_PREREQ() \
73
if (0 == fpsetmask(fpsetmask(FP_X_INV))) \
74
atf_tc_skip("FPU does not implement exception handling");
75
#endif
76
77
#ifndef FPU_PREREQ
78
#define FPU_PREREQ() /* nothing */
79
#endif
80
81
void sigfpe(int, siginfo_t *, void *);
82
83
volatile sig_atomic_t signal_caught;
84
volatile int sicode;
85
86
static volatile const float f_one = 1.0;
87
static volatile const float f_zero = 0.0;
88
static volatile const double d_one = 1.0;
89
static volatile const double d_zero = 0.0;
90
static volatile const long double ld_one = 1.0;
91
static volatile const long double ld_zero = 0.0;
92
93
static volatile const float f_huge = FLT_MAX;
94
static volatile const float f_tiny = FLT_MIN;
95
static volatile const double d_huge = DBL_MAX;
96
static volatile const double d_tiny = DBL_MIN;
97
static volatile const long double ld_huge = LDBL_MAX;
98
static volatile const long double ld_tiny = LDBL_MIN;
99
100
static volatile float f_x;
101
static volatile double d_x;
102
static volatile long double ld_x;
103
104
/* trip divide by zero */
105
static void
106
f_dz(void)
107
{
108
109
f_x = f_one / f_zero;
110
}
111
112
static void
113
d_dz(void)
114
{
115
116
d_x = d_one / d_zero;
117
}
118
119
static void
120
ld_dz(void)
121
{
122
123
ld_x = ld_one / ld_zero;
124
}
125
126
/* trip invalid operation */
127
static void
128
d_inv(void)
129
{
130
131
d_x = d_zero / d_zero;
132
}
133
134
static void
135
ld_inv(void)
136
{
137
138
ld_x = ld_zero / ld_zero;
139
}
140
141
static void
142
f_inv(void)
143
{
144
145
f_x = f_zero / f_zero;
146
}
147
148
/* trip overflow */
149
static void
150
f_ofl(void)
151
{
152
f_x = f_huge * f_huge;
153
printf("%f %f %Lf", f_x, d_x, ld_x); // avoid compiler optimization
154
}
155
156
static void
157
d_ofl(void)
158
{
159
160
d_x = d_huge * d_huge;
161
printf("%f %f %Lf", f_x, d_x, ld_x); // avoid compiler optimization
162
}
163
164
static void
165
ld_ofl(void)
166
{
167
168
ld_x = ld_huge * ld_huge;
169
printf("%f %f %Lf", f_x, d_x, ld_x); // avoid compiler optimization
170
}
171
172
/* trip underflow */
173
static void
174
f_ufl(void)
175
{
176
f_x = f_tiny * f_tiny;
177
printf("%f %f %Lf", f_x, d_x, ld_x); // avoid compiler optimization
178
}
179
180
static void
181
d_ufl(void)
182
{
183
d_x = d_tiny * d_tiny;
184
printf("%f %f %Lf", f_x, d_x, ld_x); // avoid compiler optimization
185
}
186
187
static void
188
ld_ufl(void)
189
{
190
ld_x = ld_tiny * ld_tiny;
191
printf("%f %f %Lf", f_x, d_x, ld_x); // avoid compiler optimization
192
}
193
194
struct ops {
195
void (*op)(void);
196
fp_except_t mask;
197
int sicode;
198
};
199
200
static const struct ops float_ops[] = {
201
{ f_dz, FP_X_DZ, FPE_FLTDIV },
202
{ f_inv, FP_X_INV, FPE_FLTINV },
203
{ f_ofl, FP_X_OFL, FPE_FLTOVF },
204
{ f_ufl, FP_X_UFL, FPE_FLTUND },
205
{ NULL, 0, 0 }
206
};
207
208
static const struct ops double_ops[] = {
209
{ d_dz, FP_X_DZ, FPE_FLTDIV },
210
{ d_inv, FP_X_INV, FPE_FLTINV },
211
{ d_ofl, FP_X_OFL, FPE_FLTOVF },
212
{ d_ufl, FP_X_UFL, FPE_FLTUND },
213
{ NULL, 0, 0 }
214
};
215
216
static const struct ops long_double_ops[] = {
217
{ ld_dz, FP_X_DZ, FPE_FLTDIV },
218
{ ld_inv, FP_X_INV, FPE_FLTINV },
219
{ ld_ofl, FP_X_OFL, FPE_FLTOVF },
220
{ ld_ufl, FP_X_UFL, FPE_FLTUND },
221
{ NULL, 0, 0 }
222
};
223
224
static sigjmp_buf b;
225
226
static void
227
fpsetmask_masked(const struct ops *test_ops)
228
{
229
struct sigaction sa;
230
fp_except_t ex1, ex2;
231
const struct ops *t;
232
233
/* mask all exceptions, clear history */
234
fpsetmask(0);
235
fpresetsticky(~0);
236
237
/* set up signal handler */
238
sa.sa_sigaction = sigfpe;
239
sigemptyset(&sa.sa_mask);
240
sa.sa_flags = SA_SIGINFO;
241
sigaction(SIGFPE, &sa, 0);
242
signal_caught = 0;
243
244
/*
245
* exceptions masked, check whether "sticky" bits are set correctly
246
*/
247
for (t = test_ops; t->op != NULL; t++) {
248
(*t->op)();
249
ex1 = fpgetsticky();
250
ATF_CHECK_EQ(ex1 & t->mask, t->mask);
251
ATF_CHECK_EQ(signal_caught, 0);
252
253
/* check correct fpresetsticky() behaviour */
254
ex2 = fpresetsticky(~0);
255
ATF_CHECK_EQ(fpgetsticky(), 0);
256
ATF_CHECK_EQ(ex1, ex2);
257
}
258
}
259
260
/* force delayed exceptions to be delivered */
261
#define BARRIER() fpsetmask(0); f_x = f_one * f_one
262
263
static void
264
fpsetmask_unmasked(const struct ops *test_ops)
265
{
266
struct sigaction sa;
267
int r;
268
const struct ops *volatile t;
269
270
/* mask all exceptions, clear history */
271
fpsetmask(0);
272
fpresetsticky(~0);
273
274
/* set up signal handler */
275
sa.sa_sigaction = sigfpe;
276
sigemptyset(&sa.sa_mask);
277
sa.sa_flags = SA_SIGINFO;
278
sigaction(SIGFPE, &sa, 0);
279
signal_caught = 0;
280
281
/*
282
* exception unmasked, check SIGFPE delivery and correct siginfo
283
*/
284
for (t = test_ops; t->op != NULL; t++) {
285
fpsetmask(t->mask);
286
r = sigsetjmp(b, 1);
287
printf("r = %d\n", r);
288
if (!r) {
289
(*t->op)();
290
BARRIER();
291
fpresetsticky(~0);
292
}
293
ATF_CHECK_EQ(signal_caught, 1);
294
ATF_CHECK_EQ(sicode, t->sicode);
295
signal_caught = 0;
296
sicode = 0;
297
}
298
}
299
300
void
301
sigfpe(int s, siginfo_t *si, void *c)
302
{
303
signal_caught = 1;
304
sicode = si->si_code;
305
siglongjmp(b, 1);
306
}
307
308
#define TEST(m, t) \
309
ATF_TC(m##_##t); \
310
\
311
ATF_TC_HEAD(m##_##t, tc) \
312
{ \
313
\
314
atf_tc_set_md_var(tc, "descr", \
315
"Test " ___STRING(m) " exceptions for " \
316
___STRING(t) "values"); \
317
} \
318
\
319
ATF_TC_BODY(m##_##t, tc) \
320
{ \
321
\
322
FPU_PREREQ(); \
323
\
324
if (strcmp(MACHINE, "macppc") == 0) \
325
atf_tc_expect_fail("PR port-macppc/46319"); \
326
\
327
\
328
m(t##_ops); \
329
}
330
331
TEST(fpsetmask_masked, float)
332
TEST(fpsetmask_masked, double)
333
TEST(fpsetmask_masked, long_double)
334
TEST(fpsetmask_unmasked, float)
335
TEST(fpsetmask_unmasked, double)
336
TEST(fpsetmask_unmasked, long_double)
337
338
ATF_TC(fpsetmask_basic);
339
ATF_TC_HEAD(fpsetmask_basic, tc)
340
{
341
atf_tc_set_md_var(tc, "descr", "A basic test of fpsetmask(3)");
342
}
343
344
ATF_TC_BODY(fpsetmask_basic, tc)
345
{
346
size_t i;
347
fp_except_t msk, lst[] = { FP_X_INV, FP_X_DZ, FP_X_OFL, FP_X_UFL };
348
349
FPU_PREREQ();
350
351
msk = fpgetmask();
352
for (i = 0; i < __arraycount(lst); i++) {
353
fpsetmask(msk | lst[i]);
354
ATF_CHECK((fpgetmask() & lst[i]) != 0);
355
fpsetmask(msk & ~lst[i]);
356
ATF_CHECK((fpgetmask() & lst[i]) == 0);
357
}
358
359
}
360
361
#endif /* defined(__i386__) */
362
363
ATF_TP_ADD_TCS(tp)
364
{
365
366
#ifndef __i386__
367
ATF_TP_ADD_TC(tp, no_test);
368
#else
369
ATF_TP_ADD_TC(tp, fpsetmask_basic);
370
ATF_TP_ADD_TC(tp, fpsetmask_masked_float);
371
ATF_TP_ADD_TC(tp, fpsetmask_masked_double);
372
ATF_TP_ADD_TC(tp, fpsetmask_masked_long_double);
373
ATF_TP_ADD_TC(tp, fpsetmask_unmasked_float);
374
ATF_TP_ADD_TC(tp, fpsetmask_unmasked_double);
375
ATF_TP_ADD_TC(tp, fpsetmask_unmasked_long_double);
376
#endif
377
378
return atf_no_error();
379
}
380
381