summaryrefslogtreecommitdiffstats
path: root/contrib/netbsd-tests/lib/libc/gen/t_fpsetmask.c
blob: 5edc583ce2a8db422c40339507c9ccdbf415abc8 (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
/*	$NetBSD: t_fpsetmask.c,v 1.16 2016/03/12 11:55:14 martin Exp $ */

/*-
 * Copyright (c) 1995 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
 */

#include <sys/param.h>

#include <atf-c.h>

#include <stdio.h>
#include <signal.h>
#include <float.h>
#include <setjmp.h>
#include <stdlib.h>
#include <string.h>

#include "isqemu.h"

#ifndef _FLOAT_IEEE754

ATF_TC(no_test);
ATF_TC_HEAD(no_test, tc)
{

	atf_tc_set_md_var(tc, "descr", "Dummy test case");
}

ATF_TC_BODY(no_test, tc)
{

	atf_tc_skip("Test not available on this architecture.");
}

#else /* defined(_FLOAT_IEEE754) */

#include <ieeefp.h>

#if __arm__ && !__SOFTFP__
	/*
	 * Some NEON fpus do not implement IEEE exception handling,
	 * skip these tests if running on them and compiled for
	 * hard float.
	 */
#define	FPU_PREREQ()							\
	if (0 == fpsetmask(fpsetmask(FP_X_INV)))			\
		atf_tc_skip("FPU does not implement exception handling");
#endif

#ifndef FPU_PREREQ
#define	FPU_PREREQ()	/* nothing */
#endif

void		sigfpe(int, siginfo_t *, void *);

volatile sig_atomic_t signal_caught;
volatile int sicode;

static volatile const float	f_one   = 1.0;
static volatile const float	f_zero  = 0.0;
static volatile const double	d_one   = 1.0;
static volatile const double	d_zero  = 0.0;
static volatile const long double ld_one  = 1.0;
static volatile const long double ld_zero = 0.0;

static volatile const float	f_huge = FLT_MAX;
static volatile const float	f_tiny = FLT_MIN;
static volatile const double	d_huge = DBL_MAX;
static volatile const double	d_tiny = DBL_MIN;
static volatile const long double ld_huge = LDBL_MAX;
static volatile const long double ld_tiny = LDBL_MIN;

static volatile float f_x;
static volatile double d_x;
static volatile long double ld_x;

/* trip divide by zero */
static void
f_dz(void)
{

	f_x = f_one / f_zero;
}

static void
d_dz(void)
{

	d_x = d_one / d_zero;
}

static void
ld_dz(void)
{

	ld_x = ld_one / ld_zero;
}

/* trip invalid operation */
static void
d_inv(void)
{

	d_x = d_zero / d_zero;
}

static void
ld_inv(void)
{

	ld_x = ld_zero / ld_zero;
}

static void
f_inv(void)
{

	f_x = f_zero / f_zero;
}

/* trip overflow */
static void
f_ofl(void)
{

	f_x = f_huge * f_huge;
}

static void
d_ofl(void)
{

	d_x = d_huge * d_huge;
}

static void
ld_ofl(void)
{

	ld_x = ld_huge * ld_huge;
}

/* trip underflow */
static void
f_ufl(void)
{

	f_x = f_tiny * f_tiny;
}

static void
d_ufl(void)
{

	d_x = d_tiny * d_tiny;
}

static void
ld_ufl(void)
{

	ld_x = ld_tiny * ld_tiny;
}

struct ops {
	void (*op)(void);
	fp_except mask;
	int sicode;
};

static const struct ops float_ops[] = {
	{ f_dz, FP_X_DZ, FPE_FLTDIV },
	{ f_inv, FP_X_INV, FPE_FLTINV },
	{ f_ofl, FP_X_OFL, FPE_FLTOVF },
	{ f_ufl, FP_X_UFL, FPE_FLTUND },
	{ NULL, 0, 0 }
};

static const struct ops double_ops[] = {
	{ d_dz, FP_X_DZ, FPE_FLTDIV },
	{ d_inv, FP_X_INV, FPE_FLTINV },
	{ d_ofl, FP_X_OFL, FPE_FLTOVF },
	{ d_ufl, FP_X_UFL, FPE_FLTUND },
	{ NULL, 0, 0 }
};

static const struct ops long_double_ops[] = {
	{ ld_dz, FP_X_DZ, FPE_FLTDIV },
	{ ld_inv, FP_X_INV, FPE_FLTINV },
	{ ld_ofl, FP_X_OFL, FPE_FLTOVF },
	{ ld_ufl, FP_X_UFL, FPE_FLTUND },
	{ NULL, 0, 0 }
};

static sigjmp_buf b;

static void
fpsetmask_masked(const struct ops *test_ops)
{
	struct sigaction sa;
	fp_except ex1, ex2;
	const struct ops *t;

	/* mask all exceptions, clear history */
	fpsetmask(0);
	fpsetsticky(0);

	/* set up signal handler */
	sa.sa_sigaction = sigfpe;
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = SA_SIGINFO;
	sigaction(SIGFPE, &sa, 0);
	signal_caught = 0;

	/*
	 * exceptions masked, check whether "sticky" bits are set correctly
	 */
	for (t = test_ops; t->op != NULL; t++) {
		(*t->op)();
		ex1 = fpgetsticky();
		ATF_CHECK_EQ(ex1 & t->mask, t->mask);
		ATF_CHECK_EQ(signal_caught, 0);

		/* check correct fpsetsticky() behaviour */
		ex2 = fpsetsticky(0);
		ATF_CHECK_EQ(fpgetsticky(), 0);
		ATF_CHECK_EQ(ex1, ex2);
	}
}

/* force delayed exceptions to be delivered */
#define BARRIER() fpsetmask(0); f_x = f_one * f_one

static void
fpsetmask_unmasked(const struct ops *test_ops)
{
	struct sigaction sa;
	int r;
	const struct ops *volatile t;

	/* mask all exceptions, clear history */
	fpsetmask(0);
	fpsetsticky(0);

	/* set up signal handler */
	sa.sa_sigaction = sigfpe;
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = SA_SIGINFO;
	sigaction(SIGFPE, &sa, 0);
	signal_caught = 0;

	/*
	 * exception unmasked, check SIGFPE delivery and correct siginfo
	 */
	for (t = test_ops; t->op != NULL; t++) {
		fpsetmask(t->mask);
		r = sigsetjmp(b, 1);
		if (!r) {
			(*t->op)();
			BARRIER();
		}
		ATF_CHECK_EQ(signal_caught, 1);
		ATF_CHECK_EQ(sicode, t->sicode);
		signal_caught = 0;
	}
}

void
sigfpe(int s, siginfo_t *si, void *c)
{
	signal_caught = 1;
	sicode = si->si_code;
	siglongjmp(b, 1);
}

#define TEST(m, t)							\
	ATF_TC(m##_##t);						\
									\
	ATF_TC_HEAD(m##_##t, tc)					\
	{								\
									\
		atf_tc_set_md_var(tc, "descr",				\
		    "Test " ___STRING(m) " exceptions for "		\
		    ___STRING(t) "values");				\
	}								\
									\
	ATF_TC_BODY(m##_##t, tc)					\
	{								\
									\
		FPU_PREREQ();						\
									\
		if (strcmp(MACHINE, "macppc") == 0)			\
			atf_tc_expect_fail("PR port-macppc/46319");	\
									\
		if (isQEMU())						\
			atf_tc_expect_fail("PR misc/44767");		\
									\
		m(t##_ops);						\
	}

TEST(fpsetmask_masked, float)
TEST(fpsetmask_masked, double)
TEST(fpsetmask_masked, long_double)
TEST(fpsetmask_unmasked, float)
TEST(fpsetmask_unmasked, double)
TEST(fpsetmask_unmasked, long_double)

ATF_TC(fpsetmask_basic);
ATF_TC_HEAD(fpsetmask_basic, tc)
{
	atf_tc_set_md_var(tc, "descr", "A basic test of fpsetmask(3)");
}

ATF_TC_BODY(fpsetmask_basic, tc)
{
	size_t i;
	fp_except_t msk, lst[] = { FP_X_INV, FP_X_DZ, FP_X_OFL, FP_X_UFL };

	FPU_PREREQ();

	msk = fpgetmask();
	for (i = 0; i < __arraycount(lst); i++) {
		fpsetmask(msk | lst[i]);
		ATF_CHECK((fpgetmask() & lst[i]) != 0);
		fpsetmask(msk & ~lst[i]);
		ATF_CHECK((fpgetmask() & lst[i]) == 0);
	}

}

#endif /* defined(_FLOAT_IEEE754) */

ATF_TP_ADD_TCS(tp)
{

#ifndef _FLOAT_IEEE754
	ATF_TP_ADD_TC(tp, no_test);
#else
	ATF_TP_ADD_TC(tp, fpsetmask_basic);
	ATF_TP_ADD_TC(tp, fpsetmask_masked_float);
	ATF_TP_ADD_TC(tp, fpsetmask_masked_double);
	ATF_TP_ADD_TC(tp, fpsetmask_masked_long_double);
	ATF_TP_ADD_TC(tp, fpsetmask_unmasked_float);
	ATF_TP_ADD_TC(tp, fpsetmask_unmasked_double);
	ATF_TP_ADD_TC(tp, fpsetmask_unmasked_long_double);
#endif

	return atf_no_error();
}
OpenPOWER on IntegriCloud